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

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