1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
44
45
46
47
48 public class Window extends NyxWindow {
49
50
51
52 protected JFrame window;
53
54
55
56 protected WindowListener windowListener;
57
58 boolean isInitializing = false;
59
60
61
62
63 public Window(String name) {
64 super(name);
65 }
66
67
68
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
88
89 public Object getNativeWidget() {
90 initialize();
91 return window;
92 }
93
94
95
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
118
119 if ("modal".equalsIgnoreCase(windowType)) {
120 } else if ("toolbox".equalsIgnoreCase(windowType)) {
121 } else {
122
123
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
143
144
145
146
147
148
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
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
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
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
210
211 public void focus() {
212 initialize();
213 window.requestFocus();
214 }
215
216
217
218
219 public Object getGuiValue() {
220 return null;
221 }
222
223
224
225
226 public class RepaintComponent implements Runnable {
227
228
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
240
241 public void run() {
242
243
244
245
246
247 if (window != null) {
248 window.repaint();
249 }
250 }
251 });
252 } catch (Throwable e) {
253
254 e.printStackTrace(System.out);
255 }
256 }
257
258 }
259
260
261
262 public boolean canContainValue() {
263 return false;
264 }
265
266
267
268
269 public boolean isValueEmpty() {
270 return true;
271 }
272
273
274
275
276 public void addXuluxListener(IXuluxListener listener) {
277
278 }
279
280 }