KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JComponent


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: JComponent.java,v $
11    Revision 1.34 2004/05/08 08:35:55 bobintetley
12    *** empty log message ***
13
14    Revision 1.33 2004/05/07 12:11:18 bobintetley
15    Default layout fixes and correct behaviour for null layout
16
17    Revision 1.32 2004/05/06 00:24:20 laurentmartelli
18    repaint(Rectangle) has a proper implementation (but it still amounts to repainting everything since repaint(int,int,int,int) is not fully implemented.
19
20    Revision 1.31 2004/05/05 19:29:35 bobintetley
21    JComponent.paintComponent() is now called correctly
22
23    Revision 1.30 2004/05/05 12:43:21 bobintetley
24    Patches/new files from Laurent Martell
25
26    Revision 1.29 2004/05/03 20:53:45 dannaab
27    small tweak to layoutmanager instance objects to make easier to read
28
29    Revision 1.28 2004/04/30 23:18:26 dannaab
30    List selection support, misc bug fixes
31
32    Revision 1.27 2004/04/29 12:49:27 bobintetley
33    Additional JOptionePane constants, missing JTree methods and improved awt.Image support
34
35    Revision 1.26 2004/04/28 08:38:11 bobintetley
36    Hierarchy fixes, code cleanup for base classes, additional javadocs and use of flag to identify JComponent descendants with peers
37
38    Revision 1.25 2004/04/28 07:19:31 bobintetley
39    Fixes to destruction code
40
41    Revision 1.24 2004/04/27 06:37:42 bobintetley
42    Corrected hierarchy with JComponent descending Container
43
44    Revision 1.23 2004/04/25 19:07:34 bobintetley
45    Fix to border painting code to not waste as many objects and to fix broken
46       mouse events
47
48    Revision 1.22 2004/04/23 00:52:32 dannaab
49    Handle borders in a Swing-like way. Implement EmptyBorder & TitledBorder
50
51    Revision 1.21 2004/04/20 14:34:15 bobintetley
52    Double buffering support for component paint()
53
54    Revision 1.20 2004/04/19 15:03:26 bobintetley
55    Missing Component/JComponent focus methods
56
57    Revision 1.19 2004/04/16 10:19:06 dannaab
58    Misc bug fixes, InputMap implementation, preliminary undo support
59
60    Revision 1.18 2004/03/22 15:10:22 bobintetley
61    JRootPane and JLayeredPane implementation
62
63    Revision 1.17 2004/03/01 15:58:47 bobintetley
64    Various little bug fixes
65
66    Revision 1.16 2004/02/24 09:36:39 bobintetley
67    Compatibility methods
68
69    Revision 1.15 2004/02/19 09:58:44 bobintetley
70    Various small bug fixes and JTextArea should be much faster/lighter
71
72    Revision 1.14 2004/01/27 09:05:10 bobintetley
73    ListModel and List Selection implemented. ScrollPane fix so all components
74       scrollable
75
76    Revision 1.13 2004/01/26 14:05:23 bobintetley
77    Compatibility methods
78
79    Revision 1.12 2004/01/26 10:03:37 bobintetley
80    Fixed component painting, added overloaded TabbedPane add() methods
81
82    Revision 1.11 2004/01/26 08:11:00 bobintetley
83    Many bugfixes and addition of SwingSet
84
85    Revision 1.10 2004/01/20 15:52:56 bobintetley
86    Code from an anonymous developer
87
88    Revision 1.9 2004/01/16 15:53:32 bobintetley
89    Many compatibility methods added to Container, Component, JInternalFrame,
90       UIManager, SwingUtilities, JTabbedPane, JPasswordField, JCheckBox
91       and JRadioButton.
92  
93    Revision 1.8 2004/01/15 10:11:15 bobintetley
94    Fixed AWT constructors/hierarchy
95  
96    Revision 1.7 2003/12/15 18:29:57 bobintetley
97    Changed setParent() method to setSwingWTParent() to avoid conflicts with applications
98  
99    Revision 1.6 2003/12/15 15:54:25 bobintetley
100    Additional core methods
101  
102    Revision 1.5 2003/12/14 09:13:38 bobintetley
103    Added CVS log to source headers
104  
105  */

106
107
108 package swingwtx.swing;
109
110 import java.beans.PropertyChangeListener JavaDoc;
111 import java.util.Dictionary JavaDoc;
112 import java.util.Hashtable JavaDoc;
113 import java.util.Vector JavaDoc;
114
115 import swingwt.awt.Component;
116 import swingwt.awt.Container;
117 import swingwt.awt.Dimension;
118 import swingwt.awt.Insets;
119 import swingwt.awt.Point;
120 import swingwt.awt.Rectangle;
121 import swingwtx.accessibility.AccessibleContext;
122 import swingwtx.swing.border.AbstractBorder;
123 import swingwtx.swing.border.Border;
124
125 public class JComponent extends swingwt.awt.Container implements JSWTScrollable {
126
127     /** The border for the component */
128     protected swingwtx.swing.border.Border border = null;
129     /** The component's accessible context */
130     protected AccessibleContext accessibleContext = null;
131     /** Whether or not the component should show it's horizontal scrollbar (if the peer supports it) */
132     protected boolean showHorizontalScrollBar = false;
133     /** Whether or not the component should show it's vertical scrollbar (if the peer supports it) */
134     protected boolean showVerticalScrollBar = false;
135     /** If this component is embedded in a JScrollPane, a reference to it */
136     protected JScrollPane scrollPaneContainer = null;
137     /** Whether or not subclasses are supplying a peer in their setSwingWTParent() call -
138      * if they don't, this component will supply a Composite peer (this is for Swing code
139      * that descends JComopnent and paints on it)
140      */

141     protected boolean descendantHasPeer = false;
142     /** The table of client properties */
143     protected Hashtable JavaDoc clientProperties;
144
145     protected boolean opaque = true;
146
147     protected InputMap focusInputMap = null;
148     protected InputMap ancestorInputMap = null;
149     protected InputMap windowInputMap = null;
150
151     protected Vector JavaDoc propertyChangeListeners = new Vector JavaDoc();
152
153     public static final int WHEN_FOCUSED = 0;
154     public static final int WHEN_ANCESTOR_OF_FOCUSED_COMPONENT = 1;
155     public static final int WHEN_IN_FOCUSED_WINDOW = 2;
156
157     /** Creates an empty JComponent which by default will use
158      * a Composite peer (allowing you to paint on it).
159      */

160     public JComponent() { }
161     
162     /** Adds a PropertyChangeListener to the component */
163     public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
164         propertyChangeListeners.add(l);
165     }
166     /** Adds a PropertyChangeListener to the component */
167     public void addPropertyChangeListener(String JavaDoc propertyName, PropertyChangeListener JavaDoc l) {
168         propertyChangeListeners.add(l);
169     }
170     /** Removes a PropertyChangeListener from the component */
171     public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
172         propertyChangeListeners.remove(l);
173     }
174     /** Removes a PropertyChangeListener from the component */
175     public void removePropertyChangeListener(String JavaDoc propertyName, PropertyChangeListener JavaDoc l) {
176         propertyChangeListeners.remove(l);
177     }
178     
179     /** Forces a repaint of the whole component */
180     public void paintImmediately(int x,int y,int w, int h) {
181         super.repaint();
182     }
183     
184     /** Forces a repaint of the whole component. Calls the
185      * Component.repaint() method in the superclass
186      */

187     public void paintImmediately(swingwt.awt.Rectangle r) {
188         super.repaint();
189     }
190     
191     /** Forces a repaint of a specified rectangle (actually
192       * repaints the whole thing) */

193     public void repaint(Rectangle r) {
194         repaint(r.x,r.y,r.width,r.height);
195     }
196     
197     /**
198      * Override in subclass
199      */

200     protected void paintComponent(swingwt.awt.Graphics g) {
201         paint(g);
202     }
203     
204     /**
205      * NOT IMPLEMENTED
206      */

207     protected void paintChildren(swingwt.awt.Graphics g) {
208     }
209
210     public void paintBackground(int x, int y, int width, int height) {
211          if (parent != null && !isOpaque()) {
212              Dimension size = getSize();
213              Point location = getLocation();
214              //parent.paintBackground(location.x, location.y, size.width, size.height);
215
}
216     }
217
218     /**
219      * Paints this component and any associated borders
220      * on it. Note that if no subclass overrides the
221      * paint method, the component tells the native bits
222      * to show a widget and nothing else happens
223      */

224     public void paint(swingwt.awt.Graphics g) {
225         super.paint(g);
226         if (border != null) {
227         border.paintBorder(this, g, 0, 0, getWidth(), getHeight());
228     }
229     }
230
231     /** Returns the components border. */
232     public Border getBorder() { if (border == null) return BorderFactory.createEmptyBorder(); else return border; }
233     /** Sets the components border */
234     public void setBorder(Border b) { border = b; invalidate(); }
235     
236     /** NOT IMPLEMENTED */
237     public void setRequestFocusEnabled(boolean b) {}
238     /** NOT IMPLEMENTED */
239     public void setAutoscrolls(boolean b) {}
240     /** Returns the appropriate tooltip text for a given
241      * <code>MouseEvent</code>. Since platform lacks Swing's
242      * flexibility in this regard, always returns super.getToolTipText()
243      */

244     public String JavaDoc getToolTipText(swingwt.awt.event.MouseEvent e) {
245         return getToolTipText();
246     }
247     /** Returns the appropriate location for the tooltip text
248      * for a given mouse event. This will just read the X/Y location
249      * of the cursor from the mouse event and return that.
250      */

251     public Point getToolTipLocation(swingwt.awt.event.MouseEvent e) {
252         return e.getPoint();
253     }
254     /** NOT IMPLEMENTED
255       * @return true */

256     public boolean getAutoscrolls() { return true; }
257     /** Hardcoded to return true - there's no other way for
258      * platform widgets.
259      */

260     public boolean isOpaque() { return opaque; }
261     /** Ignored - platform widgets are always opaque */
262     public void setOpaque(boolean b) { this.opaque = b; }
263     /** Whether or not this component is using double-buffering.
264      * Only applies to components with Canvas peers, as the
265      * platform is responsible for painting widgets
266      */

267     public boolean isDoubleBuffered() { return pDoubleBuffered; }
268     /** Used to determine whether or not to double buffer the
269      * component. This only has meaning for Canvas peers, and if this
270      * is true, the subclass paint() method will receive a graphics
271      * context for a buffered image to paint on, which will then
272      * be painted onto the component. Otherwise, the paint() method
273      * will receive the component's graphics context to paint on.
274      */

275     public void setDoubleBuffered(boolean b) { pDoubleBuffered = b; }
276     
277     /** NOT IMPLEMENTED - used to scroll parent JViewPort containers
278       * to the specified rectangle */

279     public void scrollRectToVisible(Rectangle aRect) {
280     }
281     
282     /** Retrieves all client properties (Hashtable) as a Dictionary */
283     private Dictionary JavaDoc getClientProperties() {
284         if (clientProperties == null) {
285             clientProperties = new Hashtable JavaDoc(2);
286         }
287         return clientProperties;
288     }
289     
290     /** Calls invalidate */
291     public void revalidate() { invalidate(); }
292     
293     /**
294      * Retrieves a property from the client, or null if
295      * the key does not exist.
296      */

297     public final Object JavaDoc getClientProperty(Object JavaDoc key) {
298         if (clientProperties == null) {
299             return null;
300         }
301         else {
302             return getClientProperties().get(key);
303         }
304     }
305     
306     /**
307      * Not implemented correctly - rather than returning
308      * the visible portion of the component, this returns
309      * a Rectangle sized to match the component
310      */

311     public swingwt.awt.Rectangle getVisibleRect() {
312         return new swingwt.awt.Rectangle(0, 0, getWidth(), getHeight());
313     }
314     
315     /** Sets a property against the component */
316     public final void putClientProperty(Object JavaDoc key, Object JavaDoc value) {
317         if (value == null && clientProperties == null) {
318             return;
319         }
320         getClientProperties().put(key, value);
321     }
322     
323     /** NOT IMPLEMENTED */
324     public void setMargin(swingwt.awt.Insets i) {}
325     
326     /** Override in subclass to return something more specific */
327     public AccessibleContext getAccessibleContext() {
328         return accessibleContext;
329     }
330     
331     /**
332      * Used by the JSWTScrollable interface to determine
333      * whether or not this component's peer should show
334      * it's horizontal ScrollBar (useful for JTextArea et al)
335      */

336     public void setHorizontalScrollPane(boolean b) {
337         showHorizontalScrollBar = b;
338     }
339     
340     /**
341      * Used by the JSWTScrollable interface to determine
342      * whether or not this component's peer should show
343      * it's vertical ScrollBar (useful for JTextArea et al)
344      */

345     public void setVerticalScrollPane(boolean b) {
346         showVerticalScrollBar = b;
347     }
348     
349     /** If this component is contained by a JScrollPane, use this
350      * to set a reference to it.
351      */

352     public void setScrollPane(JScrollPane container) {
353         scrollPaneContainer = container;
354     }
355     
356     /**
357      * Used to determine whether this component is a top level container
358      * (overriden in subclasses), so validation code knows to work up
359      * the tree to that point before validating down.
360      */

361     public boolean isValidateRoot() {
362         return false;
363     }
364     
365     /** Always returns true - we're using native widgets! */
366     public boolean isOptimizedDrawingEnabled() {
367         return true;
368     }
369     
370     /** NOT IMPLEMENTED */
371     public void setNextFocusableComponent(Component acomponent) {
372     }
373
374     /** Returns the Insets for the components (taking into account any Border) */
375     public Insets getInsets() {
376         final Insets insets = new Insets();
377
378         SwingUtilities.invokeSync( new Runnable JavaDoc() {
379             public void run() {
380                 if (border != null) {
381                     if (border instanceof AbstractBorder)
382                         ((AbstractBorder)border).getBorderInsets(JComponent.this, insets);
383                     else {
384                         Insets borderInsets = border.getBorderInsets(JComponent.this);
385                         insets.left = borderInsets.left;
386                         insets.top = borderInsets.top;
387                         insets.right = borderInsets.right;
388                         insets.bottom = borderInsets.bottom;
389                     }
390                 }
391                 else {
392                     
393                     // If we have an unrealised component, return no insets
394
if (!SwingWTUtils.isSWTControlAvailable(composite))
395                         return;
396                     
397                     org.eclipse.swt.graphics.Rectangle rect = composite.getBounds();
398                     org.eclipse.swt.graphics.Rectangle client = composite.getClientArea();
399
400                     insets.left = client.x;
401                     insets.top = client.y;
402                     insets.right = rect.width - client.width - client.x;
403                     insets.bottom = rect.height - client.height - client.y;
404                 }
405             }
406         });
407
408         return insets;
409     }
410
411     
412     /**
413      * Overridden here - we still use the implementation in the superclass, but
414      * we are just checking if we are in a scrollpane container and set the
415      * preferred size on that if necessary.
416      */

417     public void setPreferredSize(Dimension d) {
418         super.setPreferredSize(d);
419         if (scrollPaneContainer != null)
420             scrollPaneContainer.setPreferredSize(d);
421     }
422
423     /** Sets the InputMap for this component */
424     public final void setInputMap(int condition, InputMap inputMap) {
425         if (condition == WHEN_FOCUSED) { focusInputMap = inputMap; }
426         else if (condition == WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { ancestorInputMap = inputMap; }
427         else if (condition == WHEN_IN_FOCUSED_WINDOW) { windowInputMap = inputMap; }
428         else { throw new IllegalArgumentException JavaDoc("Illegal InputMap type!"); }
429     }
430
431     /** Gets the InputMap for this component */
432     public final InputMap getInputMap(int condition, boolean create) {
433         InputMap inputMap = null;
434
435         if (condition == WHEN_FOCUSED)
436         {
437             if (focusInputMap == null && create) focusInputMap = new InputMap();
438             if (focusInputMap != null) { inputMap = focusInputMap; }
439         }
440         else if (condition == WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
441         {
442             if (ancestorInputMap == null && create) ancestorInputMap = new InputMap();
443             if (ancestorInputMap != null) { inputMap = ancestorInputMap; }
444         }
445         else if (condition == WHEN_IN_FOCUSED_WINDOW)
446         {
447             if (windowInputMap == null && create) windowInputMap = new InputMap();
448             if (windowInputMap != null) { inputMap = windowInputMap; }
449         }
450         else
451         {
452             throw new IllegalArgumentException JavaDoc("Illegal InputMap type!");
453         }
454
455         return inputMap;
456     }
457
458     /** Destroys the component */
459     public void dispose() {
460     // Use dispose() from Container, which destroys child
461
// components and finally the peer itself
462
super.dispose();
463     }
464
465     /**
466      * Callback for when this JComponent is added to a container.
467      */

468     public void setSwingWTParent(Container parent) throws Exception JavaDoc {
469     
470     // Ensure child components are created where necessary (if
471
// this JComponent is a container)
472
super.setSwingWTParent(parent);
473
474     // We need to identify whether a subclass is providing it's
475
// own peer, and if not, create a Composite peer.
476
if (!descendantHasPeer) {
477         ppeer = new org.eclipse.swt.widgets.Composite(parent.getComposite(), 0);
478             peer = ppeer;
479             this.parent = parent;
480         }
481     }
482
483 }
484
Popular Tags