1 19 20 package org.netbeans.spi.project.support.ant.ui; 21 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import javax.swing.ButtonModel ; 28 import javax.swing.JToggleButton ; 29 import javax.swing.event.DocumentEvent ; 30 import javax.swing.event.DocumentListener ; 31 import javax.swing.text.BadLocationException ; 32 import javax.swing.text.Document ; 33 import javax.swing.text.PlainDocument ; 34 import org.netbeans.spi.project.support.ant.EditableProperties; 35 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 36 import org.openide.ErrorManager; 37 38 63 public class StoreGroup { 64 65 70 private Map <String ,Object []> models; 71 private Set <Document > modifiedDocuments; 72 73 private static final int BOOLEAN_KIND_TF = 0; 74 private static final int BOOLEAN_KIND_YN = 1; 75 private static final int BOOLEAN_KIND_ED = 2; 76 77 78 private DocumentListener documentListener = new DocumentListener () { 79 80 public void insertUpdate(DocumentEvent e) { 81 documentModified (e.getDocument()); 82 } 83 84 public void removeUpdate(DocumentEvent e) { 85 documentModified (e.getDocument()); 86 } 87 88 public void changedUpdate(DocumentEvent e) { 89 documentModified (e.getDocument()); 90 } 91 92 }; 93 94 public StoreGroup() { 95 models = new HashMap <String ,Object []>(); 96 modifiedDocuments = new HashSet <Document >(); 97 } 98 99 101 106 public void store( EditableProperties editableProperties ) { 107 for (Map.Entry <String ,Object []> entry : models.entrySet()) { 108 String key = entry.getKey(); 109 Object [] params = entry.getValue(); 110 111 if ( params[0] instanceof ButtonModel ) { 112 ButtonModel model = (ButtonModel )params[0]; 113 boolean value = model.isSelected(); 114 if ( params[2] == Boolean.TRUE ) { 115 value = !value; 116 } 117 editableProperties.setProperty( key, encodeBoolean( value, (Integer )params[1] ) ); 118 } 119 else if ( params[0] instanceof Document && modifiedDocuments.contains(params[0])) { 120 Document doc = (Document )params[0]; 121 String txt; 122 try { 123 txt = doc.getText(0, doc.getLength()); 124 } catch (BadLocationException e) { 125 txt = ""; } 127 editableProperties.setProperty( key, txt ); 128 } 129 130 } 131 132 } 133 134 147 public final JToggleButton.ToggleButtonModel createToggleButtonModel( PropertyEvaluator evaluator, String propertyName ) { 148 return createBooleanButtonModel( evaluator, propertyName, false ); 149 } 150 151 164 public final JToggleButton.ToggleButtonModel createInverseToggleButtonModel( PropertyEvaluator evaluator, String propertyName ) { 165 return createBooleanButtonModel( evaluator, propertyName, true ); 166 } 167 168 178 public final Document createStringDocument( PropertyEvaluator evaluator, String propertyName ) { 179 180 checkModelDoesNotExist( propertyName ); 181 182 String value = evaluator.getProperty( propertyName ); 183 if ( value == null ) { 184 value = ""; } 186 187 try { 188 Document d = new PlainDocument (); 189 d.remove(0, d.getLength()); 190 d.insertString(0, value, null); 191 d.addDocumentListener (documentListener); 192 models.put( propertyName, new Object [] { d } ); 193 return d; 194 } 195 catch ( BadLocationException e ) { 196 assert false : "Bad location exception from new document."; return new PlainDocument (); 198 } 199 } 200 201 203 private void checkModelDoesNotExist( String propertyName ) { 204 if ( models.get( propertyName ) != null ) { 205 throw new IllegalArgumentException ( "Model for property " + propertyName + "already exists." ); 206 } 207 } 208 209 private final JToggleButton.ToggleButtonModel createBooleanButtonModel( PropertyEvaluator evaluator, String propName, boolean invert ) { 210 211 checkModelDoesNotExist( propName ); 212 213 String value = evaluator.getProperty( propName ); 214 215 boolean isSelected = false; 216 217 Integer kind = BOOLEAN_KIND_TF; 218 219 if ( value != null ) { 220 String lowercaseValue = value.toLowerCase(); 221 222 if ( lowercaseValue.equals( "yes" ) || lowercaseValue.equals( "no" ) ) { kind = BOOLEAN_KIND_YN; 224 } 225 else if ( lowercaseValue.equals( "on" ) || lowercaseValue.equals( "off" ) ) { kind = BOOLEAN_KIND_ED; 227 } 228 229 if ( lowercaseValue.equals( "true") || lowercaseValue.equals( "yes") || lowercaseValue.equals( "on") ) { isSelected = true; 233 } 234 } 235 236 JToggleButton.ToggleButtonModel bm = new JToggleButton.ToggleButtonModel (); 237 bm.setSelected( invert ? !isSelected : isSelected ); 238 models.put(propName, new Object [] {bm, kind, invert}); 239 return bm; 240 } 241 242 private static String encodeBoolean( boolean value, Integer kind ) { 243 244 if ( kind == BOOLEAN_KIND_ED ) { 245 return value ? "on" : "off"; } 247 else if ( kind == BOOLEAN_KIND_YN ) { return value ? "yes" : "no"; 249 } 250 else { 251 return value ? "true" : "false"; } 253 } 254 255 private void documentModified (Document d) { 256 this.modifiedDocuments.add (d); 257 } 258 259 } 260 261 | Popular Tags |