KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > Window


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: Window.java,v $
11    Revision 1.37 2004/05/05 23:28:40 laurentmartelli
12    setTitle: checks peer is not disposed
13
14    Revision 1.36 2004/05/05 13:24:30 bobintetley
15    Bugfixes and Laurent's patch for binary compatibility on Container.add()
16
17    Revision 1.35 2004/05/05 12:43:19 bobintetley
18    Patches/new files from Laurent Martell
19
20    Revision 1.34 2004/05/04 16:30:01 bobintetley
21    Fix to check peer in repaintFix()
22
23    Revision 1.33 2004/05/04 14:59:55 bobintetley
24    Fix to ensure getBounds() works correctly for Window descendants
25
26    Revision 1.32 2004/04/30 13:20:40 bobintetley
27    Fix to unrealised peer preferred sizes, forwarding window events to
28    content panes and fix for mouse drag events.
29
30    Revision 1.31 2004/04/19 14:50:54 bobintetley
31    Window.isActive() support
32
33    Revision 1.30 2004/04/19 12:49:33 bobintetley
34    JTaskTray implementation (and demo), along with Frame repaint fix
35
36    Revision 1.29 2004/04/18 20:07:09 bobintetley
37    GTK2 repainting bug fixed
38
39    Revision 1.28 2004/04/16 10:19:06 dannaab
40    Misc bug fixes, InputMap implementation, preliminary undo support
41
42    Revision 1.27 2004/03/22 15:10:21 bobintetley
43    JRootPane and JLayeredPane implementation
44
45    Revision 1.26 2004/03/03 09:13:12 bobintetley
46    JList threading fixed and top level error handling
47
48    Revision 1.25 2004/03/01 12:25:46 bobintetley
49    Better HTML conversion, custom JFileChooser support, JLabel, Window and
50    Image fixes to improve compatibility
51
52    Revision 1.24 2004/02/24 09:51:34 bobintetley
53    Better contentPane support
54
55    Revision 1.23 2004/02/02 15:20:45 bobintetley
56    setContentPane() support and pack now includes frame decorations
57
58    Revision 1.22 2004/02/02 14:40:22 bobintetley
59    Non-modal dialogs now work correctly
60
61    Revision 1.21 2004/02/02 14:17:45 bobintetley
62    Window pack() now works correctly
63
64    Revision 1.20 2004/01/26 08:10:59 bobintetley
65    Many bugfixes and addition of SwingSet
66
67    Revision 1.19 2004/01/23 08:04:24 bobintetley
68    JComboBox fixes and better Action implementation
69
70    Revision 1.18 2004/01/20 07:38:05 bobintetley
71    Bug fixes and compatibility methods
72
73    Revision 1.17 2004/01/16 09:35:46 bobintetley
74    Full event dispatch thread support!
75
76    Revision 1.16 2004/01/06 10:29:21 bobintetley
77    Dispatch thread retardation code
78
79    Revision 1.15 2004/01/05 16:52:35 bobintetley
80    Minor class name fixes
81
82    Revision 1.14 2004/01/05 09:18:05 bobintetley
83    Merged Daniel's changes
84
85    Revision 1.13 2004/01/05 02:50:39 djspiewak
86    Added JToolBar peer functionality and commenced AWT layout manager support
87
88    Revision 1.12 2003/12/16 15:47:44 bobintetley
89    Thread safety added to common methods
90
91    Revision 1.11 2003/12/16 14:51:16 bobintetley
92    Fixed hang when a window close event closes itself again
93
94    Revision 1.10 2003/12/16 09:19:02 bobintetley
95    Various small fixes to match Swing more closely
96
97    Revision 1.9 2003/12/15 18:29:56 bobintetley
98    Changed setParent() method to setSwingWTParent() to avoid conflicts with applications
99
100    Revision 1.8 2003/12/15 15:54:25 bobintetley
101    Additional core methods
102
103    Revision 1.7 2003/12/14 09:13:38 bobintetley
104    Added CVS log to source headers
105
106  */

107
108 package swingwt.awt;
109
110 import org.eclipse.swt.widgets.*;
111 import org.eclipse.swt.*;
112 import org.eclipse.swt.events.*;
113
114 import swingwtx.swing.*;
115 import java.util.*;
116
117 public class Window extends Container {
118
119     protected Shell peer = null;
120     protected Display display = null;
121     protected Vector windowListeners = new Vector();
122     protected boolean isClosed = false;
123     protected boolean isIcon = false;
124
125     protected JRootPane rootPane = null;
126
127     /** Number of pixels to allow for frame decoration during pack() */
128     protected final static int FRAME_DECORATION_HEIGHT = 24;
129     /** Number of pixels to allow for frame decoration during pack() */
130     protected final static int FRAME_DECORATION_WIDTH = 4;
131
132     private Object JavaDoc retval = null;
133     private Container owner = null;
134
135     /** Does nothing */
136     GraphicsConfiguration graphicsConfig = null;
137
138     public Window(Frame owner) {
139         this.owner = owner;
140         graphicsConfig = (owner == null ? null : owner.getGraphicsConfiguration());
141         create();
142     }
143     public Window(Window owner) {
144         this.owner = owner;
145         graphicsConfig = (owner == null ? null : owner.getGraphicsConfiguration());
146         create();
147     }
148     public Window(Window owner, GraphicsConfiguration gc) {
149         this.owner = owner;
150         graphicsConfig = (owner == null ? null : owner.getGraphicsConfiguration());
151         create();
152     }
153
154     // Package protected helper to assist in initializing JDialogs, which have some properties that
155
// can't be handled in the sub class. Should ONLY be called from Dialog class.
156
Window(Window owner, boolean isModal)
157     {
158         this.owner = owner;
159         graphicsConfig = (owner == null ? null : owner.getGraphicsConfiguration());
160
161         int frameType = SWT.DIALOG_TRIM;
162         if (isModal) frameType |= SWT.APPLICATION_MODAL;
163         else frameType |= SWT.MODELESS;
164
165         create(frameType);
166     }
167
168     protected void create() {
169
170         int SWTFrameType = SWT.NO_TRIM;
171
172         if (this instanceof Dialog)
173             SWTFrameType = SWT.DIALOG_TRIM;
174         else if (this instanceof Frame)
175             SWTFrameType = SWT.SHELL_TRIM;
176
177         create(SWTFrameType);
178     }
179
180     /**
181      * Actually does the work of creating the Window peer
182      * All the descendants of Window call this with an appropriate SWT type
183      * as part of their constructor.
184      *
185      * A bit cheaty, but we use the Swing JRootPane stuff here as well
186      * so it only needs doing once and subclasses can use it if they want it.
187      */

188     protected void create(final int SWTFrameType) {
189         SwingWTUtils.incrementWindowReferences();
190         SwingUtilities.invokeSync(new Runnable JavaDoc() {
191             public void run() {
192                 createImpl(SWTFrameType);
193             }
194         });
195     }
196
197     /**
198      * The actual create implementation. This is just so we don't
199      * have to muck about inside anonymous inner classes.
200      *
201      * This routine creates the peer and rootpane ready for use.
202      *
203      * @param SWTFrameType The SWT constant to use when creating the Shell
204      */

205     private void createImpl(int SWTFrameType) {
206         display = SwingWTUtils.getDisplay();
207
208         if (owner == null)
209             peer = new Shell(display, SWTFrameType);
210         else
211             peer = new Shell( (Shell)owner.getPeer(), SWTFrameType);
212
213         composite = peer;
214         peer.setLayout(new swingwt.awt.swtcustomlayout.SWTBorderLayout());
215         rootPane = new JRootPane(this);
216         try { rootPane.setSwingWTParent(this); } catch (Exception JavaDoc e) { e.printStackTrace(); }
217         peer.setLayoutData(swingwt.awt.swtcustomlayout.SWTBorderLayout.CENTER);
218         registerWindowEvents();
219     }
220
221     /** Overridden to point to getContentPane() rather than throwing an error */
222     public Component add(swingwt.awt.Component c) { rootPane.getContentPane().add(c); return c; }
223     /** Overridden to point to getContentPane() rather than throwing an error */
224     public void add(swingwt.awt.Component c, Object JavaDoc layoutModifier) { rootPane.getContentPane().add(c, layoutModifier); }
225     /** Overridden to point to getContentPane() rather than throwing an error */
226     public void remove(swingwt.awt.Component c) { rootPane.getContentPane().remove(c); }
227     /** Overridden to point to getContentPane() rather than throwing an error */
228     public LayoutManager getLayout() {
229         return rootPane.getContentPane().getLayout();
230     }
231     /** Overridden to add mouse listeners to the content pane instead */
232     public void addMouseListener(swingwt.awt.event.MouseListener l) {
233         rootPane.getContentPane().addMouseListener(l);
234     }
235     /** Overridden to remove mouse listeners from the content pane instead */
236     public void removeMouseListener(swingwt.awt.event.MouseListener l) {
237         rootPane.getContentPane().removeMouseListener(l);
238     }
239     /** Overridden to point to getContentPane() rather than throwing an error */
240     public void setLayout(LayoutManager l) {
241         rootPane.getContentPane().setLayout(l);
242     }
243     /** Overridden to point to getContentPane() rather than throwing an error */
244     public void setLayout(LayoutManager2 l) {
245         rootPane.getContentPane().setLayout(l);
246     }
247
248     public void pack() {
249         final Window me = this;
250         SwingUtilities.invokeSync(new Runnable JavaDoc() {
251             public void run() {
252                 Dimension d = rootPane.getContentPane().getPreferredSize();
253                 d.width += FRAME_DECORATION_WIDTH;
254                 d.height += FRAME_DECORATION_HEIGHT;
255                 peer.setSize( d.width, d.height );
256             }
257         });
258     }
259     public void setLocation(final int x, final int y) {
260         SwingUtilities.invokeSync(new Runnable JavaDoc() {
261             public void run() {
262                 peer.setLocation(x, y);
263             }
264         });
265     }
266
267     public void setLocationRelativeTo(Component c) {
268         // Center the Window
269
Dimension paneSize = getSize();
270         Dimension screenSize = getToolkit().getScreenSize();
271         setLocation((screenSize.width - paneSize.width) / 2,
272                     (screenSize.height - paneSize.height) / 2);
273     }
274
275     public Dimension getSize() {
276         SwingUtilities.invokeSync(new Runnable JavaDoc() {
277             public void run() {
278                 retval = new Dimension(peer.getBounds().width, peer.getBounds().height);
279             }
280         });
281         return (Dimension) retval;
282     }
283
284     public Rectangle getBounds() {
285     SwingUtilities.invokeSync(new Runnable JavaDoc() {
286         public void run() {
287         retval = new Rectangle(peer.getLocation().x, peer.getLocation().y, peer.getSize().x, peer.getSize().y);
288         }
289     });
290     return (Rectangle) retval;
291     }
292
293     public void setSize(final int width, final int height) {
294         SwingUtilities.invokeSync(new Runnable JavaDoc() {
295             public void run() {
296                 peer.setSize(width, height);
297             }
298         });
299     }
300     public void setSize(final Dimension d) {
301         SwingUtilities.invokeSync(new Runnable JavaDoc() {
302             public void run() {
303                 peer.setSize(d.width, d.height);
304             }
305         });
306     }
307     public void setBounds(final int x, final int y, final int width, final int height) {
308         SwingUtilities.invokeSync(new Runnable JavaDoc() {
309             public void run() {
310                 peer.setLocation(x, y); peer.setSize(width, height);
311             }
312         });
313     }
314
315     public void setBounds(final Rectangle r) {
316         SwingUtilities.invokeSync(new Runnable JavaDoc() {
317             public void run() {
318                 peer.setLocation(r.x, r.y); peer.setSize(r.width, r.height);
319             }
320         });
321     }
322
323     public boolean isActive() {
324         final boolean[] ret = new boolean[1];
325         SwingUtilities.invokeSync(new Runnable JavaDoc() {
326             public void run() {
327                 ret[0] = (SwingWTUtils.getDisplay().getActiveShell() == peer);
328             }
329         });
330         return ret[0];
331     }
332     
333     public void show() { invalidate(); setVisible(true); }
334     public void hide() { setVisible(false); }
335     public void addWindowListener(swingwt.awt.event.WindowListener l) { windowListeners.add(l); }
336     public void removeWindowListener(swingwt.awt.event.WindowListener l) { windowListeners.remove(l); }
337
338     public void setVisible(final boolean b) {
339         SwingUtilities.invokeSync(new Runnable JavaDoc() {
340             public void run() {
341                 if (b) {
342                     peer.open();
343                     peer.layout(); // Necessary for Mac OS X users
344
processWindowEvent(swingwt.awt.event.WindowEvent.WINDOW_OPENED);
345                     
346                     // Necessary for GTK2
347
if (!SwingWTUtils.isWindows()) repaintFix();
348                 }
349                 else {
350                     // If the Window is already closing because the user hit the x
351
// button, don't do anything
352
if (isClosed) return;
353                     peer.close();
354                 }
355             }
356         });
357     }
358
359     public String JavaDoc getTitle() {
360         SwingUtilities.invokeSync(new Runnable JavaDoc() {
361             public void run() {
362                 retval = peer.getText();
363             }
364         });
365         return retval.toString();
366     }
367
368     public void setTitle(final String JavaDoc s) {
369         if (!peer.isDisposed()) {
370             SwingUtilities.invokeSync(new Runnable JavaDoc() {
371                     public void run() {
372                         peer.setText(s);
373                     }
374                 });
375         }
376     }
377
378     public Image getIconImage() {
379         final Image[] ret = new Image[1];
380         SwingUtilities.invokeSync(new Runnable JavaDoc() {
381             public void run() {
382                 ret[0] = new Image();
383                 ret[0].image = peer.getImage();
384             }
385         });
386         return ret[0];
387     }
388
389     public void setIconImage(final Image icon) {
390         SwingUtilities.invokeSync(new Runnable JavaDoc() {
391             public void run() {
392                 peer.setImage(icon.image);
393             }
394         });
395     }
396
397     public void dispose() {
398         SwingUtilities.invokeSync(new Runnable JavaDoc() {
399             public void run() {
400                 peer.dispose();
401             }
402         });
403         super.dispose();
404     }
405
406     public void setJMenuBar(JMenuBar menu) {
407         rootPane.setJMenuBar(menu);
408     }
409
410     public JMenuBar getJMenuBar() {
411         return rootPane.getJMenuBar();
412     }
413
414     public void setMenuBar(MenuBar menu) {
415         rootPane.setMenuBar(menu);
416     }
417
418     public MenuBar getMenuBar() {
419         return rootPane.getMenuBar();
420     }
421
422     public void setDefaultButton(JButton button) {
423         rootPane.setDefaultButton(button);
424     }
425
426     public JButton getDefaultButton() {
427         return rootPane.getDefaultButton();
428     }
429
430     public void registerWindowEvents() {
431
432         peer.addShellListener(new ShellListener() {
433             public void shellActivated(ShellEvent e) {
434                 processWindowEvent(swingwt.awt.event.WindowEvent.WINDOW_ACTIVATED);
435             }
436             public void shellClosed(ShellEvent e) {
437                 isClosed = true;
438                 processWindowEvent(swingwt.awt.event.WindowEvent.WINDOW_CLOSING);
439                 processWindowEvent(swingwt.awt.event.WindowEvent.WINDOW_CLOSED);
440                 SwingWTUtils.decrementWindowReferences();
441             }
442             public void shellDeactivated(ShellEvent e) {
443                 processWindowEvent(swingwt.awt.event.WindowEvent.WINDOW_DEACTIVATED);
444             }
445             public void shellDeiconified(ShellEvent e) {
446                 isIcon = false;
447                 processWindowEvent(swingwt.awt.event.WindowEvent.WINDOW_DEICONIFIED);
448             }
449             public void shellIconified(ShellEvent e) {
450                 isIcon = true;
451                 processWindowEvent(swingwt.awt.event.WindowEvent.WINDOW_ICONIFIED);
452             }
453         });
454     }
455
456     protected void processWindowEvent(int id) {
457         swingwt.awt.event.WindowEvent we = new swingwt.awt.event.WindowEvent(this, id);
458         Iterator i = windowListeners.iterator();
459         while (i.hasNext()) {
460             swingwt.awt.event.WindowListener l = (swingwt.awt.event.WindowListener) i.next();
461             switch(id) {
462                 case (swingwt.awt.event.WindowEvent.WINDOW_ACTIVATED): l.windowActivated(we); break;
463                 case (swingwt.awt.event.WindowEvent.WINDOW_CLOSED): l.windowClosed(we); break;
464                 case (swingwt.awt.event.WindowEvent.WINDOW_CLOSING): l.windowClosing(we); break;
465                 case (swingwt.awt.event.WindowEvent.WINDOW_DEACTIVATED): l.windowDeactivated(we); break;
466                 case (swingwt.awt.event.WindowEvent.WINDOW_DEICONIFIED): l.windowDeiconified(we); break;
467                 case (swingwt.awt.event.WindowEvent.WINDOW_ICONIFIED): l.windowIconified(we); break;
468                 case (swingwt.awt.event.WindowEvent.WINDOW_OPENED): l.windowOpened(we); break;
469             }
470         }
471     }
472
473     protected void processWindowEvent(swingwt.awt.event.WindowEvent e) {
474         swingwt.awt.event.WindowEvent we = e;
475         Iterator i = windowListeners.iterator();
476         while (i.hasNext()) {
477             swingwt.awt.event.WindowListener l = (swingwt.awt.event.WindowListener) i.next();
478             switch(e.getID()) {
479                 case (swingwt.awt.event.WindowEvent.WINDOW_ACTIVATED): l.windowActivated(we); break;
480                 case (swingwt.awt.event.WindowEvent.WINDOW_CLOSED): l.windowClosed(we); break;
481                 case (swingwt.awt.event.WindowEvent.WINDOW_CLOSING): l.windowClosing(we); break;
482                 case (swingwt.awt.event.WindowEvent.WINDOW_DEACTIVATED): l.windowDeactivated(we); break;
483                 case (swingwt.awt.event.WindowEvent.WINDOW_DEICONIFIED): l.windowDeiconified(we); break;
484                 case (swingwt.awt.event.WindowEvent.WINDOW_ICONIFIED): l.windowIconified(we); break;
485                 case (swingwt.awt.event.WindowEvent.WINDOW_OPENED): l.windowOpened(we); break;
486             }
487         }
488     }
489
490     protected void dispatchEvents() {
491         while (!peer.isDisposed()) {
492             try {
493                 if (!display.readAndDispatch()) {
494                     // Send this thread to sleep to allow other
495
// processes a go :)
496
display.sleep();
497                 }
498                 // If thread retardation is on, sleep
499
if (SwingWTUtils.isRetardDispatchThread()) {
500                     try {
501                         Thread.sleep(SwingWTUtils.getRetardationInterval());
502                     }
503                     catch (InterruptedException JavaDoc e) {}
504                 }
505             }
506             // By catching all exceptions and errors here, we
507
// can prevent the app going down for small event
508
// related problems. User code can still catch where
509
// necessary.
510
catch (Error JavaDoc e) {
511             }
512             catch (Exception JavaDoc e) {
513                 e.printStackTrace();
514             }
515         }
516     }
517     
518     /** Sends the window to the front */
519     public void toFront() {
520         SwingUtilities.invokeSync(new Runnable JavaDoc() {
521             public void run() {
522                 if (SwingWTUtils.isSWTControlAvailable(peer))
523                     peer.forceActive();
524             }
525         });
526     }
527     
528     /** Sends the window to the back */
529     public void toBack() {
530         SwingUtilities.invokeSync(new Runnable JavaDoc() {
531             public void run() {
532                 if (SwingWTUtils.isSWTControlAvailable(peer)) {
533                     // Find another shell to make active
534
Shell[] s = SwingWTUtils.getDisplay().getShells();
535                     for (int i = 0; i < s.length; i++) {
536                         if (!s[i].equals(peer))
537                             s[i].forceActive();
538                     }
539                 }
540             }
541         });
542     }
543     
544     /**
545      * Under GTK2 at least, SWT has some trouble issuing repaints,
546      * the only way around it seems to be to microscopically adjust the
547      * the dimensions of Shells
548      */

549     protected void repaintFix() {
550     /** This fixes a repainting bug relating to frames under GTK2 */
551     SwingUtilities.invokeIn(new Runnable JavaDoc() {
552         public void run() {
553         if (SwingWTUtils.isSWTControlAvailable(peer))
554             peer.setSize(peer.getSize().x + 1, peer.getSize().y + 1);
555         }
556     }, 1);
557     }
558
559     public Control getPeer() { return peer; }
560     public void setSwingWTParent(Composite parent) throws Exception JavaDoc { throw new Exception JavaDoc("Window can't have parent."); }
561
562 }
563
564
Popular Tags