Source for gnu.java.awt.peer.swing.SwingLabelPeer

   1: /* SwingLabelPeer.java -- A Swing based peer for AWT labels
   2:    Copyright (C)  2006  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.Image;
  41: import java.awt.Label;
  42: import java.awt.Point;
  43: import java.awt.event.KeyEvent;
  44: import java.awt.event.MouseEvent;
  45: import java.awt.peer.LabelPeer;
  46: 
  47: import javax.swing.JComponent;
  48: import javax.swing.JLabel;
  49: 
  50: 
  51: /**
  52:  * A Label peer based on {@link JLabel}.
  53:  *
  54:  * @author Roman Kennke (kennke@aicas.com)
  55:  */
  56: public class SwingLabelPeer
  57:   extends SwingComponentPeer
  58:   implements LabelPeer
  59: {
  60: 
  61:   /**
  62:    * A spezialized Swing label used to paint the label for the AWT Label. 
  63:    *
  64:    * @author Roman Kennke (kennke@aicas.com)
  65:    */
  66:   private class SwingLabel
  67:     extends JLabel
  68:     implements SwingComponent
  69:   {
  70: 
  71:     /**
  72:      * Returns this label.
  73:      *
  74:      * @return <code>this</code>
  75:      */
  76:     public JComponent getJComponent()
  77:     {
  78:       return this;
  79:     }
  80: 
  81:     /**
  82:      * Handles mouse events by forwarding it to
  83:      * <code>processMouseEvent()</code>.
  84:      *
  85:      * @param ev the mouse event
  86:      */
  87:     public void handleMouseEvent(MouseEvent ev)
  88:     {
  89:       processMouseEvent(ev);
  90:     }
  91: 
  92:     /**
  93:      * Handles mouse motion events by forwarding it to
  94:      * <code>processMouseMotionEvent()</code>.
  95:      *
  96:      * @param ev the mouse motion event
  97:      */
  98:     public void handleMouseMotionEvent(MouseEvent ev)
  99:     {
 100:       processMouseMotionEvent(ev);
 101:     }
 102: 
 103:     /**
 104:      * Handles key events by forwarding it to <code>processKeyEvent()</code>.
 105:      *
 106:      * @param ev the mouse event
 107:      */
 108:     public void handleKeyEvent(KeyEvent ev)
 109:     {
 110:       processKeyEvent(ev);
 111:     }
 112: 
 113:     /**
 114:      * Overridden so that this method returns the correct value even without a
 115:      * peer.
 116:      *
 117:      * @return the screen location of the button
 118:      */
 119:     public Point getLocationOnScreen()
 120:     {
 121:       return SwingLabelPeer.this.getLocationOnScreen();
 122:     }
 123: 
 124:     /**
 125:      * Overridden so that the isShowing method returns the correct value for the
 126:      * swing button, even if it has no peer on its own.
 127:      *
 128:      * @return <code>true</code> if the button is currently showing,
 129:      *         <code>false</code> otherwise
 130:      */
 131:     public boolean isShowing()
 132:     {
 133:       boolean retVal = false;
 134:       if (SwingLabelPeer.this.awtComponent != null)
 135:         retVal = SwingLabelPeer.this.awtComponent.isShowing();
 136:       return retVal;
 137:     }
 138: 
 139:     /**
 140:      * Overridden, so that the Swing button can create an Image without its
 141:      * own peer.
 142:      *
 143:      * @param w the width of the image
 144:      * @param h the height of the image
 145:      *
 146:      * @return an image
 147:      */
 148:     public Image createImage(int w, int h)
 149:     {
 150:       return SwingLabelPeer.this.createImage(w, h);
 151:     }
 152:     
 153:   }
 154: 
 155:   /**
 156:    * Creates a new <code>SwingLabelPeer</code> for the specified AWT label.
 157:    *
 158:    * @param label the AWT label
 159:    */
 160:   public SwingLabelPeer(Label label)
 161:   {
 162:     super();
 163:     SwingLabel swingLabel = new SwingLabel();
 164:     swingLabel.setText(label.getText());
 165:     swingLabel.setHorizontalAlignment(label.getAlignment());
 166:     swingLabel.setOpaque(true);
 167:     init(label, swingLabel);
 168:   }
 169: 
 170:   /**
 171:    * Sets the text of the label. This is implemented to set the text on the
 172:    * Swing label.
 173:    *
 174:    * @param text the text to be set
 175:    */
 176:   public void setText(String text)
 177:   {
 178:     ((JLabel) swingComponent.getJComponent()).setText(text);
 179:   }
 180: 
 181:   /**
 182:    * Sets the horizontal alignment of the label. This is implemented to
 183:    * set the alignment on the Swing label.
 184:    *
 185:    * @param alignment the horizontal alignment
 186:    *
 187:    * @see Label#LEFT
 188:    * @see Label#RIGHT
 189:    * @see Label#CENTER
 190:    */
 191:   public void setAlignment(int alignment)
 192:   {
 193:     ((JLabel) swingComponent.getJComponent()).setHorizontalAlignment(alignment);
 194:   }
 195: 
 196: }