KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JTabbedPane


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: JTabbedPane.java,v $
11    Revision 1.30 2004/05/06 12:35:22 bobintetley
12    Parity with Swing constants for Binary Compatibility + fixes to JDesktopPane
13
14    Revision 1.29 2004/05/05 13:24:32 bobintetley
15    Bugfixes and Laurent's patch for binary compatibility on Container.add()
16
17    Revision 1.28 2004/04/30 16:52:17 bobintetley
18    MenuListener support, JViewport support, TreeSelectionModel stubs, additional JTree methods
19
20    Revision 1.27 2004/04/28 08:38:12 bobintetley
21    Hierarchy fixes, code cleanup for base classes, additional javadocs and use of flag to identify JComponent descendants with peers
22
23    Revision 1.26 2004/04/27 06:37:44 bobintetley
24    Corrected hierarchy with JComponent descending Container
25
26    Revision 1.25 2004/03/30 10:42:46 bobintetley
27    Many minor bug fixes, event improvements by Dan Naab. Full swing.Icon support
28
29    Revision 1.24 2004/01/26 15:45:47 bobintetley
30    SwingSet fixes
31
32    Revision 1.23 2004/01/26 10:57:45 bobintetley
33    HTML handling (throws it away - SWT can't do anything with it)
34
35    Revision 1.22 2004/01/26 10:03:37 bobintetley
36    Fixed component painting, added overloaded TabbedPane add() methods
37
38    Revision 1.21 2004/01/26 09:50:11 bobintetley
39    Fix to tabbed pane cache
40
41    Revision 1.20 2004/01/26 08:11:00 bobintetley
42    Many bugfixes and addition of SwingSet
43
44    Revision 1.19 2004/01/20 07:38:05 bobintetley
45    Bug fixes and compatibility methods
46
47    Revision 1.18 2004/01/16 15:53:32 bobintetley
48    Many compatibility methods added to Container, Component, JInternalFrame,
49       UIManager, SwingUtilities, JTabbedPane, JPasswordField, JCheckBox
50       and JRadioButton.
51
52    Revision 1.17 2004/01/02 11:30:53 bobintetley
53    Change events are now supported
54
55    Revision 1.16 2003/12/22 14:31:29 bobintetley
56    Minor changes
57
58    Revision 1.15 2003/12/22 14:17:30 bobintetley
59    One-off timer method and better layout management for tabs
60
61    Revision 1.14 2003/12/18 09:37:33 bobintetley
62    Enabled support for JTabbedPane and JClosableTabbedPane (SWT doesn't
63    support, so a bit of magic in here)
64
65    Revision 1.13 2003/12/16 13:14:33 bobintetley
66    Use of SwingWTUtils.isSWTControlAvailable instead of null test
67
68    Revision 1.12 2003/12/16 09:19:02 bobintetley
69    Various small fixes to match Swing more closely
70
71    Revision 1.11 2003/12/15 18:29:57 bobintetley
72    Changed setParent() method to setSwingWTParent() to avoid conflicts with applications
73
74    Revision 1.10 2003/12/14 09:13:38 bobintetley
75    Added CVS log to source headers
76
77 */

78
79
80 package swingwtx.swing;
81
82 import org.eclipse.swt.widgets.*;
83 import org.eclipse.swt.*;
84
85 import swingwt.awt.Component;
86 import swingwtx.swing.event.*;
87
88 import java.util.*;
89
90 public class JTabbedPane extends JComponent implements SwingConstants {
91     
92     protected TabFolder ppeer = null;
93     protected int pTabPlacement = TOP;
94     protected Vector changeListeners = new Vector();
95     
96     /** Tab item cache until added to parent */
97     protected Vector tabs = new Vector();
98     
99     /** Thread safe accessor */
100     private int iRetVal = 0;
101     private String JavaDoc sRetVal = "";
102     private Object JavaDoc retval;
103     
104     public JTabbedPane() { }
105     
106     public void addChangeListener(ChangeListener l) {
107         changeListeners.add(l);
108     }
109     
110     public void removeChangeListener(ChangeListener l) {
111         changeListeners.remove(l);
112     }
113     
114     public void addTab(String JavaDoc title, swingwt.awt.Component component) {
115         addTab(title, null, component);
116     }
117     
118     public void addTab(String JavaDoc title, Icon icon, swingwt.awt.Component component) {
119         addTab(title, icon, component, null, true);
120     }
121     
122     public void addTab(final String JavaDoc title, final Icon icon, final swingwt.awt.Component component, final String JavaDoc tip) {
123         addTab(title, icon, component, tip, true);
124     }
125     
126     public void addTab(final String JavaDoc title, final Icon icon, final swingwt.awt.Component component, final String JavaDoc tip, final boolean enabled) {
127         addTab(title, icon, component, tip, enabled, false);
128     }
129     
130     public void add(swingwt.awt.Component component, Object JavaDoc modifiers) {
131         addTab("", component);
132     }
133     public swingwt.awt.Component add(swingwt.awt.Component component) {
134         addTab("", component);
135         return component;
136     }
137     public swingwt.awt.Component add(String JavaDoc title, swingwt.awt.Component component) {
138         addTab(title, component);
139         return component;
140     }
141     
142     public void addTab(final String JavaDoc title, final Icon icon, final swingwt.awt.Component component, final String JavaDoc tip, final boolean enabled, boolean fromcache) {
143         
144         // Create the cache entry
145
if (!fromcache) {
146             TabItemCache t = new TabItemCache();
147             t.title = title;
148             t.icon = icon;
149             t.component = component;
150             t.tip = tip;
151             t.enabled = enabled;
152             tabs.add(t);
153             t = null;
154             return;
155         }
156         
157         final JTabbedPane me = this;
158         SwingUtilities.invokeSync(new Runnable JavaDoc() {
159             public void run() {
160                 if (!SwingWTUtils.isSWTControlAvailable(ppeer)) return;
161
162                 TabItem t = new TabItem(ppeer, SWT.NONE);
163
164                 // Make sure the component contained by this
165
// TabItem gets created now.
166
try {
167                     component.setSwingWTParent(me);
168                 }
169                 catch (Exception JavaDoc e) {
170                     e.printStackTrace();
171                 }
172
173                 if (title != null) t.setText(SwingWTUtils.removeHTML(title));
174                 if (icon != null) t.setImage(SwingWTUtils.getSWTImageFromSwingIcon(me, icon));
175                 
176                 // Only display the component if it's enabled
177
if (component != null && enabled) t.setControl(component.getPeer());
178                 
179                 if (tip != null) t.setToolTipText(tip);
180                 t.setData("enabled", (enabled ? "true" : "false"));
181
182                 // Display the thing
183
postponeLayout();
184             }
185         });
186         
187     }
188     
189     public Icon getIconAt(final int index) {
190         if (!SwingWTUtils.isSWTControlAvailable(ppeer)) {
191             TabItemCache t = (TabItemCache) tabs.get(index);
192             return t.icon;
193         }
194         else {
195             SwingUtilities.invokeSync(new Runnable JavaDoc() {
196                 public void run() {
197                     swingwt.awt.Image image = new swingwt.awt.Image();
198                     image.image = ppeer.getItem(index).getImage();
199                     retval = new ImageIcon(image);
200                 }
201             });
202             return (Icon) retval;
203         }
204     }
205     public void setIconAt(final int index, final Icon icon) {
206         final JTabbedPane pthis = this;
207         if (!SwingWTUtils.isSWTControlAvailable(ppeer)) {
208             TabItemCache t = (TabItemCache) tabs.get(index);
209             t.icon = icon;
210         }
211         else
212             SwingUtilities.invokeSync(new Runnable JavaDoc() {
213                 public void run() {
214                     ppeer.getItem(index).setImage(
215                         SwingWTUtils.getSWTImageFromSwingIcon(pthis, icon));
216                 }
217             });
218     }
219     
220     public void setEnabledAt(final int index, final boolean b) {
221         if (SwingWTUtils.isSWTControlAvailable(ppeer)) {
222             SwingUtilities.invokeSync(new Runnable JavaDoc() {
223                 public void run() {
224                     ppeer.getItem(index).setData("enabled", (b ? "true" : "false"));
225                     // If we're disabled - hide the tab's control. This is about all we
226
// can do with SWT (without writing our own tabbed interface of course).
227
if (!b)
228                         ppeer.getItem(index).setControl(null);
229                     else
230                         ppeer.getItem(index).setControl( ((TabItemCache) tabs.get(index)).component.getPeer() );
231                 }
232             });
233         }
234         else {
235             TabItemCache t = (TabItemCache) tabs.get(index);
236             t.enabled = b;
237         }
238     }
239     
240     public int getTabCount() {
241         if (!SwingWTUtils.isSWTControlAvailable(ppeer))
242             return tabs.size();
243         else {
244             SwingUtilities.invokeSync(new Runnable JavaDoc() {
245                 public void run() {
246                     iRetVal = ppeer.getItemCount();
247                 }
248             });
249             return iRetVal;
250         }
251     }
252     public int getSelectedIndex() {
253         if (SwingWTUtils.isSWTControlAvailable(ppeer)) {
254             SwingUtilities.invokeSync(new Runnable JavaDoc() {
255                 public void run() {
256                     iRetVal = ppeer.getSelectionIndex();
257                 }
258             });
259         }
260         else
261             iRetVal = -1;
262         return iRetVal;
263     }
264     
265     public swingwt.awt.Component getSelectedComponent() {
266         int i = getSelectedIndex();
267         if (i != -1)
268             return ((TabItemCache) tabs.get(i)).component;
269         else
270             return null;
271     }
272     
273     public void setSelectedIndex(final int index) {
274         if (SwingWTUtils.isSWTControlAvailable(ppeer))
275             SwingUtilities.invokeSync(new Runnable JavaDoc() {
276                 public void run() {
277                     ppeer.setSelection(index);
278                 }
279             });
280     }
281     
282     public void removeTabAt(final int index) {
283         if (SwingWTUtils.isSWTControlAvailable(ppeer)) {
284             SwingUtilities.invokeSync(new Runnable JavaDoc() {
285                 public void run() {
286                     tabs.removeElementAt(index);
287                     ppeer.getItem(index).dispose();
288                 }
289             });
290         }
291         else
292             tabs.removeElementAt(index);
293     }
294     
295     public String JavaDoc getTitleAt(final int index) {
296         if (SwingWTUtils.isSWTControlAvailable(ppeer)) {
297             SwingUtilities.invokeSync(new Runnable JavaDoc() {
298                 public void run() {
299                     sRetVal = ppeer.getItem(index).getText();
300                 }
301             });
302             return sRetVal;
303         }
304         else {
305             TabItemCache t = (TabItemCache) tabs.get(index);
306             return t.title;
307         }
308     }
309     
310     public void setTitleAt(final int index, final String JavaDoc title) {
311         if (SwingWTUtils.isSWTControlAvailable(ppeer)) {
312             SwingUtilities.invokeSync(new Runnable JavaDoc() {
313                 public void run() {
314                     ppeer.getItem(index).setText(SwingWTUtils.removeHTML(title));
315                 }
316             });
317         }
318         else {
319             TabItemCache t = (TabItemCache) tabs.get(index);
320             t.title = title;
321         }
322     }
323     
324     public String JavaDoc getToolTipTextAt(final int index) {
325         if (SwingWTUtils.isSWTControlAvailable(ppeer)) {
326             SwingUtilities.invokeSync(new Runnable JavaDoc() {
327                 public void run() {
328                     sRetVal = ppeer.getItem(index).getToolTipText();
329                 }
330             });
331             return sRetVal;
332         }
333         else {
334             TabItemCache t = (TabItemCache) tabs.get(index);
335             return t.tooltip;
336         }
337     }
338     
339     public void setToolTipTextAt(final int index, final String JavaDoc text) {
340         if (SwingWTUtils.isSWTControlAvailable(ppeer)) {
341             SwingUtilities.invokeSync(new Runnable JavaDoc() {
342                 public void run() {
343                     ppeer.getItem(index).setToolTipText(text);
344                 }
345             });
346         }
347         else {
348             TabItemCache t = (TabItemCache) tabs.get(index);
349             t.tooltip = text;
350         }
351     }
352     
353     public int indexOfComponent(final Component c) {
354         final int[] ret = new int[1];
355         SwingUtilities.invokeSync(new Runnable JavaDoc() {
356             public void run() {
357                 if (SwingWTUtils.isSWTControlAvailable(ppeer)) {
358                     // Search items
359
org.eclipse.swt.widgets.TabItem[] tabs = ppeer.getItems();
360                     for (int i = 0; i < tabs.length; i++) {
361                         if (tabs[i].getControl().equals(c.getPeer())) {
362                             ret[0] = i;
363                             break;
364                         }
365                     }
366                 }
367                 else {
368                     // Search cache
369
for (int i = 0; i < tabs.size(); i++) {
370                         if (((TabItemCache) tabs.get(i)).component.equals(c)) {
371                             ret[0] = i;
372                             break;
373                         }
374                     }
375                 }
376             }
377         });
378         return ret[0];
379     }
380     
381     public int indexOfTab(final String JavaDoc name) {
382         final int[] ret = new int[1];
383         SwingUtilities.invokeSync(new Runnable JavaDoc() {
384             public void run() {
385                 if (SwingWTUtils.isSWTControlAvailable(ppeer)) {
386                     // Search items
387
org.eclipse.swt.widgets.TabItem[] tabs = ppeer.getItems();
388                     for (int i = 0; i < tabs.length; i++) {
389                         if (tabs[i].getText().equals(name)) {
390                             ret[0] = i;
391                             break;
392                         }
393                     }
394                 }
395                 else {
396                     // Search cache
397
for (int i = 0; i < tabs.size(); i++) {
398                         if (((TabItemCache) tabs.get(i)).title.equals(name)) {
399                             ret[0] = i;
400                             break;
401                         }
402                     }
403                 }
404             }
405         });
406         return ret[0];
407     }
408     
409     public int indexOfTab(final Icon icon) {
410         final int[] ret = new int[1];
411         SwingUtilities.invokeSync(new Runnable JavaDoc() {
412             public void run() {
413                 // Search cache - we can only check the cache for Icons
414
// as the objects are turned into something else for
415
// use on the SWT peer.
416
for (int i = 0; i < tabs.size(); i++) {
417                     if (((TabItemCache) tabs.get(i)).icon.equals(icon)) {
418                         ret[0] = i;
419                         break;
420                     }
421                 }
422             }
423         });
424         return ret[0];
425     }
426     
427     public Component getComponentAt(int index) {
428         return ((TabItemCache) tabs.get(index)).component;
429     }
430     
431     public int getTabPlacement() { return pTabPlacement; }
432     public void setTabPlacement(int place) { pTabPlacement = place; }
433     
434     public Control getPeer() { return ppeer; }
435     
436     protected void postponeLayout() {
437         SwingUtilities.invokeLater(new Runnable JavaDoc() {
438             public void run() {
439                  layoutTab();
440             }
441         });
442     }
443     
444     protected void layoutTab() {
445         Control p = ppeer.getItem(ppeer.getSelectionIndex()).getControl();
446         if (p instanceof Composite)
447             ((Composite) p).layout(true);
448         else
449             parent.invalidate();
450     }
451     
452     /**
453      * Process change events - fired when the tab changes
454      */

455     protected void processChangeEvent(ChangeEvent e) {
456         Iterator i = changeListeners.iterator();
457         while (i.hasNext()) {
458             ChangeListener l = (ChangeListener) i.next();
459             l.stateChanged(e);
460         }
461     }
462     
463     public void setSwingWTParent(swingwt.awt.Container parent) throws Exception JavaDoc {
464         descendantHasPeer = true;
465         ppeer = new TabFolder(parent.getComposite(), SwingWTUtils.translateSwingAlignmentConstant(pTabPlacement));
466         peer = ppeer;
467         composite = ppeer;
468         this.parent = parent;
469         
470         // Add any cached items
471
if (tabs.size() > 0) {
472             Object JavaDoc[] tabso = tabs.toArray();
473             for (int i = 0; i < tabso.length; i++) {
474                 TabItemCache t = (TabItemCache) tabso[i];
475                 addTab(t.title, t.icon, t.component, t.tip, t.enabled, true);
476             }
477         }
478         
479         // -- Tab is selected, may want to block it if we have it
480
// flagged as disabled.
481
final JTabbedPane me = this;
482         ppeer.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
483             public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
484                 // We don't send events if the tab is disabled
485
if (checkTabEnabled(e)) {
486                     postponeLayout(); // Update layout of tab if we went to a new one
487
// Send change event
488
ChangeEvent ce = new ChangeEvent(me);
489                     processChangeEvent(ce);
490                 }
491             }
492             public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {}
493         });
494         
495         // Make sure first one gets displayed
496
if (ppeer.getItemCount() > 0)
497             postponeLayout();
498         
499     }
500     
501     /** Returns true if the tab is enabled according to the data set on it.
502      * This should be called from a selection event, determining whether
503      * the user is allowed to go to it or not.
504      */

505     protected boolean checkTabEnabled(org.eclipse.swt.events.SelectionEvent e) {
506         e.doit = (e.item.getData("enabled").toString().equals("true"));
507         return e.doit;
508     }
509    
510 }
511
Popular Tags