KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > jboss4 > customizer > CustomizerDataSupport


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.modules.j2ee.jboss4.customizer;
21
22 import java.awt.event.FocusAdapter JavaDoc;
23 import java.awt.event.FocusEvent JavaDoc;
24 import java.awt.event.FocusListener JavaDoc;
25 import java.awt.event.ItemEvent JavaDoc;
26 import java.awt.event.ItemListener JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import javax.swing.ButtonGroup JavaDoc;
29 import javax.swing.ButtonModel JavaDoc;
30 import javax.swing.DefaultComboBoxModel JavaDoc;
31 import javax.swing.JToggleButton JavaDoc;
32 import javax.swing.SpinnerNumberModel JavaDoc;
33 import javax.swing.event.ChangeListener JavaDoc;
34 import javax.swing.event.DocumentEvent JavaDoc;
35 import javax.swing.event.DocumentListener JavaDoc;
36 import javax.swing.event.ListDataEvent JavaDoc;
37 import javax.swing.event.ListDataListener JavaDoc;
38 import javax.swing.text.BadLocationException JavaDoc;
39 import javax.swing.text.Document JavaDoc;
40 import javax.swing.text.PlainDocument JavaDoc;
41 import org.netbeans.api.java.platform.JavaPlatform;
42 import org.netbeans.api.java.platform.JavaPlatformManager;
43 import org.netbeans.api.java.platform.Specification;
44 import org.netbeans.modules.j2ee.jboss4.util.JBProperties;
45 import org.openide.ErrorManager;
46
47
48 /**
49  * Customizer data support keeps models for all the customizer components,
50  * initializes them, tracks model changes and performs save.
51  *
52  * @author sherold
53  */

54 public class CustomizerDataSupport {
55     
56     // models
57
private DefaultComboBoxModel JavaDoc jvmModel;
58     private Document JavaDoc javaOptsModel;
59     private ButtonModel JavaDoc proxyModel;
60     private CustomizerSupport.PathModel sourceModel;
61     private CustomizerSupport.PathModel classModel;
62     private CustomizerSupport.PathModel javadocModel;
63     
64     // model dirty flags
65
private boolean jvmModelFlag;
66     private boolean javaOptsModelFlag;
67     private boolean proxyModelFlag;
68     private boolean sourceModelFlag;
69     private boolean javadocModelFlag;
70     
71     private JBProperties properties;
72     
73     /**
74      * Creates a new instance of CustomizerDataSupport
75      */

76     public CustomizerDataSupport(JBProperties properties) {
77         this.properties = properties;
78         init();
79     }
80     
81     /** Initialize the customizer models. */
82     private void init() {
83         
84         // jvmModel
85
jvmModel = new DefaultComboBoxModel JavaDoc();
86         loadJvmModel();
87         jvmModel.addListDataListener(new ListDataListener JavaDoc() {
88             public void contentsChanged(ListDataEvent JavaDoc e) {
89                 jvmModelFlag = true;
90                 store(); // This is just temporary until the server manager has OK and Cancel buttons
91
}
92             
93             public void intervalAdded(ListDataEvent JavaDoc e) {
94             }
95
96             public void intervalRemoved(ListDataEvent JavaDoc e) {
97             }
98         });
99         
100         // javaOptions
101
javaOptsModel = createDocument(properties.getJavaOpts());
102         javaOptsModel.addDocumentListener(new ModelChangeAdapter() {
103             public void modelChanged() {
104                 javaOptsModelFlag = true;
105                 store(); // This is just temporary until the server manager has OK and Cancel buttons
106
}
107         });
108         
109         // proxyModel
110
proxyModel = createToggleButtonModel(properties.getProxyEnabled());
111         proxyModel.addItemListener(new ModelChangeAdapter() {
112             public void modelChanged() {
113                 proxyModelFlag = true;
114                 store(); // This is just temporary until the server manager has OK and Cancel buttons
115
}
116         });
117         
118         // classModel
119
classModel = new CustomizerSupport.PathModel(properties.getClasses());
120         
121         // sourceModel
122
sourceModel = new CustomizerSupport.PathModel(properties.getSources());
123         sourceModel.addListDataListener(new ModelChangeAdapter() {
124             public void modelChanged() {
125                 sourceModelFlag = true;
126                 store(); // This is just temporary until the server manager has OK and Cancel buttons
127
}
128         });
129         
130         // javadocModel
131
javadocModel = new CustomizerSupport.PathModel(properties.getJavadocs());
132         javadocModel.addListDataListener(new ModelChangeAdapter() {
133             public void modelChanged() {
134                 javadocModelFlag = true;
135                 store(); // This is just temporary until the server manager has OK and Cancel buttons
136
}
137         });
138     }
139     
140     /** Update the jvm model */
141     public void loadJvmModel() {
142         JavaPlatformManager jpm = JavaPlatformManager.getDefault();
143         JavaPlatformAdapter curJvm = (JavaPlatformAdapter)jvmModel.getSelectedItem();
144         String JavaDoc curPlatformName = null;
145         if (curJvm != null) {
146             curPlatformName = curJvm.getName();
147         } else {
148             curPlatformName = (String JavaDoc)properties.getJavaPlatform().getProperties().get(JBProperties.PLAT_PROP_ANT_NAME);
149         }
150
151         jvmModel.removeAllElements();
152         
153         // feed the combo with sorted platform list
154
JavaPlatform[] j2sePlatforms = jpm.getPlatforms(null, new Specification("J2SE", null)); // NOI18N
155
JavaPlatformAdapter[] platformAdapters = new JavaPlatformAdapter[j2sePlatforms.length];
156         for (int i = 0; i < platformAdapters.length; i++) {
157             platformAdapters[i] = new JavaPlatformAdapter(j2sePlatforms[i]);
158         }
159         Arrays.sort(platformAdapters);
160         for (int i = 0; i < platformAdapters.length; i++) {
161             JavaPlatformAdapter platformAdapter = platformAdapters[i];
162             jvmModel.addElement(platformAdapter);
163             // try to set selected item
164
if (curPlatformName != null) {
165                 if (curPlatformName.equals(platformAdapter.getName())) {
166                     jvmModel.setSelectedItem(platformAdapter);
167                 }
168             }
169         }
170     }
171     
172     // model getters ----------------------------------------------------------
173

174     public DefaultComboBoxModel JavaDoc getJvmModel() {
175         return jvmModel;
176     }
177     
178     public Document JavaDoc getJavaOptsModel() {
179         return javaOptsModel;
180     }
181     
182     public ButtonModel JavaDoc getProxyModel() {
183         return proxyModel;
184     }
185     
186     public CustomizerSupport.PathModel getClassModel() {
187         return classModel;
188     }
189     
190     public CustomizerSupport.PathModel getSourceModel() {
191         return sourceModel;
192     }
193     
194     public CustomizerSupport.PathModel getJavadocsModel() {
195         return javadocModel;
196     }
197     
198     // private helper methods -------------------------------------------------
199

200     /** Save all changes */
201     private void store() {
202         
203         if (jvmModelFlag) {
204             JavaPlatformAdapter platformAdapter = (JavaPlatformAdapter)jvmModel.getSelectedItem();
205             properties.setJavaPlatform(platformAdapter.getJavaPlatform());
206             jvmModelFlag = false;
207         }
208         
209         if (javaOptsModelFlag) {
210             properties.setJavaOpts(getText(javaOptsModel));
211             javaOptsModelFlag = false;
212         }
213         
214         if (proxyModelFlag) {
215             properties.setProxyEnabled(proxyModel.isSelected());
216             proxyModelFlag = false;
217         }
218         
219         if (sourceModelFlag) {
220             properties.setSources(sourceModel.getData());
221             sourceModelFlag = false;
222         }
223         
224         if (javadocModelFlag) {
225             properties.setJavadocs(javadocModel.getData());
226             javadocModelFlag = false;
227         }
228     }
229     
230     /** Create a Document initialized by the specified text parameter, which may be null */
231     private Document JavaDoc createDocument(String JavaDoc text) {
232         PlainDocument JavaDoc doc = new PlainDocument JavaDoc();
233         if (text != null) {
234             try {
235                 doc.insertString(0, text, null);
236             } catch(BadLocationException JavaDoc e) {
237                 ErrorManager.getDefault().notify(e);
238             }
239         }
240         return doc;
241     }
242     
243     /** Get the text value from the document */
244     private String JavaDoc getText(Document JavaDoc doc) {
245         try {
246             return doc.getText(0, doc.getLength());
247         } catch(BadLocationException JavaDoc e) {
248             ErrorManager.getDefault().notify(e);
249             return null;
250         }
251     }
252     
253     /** Create a ToggleButtonModel inilialized by the specified selected parameter. */
254     private JToggleButton.ToggleButtonModel JavaDoc createToggleButtonModel(boolean selected) {
255         JToggleButton.ToggleButtonModel JavaDoc model = new JToggleButton.ToggleButtonModel JavaDoc();
256         model.setSelected(selected);
257         return model;
258     }
259         
260     // private helper class ---------------------------------------------------
261

262     /**
263      * Adapter that implements several listeners, which is useful for dirty model
264      * monitoring.
265      */

266     private abstract class ModelChangeAdapter implements ListDataListener JavaDoc,
267             DocumentListener JavaDoc, ItemListener JavaDoc, ChangeListener JavaDoc {
268         
269         public abstract void modelChanged();
270         
271         public void contentsChanged(ListDataEvent JavaDoc e) {
272             modelChanged();
273         }
274
275         public void intervalAdded(ListDataEvent JavaDoc e) {
276             modelChanged();
277         }
278
279         public void intervalRemoved(ListDataEvent JavaDoc e) {
280             modelChanged();
281         }
282
283         public void changedUpdate(DocumentEvent JavaDoc e) {
284             modelChanged();
285         }
286
287         public void removeUpdate(DocumentEvent JavaDoc e) {
288             modelChanged();
289         }
290
291         public void insertUpdate(DocumentEvent JavaDoc e) {
292             modelChanged();
293         }
294
295         public void itemStateChanged(ItemEvent JavaDoc e) {
296             modelChanged();
297         }
298
299         public void stateChanged(javax.swing.event.ChangeEvent JavaDoc e) {
300             modelChanged();
301         }
302     }
303     
304     /** Java platform combo box model helper */
305     private static class JavaPlatformAdapter implements Comparable JavaDoc {
306         private JavaPlatform platform;
307         
308         public JavaPlatformAdapter(JavaPlatform platform) {
309             this.platform = platform;
310         }
311         
312         public JavaPlatform getJavaPlatform() {
313             return platform;
314         }
315         
316         public String JavaDoc getName() {
317             return (String JavaDoc)platform.getProperties().get(JBProperties.PLAT_PROP_ANT_NAME);
318         }
319         
320         public String JavaDoc toString() {
321             return platform.getDisplayName();
322         }
323         
324         public int compareTo(Object JavaDoc o) {
325             return toString().compareTo(o.toString());
326         }
327     }
328 }
329
Popular Tags