1 29 30 package nextapp.echo2.app.text; 31 32 import java.util.EventListener ; 33 34 import nextapp.echo2.app.Alignment; 35 import nextapp.echo2.app.Color; 36 import nextapp.echo2.app.FillImage; 37 import nextapp.echo2.app.Border; 38 import nextapp.echo2.app.Component; 39 import nextapp.echo2.app.Extent; 40 import nextapp.echo2.app.Font; 41 import nextapp.echo2.app.Insets; 42 import nextapp.echo2.app.event.ActionEvent; 43 import nextapp.echo2.app.event.ActionListener; 44 import nextapp.echo2.app.event.DocumentEvent; 45 import nextapp.echo2.app.event.DocumentListener; 46 47 50 public abstract class TextComponent 51 extends Component { 52 53 public static final String INPUT_ACTION = "action"; 54 55 public static final String PROPERTY_ACTION_COMMAND = "actionCommand"; 56 public static final String PROPERTY_ALIGNMENT = "alignment"; 57 public static final String PROPERTY_BACKGROUND_IMAGE = "backgroundImage"; 58 public static final String PROPERTY_BORDER = "border"; 59 public static final String PROPERTY_DISABLED_BACKGROUND = "disabledBackground"; 60 public static final String PROPERTY_DISABLED_BACKGROUND_IMAGE = "disabledBackgroundImage"; 61 public static final String PROPERTY_DISABLED_BORDER = "disabledBorder"; 62 public static final String PROPERTY_DISABLED_FONT = "disabledFont"; 63 public static final String PROPERTY_DISABLED_FOREGROUND = "disabledForeground"; 64 public static final String PROPERTY_HEIGHT = "height"; 65 public static final String PROPERTY_HORIZONTAL_SCROLL = "horizontalScroll"; 66 public static final String PROPERTY_INSETS = "insets"; 67 public static final String PROPERTY_MAXIMUM_LENGTH = "maximumLength"; 68 public static final String PROPERTY_TOOL_TIP_TEXT = "toolTipText"; 69 public static final String PROPERTY_VERTICAL_SCROLL = "verticalScroll"; 70 public static final String PROPERTY_WIDTH = "width"; 71 72 public static final String ACTION_LISTENERS_CHANGED_PROPERTY = "actionListeners"; 73 public static final String DOCUMENT_CHANGED_PROPERTY = "document"; 74 public static final String TEXT_CHANGED_PROPERTY = "text"; 75 76 private Document document; 77 78 81 private DocumentListener documentListener = new DocumentListener() { 82 83 86 public void documentUpdate(DocumentEvent e) { 87 firePropertyChange(TEXT_CHANGED_PROPERTY, null, ((Document) e.getSource()).getText()); 88 } 89 }; 90 91 97 public TextComponent(Document document) { 98 super(); 99 setDocument(document); 100 } 101 102 109 public void addActionListener(ActionListener l) { 110 getEventListenerList().addListener(ActionListener.class, l); 111 firePropertyChange(ACTION_LISTENERS_CHANGED_PROPERTY, null, l); 114 } 115 116 119 private void fireActionEvent() { 120 if (!hasEventListenerList()) { 121 return; 122 } 123 EventListener [] listeners = getEventListenerList().getListeners(ActionListener.class); 124 ActionEvent e = null; 125 for (int i = 0; i < listeners.length; ++i) { 126 if (e == null) { 127 e = new ActionEvent(this, (String ) getRenderProperty(PROPERTY_ACTION_COMMAND)); 128 } 129 ((ActionListener) listeners[i]).actionPerformed(e); 130 } 131 } 132 133 139 public String getActionCommand() { 140 return (String ) getProperty(PROPERTY_ACTION_COMMAND); 141 } 142 143 148 public Alignment getAlignment() { 149 return (Alignment) getProperty(PROPERTY_ALIGNMENT); 150 } 151 152 157 public FillImage getBackgroundImage() { 158 return (FillImage) getProperty(PROPERTY_BACKGROUND_IMAGE); 159 } 160 161 166 public Border getBorder() { 167 return (Border) getProperty(PROPERTY_BORDER); 168 } 169 170 176 public Color getDisabledBackground() { 177 return (Color) getProperty(PROPERTY_DISABLED_BACKGROUND); 178 } 179 180 186 public FillImage getDisabledBackgroundImage() { 187 return (FillImage) getProperty(PROPERTY_DISABLED_BACKGROUND_IMAGE); 188 } 189 190 196 public Border getDisabledBorder() { 197 return (Border) getProperty(PROPERTY_DISABLED_BORDER); 198 } 199 200 206 public Font getDisabledFont() { 207 return (Font) getProperty(PROPERTY_DISABLED_FONT); 208 } 209 210 216 public Color getDisabledForeground() { 217 return (Color) getProperty(PROPERTY_DISABLED_FOREGROUND); 218 } 219 220 225 public Document getDocument() { 226 return document; 227 } 228 229 236 public Extent getHeight() { 237 return (Extent) getProperty(PROPERTY_HEIGHT); 238 } 239 240 245 public Extent getHorizontalScroll() { 246 return (Extent) getProperty(PROPERTY_HORIZONTAL_SCROLL); 247 } 248 249 254 public Insets getInsets() { 255 return (Insets) getProperty(PROPERTY_INSETS); 256 } 257 258 264 public int getMaximumLength() { 265 Integer value = (Integer ) getProperty(PROPERTY_MAXIMUM_LENGTH); 266 return value == null ? -1 : value.intValue(); 267 } 268 269 275 public String getText() { 276 return document.getText(); 277 } 278 279 285 public String getToolTipText() { 286 return (String ) getProperty(PROPERTY_TOOL_TIP_TEXT); 287 } 288 289 294 public Extent getVerticalScroll() { 295 return (Extent) getProperty(PROPERTY_VERTICAL_SCROLL); 296 } 297 298 305 public Extent getWidth() { 306 return (Extent) getProperty(PROPERTY_WIDTH); 307 } 308 309 314 public boolean hasActionListeners() { 315 return hasEventListenerList() && getEventListenerList().getListenerCount(ActionListener.class) != 0; 316 } 317 318 323 public boolean isValidChild(Component component) { 324 return false; 325 } 326 327 330 public void processInput(String inputName, Object inputValue) { 331 super.processInput(inputName, inputValue); 332 333 if (TEXT_CHANGED_PROPERTY.equals(inputName)) { 334 setText((String ) inputValue); 335 } else if (PROPERTY_HORIZONTAL_SCROLL.equals(inputName)) { 336 setHorizontalScroll((Extent) inputValue); 337 } else if (PROPERTY_VERTICAL_SCROLL.equals(inputName)) { 338 setVerticalScroll((Extent) inputValue); 339 } else if (INPUT_ACTION.equals(inputName)) { 340 fireActionEvent(); 341 } 342 } 343 344 349 public void removeActionListener(ActionListener l) { 350 if (!hasEventListenerList()) { 351 return; 352 } 353 getEventListenerList().removeListener(ActionListener.class, l); 354 firePropertyChange(ACTION_LISTENERS_CHANGED_PROPERTY, l, null); 357 } 358 359 365 public void setActionCommand(String newValue) { 366 setProperty(PROPERTY_ACTION_COMMAND, newValue); 367 } 368 369 374 public void setAlignment(Alignment newValue) { 375 setProperty(PROPERTY_ALIGNMENT, newValue); 376 } 377 378 383 public void setBackgroundImage(FillImage newValue) { 384 setProperty(PROPERTY_BACKGROUND_IMAGE, newValue); 385 } 386 387 392 public void setBorder(Border newValue) { 393 setProperty(PROPERTY_BORDER, newValue); 394 } 395 396 401 public void setDisabledBackground(Color newValue) { 402 setProperty(PROPERTY_DISABLED_BACKGROUND, newValue); 403 } 404 405 410 public void setDisabledBackgroundImage(FillImage newValue) { 411 setProperty(PROPERTY_DISABLED_BACKGROUND_IMAGE, newValue); 412 } 413 414 419 public void setDisabledBorder(Border newValue) { 420 setProperty(PROPERTY_DISABLED_BORDER, newValue); 421 } 422 423 428 public void setDisabledFont(Font newValue) { 429 setProperty(PROPERTY_DISABLED_FONT, newValue); 430 } 431 432 437 public void setDisabledForeground(Color newValue) { 438 setProperty(PROPERTY_DISABLED_FOREGROUND, newValue); 439 } 440 441 446 public void setDocument(Document newValue) { 447 if (newValue == null) { 448 throw new IllegalArgumentException ("Document may not be null."); 449 } 450 Document oldValue = getDocument(); 451 if (oldValue != null) { 452 oldValue.removeDocumentListener(documentListener); 453 } 454 newValue.addDocumentListener(documentListener); 455 document = newValue; 456 } 457 458 465 public void setHeight(Extent newValue) { 466 setProperty(PROPERTY_HEIGHT, newValue); 467 } 468 469 475 public void setHorizontalScroll(Extent newValue) { 476 setProperty(PROPERTY_HORIZONTAL_SCROLL, newValue); 477 } 478 479 484 public void setInsets(Insets newValue) { 485 setProperty(PROPERTY_INSETS, newValue); 486 } 487 488 495 public void setMaximumLength(int newValue) { 496 if (newValue < 0) { 497 setProperty(PROPERTY_MAXIMUM_LENGTH, null); 498 } else { 499 setProperty(PROPERTY_MAXIMUM_LENGTH, new Integer (newValue)); 500 } 501 } 502 503 508 public void setText(String newValue) { 509 Integer maxLength = (Integer ) getProperty(PROPERTY_MAXIMUM_LENGTH); 510 if (newValue != null && maxLength != null && maxLength.intValue() > 0 511 && newValue.length() > maxLength.intValue()) { 512 getDocument().setText(newValue.substring(0, maxLength.intValue())); 513 } else { 514 getDocument().setText(newValue); 515 } 516 } 517 518 524 public void setToolTipText(String newValue) { 525 setProperty(PROPERTY_TOOL_TIP_TEXT, newValue); 526 } 527 528 534 public void setVerticalScroll(Extent newValue) { 535 setProperty(PROPERTY_VERTICAL_SCROLL, newValue); 536 } 537 538 545 public void setWidth(Extent newValue) { 546 setProperty(PROPERTY_WIDTH, newValue); 547 } 548 } 549 | Popular Tags |