KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > project > support > ant > ui > StoreGroup


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.spi.project.support.ant.ui;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.swing.ButtonModel JavaDoc;
28 import javax.swing.JToggleButton JavaDoc;
29 import javax.swing.event.DocumentEvent JavaDoc;
30 import javax.swing.event.DocumentListener JavaDoc;
31 import javax.swing.text.BadLocationException JavaDoc;
32 import javax.swing.text.Document JavaDoc;
33 import javax.swing.text.PlainDocument JavaDoc;
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 /** Serves as utility class for storing Swing models into project
39  * properties. Usefull for creating project customizers. <CODE>StoreGroup</CODE>
40  * is capable of doing two things: First create the representation of the project properties which
41  * can be used in the GUI. Second at some time convert the objects back to the ANT properties form and
42  * store them into the project properties.<br>
43  * <b>For creating the object representation.</b>
44  * <ol>
45  * <li>Create new instance of StoreGroup for each group of properties you want to store later
46  * e.g. project and private. Sometimes it might be useful to create temporary source group
47  * which will only be used for creating the models without being used for storing. E.g.
48  * for properties which need special handling.</li>
49  * <li>Call the factory methods e.g. {@link #createToggleButtonModel}, {@link #createStringDocument}, etc. which
50  * will create the swing models for you.</li>
51  * <li>Use the models in your Swing controls by calling <CODE>setModel()</CODE> or <CODE>setDocument()</CODE></li>
52  * </ol>
53  * <b>For storing the models back to the proprties of project.</b>
54  * <ol>
55  * <li>Get the EditableProperties you want to store the model in e.g. private or project
56  * properties</li>
57  * <li>Call the store method on given <CODE>SourceGroup<CODE> with the {@link EditableProperties} as parameter</li>
58  * <li>Manually store models which need some special handling.</li>
59  * </ol>
60  *
61  * @author Petr Hrebejk
62  */

63 public class StoreGroup {
64
65     /** The object array serves as holder for various information about models
66      * first is always the model. The rest depends on the model type
67      * 1) Button model kind, inverted
68      * 2) String model (not used)
69      */

70     private Map JavaDoc<String JavaDoc,Object JavaDoc[]> models;
71     private Set JavaDoc<Document JavaDoc> 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 JavaDoc documentListener = new DocumentListener JavaDoc () {
79         
80         public void insertUpdate(DocumentEvent JavaDoc e) {
81             documentModified (e.getDocument());
82         }
83
84         public void removeUpdate(DocumentEvent JavaDoc e) {
85             documentModified (e.getDocument());
86         }
87
88         public void changedUpdate(DocumentEvent JavaDoc e) {
89             documentModified (e.getDocument());
90         }
91         
92     };
93
94     public StoreGroup() {
95         models = new HashMap JavaDoc<String JavaDoc,Object JavaDoc[]>();
96         modifiedDocuments = new HashSet JavaDoc<Document JavaDoc>();
97     }
98
99     // Public methods ------------------------------------------------------
100

101     /** Stores all models created in the StoreGroup into given
102      * EditableProperties.
103      * @param editableProperties The properties where to store the
104      * values.
105      */

106     public void store( EditableProperties editableProperties ) {
107         for (Map.Entry JavaDoc<String JavaDoc,Object JavaDoc[]> entry : models.entrySet()) {
108             String JavaDoc key = entry.getKey();
109             Object JavaDoc[] params = entry.getValue();
110
111             if ( params[0] instanceof ButtonModel JavaDoc ) {
112                 ButtonModel JavaDoc model = (ButtonModel JavaDoc)params[0];
113                 boolean value = model.isSelected();
114                 if ( params[2] == Boolean.TRUE ) {
115                     value = !value;
116                 }
117                 editableProperties.setProperty( key, encodeBoolean( value, (Integer JavaDoc)params[1] ) );
118             }
119             else if ( params[0] instanceof Document JavaDoc && modifiedDocuments.contains(params[0])) {
120                 Document JavaDoc doc = (Document JavaDoc)params[0];
121                 String JavaDoc txt;
122                 try {
123                     txt = doc.getText(0, doc.getLength());
124                 } catch (BadLocationException JavaDoc e) {
125                     txt = ""; // NOI18N
126
}
127                 editableProperties.setProperty( key, txt );
128             }
129
130         }
131
132     }
133
134     /** Creates toggle button model representing a boolean in the StoreGroup. <BR>
135      * In case the value is one of "true", "yes" "on" the button model
136      * will be "selected". If the property does not exist or is set
137      * to some other value the result of isPressed will be false.<BR>
138      * Call to the store() method stores the model in appropriate form
139      * e.g "true/false", "yes/no", "on/off".<BR>
140      * Method will throw <CODE>IllegalArgumentException</CODE> if you try to get more
141      * than one model for one property.
142      * @param evaluator The PropertyEvaluator to be used to evaluate given
143      * property
144      * @param propertyName Name of the ANT property
145      * @return ButtonModel representing the value
146      */

147     public final JToggleButton.ToggleButtonModel JavaDoc createToggleButtonModel( PropertyEvaluator evaluator, String JavaDoc propertyName ) {
148         return createBooleanButtonModel( evaluator, propertyName, false );
149     }
150
151     /** Creates toggle button model representing a boolean in the StoreGroup. <BR>
152      * In case the value is one of "true", "yes" "on" the button model
153      * will NOT be "selcted". If the property does not exist or is set
154      * to some other value the result of isPressed will be true.<BR>
155      * Call to the store() method stores the model in appropriate form
156      * e.g "true/false", "yes/no", "on/off".<BR>
157      * Method will throw <CODE>IllegalArgumentException</CODE> if you try to get more
158      * than one model for one property.
159      * @param evaluator The PropertyEvaluator to be used to evaluate given
160      * property
161      * @param propertyName Name of the ANT property
162      * @return ButtonModel representing the value
163      */

164     public final JToggleButton.ToggleButtonModel JavaDoc createInverseToggleButtonModel( PropertyEvaluator evaluator, String JavaDoc propertyName ) {
165         return createBooleanButtonModel( evaluator, propertyName, true );
166     }
167
168     /** Creates Document containing the string value of given property.
169      * If the property does not extsts or the value of it is null the
170      * resulting document will be empty.<BR>
171      * Method will throw <CODE>IllegalArgumentException</CODE> if you try to get more
172      * than one model for one property.
173      * @param evaluator The PropertyEvaluator to be used to evaluate given
174      * property
175      * @param propertyName Name of the ANT property
176      * @return ButtonModel representing the value
177      */

178     public final Document JavaDoc createStringDocument( PropertyEvaluator evaluator, String JavaDoc propertyName ) {
179
180         checkModelDoesNotExist( propertyName );
181         
182         String JavaDoc value = evaluator.getProperty( propertyName );
183         if ( value == null ) {
184             value = ""; // NOI18N
185
}
186
187         try {
188             Document JavaDoc d = new PlainDocument JavaDoc();
189             d.remove(0, d.getLength());
190             d.insertString(0, value, null);
191             d.addDocumentListener (documentListener);
192             models.put( propertyName, new Object JavaDoc[] { d } );
193             return d;
194         }
195         catch ( BadLocationException JavaDoc e ) {
196             assert false : "Bad location exception from new document."; // NOI18N
197
return new PlainDocument JavaDoc();
198         }
199     }
200
201     // Private methods -----------------------------------------------------
202

203     private void checkModelDoesNotExist( String JavaDoc propertyName ) {
204         if ( models.get( propertyName ) != null ) {
205             throw new IllegalArgumentException JavaDoc( "Model for property " + propertyName + "already exists." );
206         }
207     }
208     
209     private final JToggleButton.ToggleButtonModel JavaDoc createBooleanButtonModel( PropertyEvaluator evaluator, String JavaDoc propName, boolean invert ) {
210
211         checkModelDoesNotExist( propName );
212         
213         String JavaDoc value = evaluator.getProperty( propName );
214
215         boolean isSelected = false;
216
217         Integer JavaDoc kind = BOOLEAN_KIND_TF;
218
219         if ( value != null ) {
220            String JavaDoc lowercaseValue = value.toLowerCase();
221
222            if ( lowercaseValue.equals( "yes" ) || lowercaseValue.equals( "no" ) ) { // NOI18N
223
kind = BOOLEAN_KIND_YN;
224            }
225            else if ( lowercaseValue.equals( "on" ) || lowercaseValue.equals( "off" ) ) { // NOI18N
226
kind = BOOLEAN_KIND_ED;
227            }
228
229            if ( lowercaseValue.equals( "true") || // NOI18N
230
lowercaseValue.equals( "yes") || // NOI18N
231
lowercaseValue.equals( "on") ) {// NOI18N
232
isSelected = true;
233            }
234         }
235
236         JToggleButton.ToggleButtonModel JavaDoc bm = new JToggleButton.ToggleButtonModel JavaDoc();
237         bm.setSelected( invert ? !isSelected : isSelected );
238         models.put(propName, new Object JavaDoc[] {bm, kind, invert});
239         return bm;
240     }
241
242     private static String JavaDoc encodeBoolean( boolean value, Integer JavaDoc kind ) {
243
244         if ( kind == BOOLEAN_KIND_ED ) {
245             return value ? "on" : "off"; // NOI18N
246
}
247         else if ( kind == BOOLEAN_KIND_YN ) { // NOI18N
248
return value ? "yes" : "no";
249         }
250         else {
251             return value ? "true" : "false"; // NOI18N
252
}
253     }
254     
255     private void documentModified (Document JavaDoc d) {
256         this.modifiedDocuments.add (d);
257     }
258
259 }
260        
261
Popular Tags