1 17 18 package swingwtx.swing.text; 19 20 import swingwt.awt.event.*; 21 import swingwtx.swing.event.*; 22 23 37 public class PlainView extends View { 38 39 protected JTextComponent comp = null; 40 41 private boolean componentJustChanged = false; 42 43 public PlainView(Document doc, JTextComponent component) { 44 super(doc); this.comp = component; 45 46 comp.addKeyListener(new KeyAdapter() { 49 public void KeyTyped(KeyEvent e) { 50 updateModelFromComponent(comp.getText()); 51 } 52 }); 53 54 doc.addDocumentListener(new DocumentListener() { 57 public void insertUpdate(DocumentEvent e) { updateComponentFromModel(); } 58 public void removeUpdate(DocumentEvent e) { updateComponentFromModel(); } 59 public void changedUpdate(DocumentEvent e) { updateComponentFromModel(); } 60 }); 61 } 62 63 65 public void updateModelFromComponent(String newText) { 66 try { 67 doc.remove(0, doc.getLength()); 68 doc.insertString(0, newText, null); 69 componentJustChanged = true; } 73 catch (BadLocationException e) { 74 e.printStackTrace(); 76 } 77 } 78 79 83 public void updateComponentFromModel() { 84 85 if (componentJustChanged) { 93 componentJustChanged = false; 94 return; 95 } 96 97 try { 98 comp.setText( doc.getText( 0, doc.getLength())); 99 } 100 catch (BadLocationException e) { 101 e.printStackTrace(); 103 } 104 } 105 106 110 public void setDocument(Document doc) { 111 this.doc = doc; 112 updateComponentFromModel(); 113 } 114 115 } 116 | Popular Tags |