1 23 24 package org.objectweb.fractal.gui.dialog.model; 25 26 import org.objectweb.fractal.gui.model.Component; 27 28 import javax.swing.text.AttributeSet ; 29 import javax.swing.text.BadLocationException ; 30 import javax.swing.text.PlainDocument ; 31 32 38 39 public class TextFieldModel extends PlainDocument { 40 41 45 46 public final static int NAME = 0; 47 48 52 53 public final static int TYPE = 1; 54 55 59 60 public final static int IMPLEMENTATION = 2; 61 62 66 67 public final static int ATTRIBUTE_CONTROLLER = 3; 68 69 73 74 public final static int TEMPLATE_CONTROLLER_DESC = 4; 75 76 80 81 public final static int COMPONENT_CONTROLLER_DESC = 5; 82 83 87 88 private int type; 89 90 93 94 private Component model; 95 96 102 103 private boolean isTyping; 104 105 111 112 public TextFieldModel (final int type) { 113 this.type = type; 114 } 115 116 121 122 void setComponentModel (final Component model) { 123 this.model = model; 124 String s = null; 125 if (model != null) { 126 switch (type) { 127 case NAME: 128 s = model.getName(); 129 break; 130 case TYPE: 131 s = model.getType(); 132 break; 133 case IMPLEMENTATION: 134 s = model.getImplementation(); 135 break; 136 case ATTRIBUTE_CONTROLLER: 137 s = model.getAttributeController(); 138 break; 139 case TEMPLATE_CONTROLLER_DESC: 140 s = model.getTemplateControllerDescriptor(); 141 break; 142 case COMPONENT_CONTROLLER_DESC: 143 s = model.getComponentControllerDescriptor(); 144 break; 145 default: 146 } 147 } 148 componentTextChanged(s); 149 } 150 151 158 159 void componentTextChanged (final String s) { 160 try { 161 if (!isTyping) { 162 super.remove(0, getLength()); 163 super.insertString(0, s, null); 164 } 165 } catch (BadLocationException ignored) { 166 } 167 } 168 169 173 public void insertString ( 174 final int offs, 175 final String str, 176 final AttributeSet a) throws BadLocationException 177 { 178 super.insertString(offs, str, a); 179 setComponentText(getText(0, getLength())); 180 } 181 182 public void remove ( 183 final int offs, 184 final int len) throws BadLocationException 185 { 186 super.remove(offs, len); 187 setComponentText(getText(0, getLength())); 188 } 189 190 197 198 private void setComponentText (final String s) { 199 if (model == null) { 200 return; 201 } 202 isTyping = true; 203 try { 204 switch (type) { 205 case NAME: 206 model.setName(s); 207 break; 208 case TYPE: 209 model.setType(s); 210 break; 211 case IMPLEMENTATION: 212 model.setImplementation(s); 213 break; 214 case ATTRIBUTE_CONTROLLER: 215 model.setAttributeController(s); 216 break; 217 case TEMPLATE_CONTROLLER_DESC: 218 model.setTemplateControllerDescriptor(s); 219 break; 220 case COMPONENT_CONTROLLER_DESC: 221 model.setComponentControllerDescriptor(s); 222 break; 223 default: 224 } 225 } finally { 226 isTyping = false; 227 } 228 } 229 } 230 | Popular Tags |