KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JDesktopPane


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: JDesktopPane.java,v $
11    Revision 1.23 2004/05/06 12:35:22 bobintetley
12    Parity with Swing constants for Binary Compatibility + fixes to JDesktopPane
13
14    Revision 1.22 2004/05/05 13:24:32 bobintetley
15    Bugfixes and Laurent's patch for binary compatibility on Container.add()
16
17    Revision 1.21 2004/04/28 08:38:11 bobintetley
18    Hierarchy fixes, code cleanup for base classes, additional javadocs and use of flag to identify JComponent descendants with peers
19
20    Revision 1.20 2004/03/04 15:32:28 bobintetley
21    JInternalFrame methods now modify their peers after creation
22
23    Revision 1.19 2004/01/30 10:49:49 bobintetley
24    JInternalFrame/JDesktopPane fixes
25
26    Revision 1.18 2004/01/30 10:38:10 bobintetley
27    Fixes to JDesktopPane that caused frames to be added multiple times
28
29    Revision 1.17 2004/01/26 08:11:00 bobintetley
30    Many bugfixes and addition of SwingSet
31
32    Revision 1.16 2004/01/20 07:38:05 bobintetley
33    Bug fixes and compatibility methods
34
35    Revision 1.15 2004/01/15 15:20:30 bobintetley
36    Java2D work
37
38    Revision 1.14 2003/12/16 18:42:38 bobintetley
39    More thread safety
40
41    Revision 1.13 2003/12/16 12:36:08 bobintetley
42    Fix to closing of internal frames
43
44    Revision 1.12 2003/12/16 09:19:02 bobintetley
45    Various small fixes to match Swing more closely
46
47    Revision 1.11 2003/12/15 18:29:57 bobintetley
48    Changed setParent() method to setSwingWTParent() to avoid conflicts with applications
49
50    Revision 1.10 2003/12/14 09:13:38 bobintetley
51    Added CVS log to source headers
52
53 */

54
55
56 package swingwtx.swing;
57
58 import java.util.Iterator JavaDoc;
59 import java.util.Vector JavaDoc;
60
61 import org.eclipse.swt.widgets.Control;
62
63 import swingwt.awt.BorderLayout;
64 import swingwt.awt.Component;
65 import swingwtx.custom.JClosableTabbedPane;
66 import swingwtx.custom.event.TabCloseListener;
67
68 public class JDesktopPane extends JComponent {
69     
70     protected org.eclipse.swt.widgets.Composite ppeer = null;
71     protected Vector JavaDoc frames = new Vector JavaDoc();
72     protected JClosableTabbedPane jtp = null;
73
74     public JDesktopPane() {}
75     
76     public Control getPeer() { return ppeer; }
77     
78     /** Overridden add method to deal with JInternalFrames */
79     public Component add(final Component c) {
80         /** If it's not a frame, add it to us, but not the tabbed pane */
81         if (!(c instanceof JInternalFrame)) {
82             doAdd(c);
83             return c;
84         }
85         else
86             return (Component) addInternalFrame((JInternalFrame) c);
87     }
88     
89     /** Overridden add method to deal with JInternalFrames */
90     public void add(Component c, Object JavaDoc layoutModifier) {
91         add(c);
92     }
93     
94     /** Overridden add method to deal with JInternalFrames */
95     public Component add(String JavaDoc name, Component c) {
96         c.setName(name);
97         return add(c);
98     }
99     
100     /** Adds a JInternalFrame to the DesktopPane */
101     protected JInternalFrame addInternalFrame(final JInternalFrame frame) {
102         frames.add(frame);
103         frame.setParentPane(this);
104         if (jtp == null) return frame;
105         
106         final JDesktopPane me = this;
107         SwingUtilities.invokeSync(new Runnable JavaDoc() {
108             public void run() {
109                 if (jtp != null) {
110                     jtp.addTab(frame.getTitle(), frame.getFrameIcon(), frame, frame.getTitle());
111                     // Set focus to the added frame
112
jtp.setSelectedIndex(jtp.getTabCount() - 1);
113                 }
114             }
115         });
116         return frame;
117     }
118     
119     public void setSwingWTParent(swingwt.awt.Container parent) throws Exception JavaDoc {
120         descendantHasPeer = true;
121         ppeer = new org.eclipse.swt.widgets.Composite(parent.getComposite(), 0);
122         peer = ppeer;
123         composite = ppeer;
124         this.parent = parent;
125         super.setSwingWTParent(parent);
126         
127         // Add items onto a tabbed pane, filling this
128
// container.
129
setLayout(new BorderLayout());
130         jtp = new JClosableTabbedPane();
131         jtp.setTabPlacement(SwingConstants.TOP);
132         add(jtp, BorderLayout.CENTER);
133         
134         Iterator JavaDoc i = frames.iterator();
135         while (i.hasNext()) {
136             JInternalFrame frame = (JInternalFrame) i.next();
137             jtp.addTab(frame.getTitle(), frame.getFrameIcon(), frame, frame.getTitle());
138         }
139         
140         // Listen for close events
141
jtp.addTabCloseListener(new TabCloseListener() {
142             public boolean tabClosed(int index) {
143                 return ((JInternalFrame) frames.get(index)).processFrameClosing();
144             }
145         });
146     }
147     
148     /**
149      * Updates a frame's components from it's cached properties.
150      */

151     protected void refreshFrame(final JInternalFrame frame) {
152         
153         // If no frame specified, drop out
154
if (frame == null) return;
155         
156         // Go onto event dispatch thread to prevent frames being
157
// closed whilst we process
158
SwingUtilities.invokeSync(new Runnable JavaDoc() {
159             public void run() {
160                 
161                 // Get all frames to find this one (drop out if none exist)
162
final JInternalFrame[] frames = getAllFrames();
163                 if (frames == null) return;
164                 
165                 for (int i = 0; i < frames.length; i++)
166                     if (frames[i] == frame)
167                         updateFrameAt(i, frame);
168             }
169         });
170     }
171     
172     /**
173      * Copies an internal frame's properties onto it's component
174      */

175     protected void updateFrameAt(int index, JInternalFrame frame) {
176         jtp.setTitleAt(index, frame.getTitle());
177         jtp.setIconAt(index, frame.getFrameIcon());
178     }
179     
180     /** Removes an internal frame */
181     protected void removeFrame(JInternalFrame frame) {
182         int i = frames.indexOf(frame);
183         if (i != -1) {
184             jtp.removeTabAt(i);
185             frames.remove(frame);
186         }
187     }
188     
189     public JInternalFrame[] getAllFrames() {
190         JInternalFrame[] ret = new JInternalFrame[frames.size()];
191         Object JavaDoc[] jf = frames.toArray();
192         for (int i = 0; i < jf.length; i ++) {
193             ret[i] = (JInternalFrame) jf[i];
194         }
195         jf = null;
196         return ret;
197     }
198     
199     public JInternalFrame getSelectedFrame() {
200         if (jtp == null) return null;
201         return (JInternalFrame) frames.get(jtp.getSelectedIndex());
202     }
203     
204     public void setSelectedFrame(JInternalFrame frame) {
205         if (jtp == null) return;
206         int i = frames.indexOf(frame);
207         if (i != -1)
208             jtp.setSelectedIndex(i);
209     }
210     
211 }
212
Popular Tags