OSDN Git Service

libjava/
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / java / awt / peer / swing / SwingLabelPeer.java
1 /* SwingLabelPeer.java -- A Swing based peer for AWT labels
2    Copyright (C)  2006, 2007  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38 package gnu.java.awt.peer.swing;
39
40 import java.awt.Container;
41 import java.awt.Graphics;
42 import java.awt.Image;
43 import java.awt.Label;
44 import java.awt.Point;
45 import java.awt.event.FocusEvent;
46 import java.awt.event.KeyEvent;
47 import java.awt.event.MouseEvent;
48 import java.awt.peer.LabelPeer;
49
50 import javax.swing.JComponent;
51 import javax.swing.JLabel;
52
53
54 /**
55  * A Label peer based on {@link JLabel}.
56  *
57  * @author Roman Kennke (kennke@aicas.com)
58  */
59 public class SwingLabelPeer
60   extends SwingComponentPeer
61   implements LabelPeer
62 {
63
64   /**
65    * A spezialized Swing label used to paint the label for the AWT Label. 
66    *
67    * @author Roman Kennke (kennke@aicas.com)
68    */
69   private class SwingLabel
70     extends JLabel
71     implements SwingComponent
72   {
73     Label label;
74       
75       
76     SwingLabel(Label label)
77     {
78         this.label = label;
79     }
80     
81     /**
82      * Returns this label.
83      *
84      * @return <code>this</code>
85      */
86     public JComponent getJComponent()
87     {
88       return this;
89     }
90
91     /**
92      * Handles mouse events by forwarding it to
93      * <code>processMouseEvent()</code>.
94      *
95      * @param ev the mouse event
96      */
97     public void handleMouseEvent(MouseEvent ev)
98     {
99       processMouseEvent(ev);
100     }
101
102     /**
103      * Handles mouse motion events by forwarding it to
104      * <code>processMouseMotionEvent()</code>.
105      *
106      * @param ev the mouse motion event
107      */
108     public void handleMouseMotionEvent(MouseEvent ev)
109     {
110       processMouseMotionEvent(ev);
111     }
112
113     /**
114      * Handles key events by forwarding it to <code>processKeyEvent()</code>.
115      *
116      * @param ev the mouse event
117      */
118     public void handleKeyEvent(KeyEvent ev)
119     {
120       processKeyEvent(ev);
121     }
122
123     /**
124      * Handles focus events by forwarding it to
125      * <code>processFocusEvent()</code>.
126      *
127      * @param ev the Focus event
128      */
129     public void handleFocusEvent(FocusEvent ev)
130     {
131       processFocusEvent(ev);
132     }
133
134     /**
135      * Overridden so that this method returns the correct value even without a
136      * peer.
137      *
138      * @return the screen location of the button
139      */
140     public Point getLocationOnScreen()
141     {
142       return SwingLabelPeer.this.getLocationOnScreen();
143     }
144
145     /**
146      * Overridden so that the isShowing method returns the correct value for the
147      * swing button, even if it has no peer on its own.
148      *
149      * @return <code>true</code> if the button is currently showing,
150      *         <code>false</code> otherwise
151      */
152     public boolean isShowing()
153     {
154       boolean retVal = false;
155       if (label != null)
156         retVal = label.isShowing();
157       return retVal;
158     }
159
160     /**
161      * Overridden, so that the Swing button can create an Image without its
162      * own peer.
163      *
164      * @param w the width of the image
165      * @param h the height of the image
166      *
167      * @return an image
168      */
169     public Image createImage(int w, int h)
170     {
171       return SwingLabelPeer.this.createImage(w, h);
172     }
173
174     public Graphics getGraphics()
175     {
176       return SwingLabelPeer.this.getGraphics();
177     }
178
179     public Container getParent()
180     {
181       Container par = null;
182       if (label != null)
183         par = label.getParent();
184       return par;
185     }
186   }
187
188   /**
189    * Creates a new <code>SwingLabelPeer</code> for the specified AWT label.
190    *
191    * @param label the AWT label
192    */
193   public SwingLabelPeer(Label label)
194   {
195     super();
196     SwingLabel swingLabel = new SwingLabel(label);
197     swingLabel.setText(label.getText());
198     swingLabel.setOpaque(true);
199     init(label, swingLabel);
200     setAlignment(label.getAlignment());
201   }
202
203   /**
204    * Sets the text of the label. This is implemented to set the text on the
205    * Swing label.
206    *
207    * @param text the text to be set
208    */
209   public void setText(String text)
210   {
211     ((JLabel) swingComponent.getJComponent()).setText(text);
212   }
213
214   /**
215    * Sets the horizontal alignment of the label. This is implemented to
216    * set the alignment on the Swing label.
217    *
218    * @param alignment the horizontal alignment
219    *
220    * @see Label#LEFT
221    * @see Label#RIGHT
222    * @see Label#CENTER
223    */
224   public void setAlignment(int alignment)
225   {
226     JLabel swingLabel = (JLabel) swingComponent.getJComponent();
227     switch (alignment)
228       {
229       case Label.RIGHT:
230         swingLabel.setHorizontalAlignment(JLabel.RIGHT);
231         break;
232       case Label.CENTER:
233         swingLabel.setHorizontalAlignment(JLabel.CENTER);
234         break;
235       case Label.LEFT:
236       default:
237         swingLabel.setHorizontalAlignment(JLabel.LEFT);
238         break;
239       }
240   }
241
242 }