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.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
39
40
41
42
43 public class Dialog extends NyxWindow {
44
45 protected JDialog dialog;
46 protected WindowListener windowListener;
47
48
49
50
51 public Dialog(String name) {
52 super(name);
53 }
54
55
56
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
77
78 public Object getNativeWidget() {
79 if (!initialized) {
80 initialize();
81 }
82 return dialog;
83 }
84
85
86
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
140
141 public Object getGuiValue() {
142 return null;
143 }
144
145
146
147
148 public void focus() {
149 dialog.requestFocus();
150 }
151
152
153
154
155
156 public boolean isValueEmpty() {
157 return false;
158 }
159
160
161
162
163 public boolean canContainValue() {
164 return false;
165 }
166
167
168
169
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
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
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 }