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