1 19 package org.netbeans.modules.xml.multiview; 20 21 import javax.swing.text.AttributeSet ; 22 import javax.swing.text.BadLocationException ; 23 import javax.swing.text.JTextComponent ; 24 import javax.swing.text.PlainDocument ; 25 26 32 public class ItemEditorHelper implements Refreshable { 33 34 protected ItemEditorHelper.ItemDocument doc; 35 36 39 public static abstract class ItemEditorModel { 40 41 private ItemEditorHelper itemEditorHelper; 42 43 48 public final String getEditorText() { 49 return itemEditorHelper == null ? null : itemEditorHelper.getEditorText(); 50 } 51 52 57 public final JTextComponent getEditorComponent() { 58 return itemEditorHelper == null ? null : itemEditorHelper.getEditorComponent(); 59 } 60 61 66 public abstract String getItemValue(); 67 68 76 public abstract boolean setItemValue(String value); 77 78 83 public abstract void documentUpdated(); 84 85 } 86 87 private JTextComponent getEditorComponent() { 88 return editorComponent; 89 } 90 91 private final JTextComponent editorComponent; 92 private ItemEditorModel model; 93 94 99 public ItemEditorHelper(final JTextComponent textComponent) { 100 this(textComponent, null); 101 } 102 103 112 public ItemEditorHelper(final JTextComponent textComponent, ItemEditorModel model) { 113 this.editorComponent = textComponent; 114 doc = new ItemDocument(); 115 setModel(model); 116 editorComponent.setDocument(doc); 117 refresh(); 118 } 119 120 125 public ItemEditorModel getModel() { 126 return model; 127 } 128 129 private void setModel(ItemEditorModel model) { 130 this.model = model != null ? model : createDefaultModel(); 131 this.model.itemEditorHelper = this; 132 } 133 134 private static ItemEditorModel createDefaultModel() { 135 return new ItemEditorModel() { 136 private String value; 137 138 public String getItemValue() { 139 return value; 140 } 141 142 public boolean setItemValue(String value) { 143 this.value = value; 144 return true; 145 } 146 147 public void documentUpdated() { 148 } 149 }; 150 } 151 152 155 public void refresh() { 156 doc.refresh(); 157 } 158 159 164 public String getEditorText() { 165 return editorComponent.getText(); 166 } 167 168 private class ItemDocument extends PlainDocument { 169 170 boolean refreshing = false; 171 172 public void remove(int offs, int len) throws BadLocationException { 173 super.remove(offs, len); 174 updateModel(); 175 } 176 177 public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { 178 super.insertString(offs, str, a); 179 updateModel(); 180 } 181 182 private void updateModel() { 183 if (!refreshing) { 184 model.documentUpdated(); 185 refresh(); 186 } 187 } 188 189 public void refresh() { 190 refreshing = true; 191 try { 192 String itemValue = model.getItemValue(); 193 String text; 194 try { 195 text = getText(0, getLength()); 196 } catch (BadLocationException e) { 197 text = ""; 198 e.printStackTrace(); 199 } 200 if (!text.equals(itemValue)) { 201 try { 202 super.remove(0, getLength()); 203 } catch (BadLocationException e) { 204 e.printStackTrace(); 205 } 206 try { 207 super.insertString(0, itemValue, null); 208 } catch (BadLocationException e) { 209 e.printStackTrace(); 210 } 211 } 212 } finally { 213 refreshing = false; 214 } 215 } 216 } 217 } 218 | Popular Tags |