1 14 package org.wings; 15 16 import org.wings.event.SDocumentEvent; 17 import org.wings.event.SDocumentListener; 18 import org.wings.text.DefaultDocument; 19 import org.wings.text.SDocument; 20 21 import javax.swing.text.BadLocationException ; 22 import java.awt.event.TextEvent ; 23 import java.awt.event.TextListener ; 24 25 29 public abstract class STextComponent extends SComponent implements LowLevelEventListener, SDocumentListener { 30 31 private boolean editable = true; 32 33 private SDocument document; 34 35 38 private boolean epochCheckEnabled = true; 39 40 public STextComponent() { 41 this(new DefaultDocument(), true); 42 } 43 44 public STextComponent(String text) { 45 this(new DefaultDocument(text), true); 46 } 47 48 public STextComponent(SDocument document) { 49 this(document, true); 50 } 51 52 public STextComponent(SDocument document, boolean editable) { 53 setDocument(document); 54 setEditable(editable); 55 } 56 57 public SDocument getDocument() { 58 return document; 59 } 60 61 public void setDocument(SDocument document) { 62 if (document == null) 63 throw new IllegalArgumentException ("null"); 64 65 SDocument oldDocument = this.document; 66 this.document = document; 67 if (oldDocument != null) 68 oldDocument.removeDocumentListener(this); 69 document.addDocumentListener(this); 70 reloadIfChange(oldDocument, document); 71 } 72 73 74 public void setEditable(boolean ed) { 75 boolean oldEditable = editable; 76 editable = ed; 77 if (editable != oldEditable) 78 reload(); 79 } 80 81 82 public boolean isEditable() { 83 return editable; 84 } 85 86 87 public void setText(String text) { 88 document.setText(text); 89 } 90 91 92 public String getText() { 93 return document.getText(); 94 } 95 96 102 public void append(String text) { 103 try { 104 document.insert(document.getLength(), text); 105 } catch (BadLocationException e) { 106 } 107 } 108 109 public void processLowLevelEvent(String action, String [] values) { 110 processKeyEvents(values); 111 112 if (isEditable() && isEnabled()) { 113 if (values[0] != null) 114 values[0] = values[0].trim(); 115 if (getText() == null || !getText().equals(values[0])) { 116 setText(values[0]); 117 SForm.addArmedComponent(this); 118 } 119 } 120 } 121 122 public void addDocumentListener(SDocumentListener listener) { 123 getDocument().addDocumentListener(listener); 124 } 125 126 public void removeDocumentListener(SDocumentListener listener) { 127 getDocument().removeDocumentListener(listener); 128 } 129 130 133 protected void fireTextValueChanged() { 134 TextEvent event = null; 135 Object [] listeners = getListenerList(); 137 for (int i = listeners.length - 2; i >= 0; i -= 2) { 140 if (listeners[i] == TextListener .class) { 141 if (event == null) { 142 event = new TextEvent (this, TextEvent.TEXT_VALUE_CHANGED); 143 } ((TextListener ) listeners[i + 1]).textValueChanged(event); 145 } 146 } 147 } 148 149 public void fireIntermediateEvents() { 150 fireTextValueChanged(); 151 } 152 153 156 public boolean isEpochCheckEnabled() { 157 return epochCheckEnabled; 158 } 159 160 163 public void setEpochCheckEnabled(boolean epochCheckEnabled) { 164 this.epochCheckEnabled = epochCheckEnabled; 165 } 166 167 public void insertUpdate(SDocumentEvent e) { 169 fireTextValueChanged(); 170 reload(); 171 } 172 173 public void removeUpdate(SDocumentEvent e) { 174 fireTextValueChanged(); 175 reload(); 176 } 177 178 public void changedUpdate(SDocumentEvent e) { 179 fireTextValueChanged(); 180 reload(); 181 } 182 } 183 184 185 | Popular Tags |