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.Color;
19 import java.awt.Component;
20 import java.awt.Container;
21 import java.awt.LayoutManager;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import javax.swing.JComponent;
26 import javax.swing.JPanel;
27 import javax.swing.JTabbedPane;
28 import javax.swing.border.BevelBorder;
29 import javax.swing.border.EtchedBorder;
30 import javax.swing.border.LineBorder;
31
32 import org.xulux.api.gui.IShowChildWidgets;
33 import org.xulux.api.gui.IWidget;
34 import org.xulux.api.gui.IXuluxLayout;
35 import org.xulux.api.gui.IXuluxListener;
36 import org.xulux.core.XuluxContext;
37 import org.xulux.gui.ContainerWidget;
38 import org.xulux.gui.XuluxWidget;
39 import org.xulux.gui.utils.ColorUtils;
40 import org.xulux.guilayer.swing.extensions.IconTitledBorder;
41 import org.xulux.guilayer.swing.extensions.NyxTitledBorder;
42 import org.xulux.guilayer.swing.util.SwingUtils;
43
44
45
46
47
48
49
50 public class Panel extends ContainerWidget {
51
52
53
54
55 private JPanel panel;
56
57
58
59
60
61 public Panel(String name) {
62 super(name);
63 }
64
65
66
67
68 public void destroy() {
69 super.destroy();
70 if (panel == null) {
71 return;
72 }
73 Container container = panel.getParent();
74 panel.setVisible(false);
75 panel.removeAll();
76 if (container != null) {
77 container.remove(panel);
78 }
79 panel = null;
80 }
81
82
83
84
85 public Object getNativeWidget() {
86 initialize();
87 return panel;
88 }
89
90
91
92
93
94 public void initialize() {
95 if (initialized) {
96 return;
97 }
98
99 initializing = true;
100 initialized = true;
101 IXuluxLayout layout = XuluxContext.getGuiDefaults().getLayout(null, getProperty("layout"));
102 if (layout == null) {
103 layout = XuluxContext.getGuiDefaults().getDefaultLayout();
104 }
105 layout.setParent(this);
106 panel = new JPanel((LayoutManager) layout);
107 initializeChildren();
108 initializing = false;
109 refresh();
110 processInit();
111 }
112
113
114
115
116 public void refresh() {
117 initialize();
118 panel.setVisible(isVisible());
119 String backgroundColor = getProperty("default-background-color");
120
121 if (backgroundColor != null) {
122 panel.setBackground(ColorUtils.getSwingColor(backgroundColor));
123 }
124
125
126
127
128 String border = getProperty("border");
129 if (border != null) {
130 Color color = panel.getForeground();
131 String borderColor = getProperty("border-color");
132 if (borderColor != null) {
133 color = ColorUtils.getSwingColor(borderColor);
134 }
135 if (border.equalsIgnoreCase("bevel")) {
136 String borderType = getProperty("border-type");
137
138 int bType = BevelBorder.LOWERED;
139 if (borderType != null && borderType.equalsIgnoreCase("raised")) {
140 bType = BevelBorder.RAISED;
141 }
142 panel.setBorder(new BevelBorder(bType));
143 } else if (border.equalsIgnoreCase("titled")) {
144 String borderTitle = getProperty("border-title");
145 if (borderTitle == null) {
146 borderTitle = "";
147 }
148
149 Color titleColor = null;
150 if (getProperty("border-title-color") != null) {
151 titleColor = ColorUtils.getSwingColor(getProperty("border-title-color"));
152 }
153 int borderSize = 1;
154 if (getProperty("border-size") != null) {
155 try {
156 borderSize = Integer.parseInt(getProperty("border-size"));
157 } catch (NumberFormatException nfe) {
158 nfe.printStackTrace();
159 }
160 }
161
162 NyxTitledBorder titledBorder = new NyxTitledBorder(borderTitle);
163 if (titleColor != null) {
164 titledBorder.setTitleColor(titleColor);
165 }
166 panel.setBorder(titledBorder);
167 } else if (border.equalsIgnoreCase("line")) {
168 int borderSize = 1;
169 if (getProperty("border-size") != null) {
170 try {
171 borderSize = Integer.parseInt(getProperty("border-size"));
172 } catch (NumberFormatException nfe) {
173 nfe.printStackTrace();
174 }
175 }
176 panel.setBorder(new LineBorder(color, borderSize));
177 } else if (border.equalsIgnoreCase("etched")) {
178 EtchedBorder etched = new EtchedBorder(EtchedBorder.RAISED);
179 panel.setBorder(etched);
180
181 } else if (border.equalsIgnoreCase("titledicon")) {
182 String borderTitle = getProperty("border-title");
183 if (borderTitle == null) {
184 borderTitle = "";
185 }
186
187 Color titleColor = null;
188 if (getProperty("border-title-color") != null) {
189 titleColor = ColorUtils.getSwingColor(getProperty("border-title-color"));
190 }
191 IconTitledBorder itBorder = new IconTitledBorder();
192 int borderSize = 1;
193 if (getProperty("border-size") != null) {
194 try {
195 borderSize = Integer.parseInt(getProperty("border-size"));
196 itBorder.setThickness(borderSize);
197 } catch (NumberFormatException nfe) {
198 nfe.printStackTrace();
199 }
200 }
201 if (getProperty("border-image") != null) {
202 itBorder.setImage(SwingUtils.getImage(getProperty("border-image"), this));
203 }
204
205
206
207 if (borderTitle != null) {
208 itBorder.setTitle(borderTitle);
209 }
210 panel.setBorder(itBorder);
211
212 }
213 } else {
214 panel.setBorder(null);
215 }
216
217 String tabId = getProperty(TabPanel.TABID);
218 if (tabId == null) {
219 panel.setEnabled(isEnabled());
220 } else {
221 System.out.println("tabId : " + tabId);
222 ((JTabbedPane) getParent().getNativeWidget()).setEnabledAt(Integer.parseInt(tabId), isEnabled());
223 }
224 panel.repaint();
225 }
226
227
228
229
230 public boolean canContainChildren() {
231 return true;
232 }
233
234
235
236
237 public List getChildWidgets() {
238 return widgets;
239 }
240
241
242
243
244 public void addToParent(IWidget widget) {
245 if (widget instanceof IShowChildWidgets) {
246 List children = widget.getChildWidgets();
247 if (children != null && children.size() > 0) {
248 Iterator it = children.iterator();
249 while (it.hasNext()) {
250 IWidget w = (IWidget) it.next();
251 panel.add((JComponent) w.getNativeWidget(), w);
252 w.refresh();
253 }
254 }
255 } else {
256 panel.add((Component) widget.getNativeWidget(), widget);
257 widget.refresh();
258 }
259 }
260
261
262
263
264 public void focus() {
265 isRefreshing = true;
266 initialize();
267 panel.requestFocus();
268 isRefreshing = false;
269
270
271
272 if (!panel.isShowing() && getParent() != null ||
273 getProperty(TabPanel.TABID) != null) {
274
275
276 getPart().getSession().setValue("nyx.focusrequest", this);
277 getParent().focus();
278
279 getPart().getSession().remove("nyx.focusrequest");
280 panel.requestFocus();
281 }
282 }
283
284
285
286 public Object getGuiValue() {
287 return null;
288 }
289
290
291
292
293 public boolean canContainValue() {
294 return false;
295 }
296
297
298
299
300 public boolean isValueEmpty() {
301 return true;
302 }
303
304
305
306
307 public void addXuluxListener(IXuluxListener listener) {
308 }
309
310 }