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

   1: /* SwingTextAreaPeer.java -- A Swing based peer for AWT textareas
   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.Component;
  41: import java.awt.Container;
  42: import java.awt.Dimension;
  43: import java.awt.Graphics;
  44: import java.awt.Image;
  45: import java.awt.Point;
  46: import java.awt.Rectangle;
  47: import java.awt.TextArea;
  48: import java.awt.event.ComponentEvent;
  49: import java.awt.event.FocusEvent;
  50: import java.awt.event.HierarchyEvent;
  51: import java.awt.event.InputMethodEvent;
  52: import java.awt.event.KeyEvent;
  53: import java.awt.event.MouseEvent;
  54: import java.awt.event.MouseWheelEvent;
  55: import java.awt.im.InputMethodRequests;
  56: import java.awt.peer.TextAreaPeer;
  57: 
  58: import javax.swing.JComponent;
  59: import javax.swing.JScrollPane;
  60: import javax.swing.JTextArea;
  61: import javax.swing.JViewport;
  62: import javax.swing.text.BadLocationException;
  63: 
  64: public class SwingTextAreaPeer
  65:     extends SwingComponentPeer
  66:     implements TextAreaPeer
  67: {
  68: 
  69:   /**
  70:    * A spezialized Swing scroller used to hold the textarea. 
  71:    *
  72:    * @author Roman Kennke (kennke@aicas.com)
  73:    */
  74:   private class SwingScrollPane
  75:     extends JScrollPane
  76:     implements SwingComponent
  77:   {
  78: 
  79:     SwingTextArea textArea;
  80: 
  81:     SwingScrollPane(SwingTextArea textArea)
  82:     {
  83:       super(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  84:             JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  85: 
  86:       this.textArea = textArea;
  87:     }
  88: 
  89:     /**
  90:      * Returns this label.
  91:      *
  92:      * @return <code>this</code>
  93:      */
  94:     public JComponent getJComponent()
  95:     {
  96:       return this;
  97:     }
  98: 
  99:     /**
 100:      * Handles mouse events by forwarding it to
 101:      * <code>processMouseEvent()</code>.
 102:      *
 103:      * @param ev the mouse event
 104:      */
 105:     public void handleMouseEvent(MouseEvent ev)
 106:     {
 107:       JViewport viewPort = getViewport();
 108:       if(viewPort.contains(ev.getPoint()))
 109:         {
 110:           ev.setSource(textArea);
 111:           textArea.dispatchEvent(ev);
 112:         }
 113:       else
 114:         {
 115:           ev.setSource(this);
 116:           this.dispatchEvent(ev);        
 117:         }
 118:     }
 119: 
 120:     /**
 121:      * Force lightweight mouse dispatching.
 122:      */
 123:     public boolean isLightweight()
 124:     {
 125:       return false;
 126:     }
 127: 
 128:     /**
 129:      * Handles mouse motion events by forwarding it to
 130:      * <code>processMouseMotionEvent()</code>.
 131:      *
 132:      * @param ev the mouse motion event
 133:      */
 134:     public void handleMouseMotionEvent(MouseEvent ev)
 135:     {
 136:       textArea.processMouseMotionEvent(ev);
 137:     }
 138: 
 139:     /**
 140:      * Handles key events by forwarding it to <code>processKeyEvent()</code>.
 141:      *
 142:      * @param ev the mouse event
 143:      */
 144:     public void handleKeyEvent(KeyEvent ev)
 145:     {
 146:       textArea.processKeyEvent(ev);
 147:     }
 148: 
 149:     /**
 150:      * Handles focus events by forwarding it to
 151:      * <code>processFocusEvent()</code>.
 152:      *
 153:      * @param ev the Focus event
 154:      */
 155:     public void handleFocusEvent(FocusEvent ev)
 156:     {
 157:       textArea.processFocusEvent(ev);
 158:     }
 159: 
 160:     /**
 161:      * Overridden so that this method returns the correct value even without a
 162:      * peer.
 163:      *
 164:      * @return the screen location of the button
 165:      */
 166:     public Point getLocationOnScreen()
 167:     {
 168:       return SwingTextAreaPeer.this.getLocationOnScreen();
 169:     }
 170: 
 171:     /**
 172:      * Overridden so that the isShowing method returns the correct value for the
 173:      * swing button, even if it has no peer on its own.
 174:      *
 175:      * @return <code>true</code> if the button is currently showing,
 176:      *         <code>false</code> otherwise
 177:      */
 178:     public boolean isShowing()
 179:     {
 180:       boolean retVal = false;
 181:       if (SwingTextAreaPeer.this.awtComponent != null)
 182:         retVal = SwingTextAreaPeer.this.awtComponent.isShowing();
 183:       return retVal;
 184:     }
 185: 
 186:     /**
 187:      * Overridden, so that the Swing button can create an Image without its
 188:      * own peer.
 189:      *
 190:      * @param w the width of the image
 191:      * @param h the height of the image
 192:      *
 193:      * @return an image
 194:      */
 195:     public Image createImage(int w, int h)
 196:     {
 197:       return SwingTextAreaPeer.this.createImage(w, h);
 198:     }
 199: 
 200:     public Graphics getGraphics()
 201:     {
 202:       return SwingTextAreaPeer.this.getGraphics();
 203:     }
 204: 
 205:     public Container getParent()
 206:     {
 207:       Container par = null;
 208:       if (SwingTextAreaPeer.this.awtComponent != null)
 209:         par = SwingTextAreaPeer.this.awtComponent.getParent();
 210:       return par;
 211:     }
 212:     
 213:     public void requestFocus() {
 214:         SwingTextAreaPeer.this.requestFocus(awtComponent, false, true, 0);
 215:     }
 216: 
 217:     public boolean requestFocus(boolean temporary) {
 218:         return SwingTextAreaPeer.this.requestFocus(awtComponent, temporary,
 219:                                                    true, 0);
 220:     }
 221: 
 222:   }
 223: 
 224:   private class SwingTextArea extends JTextArea
 225:   {
 226:     /**
 227:      * Make this method accessible in this Package.
 228:      */
 229:     protected final void processComponentKeyEvent(KeyEvent e)
 230:     {
 231:       super.processComponentKeyEvent(e);
 232:     }
 233: 
 234:     /**
 235:      * Make this method accessible in this Package.
 236:      */
 237:     protected final void processMouseMotionEvent(MouseEvent ev)
 238:     {
 239:       super.processMouseMotionEvent(ev);
 240:     }
 241: 
 242:     /**
 243:      * Make this method accessible in this Package.
 244:      */
 245:     protected final void processComponentEvent(ComponentEvent e)
 246:     {
 247:       super.processComponentEvent(e);
 248:     }
 249: 
 250:     /**
 251:      * Make this method accessible in this Package.
 252:      */
 253:     protected final void processFocusEvent(FocusEvent e)
 254:     {
 255:       super.processFocusEvent(e);
 256:     }
 257: 
 258:     /**
 259:      * Make this method accessible in this Package.
 260:      */
 261:     protected final void processHierarchyBoundsEvent(HierarchyEvent e)
 262:     {
 263:       super.processHierarchyBoundsEvent(e);
 264:     }
 265: 
 266:     /**
 267:      * Make this method accessible in this Package.
 268:      */
 269:     protected final void processHierarchyEvent(HierarchyEvent e)
 270:     {
 271:       super.processHierarchyEvent(e);
 272:     }
 273: 
 274:     /**
 275:      * Make this method accessible in this Package.
 276:      */
 277:     protected final void processInputMethodEvent(InputMethodEvent e)
 278:     {
 279:       super.processInputMethodEvent(e);
 280:     }
 281: 
 282:     /**
 283:      * Make this method accessible in this Package.
 284:      */
 285:     protected final void processMouseEvent(MouseEvent e)
 286:     {
 287:       super.processMouseEvent(e);
 288:     }
 289: 
 290:     /**
 291:      * Make this method accessible in this Package.
 292:      */
 293:     protected final void processMouseWheelEvent(MouseWheelEvent e)
 294:     {
 295:       super.processMouseWheelEvent(e);
 296:     }
 297: 
 298:     /**
 299:      * Make this method accessible in this Package.
 300:      */
 301:     protected final void processKeyEvent(KeyEvent e)
 302:     {
 303:       super.processKeyEvent(e);
 304:     }
 305: 
 306:     public void requestFocus() {
 307:       SwingTextAreaPeer.this.requestFocus(awtComponent, false, true, 0);
 308:     }
 309: 
 310:     public boolean requestFocus(boolean temporary) {
 311:       return SwingTextAreaPeer.this.requestFocus(awtComponent, temporary,
 312:                                                  true, 0);
 313:     }
 314:   }
 315:   
 316:   /**
 317:    * The actual JTextArea.
 318:    */
 319:   private SwingTextArea jTextArea;
 320: 
 321:   public SwingTextAreaPeer(TextArea textArea)
 322:   {
 323:     super();
 324:     jTextArea = new SwingTextArea();
 325:     SwingScrollPane swingArea = new SwingScrollPane(jTextArea);
 326:     init(textArea, swingArea);
 327: 
 328:     JViewport viewport = new JViewport()
 329:       {
 330:         public Image createImage(int width, int height)
 331:         {
 332:           return awtComponent.createImage(width, height);
 333:         }
 334:       };
 335: 
 336:     viewport.setView(jTextArea);
 337:     swingArea.setViewport(viewport);
 338:     // Pull over the text from the text area.
 339:     setText(textArea.getText());
 340: 
 341:     // Pull over the number of rows and columns
 342:     // if non were set use default values
 343:     int columns = textArea.getColumns();
 344:     int rows = textArea.getRows();
 345: 
 346:     if(columns == 0 && rows == 0)
 347:       {
 348:         columns = 25;
 349:         textArea.setColumns(columns);
 350:         rows = 5;
 351:         textArea.setRows(rows);
 352:       }
 353:     
 354:     jTextArea.setColumns(columns);
 355:     jTextArea.setRows(rows);
 356:   }
 357: 
 358:   public Dimension getMinimumSize(int rows, int cols)
 359:   {
 360:     return jTextArea.getMinimumSize();
 361:   }
 362: 
 363:   public Dimension getPreferredSize(int rows, int cols)
 364:   {
 365:     return jTextArea.getPreferredSize();
 366:   }
 367: 
 368:   public void insert(String text, int pos)
 369:   {
 370:     jTextArea.insert(text, pos);
 371:   }
 372: 
 373:   public void insertText(String text, int pos)
 374:   {
 375:     jTextArea.insert(text, pos);
 376:   }
 377: 
 378:   public Dimension minimumSize()
 379:   {
 380:     return jTextArea.getMinimumSize();
 381:   }
 382: 
 383:   public Dimension preferredSize()
 384:   {
 385:     return jTextArea.getPreferredSize();
 386:   }
 387: 
 388:   public Dimension minimumSize(int rows, int cols)
 389:   {
 390:       return jTextArea.getMinimumSize();
 391:   }
 392: 
 393:   public Dimension preferredSize(int rows, int cols)
 394:   {
 395:       return jTextArea.getPreferredSize();
 396:   }
 397: 
 398:   public void replaceRange(String text, int start, int end)
 399:   {
 400:     jTextArea.replaceRange(text, start, end);
 401:   }
 402: 
 403:   public void replaceText(String text, int start, int end)
 404:   {
 405:     jTextArea.replaceRange(text, start, end);
 406:   }
 407: 
 408:   public long filterEvents(long filter)
 409:   {
 410:     // TODO Auto-generated method stub
 411:     return 0;
 412:   }
 413: 
 414:   public int getCaretPosition()
 415:   {
 416:     return jTextArea.getCaretPosition();
 417:   }
 418: 
 419:   public Rectangle getCharacterBounds(int pos)
 420:   {
 421:     Rectangle r;
 422:     try
 423:       {
 424:         return jTextArea.modelToView(pos);
 425:       }
 426:     catch (BadLocationException ex)
 427:       {
 428:         r = null;
 429:       }
 430:     return r;
 431:   }
 432: 
 433:   public int getIndexAtPoint(int x, int y)
 434:   {
 435:     return jTextArea.viewToModel(new Point(x, y));
 436:   }
 437: 
 438:   public InputMethodRequests getInputMethodRequests()
 439:   {
 440:     // TODO Auto-generated method stub
 441:     return null;
 442:   }
 443: 
 444:   public int getSelectionEnd()
 445:   {
 446:     return jTextArea.getSelectionEnd();
 447:   }
 448: 
 449:   public int getSelectionStart()
 450:   {
 451:     return jTextArea.getSelectionStart();
 452:   }
 453: 
 454:   public String getText()
 455:   {
 456:     return jTextArea.getText();
 457:   }
 458: 
 459:   public void select(int start, int end)
 460:   {
 461:     jTextArea.select(start, end);
 462:   }
 463: 
 464:   public void setCaretPosition(int pos)
 465:   {
 466:     jTextArea.setCaretPosition(pos);
 467:   }
 468: 
 469:   public void setEditable(boolean editable)
 470:   {
 471:     jTextArea.setEditable(editable);
 472:   }
 473: 
 474:   public void setText(String text)
 475:   {
 476:     jTextArea.setText(text);
 477:   }
 478: 
 479:   public void reshape(int x, int y, int width, int height)
 480:   {
 481:     if (swingComponent != null)
 482:       {
 483:         swingComponent.getJComponent().setBounds(x, y, width, height);
 484:         swingComponent.getJComponent().validate();
 485:       }
 486:   }
 487: 
 488: }