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

   1: /* SwingTextFieldPeer.java -- A Swing based peer for AWT textfields
   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: package gnu.java.awt.peer.swing;
  38: 
  39: import java.awt.Dimension;
  40: import java.awt.Image;
  41: import java.awt.Point;
  42: import java.awt.Rectangle;
  43: import java.awt.TextField;
  44: import java.awt.event.KeyEvent;
  45: import java.awt.event.MouseEvent;
  46: import java.awt.im.InputMethodRequests;
  47: import java.awt.peer.TextFieldPeer;
  48: 
  49: import javax.swing.JComponent;
  50: import javax.swing.JTextField;
  51: 
  52: /**
  53:  * A TextFieldPeer based on Swing JTextField.
  54:  *
  55:  * @author Roman Kennke (kennke@aicas.com)
  56:  */
  57: public class SwingTextFieldPeer
  58:   extends SwingComponentPeer
  59:   implements TextFieldPeer
  60: {
  61: 
  62:   /**
  63:    * A specialized Swing textfield for use in the peer.
  64:    *
  65:    * @author Roman Kennke (kennke@aicas.com)
  66:    */
  67:   private class SwingTextField
  68:     extends JTextField
  69:     implements SwingComponent
  70:   {
  71: 
  72:     /**
  73:      * Overridden to provide normal behaviour even without a real peer
  74:      * attached.
  75:      *
  76:      * @return the location of the textfield on screen
  77:      */
  78:     public Point getLocationOnScreen()
  79:     {
  80:       return SwingTextFieldPeer.this.getLocationOnScreen();
  81:     }
  82: 
  83:     /**
  84:      * Overridden so that the isShowing method returns the correct value for the
  85:      * swing button, even if it has no peer on its own.
  86:      *
  87:      * @return <code>true</code> if the button is currently showing,
  88:      *         <code>false</code> otherwise
  89:      */
  90:     public boolean isShowing()
  91:     {
  92:       boolean retVal = false;
  93:       if (SwingTextFieldPeer.this.awtComponent != null)
  94:         retVal = SwingTextFieldPeer.this.awtComponent.isShowing();
  95:       return retVal;
  96:     }
  97: 
  98:     /**
  99:      * Overridden, so that the Swing button can create an Image without its
 100:      * own peer.
 101:      *
 102:      * @param w the width of the image
 103:      * @param h the height of the image
 104:      *
 105:      * @return an image
 106:      */
 107:     public Image createImage(int w, int h)
 108:     {
 109:       return SwingTextFieldPeer.this.createImage(w, h);
 110:     }
 111: 
 112:     /**
 113:      * Returns this textfield.
 114:      *
 115:      * @return <code>this</code>
 116:      */
 117:     public JComponent getJComponent()
 118:     {
 119:       return this;
 120:     }
 121: 
 122:     /**
 123:      * Handles mouse events by forwarding it to the swing textfield.
 124:      *
 125:      * @param ev the mouse event
 126:      */
 127:     public void handleMouseEvent(MouseEvent ev)
 128:     {
 129:       ev.setSource(this);
 130:       processMouseEvent(ev);
 131:     }
 132: 
 133:     /**
 134:      * Handles mouse motion events by forwarding it to the swing textfield.
 135:      *
 136:      * @param ev the mouse motion event
 137:      */
 138:     public void handleMouseMotionEvent(MouseEvent ev)
 139:     {
 140:       ev.setSource(this);
 141:       processMouseMotionEvent(ev);
 142:     }
 143: 
 144:     /**
 145:      * Handles key events by forwarding it to the swing textfield.
 146:      *
 147:      * @param ev the key event
 148:      */
 149:     public void handleKeyEvent(KeyEvent ev)
 150:     {
 151:       ev.setSource(this);
 152:       processKeyEvent(ev);
 153:     }
 154:     
 155:   }
 156: 
 157:   /**
 158:    * Creates a new <code>SwingTextFieldPeer</code> instance for the specified
 159:    * AWT textfield.
 160:    *
 161:    * @param textField the AWT textfield
 162:    */
 163:   public SwingTextFieldPeer(TextField textField)
 164:   {
 165:     SwingTextField swingTextField = new SwingTextField();
 166:     swingTextField.setText(textField.getText());
 167:     init(textField, swingTextField);
 168:   }
 169: 
 170:   /**
 171:    * Returns the minimum size of the textfield.
 172:    *
 173:    * @param len not used here
 174:    *
 175:    * @return the minimum size of the textfield
 176:    */
 177:   public Dimension minimumSize(int len)
 178:   {
 179:     return swingComponent.getJComponent().getMinimumSize();
 180:   }
 181: 
 182:   /**
 183:    * Returns the preferred size of the textfield.
 184:    *
 185:    * @param len not used here
 186:    *
 187:    * @return the preferred size of the textfield
 188:    */
 189:   public Dimension preferredSize(int len)
 190:   {
 191:     return swingComponent.getJComponent().getPreferredSize();
 192:   }
 193: 
 194:   /**
 195:    * Returns the minimum size of the textfield.
 196:    *
 197:    * @param len not used here
 198:    *
 199:    * @return the minimum size of the textfield
 200:    */
 201:   public Dimension getMinimumSize(int len)
 202:   {
 203:     return swingComponent.getJComponent().getMinimumSize();
 204:   }
 205: 
 206:   /**
 207:    * Returns the preferred size of the textfield.
 208:    *
 209:    * @param len not used here
 210:    *
 211:    * @return the preferred size of the textfield
 212:    */
 213:   public Dimension getPreferredSize(int len)
 214:   {
 215:     return swingComponent.getJComponent().getPreferredSize();
 216:   }
 217: 
 218:   /**
 219:    * Sets the echo character.
 220:    *
 221:    * @param echoChar the echo character to be set
 222:    */
 223:   public void setEchoChar(char echoChar)
 224:   {
 225:     // TODO: Must be implemented.
 226:   }
 227: 
 228:   /**
 229:    * Sets the echo character.
 230:    *
 231:    * @param echoChar the echo character to be set
 232:    */
 233:   public void setEchoCharacter(char echoChar)
 234:   {
 235:     // TODO: Must be implemented.
 236:   }
 237: 
 238:   /**
 239:    * Returns the end index of the current selection.
 240:    *
 241:    * @return the end index of the current selection
 242:    */
 243:   public int getSelectionEnd()
 244:   {
 245:     // TODO: Must be implemented.
 246:     return 0;
 247:   }
 248: 
 249:   /**
 250:    * Returns the start index of the current selection.
 251:    *
 252:    * @return the start index of the current selection
 253:    */
 254:   public int getSelectionStart()
 255:   {
 256:     // TODO: Must be implemented.
 257:     return 0;
 258:   }
 259: 
 260:   /**
 261:    * Returns the current content of the textfield.
 262:    *
 263:    * @return the current content of the textfield
 264:    */
 265:   public String getText()
 266:   {
 267:     return ((JTextField) swingComponent.getJComponent()).getText();
 268:   }
 269: 
 270:   /**
 271:    * Sets the content of the textfield.
 272:    *
 273:    * @param text the text to set
 274:    */
 275:   public void setText(String text)
 276:   {
 277:     ((JTextField) swingComponent.getJComponent()).setText(text);
 278:   }
 279: 
 280:   /**
 281:    * Sets the current selection.
 282:    *
 283:    * @param startPos the start index of the selection
 284:    * @param endPos the start index of the selection
 285:    */
 286:   public void select(int startPos, int endPos)
 287:   {
 288:     // TODO: Must be implemented.
 289:   }
 290: 
 291:   /**
 292:    * Sets the editable flag of the text field.
 293:    *
 294:    * @param editable <code>true</code> to make the textfield editable,
 295:    *        <code>false</code> to make it uneditable
 296:    */
 297:   public void setEditable(boolean editable)
 298:   {
 299:     ((JTextField) swingComponent.getJComponent()).setEditable(editable);
 300:   }
 301: 
 302:   /**
 303:    * Returns the current caret position.
 304:    *
 305:    * @return the current caret position
 306:    */
 307:   public int getCaretPosition()
 308:   {
 309:     return ((JTextField) swingComponent.getJComponent()).getCaret().getDot();
 310:   }
 311: 
 312:   /**
 313:    * Sets the current caret position.
 314:    *
 315:    * @param pos the caret position to set
 316:    */
 317:   public void setCaretPosition(int pos)
 318:   {
 319:     ((JTextField) swingComponent.getJComponent()).getCaret().setDot(pos);
 320:   }
 321: 
 322:   /**
 323:    * Returns the index of the character at the specified location.
 324:    *
 325:    * @param x the X coordinate of the point to query
 326:    * @param y the Y coordinate of the point to query
 327:    *
 328:    * @return the index of the character at the specified location
 329:    */
 330:   public int getIndexAtPoint(int x, int y)
 331:   {
 332:     // TODO: Must be implemented.
 333:     return 0;
 334:   }
 335: 
 336:   /**
 337:    * Returns the bounds of the character at the specified index.
 338:    *
 339:    * @param pos the index of the character
 340:    *
 341:    * @return the bounds of the character at the specified index
 342:    */
 343:   public Rectangle getCharacterBounds(int pos)
 344:   {
 345:     // TODO: Must be implemented.
 346:     return null;
 347:   }
 348: 
 349:   /**
 350:    * Not used.
 351:    */
 352:   public long filterEvents(long filter)
 353:   {
 354:     // TODO: Must be implemented.
 355:     return 0;
 356:   }
 357: 
 358:   /**
 359:    * Not used.
 360:    */
 361:   public InputMethodRequests getInputMethodRequests()
 362:   {
 363:     // TODO: Must be implemented.
 364:     return null;
 365:   }
 366: 
 367: }