KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > Component


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 is now at the end of this file since it had gotten rather large.
11
12 */

13
14 package swingwt.awt;
15
16
17 import java.lang.reflect.InvocationTargetException JavaDoc;
18 import java.lang.reflect.Method JavaDoc;
19 import java.text.AttributedCharacterIterator JavaDoc;
20 import java.text.AttributedString JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.custom.CCombo;
26 import org.eclipse.swt.custom.ExtendedModifyEvent;
27 import org.eclipse.swt.custom.StyledText;
28 import org.eclipse.swt.events.ModifyEvent;
29 import org.eclipse.swt.events.ModifyListener;
30 import org.eclipse.swt.widgets.Combo;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Text;
33
34 import swingwt.awt.event.ActionEvent;
35 import swingwt.awt.event.ActionListener;
36 import swingwt.awt.event.ComponentEvent;
37 import swingwt.awt.event.ComponentListener;
38 import swingwt.awt.event.FocusEvent;
39 import swingwt.awt.event.FocusListener;
40 import swingwt.awt.event.InputMethodEvent;
41 import swingwt.awt.event.InputMethodListener;
42 import swingwt.awt.event.KeyEvent;
43 import swingwt.awt.event.KeyListener;
44 import swingwt.awt.event.MouseEvent;
45 import swingwt.awt.event.MouseListener;
46 import swingwt.awt.event.MouseMotionListener;
47 import swingwt.awt.font.TextHitInfo;
48 import swingwtx.accessibility.AccessibleContext;
49 import swingwtx.swing.JEditorPane;
50 import swingwtx.swing.JScrollPane;
51 import swingwtx.swing.JTable;
52 import swingwtx.swing.JTextArea;
53 import swingwtx.swing.JToolBar;
54 import swingwtx.swing.JTree;
55 import swingwtx.swing.SwingUtilities;
56 import swingwtx.swing.SwingWTUtils;
57
58 /**
59  *
60  * Tag class for AWT compatibility - subclass JSWTComponent has
61  * all the good stuff.
62  *
63  */

64 public class Component implements ImageObserver {
65
66     /** The SWT peer this component is representing */
67     public Control peer = null;
68     /** If a default instance of Component is used, then we map a paintable
69      * composite canvas as AWT does */

70     protected org.eclipse.swt.widgets.Composite ppeer = null;
71
72     /** The name of this component */
73     protected String JavaDoc componentName = this.getClass().getName();
74
75     /** The graphics configuration */
76     protected static GraphicsConfiguration graphicsConfig = new GraphicsConfiguration();
77
78     /** The parent container */
79     protected Container parent = null;
80
81     /** Used when caching objects with layouts */
82     public Object JavaDoc layoutModifier = null;
83
84     /** Cache value */
85     protected boolean pVisible = true;
86     /** Cache value */
87     protected boolean pEnabled = true;
88     /** Cache value */
89     protected String JavaDoc pToolTipText = "";
90     /** Cache value */
91     protected swingwt.awt.Color pBackground = null;
92     /** Cache value */
93     protected swingwt.awt.Color pForeground = null;
94     /** Cache value */
95     protected Dimension pSize = null;
96     /** Cache value */
97     protected Dimension pPrefSize = null;
98     /** Cache value */
99     protected Dimension pMinSize = null;
100     /** Cache value */
101     protected Dimension pMaxSize = null;
102     /** Cache value */
103     protected swingwt.awt.Point pLocation = null;
104     /** Cache value */
105     protected swingwt.awt.Font pFont = null;
106     /** Cache value */
107     protected Object JavaDoc pLayoutData = null;
108     /** Cache value */
109     protected swingwt.awt.Cursor pCursor = swingwt.awt.Cursor.getPredefinedCursor(swingwt.awt.Cursor.DEFAULT_CURSOR);
110     /** Whether paint events give direct access to component or use an image buffer and render in one go */
111     protected boolean pDoubleBuffered = false;
112
113     /** Action Listener objects */
114     protected Vector JavaDoc actionListeners = new Vector JavaDoc();
115     /** Mouse Listener objects */
116     protected Vector JavaDoc mouseListeners = new Vector JavaDoc();
117     /** Mouse Motion Listener objects */
118     protected Vector JavaDoc mouseMotionListeners = new Vector JavaDoc();
119     /** Keyboard Listener objects */
120     protected Vector JavaDoc keyListeners = new Vector JavaDoc();
121     /** Focus Listener objects */
122     protected Vector JavaDoc focusListeners = new Vector JavaDoc();
123     /** Component Listener objects */
124     protected Vector JavaDoc componentListeners = new Vector JavaDoc();
125     /** Input Method Listeners objects */
126     protected Vector JavaDoc inputMethodListeners = new Vector JavaDoc();
127     /** Used with TableLayout to determine whether this component should use up extra width */
128     protected boolean consumeAvailableWidth = false;
129     /** Used with TableLayout to determine whether this component should use up extra height */
130     protected boolean consumeAvailableHeight = false;
131     /** Action command */
132     protected String JavaDoc actionCommand = "";
133     /** Whether a calling program has set the font for this component */
134     protected boolean isUserSetFont = false;
135     /** Whether this component can get the focus */
136     protected boolean focusable = true;
137     /** Thread safe object for returning values */
138     private Object JavaDoc retval = null;
139     private int iretval = 0;
140     /** Default adapter for Action Events */
141     protected org.eclipse.swt.events.MouseListener swingWTDefaultActionEventAdapter = null;
142     /** All-in-one event adapter for components */
143     protected Component.SWTEventAdapter swingWTEventAdapter = null;
144     /** The accessible context */
145     protected AccessibleContext accessibleContext = null;
146
147     /** Constants */
148     public static final float TOP_ALIGNMENT = 0.0f;
149     public static final float CENTER_ALIGNMENT = 0.5f;
150     public static final float BOTTOM_ALIGNMENT = 1.0f;
151     public static final float LEFT_ALIGNMENT = 0.0f;
152     public static final float RIGHT_ALIGNMENT = 1.0f;
153
154     /** Override in subclasses to paint on the component */
155     public void paint(Graphics g) {
156     }
157
158     /** Not normally in Component - member of JComponent,
159      * implemented here to save a load of class casting
160      * around and to call into paint(Graphics) - this
161      * is the first point of call for repainting..
162      */

163     protected void paintComponent(Graphics g) {
164     paint(g);
165     }
166
167     //abstract void paintBackground(int x, int y, int width, int height);
168

169     /**
170      * Once a parent component receives an "add" call for a child, this being
171      * the child, this should be called to tell us to instantiate the peer
172      * and load in any cached properties.
173      */

174     public void setSwingWTParent(swingwt.awt.Container parent) throws Exception JavaDoc {
175         ppeer = new org.eclipse.swt.widgets.Canvas(parent.getComposite(), 0);
176         peer = ppeer;
177         this.parent = parent;
178     }
179
180     /** Returns the SWT peer */
181     public Control getPeer() { return peer; }
182
183     /** Makes the component visible */
184     public void show() { setVisible(true); }
185     /** Makes the component invisible */
186     public void hide() { setVisible(false); }
187
188     /** Change the visibility of the component */
189     public void setVisible(final boolean b) {
190         pVisible = b;
191         SwingUtilities.invokeSync(new Runnable JavaDoc() {
192             public void run() {
193                 if (SwingWTUtils.isSWTControlAvailable(peer))
194                     peer.setVisible(b);
195             }
196         });
197     }
198     /** Returns true if the component is visible */
199     public boolean isVisible() { return pVisible; }
200     /** Returns true if the component is visible */
201     public boolean isShowing() { return isVisible(); }
202
203     /** Change the enabled state of the component */
204     public void setEnabled(final boolean b) {
205         pEnabled = b;
206         SwingUtilities.invokeAsync(new Runnable JavaDoc() {
207             public void run() {
208                 if (SwingWTUtils.isSWTControlAvailable(peer))
209                     peer.setEnabled(b);
210             }
211         });
212     }
213     /** Returns true if the component is enabled */
214     public boolean isEnabled() { return pEnabled; }
215     /** Requests the component receives the focus */
216     public void requestFocus() { SwingUtilities.invokeSync(new Runnable JavaDoc() { public void run() { if (SwingWTUtils.isSWTControlAvailable(peer)) peer.setFocus();}}); }
217     /** Forces keyboard focus to the component */
218     public void grabFocus() { SwingUtilities.invokeSync(new Runnable JavaDoc() { public void run() {if (SwingWTUtils.isSWTControlAvailable(peer)) peer.forceFocus();}}); }
219
220     public void repaint(int x, int y, int width, int height) {
221         repaint(0, x, y, width, height);
222     }
223
224     public void repaint(long tm, int x, int y, int width, int height) {
225         // TODO
226
repaint();
227     }
228
229     /** Redraws the component */
230     public void repaint() {
231         SwingUtilities.invokeSync(new Runnable JavaDoc() {
232             public void run() {
233                 if (SwingWTUtils.isSWTControlAvailable(peer)) {
234                     // If this component is drawable peer (ie. has
235
// no descendant overriding it), then we assume the developer
236
// wanted to paint on it, so we call down to paint() again
237
if (ppeer != null) {
238                         Graphics g = getGraphics();
239             paintComponent(g);
240                         g.dispose();
241                     }
242                     else {
243                         // Otherwise, tell the SWT peer widget to redraw
244
peer.redraw();
245             }
246                 }
247             }
248         });
249     }
250
251     /** Returns the background colour of the component */
252     public swingwt.awt.Color getBackground() {
253         if (!SwingWTUtils.isSWTControlAvailable(peer))
254             return pBackground;
255         else {
256             SwingUtilities.invokeSync(new Runnable JavaDoc() {
257                 public void run() {
258                     retval = new swingwt.awt.Color(peer.getBackground());
259                 }
260             });
261             return (Color) retval;
262         }
263     }
264     /** Sets the background colour of the component */
265     public void setBackground(final swingwt.awt.Color c) {
266         if (!SwingWTUtils.isSWTControlAvailable(peer))
267             pBackground = c;
268         else
269             SwingUtilities.invokeSync(new Runnable JavaDoc() {
270                 public void run() {
271                     peer.setBackground(c.getSWTColor());
272                 }
273             });
274     }
275     /** Returns the foreground colour of the component */
276     public swingwt.awt.Color getForeground() {
277         if (!SwingWTUtils.isSWTControlAvailable(peer))
278             return pForeground;
279         else {
280             SwingUtilities.invokeSync(new Runnable JavaDoc() {
281                 public void run() {
282                    retval = new swingwt.awt.Color(peer.getForeground());
283                 }
284             });
285             return (Color) retval;
286         }
287     }
288     /** Sets the foreground colour of the component */
289     public void setForeground(final swingwt.awt.Color c) {
290         if (!SwingWTUtils.isSWTControlAvailable(peer))
291             pForeground = c;
292         else
293             SwingUtilities.invokeSync(new Runnable JavaDoc() {
294                 public void run() {
295                     peer.setForeground(c.getSWTColor());
296                 }
297             });
298     }
299
300     /** Returns the width of the component */
301     public int getWidth() {
302         return getSize().width;
303     }
304
305     /** Returns the height of the component */
306     public int getHeight() {
307         return getSize().height;
308     }
309
310     /** Sets the width of the component. */
311     public void setWidth(final int width) {
312         if (!SwingWTUtils.isSWTControlAvailable(peer)) return;
313         SwingUtilities.invokeSync(new Runnable JavaDoc() {
314             public void run() {
315                 org.eclipse.swt.graphics.Point p = peer.getSize();
316                 p.x = width; peer.setSize(p);
317             }
318         });
319     }
320
321     /** Sets the height of the component. */
322     public void setHeight(final int height) {
323         if (!SwingWTUtils.isSWTControlAvailable(peer)) return;
324         SwingUtilities.invokeSync(new Runnable JavaDoc() {
325             public void run() {
326                 org.eclipse.swt.graphics.Point p = peer.getSize();
327                 p.y = height; peer.setSize(p);
328             }
329         });
330     }
331
332     /** Return a graphics context for drawing on this object. Note that only instances of JComponent
333      * are allowed */

334     public Graphics getGraphics() {
335         if (!SwingWTUtils.isSWTControlAvailable(peer))
336             return null;
337         else {
338             SwingUtilities.invokeSync(new Runnable JavaDoc() {
339                 public void run() {
340                     graphics = new SWTGraphics2DRenderer(new org.eclipse.swt.graphics.GC(peer), true);
341                 }
342             });
343             return (Graphics) graphics;
344         }
345     }
346     private Object JavaDoc graphics = null;
347
348    /** Returns the preferred size the component would like to be displayed at -
349      * very important for proper layout managers.
350      * @author Robin Rawson-Tetley
351      */

352     public Dimension getPreferredSize() {
353         
354         // Since preferred size is fulfilling it's "real" role now, it needs to be
355
// the height the component would prefer to be displayed at. This is either
356
// what the user set, or (if not set), a computed estimate
357
//
358
if (pPrefSize != null)
359             return pPrefSize;
360     
361         else if (SwingWTUtils.isSWTControlAvailable(peer)) {
362             // Toolbars and CoolBars don't use AWT layouts
363
if (this instanceof JToolBar || this instanceof swingwtx.custom.JCoolBar) {
364                 org.eclipse.swt.graphics.Point p = peer.computeSize(-1, -1);
365                 return new Dimension(p.x, p.y);
366             }
367         
368             // Container classes
369
else if (this instanceof Container) {
370
371                 // If there's no composite peer (ie. It's really a component),
372
// use the component size
373
if (((Container) this).getComposite() == null) {
374             org.eclipse.swt.graphics.Point p = peer.computeSize(-1, -1);
375             return new Dimension(p.x, p.y);
376         }
377             
378         // If there's no layout, retun 0, 0
379
if (((Container) this).getLayout() == null)
380             return new Dimension(0, 0);
381         
382                 return ((Container) this).getLayout().preferredLayoutSize((Container) this);
383             }
384             else
385             {
386                 org.eclipse.swt.graphics.Point p = peer.computeSize(-1, -1);
387                 return new Dimension(p.x, p.y);
388             }
389         }
390         else {
391             // The component has not yet been realised, so we are going to
392
// have to make some guesses based on the type
393
return calculatePreferredSize();
394         }
395     }
396     /** Sets the preferred size of the object for layout purposes */
397     public void setPreferredSize(Dimension d) { pPrefSize = d; }
398     /**
399      * Override in subclasses to return the preferred size for
400      * a given (non-realised) component.
401      *
402      * Note that you should set the size to whatever this routine
403      * returns once called to prevent inconsistency (otherwise
404      * the component will use it's SWT preferred size).
405      */

406     protected Dimension calculatePreferredSize() {
407         Dimension size = new Dimension(150, 50);
408         setSize(size);
409         return size;
410     }
411     
412     /** Returns the maximum size of the component */
413     public Dimension getMaximumSize() { if (pMaxSize == null) pMaxSize = new Dimension(1024, 1024); return pMaxSize; }
414     /** Sets the maximum size of the component */
415     public void setMaximumSize(Dimension d) { pMaxSize = d; }
416     /** Returns the minimum size of the component */
417     public Dimension getMinimumSize() { if (pMinSize == null) pMinSize = new Dimension(1, 1); return pMinSize; }
418     /** Sets the minimum size of the component */
419     public void setMinimumSize(Dimension d) { pMinSize = d; }
420     public boolean isMinimumSizeSet() { return pMinSize!=null; }
421
422     /** Sets the peer's actual size */
423     public void setSize(final int width, final int height) {
424         setSize(new Dimension(width, height));
425     }
426     /** Sets the peer's actual size */
427     public void setSize(final Dimension d) {
428         
429         pSize = d;
430         if (!SwingWTUtils.isSWTControlAvailable(peer)) return;
431         
432         SwingUtilities.invokeSync(new Runnable JavaDoc() {
433             public void run() {
434                 peer.setBounds(peer.getLocation().x, peer.getLocation().y, d.width, d.height);
435             }
436         });
437     }
438
439     /**
440      * Returns the peers actual size. Or, if the peer is embedded inside
441      * a ScrollPane component, the larger of the preferredSize/peers
442      * actual size.
443      * This works rather nicely for handling layouts inside a scrollbar as
444      * we can simply offset a large composite according to the scrollbar
445      * positions inside JScrollPane.
446      *
447      * One note - if the component is JTree, JTable, JTextArea or JEditorPane, then
448      * we return the size as usual since these components handle their
449      * own scrolling and we don't want the JScrollPane behaving weirdly.
450      */

451     public Dimension getSize() {
452         if (parent instanceof JScrollPane &&
453            (!(this instanceof JTable)) &&
454            (!(this instanceof JTextArea)) &&
455            (!(this instanceof JTree)) &&
456            (!(this instanceof JEditorPane))
457            ) {
458             Dimension returnDim = getPreferredSize();
459             Dimension peers = getPeerSize();
460             if (returnDim.width < peers.width) returnDim.width = peers.width;
461             if (returnDim.height < peers.height) returnDim.height = peers.height;
462             return returnDim;
463         }
464         else
465             return getPeerSize();
466     }
467
468     /** Returns the peers actual size, or 0,0 if it hasn't been created yet/is invisible */
469     public Dimension getPeerSize() {
470         final Dimension d = new Dimension();
471         if (!SwingWTUtils.isSWTControlAvailable(peer)) return d;
472         SwingUtilities.invokeSync(new Runnable JavaDoc() {
473             public void run() {
474                 d.height = peer.getSize().y;
475                 d.width = peer.getSize().x;
476             }
477         });
478         return d;
479     }
480     
481     /** FIXME: NOT IMPLEMENTED */
482     public void setFocusTraversalKeysEnabled(boolean b) {}
483
484     /** Returns the component's location on screen */
485     public Point getLocation() {
486         final Point p = new Point(0, 0);
487         if (!SwingWTUtils.isSWTControlAvailable(peer)) return p;
488         SwingUtilities.invokeSync(new Runnable JavaDoc() {
489             public void run() {
490                 p.x = peer.getLocation().x;
491                 p.y = peer.getLocation().y;
492             }
493         });
494         return p;
495     }
496
497     /** FIXME: Bad algorithm here, but can't find appropriate routine. Display.map() looks
498      * interesting, but it is undocumented.
499      */

500     public Point getLocationOnScreen() {
501         final Point p = new Point(0, 0);
502         if (!SwingWTUtils.isSWTControlAvailable(peer)) return p;
503         SwingUtilities.invokeSync(new Runnable JavaDoc() {
504             public void run() {
505                 p.x = peer.getLocation().x + peer.getShell().getLocation().x;
506                 p.y = peer.getLocation().y + peer.getShell().getLocation().y;
507             }
508         });
509         return p;
510     }
511
512     /** Creates and returns an image */
513     public Image createImage(int width, int height) {
514         swingwt.awt.image.BufferedImage i = new swingwt.awt.image.BufferedImage(width, height, swingwt.awt.image.BufferedImage.TYPE_INT_RGB);
515         return i;
516     }
517
518     /** Creates and returns a volatile image */
519     public swingwt.awt.image.VolatileImage createVolatileImage(int width, int height) {
520         swingwt.awt.image.VolatileImage i = new swingwt.awt.image.VolatileImage(width, height, swingwt.awt.image.BufferedImage.TYPE_INT_RGB);
521         return i;
522     }
523
524     /** Returns the font metrics for the given font */
525     public swingwt.awt.FontMetrics getFontMetrics(swingwt.awt.Font f) {
526         return Toolkit.getDefaultToolkit().getFontMetrics(f);
527     }
528     
529
530     /** Returns the Font used on the component */
531     public swingwt.awt.Font getFont() {
532         if (!SwingWTUtils.isSWTControlAvailable(peer))
533             return pFont;
534         else {
535             SwingUtilities.invokeSync(new Runnable JavaDoc() {
536                 public void run() {
537                     retval = new swingwt.awt.Font(peer.getFont());
538                 }
539             });
540             return (swingwt.awt.Font) retval;
541         }
542     }
543     /** Sets the font used by the component */
544     public void setFont(final swingwt.awt.Font f) {
545         if (f!=null) {
546             isUserSetFont = true;
547             if (!SwingWTUtils.isSWTControlAvailable(peer))
548                 pFont = f;
549             else
550                 SwingUtilities.invokeSync(new Runnable JavaDoc() {
551                         public void run() {
552                             peer.setFont(f.getSWTFont());
553                         }
554                     });
555         } else {
556             isUserSetFont = false;
557         }
558     }
559
560     /**
561      * Returns true if a calling process has set the font on
562      * this component. So other parts of SwingWT know when to
563      * override the standard font as part of your system theme.
564      */

565     public boolean hasSetFont() {
566         return isUserSetFont;
567     }
568
569     /** Returns the tooltip text used on the component */
570     public String JavaDoc getToolTipText() { return pToolTipText; }
571     /** Sets the tooltip text used on the component */
572     public void setToolTipText(final String JavaDoc text) { pToolTipText = text; SwingUtilities.invokeSync(new Runnable JavaDoc() { public void run() {if (SwingWTUtils.isSWTControlAvailable(peer)) peer.setToolTipText(text);}}); }
573     /** Sets the physical bounds of the component - used by layout managers */
574     public void setBounds(final int x, final int y, final int width, final int height) {
575         
576         pSize = new Dimension(width, height);
577         pLocation = new Point(x, y);
578         
579         if (!SwingWTUtils.isSWTControlAvailable(peer)) return;
580         
581         SwingUtilities.invokeSync(new Runnable JavaDoc() {
582             public void run() {
583                 peer.setBounds(x, y, width, height);
584             }
585         });
586     }
587     /** Sets the physical bounds of the component */
588     public void setBounds(Rectangle r) {
589         setBounds(r.x, r.y, r.width, r.height);
590     }
591     /** Returns the actual bounds of the component (or 0,0,0,0 if it isn't realised yet) */
592     public Rectangle getBounds() {
593         if (!SwingWTUtils.isSWTControlAvailable(peer)) return new Rectangle(0, 0, 0, 0);
594         SwingUtilities.invokeSync(new Runnable JavaDoc() {
595             public void run() {
596                 retval = new Rectangle(peer.getLocation().x, peer.getLocation().y, peer.getSize().x, peer.getSize().y);
597             }
598         });
599         return (Rectangle) retval;
600     }
601     /** NOT IMPLEMENTED */
602     public float getAlignmentX() { return 0; }
603     /** NOT IMPLEMENTED */
604     public float getAlignmentY() { return 0; }
605     /** NOT IMPLEMENTED */
606     public void setAlignmentX(float val) {}
607     /** NOT IMPLEMENTED */
608     public void setAlignmentY(float val) {}
609     
610     /** Sets the focus to the next component as if tab was pressed */
611     public void transferFocus() {
612         SwingUtilities.invokeSync(new Runnable JavaDoc() {
613             public void run() {
614                 peer.traverse(SWT.TRAVERSE_TAB_NEXT);
615             }
616         });
617     }
618     
619     /** Sets the focus to the next component as if shift+tab was pressed */
620     public void transferFocusBackward() {
621         SwingUtilities.invokeSync(new Runnable JavaDoc() {
622             public void run() {
623                 peer.traverse(SWT.TRAVERSE_TAB_PREVIOUS);
624             }
625         });
626     }
627     
628     /** NOT IMPLEMENTED: FIXME - focusCycleRoot not done */
629     public void transferFocusUpCycle() {
630     }
631
632     /** Stubbed - does not define component region like AWT */
633     public boolean contains(int x, int y) { return false; }
634
635     /** Override in container classes to relayout child
636       * components - calls repaint here */

637     public void invalidate() { repaint(); }
638
639     /** NOT IMPLEMENTED */
640     public void validate() {}
641     
642     /** Returns the action command for this component */
643     public String JavaDoc getActionCommand() { return actionCommand; }
644     /** Sets the action command for this component */
645     public void setActionCommand(String JavaDoc command) { actionCommand = command; }
646     /** Sets the component's location in it's container */
647     public void setLocation(final Point p) {
648         pLocation = p;
649         SwingUtilities.invokeSync(new Runnable JavaDoc() {
650             public void run() {
651                 if (SwingWTUtils.isSWTControlAvailable(peer))
652                     peer.setBounds(p.x, p.y, peer.getSize().x, peer.getSize().y);
653             }
654         });
655     }
656     /** Sets the component's location in it's container */
657     public void setLocation(int x, int y) {
658         setLocation(new Point(x, y));
659     }
660     /** Returns the component's X position in it's parent container */
661     public int getX() {
662         SwingUtilities.invokeSync(new Runnable JavaDoc() {
663             public void run() {
664                 if (SwingWTUtils.isSWTControlAvailable(peer))
665                     iretval = peer.getLocation().x;
666                 else
667                     iretval = 1;
668             }
669         });
670         return iretval;
671     }
672     /** Returns the component's Y position in it's parent container */
673     public int getY() {
674             SwingUtilities.invokeSync(new Runnable JavaDoc() {
675             public void run() {
676                 if (SwingWTUtils.isSWTControlAvailable(peer))
677                     iretval = peer.getLocation().y;
678                 else
679                     iretval = 1;
680             }
681         });
682         return iretval;
683     }
684     /** Returns the component's parent */
685     public Container getParent() { return parent; }
686
687     /** Returns the components name */
688     public String JavaDoc getName() { return componentName; }
689     /** Sets the components name */
690     public void setName(String JavaDoc newName) { componentName = newName; }
691     /** Sets the components cursor */
692     public void setCursor(final swingwt.awt.Cursor c) {
693         pCursor = c;
694         SwingUtilities.invokeSync(new Runnable JavaDoc() {
695             public void run() {
696         try {
697                     if (SwingWTUtils.isSWTControlAvailable(peer))
698             peer.setCursor(c.getSWTCursor());
699         }
700         catch (Exception JavaDoc e) {
701             e.printStackTrace();
702         }
703             }
704         });
705     }
706     /** Returns true if the component can receive the focus */
707     public boolean isFocusable() { return focusable; }
708     /** Set whether the component can be focused */
709     public void setFocusable(boolean b) { focusable = b; }
710     /** Returns the components cursor */
711     public swingwt.awt.Cursor getCursor() { return pCursor; }
712     /** Gets the Toolkit for this component */
713     public Toolkit getToolkit() { return Toolkit.getDefaultToolkit(); }
714     /** Gets the GraphicsConfiguration for this component */
715     public GraphicsConfiguration getGraphicsConfiguration() { return graphicsConfig; }
716
717     public ComponentOrientation getComponentOrientation() { return ComponentOrientation.LEFT_TO_RIGHT; }
718     public void setComponentOrientation(ComponentOrientation o) { }
719
720     /** Destroys the component and stops all listeners */
721     public void dispose() {
722         componentOnlyDispose();
723     }
724
725     /**
726      * Because dispose() can be overridden to do more, calling this routine
727      * guarantees that destruction of the component occurs (and nothing else).
728      */

729     public void componentOnlyDispose() {
730
731         if (SwingWTUtils.isSWTControlAvailable(peer)) {
732             SwingUtilities.invokeSync(new Runnable JavaDoc() {
733                 public void run() {
734                     peer.dispose();
735                 }
736             });
737         }
738
739         peer = null;
740         ppeer = null;
741
742         mouseListeners.removeAllElements();
743         mouseMotionListeners.removeAllElements();
744         actionListeners.removeAllElements();
745         focusListeners.removeAllElements();
746         keyListeners.removeAllElements();
747         componentListeners.removeAllElements();
748         inputMethodListeners.removeAllElements();
749     }
750
751     /** Add an <code>ActionListener</code> to this component */
752     public void addActionListener(ActionListener l) {
753         actionListeners.add(l);
754     }
755
756     /** Remove an <code>ActionListener</code> from the component */
757     public void removeActionListener(ActionListener l) {
758         actionListeners.remove(l);
759     }
760
761     /** Add an <code>ComponentListener</code> to this component */
762     public void addComponentListener(ComponentListener l) {
763         componentListeners.add(l);
764     }
765
766     /** Remove an <code>ComponentListener</code> from the component */
767     public void removeComponentListener(ComponentListener l) {
768         componentListeners.remove(l);
769     }
770
771     /** Add a <code>MouseListener</code> to this component */
772     public void addMouseListener(MouseListener l) {
773         mouseListeners.add(l);
774     }
775
776     /** Remove an <code>MouseListener</code> from the component */
777     public void removeMouseListener(MouseListener l) {
778         mouseListeners.remove(l);
779     }
780
781     /** Add a <code>MouseMotionListener</code> to this component */
782     public void addMouseMotionListener(MouseMotionListener l) {
783         mouseMotionListeners.add(l);
784     }
785
786     /** Remove an <code>MouseMotionListener</code> from the component */
787     public void removeMouseMotionListener(MouseMotionListener l) {
788         mouseMotionListeners.remove(l);
789     }
790
791     /** Add a <code>KeyListener</code> to this component */
792     public void addKeyListener(KeyListener l) {
793         keyListeners.add(l);
794     }
795
796     /** Remove an <code>KeyListener</code> from the component */
797     public void removeKeyListener(KeyListener l) {
798         keyListeners.remove(l);
799     }
800
801     /** Add a <code>KeyListener</code> to this component */
802     public void addFocusListener(FocusListener l) {
803         focusListeners.add(l);
804     }
805
806     /** Remove an <code>KeyListener</code> from the component */
807     public void removeFocusListener(FocusListener l) {
808         focusListeners.remove(l);
809     }
810
811     /** Add an <code>InputMethodListener</code> to this component */
812     public void addInputMethodListener(InputMethodListener l) {
813         inputMethodListeners.add(l);
814     }
815
816     /** Remove an <code>InputMethodListener</code> to this component */
817     public void removeInputMethodListener(InputMethodListener l) {
818         inputMethodListeners.remove(l);
819     }
820
821     /**
822      * Sends action events to listeners
823      */

824     public void processActionEvent(int id) {
825         Iterator JavaDoc i = actionListeners.iterator();
826         ActionEvent ae = new ActionEvent(this, id, this.getActionCommand());
827         while (i.hasNext()) {
828             ActionListener al = (ActionListener) i.next();
829             al.actionPerformed(ae);
830         }
831     }
832
833     public void processComponentEvent(ComponentEvent e) {
834         for (int i = 0; i < componentListeners.size(); i++) {
835             if (e.getID() == ComponentEvent.COMPONENT_HIDDEN)
836                 ((ComponentListener) componentListeners.get(i)).componentHidden(e);
837             if (e.getID() == ComponentEvent.COMPONENT_MOVED)
838                 ((ComponentListener) componentListeners.get(i)).componentMoved(e);
839             if (e.getID() == ComponentEvent.COMPONENT_RESIZED)
840                 ((ComponentListener) componentListeners.get(i)).componentResized(e);
841             if (e.getID() == ComponentEvent.COMPONENT_SHOWN)
842                 ((ComponentListener) componentListeners.get(i)).componentShown(e);
843         }
844     }
845
846     /**
847      * Sends mouse events to component listeners
848      */

849     public void processMouseEvent(MouseEvent e) {
850         Iterator JavaDoc i = mouseListeners.iterator();
851         while (i.hasNext()) {
852             MouseListener ml = (MouseListener) i.next();
853             if (e.eventID == MouseEvent.CLICKED) ml.mouseClicked(e);
854             else if (e.eventID == MouseEvent.ENTERED) ml.mouseEntered(e);
855             else if (e.eventID == MouseEvent.EXITED) ml.mouseExited(e);
856             else if (e.eventID == MouseEvent.PRESSED) ml.mousePressed(e);
857             else if (e.eventID == MouseEvent.RELEASED) ml.mouseReleased(e);
858         }
859     }
860
861     /**
862      * Sends mouse motion events to component listeners
863      */

864     public void processMouseMotionEvent(MouseEvent e) {
865         for (int i = 0; i < mouseMotionListeners.size(); i++) {
866             MouseMotionListener ml = (MouseMotionListener) mouseMotionListeners.get(i);
867             if (e.getButton() != 0) {
868                 ml.mouseDragged(e);
869             }
870             else
871                 ml.mouseMoved(e);
872         }
873     }
874
875     /**
876      * Sends KeyEvents to component listeners
877      */

878     public void processKeyEvent(KeyEvent e) {
879         Iterator JavaDoc i = keyListeners.iterator();
880         while (i.hasNext()) {
881             KeyListener ml = (KeyListener) i.next();
882             if (e.eventID == KeyEvent.TYPED) ml.keyTyped(e);
883             if (e.eventID == KeyEvent.RELEASED) ml.keyReleased(e);
884             if (e.eventID == KeyEvent.PRESSED) ml.keyPressed(e);
885         }
886     }
887
888     /**
889      * Sends Focus Events to component listeners
890      */

891     public void processFocusEvent(FocusEvent e) {
892         Iterator JavaDoc i = focusListeners.iterator();
893         while (i.hasNext()) {
894             FocusListener fl = (FocusListener) i.next();
895             if (e.getID() == FocusEvent.FOCUS_GAINED) fl.focusGained(e);
896             if (e.getID() == FocusEvent.FOCUS_LOST) fl.focusLost(e);
897         }
898     }
899
900      /**
901       * Sends Input Method Events to component listeners
902       */

903      public void processInputMethodEvent(InputMethodEvent e) {
904          Iterator JavaDoc i = inputMethodListeners.iterator();
905          while (i.hasNext()) {
906              InputMethodListener iml = (InputMethodListener) i.next();
907              if (e.getID() == InputMethodEvent.CARET_POSITION_CHANGED) iml.caretPositionChanged(e);
908              if (e.getID() == InputMethodEvent.INPUT_METHOD_TEXT_CHANGED) iml.inputMethodTextChanged(e);
909          }
910      }
911
912     /**
913      * If the component has just been added to a container, but
914      * properties were set in the meantime, this routine gets
915      * called by JSWTComposite to set the cached properties.
916      */

917     public void setCachedProperties() {
918         if (peer == null) return;
919         peer.setToolTipText(pToolTipText);
920         if (pBackground != null) peer.setBackground(pBackground.getSWTColor());
921         if (pForeground != null) peer.setForeground(pForeground.getSWTColor());
922         if (pFont != null) peer.setFont(pFont.getSWTFont());
923         if (pSize != null && pLocation != null) {
924             peer.setBounds(pLocation.x, pLocation.y, pSize.width, pSize.height);
925         }
926         else if (pSize != null)
927             peer.setBounds(peer.getLocation().x, peer.getLocation().y, pSize.width, pSize.height);
928         else if (pLocation != null)
929             peer.setBounds(pLocation.x, pLocation.y, peer.getSize().x, peer.getSize().y);
930         if (pCursor != null && !pCursor.equals(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR))) peer.setCursor(pCursor.getSWTCursor());
931         if (pLayoutData != null) peer.setLayoutData(pLayoutData);
932     }
933
934     /**
935      * When the component has been added to a container, this
936      * routine gets called to tell it to set up SWT listeners
937      * for all its events. We can map these then to the
938      * AWT-style events
939      */

940     public void registerEvents() {
941
942         if (!SwingWTUtils.isSWTControlAvailable(peer)) return;
943
944         if (swingWTEventAdapter == null)
945             swingWTEventAdapter = new Component.SWTEventAdapter(this);
946
947         registerActionEvents();
948         registerComponentEvents();
949         registerMouseEvents();
950         registerKeyEvents();
951         registerFocusEvents();
952         registerTraversalEvents();
953         registerPaintEvents();
954         registerModifyEvents();
955         registerExtendedModifyEvents();
956     }
957
958     /**
959      * Adapter class to map SWT events to SwingWT ones. We use this for 2 reasons:
960      * 1. Less anonymous inner classes - only needs one to handle all events.
961      * 2. We can guarantee there will only be one SwingWT adapter
962      */

963     private class SWTEventAdapter implements org.eclipse.swt.events.PaintListener,
964                                              org.eclipse.swt.events.TraverseListener,
965                                              org.eclipse.swt.events.FocusListener,
966                                              org.eclipse.swt.events.MouseListener,
967                                              org.eclipse.swt.events.MouseTrackListener,
968                                              org.eclipse.swt.events.MouseMoveListener,
969                                              org.eclipse.swt.events.KeyListener,
970                                              org.eclipse.swt.events.ControlListener,
971                                              org.eclipse.swt.custom.ExtendedModifyListener,
972                                              org.eclipse.swt.events.ModifyListener {
973
974         /** The component this class is acting as an event adapter for */
975         private Component parent = null;
976
977         public SWTEventAdapter(Component parent) {
978             this.parent = parent;
979         }
980         public void paintControl(org.eclipse.swt.events.PaintEvent e) {
981             
982             // If the double-buffered flag is set, give the component an image to paint
983
// on and we'll render that onto the GC instead.
984
if (pDoubleBuffered) {
985                 org.eclipse.swt.graphics.Image img =
986                     new org.eclipse.swt.graphics.Image(SwingWTUtils.getDisplay(), e.width, e.height);
987                 org.eclipse.swt.graphics.GC gc =
988                     new org.eclipse.swt.graphics.GC(img);
989                 // Call out to the paint method
990
paintComponent(new SWTGraphics2DRenderer(gc));
991                 // Copy the buffered image onto the real gc
992
e.gc.drawImage(img, 0, 0);
993                 // Destroy our temporary gc and image
994
gc.dispose(); img.dispose();
995             }
996             else
997              paintComponent(new SWTGraphics2DRenderer(e.gc));
998             
999         }
1000        public void keyTraversed(org.eclipse.swt.events.TraverseEvent e) {
1001            processTraverseEvent(e);
1002        }
1003        public void focusGained(org.eclipse.swt.events.FocusEvent e) {
1004            FocusEvent ev = new FocusEvent(parent, FocusEvent.FOCUS_GAINED);
1005            processFocusEvent(ev);
1006        }
1007        public void focusLost(org.eclipse.swt.events.FocusEvent e) {
1008            FocusEvent ev = new FocusEvent(parent, FocusEvent.FOCUS_LOST);
1009            processFocusEvent(ev);
1010        }
1011        public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
1012            MouseEvent ev = new MouseEvent(parent);
1013            ev.clickCount = 1;
1014            ev.eventID = MouseEvent.PRESSED;
1015            ev.setX(e.x);
1016            ev.setY(e.y);
1017            ev.setButton(ev.translateSWTButton(e.button));
1018            processMouseEvent(ev);
1019            ev.eventID = MouseEvent.CLICKED;
1020            processMouseEvent(ev);
1021        }
1022        public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
1023            MouseEvent ev = new MouseEvent(parent);
1024            ev.clickCount = 2;
1025            ev.eventID = MouseEvent.CLICKED;
1026            ev.setX(e.x);
1027            ev.setY(e.y);
1028            ev.setButton(ev.translateSWTButton(e.button));
1029            processMouseEvent(ev);
1030        }
1031        public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
1032            MouseEvent ev = new MouseEvent(parent);
1033            ev.clickCount = 1;
1034            ev.eventID = MouseEvent.RELEASED;
1035            ev.setX(e.x);
1036            ev.setY(e.y);
1037            ev.setButton(ev.translateSWTButton(e.button));
1038            processMouseEvent(ev);
1039            processMouseMotionEvent(ev); // Released should also fire a drag event
1040
}
1041        public void mouseEnter(org.eclipse.swt.events.MouseEvent e) {
1042            MouseEvent ev = new MouseEvent(parent);
1043            ev.clickCount = 0;
1044            ev.eventID = MouseEvent.ENTERED;
1045            ev.setX(e.x);
1046            ev.setY(e.y);
1047            ev.setButton(ev.translateSWTButton(e.button));
1048            processMouseEvent(ev);
1049        }
1050        public void mouseExit(org.eclipse.swt.events.MouseEvent e) {
1051            MouseEvent ev = new MouseEvent(parent);
1052            ev.clickCount = 0;
1053            ev.eventID = MouseEvent.EXITED;
1054            ev.setX(e.x);
1055            ev.setY(e.y);
1056            ev.setButton(ev.translateSWTButton(e.button));
1057            processMouseEvent(ev);
1058        }
1059        public void mouseHover(org.eclipse.swt.events.MouseEvent e) {
1060            MouseEvent ev = new MouseEvent(parent);
1061            ev.eventID = MouseEvent.ENTERED;
1062            ev.setX(e.x);
1063            ev.setY(e.y);
1064            ev.setButton(ev.translateSWTButton(e.button));
1065            processMouseMotionEvent(ev);
1066        }
1067        public void mouseMove(org.eclipse.swt.events.MouseEvent e) {
1068            MouseEvent ev = new MouseEvent(parent);
1069            ev.eventID = MouseEvent.ENTERED;
1070            ev.setX(e.x);
1071            ev.setY(e.y);
1072            ev.setButton(ev.translateSWTButton(e.button));
1073            processMouseMotionEvent(ev);
1074        }
1075        public void keyPressed(org.eclipse.swt.events.KeyEvent e) {
1076            KeyEvent ev = new KeyEvent(parent);
1077            ev.eventID = KeyEvent.PRESSED;
1078            ev.setKeyChar(e.character);
1079            ev.setKeyCode(ev.translateSWTKey(e.keyCode));
1080            ev.setModifiers(ev.translateSWTModifiers(e.stateMask));
1081            processKeyEvent(ev);
1082
1083            // consumption
1084
if (e.doit) e.doit = !ev.isConsumed();
1085        }
1086        public void keyReleased(org.eclipse.swt.events.KeyEvent e) {
1087            KeyEvent ev = new KeyEvent(parent);
1088            ev.eventID = KeyEvent.RELEASED;
1089            ev.setKeyChar(e.character);
1090            ev.setKeyCode(ev.translateSWTKey(e.keyCode));
1091            ev.setModifiers(ev.translateSWTModifiers(e.stateMask));
1092            processKeyEvent(ev);
1093
1094            // consumption
1095
if (e.doit) e.doit = !ev.isConsumed();
1096        }
1097
1098        public void controlMoved(org.eclipse.swt.events.ControlEvent controlEvent) {
1099            processComponentEvent(new ComponentEvent(parent, ComponentEvent.COMPONENT_MOVED));
1100        }
1101
1102        public void controlResized(org.eclipse.swt.events.ControlEvent controlEvent) {
1103            processComponentEvent(new ComponentEvent(parent, ComponentEvent.COMPONENT_RESIZED));
1104        }
1105
1106        public void modifyText(ExtendedModifyEvent modifyEvent) {
1107
1108            // Modified text uses the INPUT_METHOD_TEXT_CHANGED event. caret change? not sure yet
1109
int id = InputMethodEvent.INPUT_METHOD_TEXT_CHANGED;
1110
1111            if (id > 0) {
1112
1113                String JavaDoc fullText = "";
1114                if ( peer instanceof StyledText ) fullText = ((StyledText)peer).getText();
1115                if ( peer instanceof Text ) fullText = ((Text)peer).getText();
1116                if ( peer instanceof CCombo ) fullText = ((CCombo)peer).getText();
1117                if ( peer instanceof Combo ) fullText = ((Combo)peer).getText();
1118
1119                int caretPosition = modifyEvent.start + modifyEvent.length;
1120
1121                AttributedString JavaDoc attributedString = new AttributedString JavaDoc(fullText);
1122                AttributedCharacterIterator JavaDoc text = attributedString.
1123                        getIterator(null, modifyEvent.start, caretPosition);
1124                TextHitInfo textHitInfo = TextHitInfo.trailing(caretPosition);
1125                InputMethodEvent inputMethodEvent = new InputMethodEvent( parent, id, text, modifyEvent.length,
1126                                                                          textHitInfo, textHitInfo );
1127                processInputMethodEvent(inputMethodEvent);
1128
1129            }
1130            else {
1131                throw new IllegalComponentStateException("Unknown ExtendedModifyState error");
1132            }
1133        }
1134
1135        public void modifyText(ModifyEvent modifyEvent) {
1136            // HTF do you use this event?
1137
}
1138
1139        
1140    }
1141
1142
1143    protected void registerPaintEvents() {
1144        peer.removePaintListener(swingWTEventAdapter);
1145        peer.addPaintListener(swingWTEventAdapter);
1146    }
1147
1148    protected void registerTraversalEvents() {
1149        peer.removeTraverseListener(swingWTEventAdapter);
1150        peer.addTraverseListener(swingWTEventAdapter);
1151    }
1152
1153    protected void registerFocusEvents() {
1154        peer.removeFocusListener(swingWTEventAdapter);
1155        peer.addFocusListener(swingWTEventAdapter);
1156    }
1157
1158    protected void registerComponentEvents() {
1159        peer.removeControlListener(swingWTEventAdapter);
1160        peer.addControlListener(swingWTEventAdapter);
1161    }
1162
1163    protected void registerMouseEvents() {
1164        peer.removeMouseListener(swingWTEventAdapter);
1165        peer.removeMouseTrackListener(swingWTEventAdapter);
1166        peer.addMouseListener(swingWTEventAdapter);
1167        peer.addMouseTrackListener(swingWTEventAdapter);
1168    }
1169
1170    protected void registerKeyEvents() {
1171        peer.removeKeyListener(swingWTEventAdapter);
1172        peer.addKeyListener(swingWTEventAdapter);
1173    }
1174
1175    protected void registerActionEvents() {
1176        if (swingWTDefaultActionEventAdapter == null)
1177            swingWTDefaultActionEventAdapter = new org.eclipse.swt.events.MouseListener() {
1178                public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
1179                    processActionEvent(0);
1180                }
1181                public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
1182                    processActionEvent(0);
1183                }
1184                public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
1185                }
1186            };
1187        peer.removeMouseListener(swingWTDefaultActionEventAdapter);
1188        peer.addMouseListener(swingWTDefaultActionEventAdapter);
1189    }
1190
1191
1192    /**
1193     * Handle ModifyEvents (maps to ImputMethodEvents) by checking the final derivation
1194     * of this Component. Unusual way to do it, but since InputMethodEvents are handled in the base Component,
1195     * unlike SWT, it's the most effective way to maintain backwards compatibility.
1196     *
1197     * This method is being used for speed - the alternatives are to use peer.getClass().getMethod(...) or
1198     * peer.getClass().getMethods() and search for required methods. getMethod() has the problem of throwing
1199     * an exception, which will eat up resources given how often this code will be called. getMethods(), likewise,
1200     * will usually return a very large list, taking a long time to traverse.
1201     *
1202     * This means if SWT is updated to have new components return ModifyEvents, it will need to be added here.
1203     */

1204    protected void registerModifyEvents() {
1205        if ( peer instanceof StyledText || peer instanceof Text ||
1206             peer instanceof CCombo || peer instanceof Combo )
1207        {
1208            boolean success = false;
1209            try
1210            {
1211                Method JavaDoc addMethod = peer.getClass().getMethod("addModifyListener", new Class JavaDoc[] {ModifyListener.class});
1212                Method JavaDoc removeMethod = peer.getClass().getMethod("removeModifyListener", new Class JavaDoc[] {ModifyListener.class});
1213                removeMethod.invoke(peer, new Object JavaDoc[] {swingWTEventAdapter});
1214                addMethod.invoke(peer, new Object JavaDoc[] {swingWTEventAdapter});
1215
1216                success = true;
1217            }
1218            catch (NoSuchMethodException JavaDoc e) {}
1219            catch (SecurityException JavaDoc e) {}
1220            catch (IllegalAccessException JavaDoc e) {}
1221            catch (InvocationTargetException JavaDoc e) {}
1222
1223            if (!success)
1224                throw new IllegalComponentStateException("SWT peer missing required methods! (?)");
1225        }
1226    }
1227
1228    /**
1229     * Same as above, but for ExtendedModifyEvents
1230     */

1231    protected void registerExtendedModifyEvents() {
1232        if ( peer instanceof StyledText )
1233        {
1234            boolean success = false;
1235            try
1236            {
1237                Method JavaDoc addMethod = peer.getClass().getMethod("addExtendedModifyListener",
1238                                                             new Class JavaDoc[] {ModifyListener.class});
1239                Method JavaDoc removeMethod = peer.getClass().getMethod("removeExtendedModifyListener",
1240                                                                new Class JavaDoc[] {ModifyListener.class});
1241                removeMethod.invoke(peer, new Object JavaDoc[] {swingWTEventAdapter});
1242                addMethod.invoke(peer, new Object JavaDoc[] {swingWTEventAdapter});
1243
1244                success = true;
1245            }
1246            catch (NoSuchMethodException JavaDoc e) {}
1247            catch (SecurityException JavaDoc e) {}
1248            catch (IllegalAccessException JavaDoc e) {}
1249            catch (InvocationTargetException JavaDoc e) {}
1250
1251            if (!success)
1252                throw new IllegalComponentStateException("SWT peer missing required methods! (?)");
1253        }
1254    }
1255
1256    /**
1257     * Called when a traversal key is pressed by the user. This routine
1258     * determines whether we should use the custom focus manager if one is
1259     * avaliable, or just let it go.
1260     */

1261    protected void processTraverseEvent(org.eclipse.swt.events.TraverseEvent e) {
1262
1263        // If it is ENTER that's pressed, then that is actually an ActionEvent
1264
// in the Swing world!
1265
if (e.keyCode == SWT.CR) {
1266            processActionEvent(0);
1267        }
1268
1269        // If there is no focus manager - forget it - use whatever the
1270
// hell SWT uses (creation order I think)
1271
if (swingwtx.swing.FocusManager.getCurrentManager() == null) { return; }
1272
1273        // Focus previous component
1274
if (((e.keyCode == SWT.TAB) && ((e.stateMask & SWT.SHIFT)) != 0) ||
1275           (e.detail == SWT.TRAVERSE_TAB_PREVIOUS)) {
1276            e.doit = false;
1277            swingwtx.swing.FocusManager.getCurrentManager().focusPreviousComponent(this);
1278        }
1279
1280        // Focus next component
1281
if (((e.keyCode == SWT.TAB) && ((e.stateMask & SWT.SHIFT)) == 0) ||
1282           (e.detail == SWT.TRAVERSE_TAB_NEXT)) {
1283            e.doit = false;
1284            swingwtx.swing.FocusManager.getCurrentManager().focusNextComponent(this);
1285        }
1286
1287    }
1288
1289    /**
1290     * When a container removes a component, it calls this method. You can override
1291     * this in your subclass to perform any specific action you need to take
1292     * when that component is removed.
1293     */

1294    protected void setComponentRemoved() {
1295    }
1296
1297    /** Here for compatibility - I'm not sure what it's for. Doesn't do anything anyway. :-) */
1298    public boolean imageUpdate(
1299            Image img,
1300            int infoflags,
1301            int x,
1302            int y,
1303            int width,
1304            int height) {
1305            //Graphics g = img.getGraphics();
1306
return false;
1307    }
1308    /** Returns true if this component has the focus */
1309    public boolean hasFocus() {
1310    final boolean[] ret = new boolean[1];
1311    SwingUtilities.invokeSync(
1312        new Runnable JavaDoc() {
1313        public void run() {
1314                    if (SwingWTUtils.isSWTControlAvailable(peer))
1315            ret[0] = peer.isFocusControl();
1316            else
1317                ret[0] = false;
1318        }
1319        });
1320    return ret[0];
1321    }
1322
1323    /** For compatibility with 3rd party layout managers -
1324     * SwingWT doesn't need this as all laying out is
1325     * marshalled onto a queued single thread
1326     */

1327    protected Object JavaDoc LOCK = new Object JavaDoc();
1328    /** For compatibility with 3rd party layout managers -
1329     * SwingWT doesn't need this as all laying out is
1330     * marshalled onto a queued single thread
1331     */

1332    public Object JavaDoc getTreeLock() { return LOCK; }
1333
1334    /** Returns the component's accessible context */
1335    public AccessibleContext getAccessibleContext() { return accessibleContext; }
1336
1337}
1338
1339/*
1340   $Log: Component.java,v $
1341   Revision 1.80 2004/05/07 12:11:15 bobintetley
1342   Default layout fixes and correct behaviour for null layout
1343
1344   Revision 1.79 2004/05/06 12:35:21 bobintetley
1345   Parity with Swing constants for Binary Compatibility + fixes to JDesktopPane
1346
1347   Revision 1.78 2004/05/05 23:39:39 laurentmartelli
1348    - Added more repaint() methods
1349    - Added isMinimumSizeSet()
1350    - setFont(Font): do not set isUserSetFont is font is null
1351
1352   Revision 1.77 2004/05/05 19:29:35 bobintetley
1353   JComponent.paintComponent() is now called correctly
1354
1355   Revision 1.76 2004/05/05 12:43:19 bobintetley
1356   Patches/new files from Laurent Martell
1357
1358   Revision 1.75 2004/05/04 00:57:02 dannaab
1359   Sun java and javax package naming improvements. Fix ambiguous class names by hardcoding SWT full class name
1360
1361   Revision 1.74 2004/05/03 20:53:42 dannaab
1362   small tweak to layoutmanager instance objects to make easier to read
1363
1364   Revision 1.73 2004/04/30 23:18:24 dannaab
1365   List selection support, misc bug fixes
1366
1367   Revision 1.72 2004/04/30 21:54:21 bobintetley
1368   Moved log to the end of commonly changed files
1369
1370   Revision 1.71 2004/04/30 13:20:40 bobintetley
1371   Fix to unrealised peer preferred sizes, forwarding window events to
1372   content panes and fix for mouse drag events.
1373
1374   Revision 1.70 2004/04/28 08:38:05 bobintetley
1375   Hierarchy fixes, code cleanup for base classes, additional javadocs and use of flag to identify JComponent descendants with peers
1376
1377   Revision 1.69 2004/04/27 06:37:42 bobintetley
1378   Corrected hierarchy with JComponent descending Container
1379
1380   Revision 1.68 2004/04/23 10:02:47 bobintetley
1381   MouseEvent BUTTON_MASK constants, JSlider.createStandardLabels(), FontMetrics
1382   thread safety, Component/Toolkit.getFontMetrics() and MouseMotionAdapter
1383
1384   Revision 1.67 2004/04/23 00:52:31 dannaab
1385   Handle borders in a Swing-like way. Implement EmptyBorder & TitledBorder
1386
1387   Revision 1.66 2004/04/21 07:10:44 bobintetley
1388   Fixes to native build script and a few code tweaks. Removal of AWT
1389   native lib for compilation purposes.
1390
1391   Revision 1.65 2004/04/20 19:05:38 bobintetley
1392   Code cleanup/refactoring
1393
1394   Revision 1.64 2004/04/20 14:34:15 bobintetley
1395   Double buffering support for component paint()
1396
1397   Revision 1.63 2004/04/19 15:03:25 bobintetley
1398   Missing Component/JComponent focus methods
1399
1400   Revision 1.62 2004/04/16 18:29:24 bobintetley
1401   Fixes to JTable to only use TableCursors if necessary (at least one row of data and at least one editable column)
1402
1403   Revision 1.61 2004/04/16 10:19:05 dannaab
1404   Misc bug fixes, InputMap implementation, preliminary undo support
1405
1406   Revision 1.60 2004/04/15 12:28:00 bobintetley
1407   Fix to cache location/dimension info
1408
1409   Revision 1.59 2004/04/15 11:24:32 bobintetley
1410   (Dan Naab) ComponentUI, UIDefaults/UIManager and Accessibility support.
1411   (Antonio Weber) TableColumnModelListener implementation and support
1412
1413   Revision 1.58 2004/03/31 21:26:11 bobintetley
1414   Component.hasFocus() support
1415
1416   Revision 1.57 2004/03/31 08:26:41 bobintetley
1417   MouseMotionListener support
1418
1419   Revision 1.56 2004/03/31 08:07:34 bobintetley
1420   JTable bug fixed that prevented it redrawing after being dropped from
1421   a container. Protected component method allows catching of when a component
1422   is removed from a container.
1423
1424   Revision 1.55 2004/03/30 14:22:27 bobintetley
1425   Fix to Component min/max sizing code, fix to JPanel insets with titled
1426   borders (all of which indirectly fix problems with BoxLayout). Addition
1427   of ComponentAdapter
1428
1429   Revision 1.54 2004/03/12 11:05:23 bobintetley
1430   Fixed memory leak in container destruction
1431
1432   Revision 1.53 2004/03/02 08:39:53 bobintetley
1433   Less resource intensive event pump and JTable defaults gridlines now
1434
1435   Revision 1.52 2004/03/01 15:58:28 bobintetley
1436   Various little bug fixes
1437
1438   Revision 1.51 2004/03/01 12:25:46 bobintetley
1439   Better HTML conversion, custom JFileChooser support, JLabel, Window and
1440   Image fixes to improve compatibility
1441
1442   Revision 1.50 2004/02/24 09:36:39 bobintetley
1443   Compatibility methods
1444
1445   Revision 1.49 2004/02/23 12:11:21 bobintetley
1446   JScrollPane bug fixed, tabbing in JTextArea fixed
1447
1448   Revision 1.48 2004/02/23 10:07:09 bobintetley
1449   Cell render font support
1450
1451   Revision 1.47 2004/02/02 14:11:50 bobintetley
1452   Tweaks and refining of JScrollPane implementation
1453
1454   Revision 1.46 2004/02/02 12:36:36 bobintetley
1455   Proper JScrollPane/ScrollBar implementation
1456
1457   Revision 1.45 2004/01/27 11:06:09 bobintetley
1458   Fixed bugs in dropping/re-adding the same components
1459
1460   Revision 1.44 2004/01/26 14:26:29 bobintetley
1461   Container/Component Listener/Event support
1462
1463   Revision 1.43 2004/01/26 12:02:49 bobintetley
1464   JPanel titled border support
1465
1466   Revision 1.42 2004/01/26 10:37:09 bobintetley
1467   Better painting support
1468
1469   Revision 1.41 2004/01/26 08:10:59 bobintetley
1470   Many bugfixes and addition of SwingSet
1471
1472   Revision 1.40 2004/01/20 07:38:05 bobintetley
1473   Bug fixes and compatibility methods
1474
1475   Revision 1.39 2004/01/16 15:53:32 bobintetley
1476   Many compatibility methods added to Container, Component, JInternalFrame,
1477      UIManager, SwingUtilities, JTabbedPane, JPasswordField, JCheckBox
1478      and JRadioButton.
1479
1480   Revision 1.38 2004/01/15 15:58:41 bobintetley
1481   Extra thread safety
1482
1483   Revision 1.37 2004/01/15 15:20:29 bobintetley
1484   Java2D work
1485
1486   Revision 1.36 2004/01/15 10:11:14 bobintetley
1487   Fixed AWT constructors/hierarchy
1488
1489   Revision 1.35 2004/01/13 11:14:25 bobintetley
1490   ErrorProvider and validation implementation
1491
1492   Revision 1.34 2004/01/09 10:33:56 bobintetley
1493   Changes for JToolBar to allow platform ToolBars, mixed with other components
1494
1495   Revision 1.33 2004/01/08 12:56:30 bobintetley
1496   Experiment with hotkeys - unworkable in current SWT
1497
1498   Revision 1.32 2004/01/05 15:29:35 bobintetley
1499   TableLayout fixes
1500
1501   Revision 1.31 2004/01/05 11:41:34 bobintetley
1502   Closer to working...
1503
1504   Revision 1.30 2004/01/05 11:31:46 bobintetley
1505   More layout fixes
1506
1507   Revision 1.29 2004/01/05 10:09:52 bobintetley
1508   Various fixes for new layouts
1509
1510   Revision 1.28 2004/01/05 08:36:42 bobintetley
1511   Had to merge Daniel's changes in by hand (thanks)
1512
1513   Revision 1.26 2003/12/22 15:24:17 bobintetley
1514   Additional methods to allow components using TableLayout to consume
1515   available space
1516
1517   Revision 1.25 2003/12/17 16:30:35 bobintetley
1518   Flowlayout fix, vertical toolbar support and cleaned up text alignment
1519   hierarchy.
1520
1521   Revision 1.24 2003/12/17 15:24:33 bobintetley
1522   Threading fixes
1523
1524   Revision 1.23 2003/12/17 11:37:46 bobintetley
1525   Better GridBag support
1526
1527   Revision 1.22 2003/12/17 10:57:35 bobintetley
1528   JTableHeader implementation plus Table event/model fixes
1529
1530   Revision 1.21 2003/12/16 15:47:44 bobintetley
1531   Thread safety added to common methods
1532
1533   Revision 1.20 2003/12/16 14:08:05 bobintetley
1534   Corrected event hierarchy for Button ActionEvents
1535
1536   Revision 1.19 2003/12/16 13:48:27 bobintetley
1537   ActionEvent maps to CR press
1538
1539   Revision 1.18 2003/12/16 13:44:04 bobintetley
1540   Fix to broken focus management
1541
1542   Revision 1.17 2003/12/16 13:14:33 bobintetley
1543   Use of SwingWTUtils.isSWTControlAvailable instead of null test
1544
1545   Revision 1.16 2003/12/16 12:23:31 bobintetley
1546   Corrected handling of table selection + keyboard action events
1547
1548   Revision 1.15 2003/12/16 11:43:57 bobintetley
1549   Fixed focus traversal for use by FocusManager
1550
1551   Revision 1.14 2003/12/16 11:40:37 bobintetley
1552   JTable refreshes now when model updates
1553
1554   Revision 1.13 2003/12/16 09:20:07 bobintetley
1555   KeyEvent consumption logic wrong way round
1556
1557   Revision 1.12 2003/12/16 09:19:02 bobintetley
1558   Various small fixes to match Swing more closely
1559
1560   Revision 1.11 2003/12/15 18:29:56 bobintetley
1561   Changed setParent() method to setSwingWTParent() to avoid conflicts with applications
1562
1563   Revision 1.10 2003/12/14 09:13:38 bobintetley
1564   Added CVS log to source headers
1565
1566*/

1567
Popular Tags