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.Dimension;
20  import java.awt.LayoutManager;
21  import java.awt.Toolkit;
22  import java.awt.event.WindowListener;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import javax.swing.JComponent;
27  import javax.swing.JFrame;
28  import javax.swing.JMenuBar;
29  import javax.swing.SwingUtilities;
30  
31  import org.xulux.api.gui.IShowChildWidgets;
32  import org.xulux.api.gui.IWidget;
33  import org.xulux.api.gui.IXuluxLayout;
34  import org.xulux.api.gui.IXuluxListener;
35  import org.xulux.core.XuluxContext;
36  import org.xulux.gui.XuluxWidget;
37  import org.xulux.gui.NyxWindow;
38  import org.xulux.guilayer.swing.listeners.XuluxWindowListener;
39  import org.xulux.guilayer.swing.util.SwingUtils;
40  import org.xulux.utils.BooleanUtils;
41  
42  /**
43   * This is a swing window.
44   *
45   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
46   * @version $Id: Window.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
47   */
48  public class Window extends NyxWindow {
49      /**
50       * the native window
51       */
52      protected JFrame window;
53      /**
54       * the window listener
55       */
56      protected WindowListener windowListener;
57  
58      boolean isInitializing = false;
59      /**
60       * Constructor for NyxWindow.
61       * @param name the name of the window
62       */
63      public Window(String name) {
64          super(name);
65      }
66  
67      /**
68       * @see org.xulux.api.gui.IWidget#destroy()
69       */
70      public void destroy() {
71          super.destroy();
72          if (window != null) {
73            window.removeAll();
74            window.removeWindowListener(windowListener);
75            windowListener = null;
76            window.setVisible(false);
77            Container container = window.getParent();
78            if (container != null) {
79                container.remove(window);
80            }
81            window.dispose();
82            window = null;
83          }
84      }
85  
86      /**
87       * @see org.xulux.api.gui.IWidget#getNativeWidget()
88       */
89      public Object getNativeWidget() {
90          initialize();
91          return window;
92      }
93  
94      /**
95       * @see org.xulux.api.gui.IWidget#initialize()
96       */
97      public void initialize() {
98          if (this.initialized) {
99              return;
100         }
101         initialized = true;
102         isInitializing = true;
103         String title = getProperty("title");
104         if (title == null) {
105             title = "";
106         }
107         window = new JFrame(title);
108         IXuluxLayout layout = XuluxContext.getGuiDefaults().getLayout(null, getProperty("layout"));
109         if (layout == null) {
110             layout = XuluxContext.getGuiDefaults().getDefaultLayout();
111         }
112         layout.setParent(this);
113         window.getContentPane().setLayout((LayoutManager) layout);
114         this.windowListener = new XuluxWindowListener(this);
115         window.addWindowListener(this.windowListener);
116         String windowType = getProperty("window-type");
117         // don't have a clue yet what to use here
118         // for swing to work correctlly
119         if ("modal".equalsIgnoreCase(windowType)) {
120         } else if ("toolbox".equalsIgnoreCase(windowType)) {
121         } else {
122             // @todo Introduce MDI type of windowing
123             // mdi is the default.
124         }
125         boolean autoSize = BooleanUtils.toBoolean(getProperty("autosize"));
126         if (autoSize) {
127             Dimension dim = window.getContentPane().getLayout().preferredLayoutSize(window.getContentPane());
128             window.pack();
129         } else {
130             window.setSize(getRectangle().getWidth(), getRectangle().getHeight());
131         }
132         if ("center".equalsIgnoreCase(getProperty("position"))) {
133         	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
134         	Dimension prefSize = window.getPreferredSize();
135         	if (prefSize.width == 0 && prefSize.height == 0) {
136         	    prefSize = getRectangle().getRectangle().getSize();
137         	}
138         	System.out.println("screenSize : " + screenSize);
139         	System.out.println("Pref Size : " + prefSize);
140         	window.setLocation((screenSize.width-prefSize.width)/2, (screenSize.height-prefSize.height)/2);
141         }
142 //        if (autoSize) {
143 //            Dimension dim = window.getContentPane().getLayout().preferredLayoutSize(window.getContentPane());
144 //        }
145 //        if ("center".equalsIgnoreCase(getProperty("position"))) {
146 //        	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
147 //        	Dimension prefSize = window.getPreferredSize();
148 //        	window.setLocation((screenSize.width-prefSize.width)/2, (screenSize.height-prefSize.height)/2);
149 //        }
150         window.setVisible(false);
151         initializeChildren();
152         window.setVisible(false);
153         processInit();
154         if (getProperty("resizable") != null) {
155           boolean resize = BooleanUtils.toBoolean(getProperty("resizable"));
156             window.setResizable(resize);
157         }
158         window.setVisible(true);
159         isInitializing = false;
160         if (!isRefreshing()) {
161             refresh();
162         }
163         new Thread(new RepaintComponent()).start();
164     }
165 
166     /**
167      * @see org.xulux.nyx.gui.XuluxWidget#refresh()
168      */
169     public void refresh() {
170     	if (isInitializing) {
171     		return;
172     	}
173         isRefreshing = true;
174         initialize();
175         String image = getProperty("icon");
176         if (image != null) {
177             window.setIconImage(SwingUtils.getImage(image, this));
178         }
179         window.setTitle(getProperty("title"));
180         window.setVisible(isVisible());
181         window.setEnabled(isEnabled());
182         isRefreshing = false;
183     }
184 
185     /**
186      * @see org.xulux.api.gui.IContainerWidgetHandler#addToParent(org.xulux.api.gui.IWidget)
187      */
188     public void addToParent(IWidget widget) {
189         if (widget instanceof IShowChildWidgets) {
190             List children = widget.getChildWidgets();
191             if (children != null && children.size() > 0) {
192                 Iterator it = children.iterator();
193                 while (it.hasNext()) {
194                     IWidget w = (IWidget) it.next();
195                     window.getContentPane().add((JComponent) w.getNativeWidget(), w);
196                 }
197             }
198         } else {
199 //            System.out.println("Window widget : " + widget.getNativeWidget().getClass());
200             if (widget.getNativeWidget() instanceof JMenuBar) {
201                 window.getRootPane().setJMenuBar((JMenuBar) widget.getNativeWidget());
202             } else {
203                 window.getContentPane().add((JComponent) widget.getNativeWidget(), widget);
204             }
205         }
206     }
207 
208     /**
209      * @see org.xulux.nyx.gui.XuluxWidget#focus()
210      */
211     public void focus() {
212         initialize();
213         window.requestFocus();
214     }
215 
216     /**
217      * @see org.xulux.nyx.gui.XuluxWidget#getGuiValue()
218      */
219     public Object getGuiValue() {
220         return null;
221     }
222 
223     /**
224      * Repaints the window when it is shown.
225      */
226     public class RepaintComponent implements Runnable {
227         /**
228          * @see java.lang.Runnable#run()
229          */
230         public void run() {
231             if (getPart() == null) {
232                 return;
233             }
234             while (getPart().isActivating()) {
235             };
236             try {
237                 SwingUtilities.invokeAndWait(new Runnable() {
238                     /**
239                      * @see java.lang.Runnable#run()
240                      */
241                     public void run() {
242                         // TODO : Look at painting problem
243                         // we for now rerun the rules, so
244                         // the window will show correctly.
245                         // check first if the window is still there
246                         // it could well be destroyed
247                         if (window != null) {
248                             window.repaint();
249                         }
250                     }
251                 });
252             } catch (Throwable e) {
253                 // catching throwable here. Especially during tests window open close is just too fast..
254                 e.printStackTrace(System.out);
255             }
256         }
257 
258     }
259     /**
260      * @see org.xulux.nyx.gui.XuluxWidget#canContainValue()
261      */
262     public boolean canContainValue() {
263         return false;
264     }
265 
266     /**
267      * @see org.xulux.nyx.gui.XuluxWidget#isValueEmpty()
268      */
269     public boolean isValueEmpty() {
270         return true;
271     }
272 
273     /**
274      * @see org.xulux.nyx.gui.XuluxWidget#addXuluxListener(org.xulux.nyx.gui.XuluxListener)
275      */
276     public void addXuluxListener(IXuluxListener listener) {
277         // TODO
278     }
279 
280 }