View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.xulux.guilayer.swing.widgets;
17  
18  import java.awt.Container;
19  import java.awt.Frame;
20  import java.awt.LayoutManager;
21  import java.awt.event.WindowListener;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import javax.swing.JComponent;
26  import javax.swing.JDialog;
27  import javax.swing.JMenuBar;
28  
29  import org.xulux.api.gui.IShowChildWidgets;
30  import org.xulux.api.gui.IWidget;
31  import org.xulux.api.gui.IXuluxLayout;
32  import org.xulux.core.XuluxContext;
33  import org.xulux.gui.NyxWindow;
34  import org.xulux.guilayer.swing.listeners.XuluxWindowListener;
35  import org.xulux.utils.BooleanUtils;
36  
37  /**
38   * A dialog
39   *
40   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
41   * @version $Id: Dialog.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
42   */
43  public class Dialog extends NyxWindow {
44    
45    protected JDialog dialog;
46    protected WindowListener windowListener;
47  
48    /**
49     * @param name
50     */
51    public Dialog(String name) {
52      super(name);
53    }
54  
55    /**
56     * @see org.xulux.api.gui.IWidget#destroy()
57     */
58    public void destroy() {
59      super.destroy();
60      if (dialog != null) {
61        dialog.removeAll();
62        dialog.removeWindowListener(windowListener);
63        windowListener = null;
64        dialog.setVisible(false);
65        Container container = dialog.getParent();
66        if (container != null) {
67            container.remove(dialog);
68        }
69        dialog.dispose();
70        dialog = null;
71      }
72  
73    }
74  
75    /**
76     * @see org.xulux.api.gui.IWidget#getNativeWidget()
77     */
78    public Object getNativeWidget() {
79      if (!initialized) {
80        initialize();
81      }
82      return dialog;
83    }
84  
85    /**
86     * @see org.xulux.api.gui.IWidget#initialize()
87     */
88    public void initialize() {
89      if (this.initialized) {
90          return;
91      }
92      initialized = true;
93      String title = getProperty("title");
94      if (title == null) {
95          title = "";
96      }
97      if (getPart().getParentPart() != null) {
98        IWidget parentWindow = getPart().getParentPart().getWidget(getProperty("parentWindow"));
99        if (parentWindow != null) {
100         Object nativeParent = parentWindow.getNativeWidget();
101         if (nativeParent instanceof Frame) {
102           dialog = new JDialog((Frame) nativeParent);
103         } else if (nativeParent instanceof JDialog) {
104           dialog = new JDialog((JDialog) nativeParent);
105         }
106       }
107     }
108     if (dialog == null) {    
109       dialog = new JDialog();
110     }
111     dialog.setTitle(title);
112     IXuluxLayout layout = XuluxContext.getGuiDefaults().getLayout(null, getProperty("layout"));
113     if (layout == null) {
114         layout = XuluxContext.getGuiDefaults().getDefaultLayout();
115     }
116     layout.setParent(this);
117     dialog.getContentPane().setLayout((LayoutManager) layout);
118     this.windowListener = new XuluxWindowListener(this);
119     dialog.addWindowListener(this.windowListener);
120     initializeChildren();
121     boolean autoSize = BooleanUtils.toBoolean(getProperty("autosize"));
122     if (autoSize) {
123         dialog.pack();
124     } else {
125         dialog.setSize(getRectangle().getWidth(), getRectangle().getHeight());
126     }
127     if (getProperty("resizable") != null) {
128         boolean resize = BooleanUtils.toBoolean(getProperty("resizable"));
129         dialog.setResizable(resize);
130     }
131     if (!isRefreshing()) {
132         refresh();
133     }
134     processInit();
135     dialog.setModal(BooleanUtils.toBoolean(getProperty("modal")));
136   }
137 
138   /**
139    * @see org.xulux.api.gui.IWidget#getGuiValue()
140    */
141   public Object getGuiValue() {
142     return null;
143   }
144 
145   /**
146    * @see org.xulux.api.gui.IWidget#focus()
147    */
148   public void focus() {
149     dialog.requestFocus();
150   }
151 
152 
153   /**
154    * @see org.xulux.api.gui.IWidget#isValueEmpty()
155    */
156   public boolean isValueEmpty() {
157     return false;
158   }
159 
160   /**
161    * @see org.xulux.api.gui.IWidget#canContainValue()
162    */
163   public boolean canContainValue() {
164     return false;
165   }
166 
167 
168   /**
169    * @see org.xulux.api.gui.IContainerWidgetHandler#addToParent(org.xulux.api.gui.IWidget)
170    */
171   public void addToParent(IWidget widget) {
172     if (widget instanceof IShowChildWidgets) {
173         List children = widget.getChildWidgets();
174         if (children != null && children.size() > 0) {
175             Iterator it = children.iterator();
176             while (it.hasNext()) {
177                 IWidget w = (IWidget) it.next();
178                 dialog.getContentPane().add((JComponent) w.getNativeWidget(), w);
179             }
180         }
181     } else {
182 //        System.out.println("Window widget : " + widget.getNativeWidget().getClass());
183         if (widget.getNativeWidget() instanceof JMenuBar) {
184             dialog.getRootPane().setJMenuBar((JMenuBar) widget.getNativeWidget());
185         } else {
186             dialog.getContentPane().add((JComponent) widget.getNativeWidget(), widget);
187         }
188     }
189   }
190 
191   /**
192    * @see org.xulux.api.gui.IWidget#refresh()
193    */
194   public void refresh() {
195     isRefreshing = true;
196     initialize();
197     dialog.setTitle(getProperty("title"));
198     dialog.setEnabled(isEnabled());
199     dialog.setVisible(isVisible());
200     isRefreshing = false;
201   }
202 
203 }