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.Component;
19 import java.awt.Container;
20 import java.awt.event.HierarchyEvent;
21 import java.awt.event.HierarchyListener;
22 import java.util.Iterator;
23
24 import javax.swing.Icon;
25 import javax.swing.JComponent;
26 import javax.swing.JTabbedPane;
27 import javax.swing.SwingUtilities;
28
29 import org.xulux.api.gui.IWidget;
30 import org.xulux.api.gui.IXuluxListener;
31 import org.xulux.gui.ContainerWidget;
32 import org.xulux.guilayer.swing.util.SwingUtils;
33
34
35
36
37
38
39
40
41
42 public class TabPanel extends ContainerWidget {
43
44
45
46
47 private JTabbedPane tabPanel;
48
49
50
51
52 private int tabCount;
53
54
55
56
57
58 public static final String TABID = "nyx-tab-id";
59
60
61
62
63 private String initialFocus;
64
65
66
67 private RepaintComponent repaintComponent;
68
69
70
71 private Thread repaintThread;
72
73
74
75
76 public TabPanel(String name) {
77 super(name);
78 }
79
80
81
82
83 public void destroy() {
84 super.destroy();
85 if (tabPanel == null) {
86 return;
87 }
88 if (repaintComponent != null) {
89 tabPanel.removeHierarchyListener(repaintComponent);
90 repaintComponent = null;
91 }
92 Container container = tabPanel.getParent();
93 tabPanel.setVisible(false);
94 tabPanel.removeAll();
95 if (container != null) {
96 container.remove(tabPanel);
97 }
98 tabPanel = null;
99 }
100
101
102
103
104
105 public void focus() {
106 Object object = getPart().getSession().getValue("nyx.focusrequest");
107 if (object != null) {
108 IWidget w = (IWidget) object;
109 System.out.println("tabcount : " + w.getProperty(TABID));
110 w = findChildAndParentForFocus(w);
111 this.tabPanel.setSelectedComponent((JComponent) w.getNativeWidget());
112 }
113 this.tabPanel.requestFocus();
114 }
115
116
117
118
119
120
121
122 private IWidget findChildAndParentForFocus(IWidget w) {
123 while (w != null) {
124 if (getChildWidgets().contains(w)) {
125 return w;
126 } else {
127 w = w.getParent();
128 }
129 }
130 return null;
131 }
132
133
134
135
136 public Object getNativeWidget() {
137 initialize();
138 return this.tabPanel;
139 }
140
141
142
143
144 public void initialize() {
145 if (initialized) {
146 return;
147 }
148
149 initialized = true;
150 tabPanel = new JTabbedPane();
151
152
153
154
155
156 initializeChildren();
157 refresh();
158 processInit();
159 }
160
161
162
163
164 public void refresh() {
165 isRefreshing = true;
166 initialize();
167
168 isRefreshing = false;
169 }
170
171
172
173
174
175
176
177
178
179 public void addToParent(IWidget widget) {
180
181 if (widget instanceof Panel || widget instanceof Tab) {
182 System.out.println("Adding panel " + widget);
183 String tabTitle = widget.getProperty("title");
184 String tabTip = widget.getProperty("tip");
185 String tabIcon = widget.getProperty("icon");
186 Icon icon = null;
187 if (tabIcon != null) {
188 try {
189 icon = SwingUtils.getIcon(tabIcon, this);
190 } catch (Exception e) {
191 System.out.println("Icon resource " + tabIcon + " cannot be found");
192 }
193 }
194
195 widget.setParent(this);
196 tabPanel.addTab(tabTitle, icon, (Component) widget.getNativeWidget(), tabTip);
197
198
199 tabPanel.setToolTipTextAt(tabCount, tabTip);
200
201 widget.setLazyProperty(TABID, String.valueOf(tabCount));
202
203 if (initialFocus == null && widget.isEnabled() && widget.isVisible()) {
204 initialFocus = String.valueOf(tabCount);
205 tabPanel.setSelectedIndex(tabCount);
206 }
207
208 tabCount++;
209 } else {
210
211
212 System.out.println("Only panel widgets are allowed on top of a tabPanel, skipping widget " + widget);
213 }
214 }
215
216
217
218 public Object getGuiValue() {
219 return null;
220 }
221
222
223
224
225 public boolean canContainValue() {
226 return false;
227 }
228
229
230
231
232 public boolean isValueEmpty() {
233 return true;
234 }
235
236
237
238
239 public void addXuluxListener(IXuluxListener listener) {
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253
254 public class RepaintComponent implements Runnable, HierarchyListener {
255
256
257
258 private int index = 0;
259
260
261
262 private boolean isRunning;
263
264
265
266
267 public RepaintComponent() {
268 }
269
270
271
272
273 public void run() {
274 if (getPart() == null) {
275 return;
276 }
277 while (!tabPanel.isShowing()) {
278
279 boolean sleep = true;
280 if (sleep) {
281 try {
282 Thread.sleep(1000);
283 } catch (InterruptedException ie) {
284
285 sleep = false;
286 break;
287 }
288 }
289 }
290 int selected = tabPanel.getSelectedIndex();
291 for (int i = 0; i < tabPanel.getTabCount(); i++) {
292 selectIndex(i);
293 }
294 selectIndex(selected);
295
296 tabPanel.validate();
297 tabPanel.repaint();
298 }
299
300
301
302
303
304 public void selectIndex(final int index) {
305 try {
306 SwingUtilities.invokeAndWait(new Runnable() {
307
308
309
310
311 public void run() {
312 tabPanel.setSelectedIndex(index);
313 }
314 });
315 } catch (Exception e) {
316 e.printStackTrace(System.out);
317 }
318 }
319
320
321
322
323 public void hierarchyChanged(HierarchyEvent e) {
324 if (isRunning) {
325 return;
326 }
327 Iterator it = getPart().getWidgets().iterator();
328 while (it.hasNext()) {
329 IWidget w = (IWidget) it.next();
330 if (e.getChanged().equals(w.getNativeWidget())) {
331 break;
332 }
333 }
334
335 if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) {
336 repaintThread.interrupt();
337 }
338 }
339
340 }
341 }