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