KickJava   Java API By Example, From Geeks To Geeks.

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

52 public class CustomizerDataSupport {
53     
54     // models
55
private DefaultComboBoxModel JavaDoc jvmModel;
56     private Document JavaDoc javaOptsModel;
57     private ButtonModel JavaDoc secManagerModel;
58     private Document JavaDoc catalinaHomeModel;
59     private Document JavaDoc catalinaBaseModel;
60     private Document JavaDoc usernameModel;
61     private Document JavaDoc passwordModel;
62     private Document JavaDoc scriptPathModel;
63     private ButtonModel JavaDoc customScriptModel;
64     private ButtonModel JavaDoc forceStopModel;
65     private ButtonModel JavaDoc sharedMemModel;
66     private ButtonModel JavaDoc socketModel;
67     private ButtonModel JavaDoc monitorModel;
68     private ButtonModel JavaDoc proxyModel;
69     private Document JavaDoc sharedMemNameModel;
70     private CustomizerSupport.PathModel sourceModel;
71     private CustomizerSupport.PathModel classModel;
72     private CustomizerSupport.PathModel javadocModel;
73     private SpinnerNumberModel JavaDoc serverPortModel;
74     private SpinnerNumberModel JavaDoc shutdownPortModel;
75     private SpinnerNumberModel JavaDoc debugPortModel;
76     
77     // model dirty flags
78
private boolean jvmModelFlag;
79     private boolean javaOptsModelFlag;
80     private boolean secManagerModelFlag;
81     private boolean usernameModelFlag;
82     private boolean passwordModelFlag;
83     private boolean scriptPathModelFlag;
84     private boolean customScriptModelFlag;
85     private boolean forceStopModelFlag;
86     private boolean sharedMemModelFlag;
87     private boolean socketModelFlag;
88     private boolean monitorModelFlag;
89     private boolean proxyModelFlag;
90     private boolean sharedMemNameModelFlag;
91     private boolean sourceModelFlag;
92     private boolean javadocModelFlag;
93     private boolean serverPortModelFlag;
94     private boolean shutdownPortModelFlag;
95     private boolean debugPortModelFlag;
96     
97     private TomcatProperties tp;
98     private TomcatManager tm;
99     
100     /**
101      * Creates a new instance of CustomizerDataSupport
102      */

103     public CustomizerDataSupport(TomcatManager tm) {
104         this.tm = tm;
105         tp = tm.getTomcatProperties();
106         init();
107     }
108     
109     /** Initialize the customizer models. */
110     private void init() {
111         
112         // jvmModel
113
jvmModel = new DefaultComboBoxModel JavaDoc();
114         loadJvmModel();
115         jvmModel.addListDataListener(new ListDataListener JavaDoc() {
116             public void contentsChanged(ListDataEvent JavaDoc e) {
117                 jvmModelFlag = true;
118                 store(); // This is just temporary until the server manager has OK and Cancel buttons
119
}
120             
121             public void intervalAdded(ListDataEvent JavaDoc e) {
122             }
123
124             public void intervalRemoved(ListDataEvent JavaDoc e) {
125             }
126         });
127         
128         // javaOptions
129
javaOptsModel = createDocument(tp.getJavaOpts());
130         javaOptsModel.addDocumentListener(new ModelChangeAdapter() {
131             public void modelChanged() {
132                 javaOptsModelFlag = true;
133                 store(); // This is just temporary until the server manager has OK and Cancel buttons
134
}
135         });
136         
137         // catalinaHomeModel
138
catalinaHomeModel = createDocument(tp.getCatalinaHome().toString());
139         
140         // catalinaBaseModel
141
catalinaBaseModel = createDocument(tp.getCatalinaDir().toString());
142         
143         // usernameModel
144
usernameModel = createDocument(tp.getUsername());
145         usernameModel.addDocumentListener(new ModelChangeAdapter() {
146             public void modelChanged() {
147                 usernameModelFlag = true;
148                 store(); // This is just temporary until the server manager has OK and Cancel buttons
149
}
150         });
151         
152         // passwordModel
153
passwordModel = createDocument(tp.getPassword());
154         passwordModel.addDocumentListener(new ModelChangeAdapter() {
155             public void modelChanged() {
156                 passwordModelFlag = true;
157                 store(); // This is just temporary until the server manager has OK and Cancel buttons
158
}
159         });
160         
161         // sharedMemNameModel
162
sharedMemNameModel = createDocument(tp.getSharedMem());
163         sharedMemNameModel.addDocumentListener(new ModelChangeAdapter() {
164             public void modelChanged() {
165                 sharedMemNameModelFlag = true;
166                 store(); // This is just temporary until the server manager has OK and Cancel buttons
167
}
168         });
169
170         // scriptPathModel
171
scriptPathModel = createDocument(tp.getScriptPath());
172         scriptPathModel.addDocumentListener(new ModelChangeAdapter() {
173             public void modelChanged() {
174                 scriptPathModelFlag = true;
175                 store(); // This is just temporary until the server manager has OK and Cancel buttons
176
}
177         });
178         
179         // secManagerModel
180
secManagerModel = createToggleButtonModel(tp.getSecManager());
181         secManagerModel.addItemListener(new ModelChangeAdapter() {
182             public void modelChanged() {
183                 secManagerModelFlag = true;
184                 store(); // This is just temporary until the server manager has OK and Cancel buttons
185
}
186         });
187         
188         // customScriptModel
189
customScriptModel = createToggleButtonModel(tp.getCustomScript());
190         customScriptModel.addItemListener(new ModelChangeAdapter() {
191             public void modelChanged() {
192                 customScriptModelFlag = true;
193                 store(); // This is just temporary until the server manager has OK and Cancel buttons
194
}
195         });
196         
197         // forceStopModel
198
forceStopModel = createToggleButtonModel(tp.getForceStop());
199         forceStopModel.addItemListener(new ModelChangeAdapter() {
200             public void modelChanged() {
201                 forceStopModelFlag = true;
202                 store(); // This is just temporary until the server manager has OK and Cancel buttons
203
}
204         });
205         
206         // monitorModel
207
monitorModel = createToggleButtonModel(tp.getMonitor());
208         monitorModel.addItemListener(new ModelChangeAdapter() {
209             public void modelChanged() {
210                 monitorModelFlag = true;
211                 store(); // This is just temporary until the server manager has OK and Cancel buttons
212
}
213         });
214         
215         // proxyModel
216
proxyModel = createToggleButtonModel(tp.getProxyEnabled());
217         proxyModel.addItemListener(new ModelChangeAdapter() {
218             public void modelChanged() {
219                 proxyModelFlag = true;
220                 store(); // This is just temporary until the server manager has OK and Cancel buttons
221
}
222         });
223
224         // classModel
225
classModel = new CustomizerSupport.PathModel(tp.getClasses());
226         
227         // sourceModel
228
sourceModel = new CustomizerSupport.PathModel(tp.getSources());
229         sourceModel.addListDataListener(new ModelChangeAdapter() {
230             public void modelChanged() {
231                 sourceModelFlag = true;
232                 store(); // This is just temporary until the server manager has OK and Cancel buttons
233
}
234         });
235         
236         // javadocModel
237
javadocModel = new CustomizerSupport.PathModel(tp.getJavadocs());
238         javadocModel.addListDataListener(new ModelChangeAdapter() {
239             public void modelChanged() {
240                 javadocModelFlag = true;
241                 store(); // This is just temporary until the server manager has OK and Cancel buttons
242
}
243         });
244         
245         // serverPortModel
246
serverPortModel = new SpinnerNumberModel JavaDoc(tm.getServerPort(), 0, 65535, 1);
247         serverPortModel.addChangeListener(new ModelChangeAdapter() {
248             public void modelChanged() {
249                 serverPortModelFlag = true;
250                 store(); // This is just temporary until the server manager has OK and Cancel buttons
251
}
252         });
253         
254         // shutdownPortModel
255
shutdownPortModel = new SpinnerNumberModel JavaDoc(tm.getShutdownPort(), 0, 65535, 1);
256         shutdownPortModel.addChangeListener(new ModelChangeAdapter() {
257             public void modelChanged() {
258                 shutdownPortModelFlag = true;
259                 store(); // This is just temporary until the server manager has OK and Cancel buttons
260
}
261         });
262         
263         // debugPortModel
264
debugPortModel = new SpinnerNumberModel JavaDoc(tp.getDebugPort(), 0, 65535, 1);
265         debugPortModel.addChangeListener(new ModelChangeAdapter() {
266             public void modelChanged() {
267                 debugPortModelFlag = true;
268                 store(); // This is just temporary until the server manager has OK and Cancel buttons
269
}
270         });
271         
272         ButtonGroup JavaDoc debugButtonGroup = new ButtonGroup JavaDoc();
273         
274         // socketModel
275
socketModel = new JToggleButton.ToggleButtonModel JavaDoc();
276         socketModel.setGroup(debugButtonGroup);
277         socketModel.addItemListener(new ModelChangeAdapter() {
278             public void modelChanged() {
279                 socketModelFlag = true;
280                 store(); // This is just temporary until the server manager has OK and Cancel buttons
281
}
282         });
283         
284         // sharedMemModel
285
sharedMemModel = new JToggleButton.ToggleButtonModel JavaDoc();
286         sharedMemModel.setGroup(debugButtonGroup);
287         sharedMemModel.addItemListener(new ModelChangeAdapter() {
288             public void modelChanged() {
289                 sharedMemModelFlag = true;
290                 store(); // This is just temporary until the server manager has OK and Cancel buttons
291
}
292         });
293         
294         boolean socketEnabled = TomcatProperties.DEBUG_TYPE_SOCKET.equalsIgnoreCase(tp.getDebugType());
295         debugButtonGroup.setSelected(socketEnabled ? socketModel : sharedMemModel, true);
296     }
297     
298     /** Update the jvm model */
299     public void loadJvmModel() {
300         JavaPlatformManager jpm = JavaPlatformManager.getDefault();
301         JavaPlatformAdapter curJvm = (JavaPlatformAdapter)jvmModel.getSelectedItem();
302         String JavaDoc curPlatformName = null;
303         if (curJvm != null) {
304             curPlatformName = curJvm.getName();
305         } else {
306             curPlatformName = (String JavaDoc)tp.getJavaPlatform().getProperties().get(TomcatProperties.PLAT_PROP_ANT_NAME);
307         }
308
309         jvmModel.removeAllElements();
310         
311         // feed the combo with sorted platform list
312
JavaPlatform[] j2sePlatforms = jpm.getPlatforms(null, new Specification("J2SE", null)); // NOI18N
313
JavaPlatformAdapter[] platformAdapters = new JavaPlatformAdapter[j2sePlatforms.length];
314         for (int i = 0; i < platformAdapters.length; i++) {
315             platformAdapters[i] = new JavaPlatformAdapter(j2sePlatforms[i]);
316         }
317         Arrays.sort(platformAdapters);
318         for (int i = 0; i < platformAdapters.length; i++) {
319             JavaPlatformAdapter platformAdapter = platformAdapters[i];
320             jvmModel.addElement(platformAdapter);
321             // try to set selected item
322
if (curPlatformName != null) {
323                 if (curPlatformName.equals(platformAdapter.getName())) {
324                     jvmModel.setSelectedItem(platformAdapter);
325                 }
326             }
327         }
328     }
329     
330     // model getters ----------------------------------------------------------
331

332     public DefaultComboBoxModel JavaDoc getJvmModel() {
333         return jvmModel;
334     }
335     
336     public Document JavaDoc getJavaOptsModel() {
337         return javaOptsModel;
338     }
339     
340     public Document JavaDoc getCatalinaHomeModel() {
341         return catalinaHomeModel;
342     }
343     
344     public Document JavaDoc getCatalinaBaseModel() {
345         return catalinaBaseModel;
346     }
347     
348     public Document JavaDoc getUsernameModel() {
349         return usernameModel;
350     }
351     
352     public Document JavaDoc getPasswordModel() {
353         return passwordModel;
354     }
355     
356     public ButtonModel JavaDoc getCustomScriptModel() {
357         return customScriptModel;
358     }
359     
360     public ButtonModel JavaDoc getForceStopModel() {
361         return forceStopModel;
362     }
363     
364     public Document JavaDoc getScriptPathModel() {
365         return scriptPathModel;
366     }
367     
368     public ButtonModel JavaDoc getSharedMemModel() {
369         return sharedMemModel;
370     }
371     
372     public ButtonModel JavaDoc getSocketModel() {
373         return socketModel;
374     }
375     
376     public ButtonModel JavaDoc getMonitorModel() {
377         return monitorModel;
378     }
379     
380     public ButtonModel JavaDoc getProxyModel() {
381         return proxyModel;
382     }
383     
384     public ButtonModel JavaDoc getSecManagerModel() {
385         return secManagerModel;
386     }
387     
388     public Document JavaDoc getSharedMemNameModel() {
389         return sharedMemNameModel;
390     }
391     
392     public CustomizerSupport.PathModel getClassModel() {
393         return classModel;
394     }
395     
396     public CustomizerSupport.PathModel getSourceModel() {
397         return sourceModel;
398     }
399     
400     public CustomizerSupport.PathModel getJavadocsModel() {
401         return javadocModel;
402     }
403     
404     public SpinnerNumberModel JavaDoc getServerPortModel() {
405         return serverPortModel;
406     }
407     
408     public SpinnerNumberModel JavaDoc getShutdownPortModel() {
409         return shutdownPortModel;
410     }
411     
412     public SpinnerNumberModel JavaDoc getDebugPortModel() {
413         return debugPortModel;
414     }
415     
416     // private helper methods -------------------------------------------------
417

418     /** Save all changes */
419     private void store() {
420         
421         if (jvmModelFlag) {
422             JavaPlatformAdapter platformAdapter = (JavaPlatformAdapter)jvmModel.getSelectedItem();
423             tp.setJavaPlatform(platformAdapter.getJavaPlatform());
424             jvmModelFlag = false;
425         }
426         
427         if (javaOptsModelFlag) {
428             tp.setJavaOpts(getText(javaOptsModel));
429             javaOptsModelFlag = false;
430         }
431         
432         if (secManagerModelFlag) {
433             tp.setSecManager(secManagerModel.isSelected());
434             secManagerModelFlag = false;
435         }
436         
437         if (usernameModelFlag) {
438             tp.setUsername(getText(usernameModel));
439             usernameModelFlag = false;
440         }
441         
442         if (passwordModelFlag) {
443             tp.setPassword(getText(passwordModel));
444             passwordModelFlag = false;
445         }
446         
447         if (scriptPathModelFlag) {
448             tp.setScriptPath(getText(scriptPathModel));
449             scriptPathModelFlag = false;
450         }
451         
452         if (customScriptModelFlag) {
453             tp.setCustomScript(customScriptModel.isSelected());
454             customScriptModelFlag = false;
455         }
456         
457         if (forceStopModelFlag) {
458             tp.setForceStop(forceStopModel.isSelected());
459             forceStopModelFlag = false;
460         }
461         
462         if (sharedMemModelFlag || socketModelFlag) {
463             tp.setDebugType(sharedMemModel.isSelected() ? TomcatProperties.DEBUG_TYPE_SHARED
464                                                         : TomcatProperties.DEBUG_TYPE_SOCKET);
465             sharedMemModelFlag = false;
466             socketModelFlag = false;
467         }
468         
469         if (monitorModelFlag) {
470             tp.setMonitor(monitorModel.isSelected());
471             monitorModelFlag = false;
472         }
473         
474         if (proxyModelFlag) {
475             tp.setProxyEnabled(proxyModel.isSelected());
476             proxyModelFlag = false;
477         }
478         
479         if (sharedMemNameModelFlag) {
480             tp.setSharedMem(getText(sharedMemNameModel));
481             sharedMemNameModelFlag = false;
482         }
483         
484         if (sourceModelFlag) {
485             tp.setSources(sourceModel.getData());
486             sourceModelFlag = false;
487         }
488         
489         if (javadocModelFlag) {
490             tp.setJavadocs(javadocModel.getData());
491             javadocModelFlag = false;
492         }
493         
494         if (serverPortModelFlag) {
495             tm.setServerPort(((Integer JavaDoc)serverPortModel.getValue()).intValue());
496             serverPortModelFlag = false;
497         }
498         
499         if (shutdownPortModelFlag) {
500             tm.setShutdownPort(((Integer JavaDoc)shutdownPortModel.getValue()).intValue());
501             shutdownPortModelFlag = false;
502         }
503         
504         if (debugPortModelFlag) {
505             tp.setDebugPort(((Integer JavaDoc)debugPortModel.getValue()).intValue());
506             debugPortModelFlag = false;
507         }
508     }
509     
510     /** Create a Document initialized by the specified text parameter, which may be null */
511     private Document JavaDoc createDocument(String JavaDoc text) {
512         PlainDocument JavaDoc doc = new PlainDocument JavaDoc();
513         if (text != null) {
514             try {
515                 doc.insertString(0, text, null);
516             } catch(BadLocationException JavaDoc e) {
517                 ErrorManager.getDefault().notify(e);
518             }
519         }
520         return doc;
521     }
522     
523     /** Create a ToggleButtonModel inilialized by the specified selected parameter. */
524     private JToggleButton.ToggleButtonModel JavaDoc createToggleButtonModel(boolean selected) {
525         JToggleButton.ToggleButtonModel JavaDoc model = new JToggleButton.ToggleButtonModel JavaDoc();
526         model.setSelected(selected);
527         return model;
528     }
529     
530     /** Get the text value from the document */
531     private String JavaDoc getText(Document JavaDoc doc) {
532         try {
533             return doc.getText(0, doc.getLength());
534         } catch(BadLocationException JavaDoc e) {
535             ErrorManager.getDefault().notify(e);
536             return null;
537         }
538     }
539         
540     // private helper class ---------------------------------------------------
541

542     /**
543      * Adapter that implements several listeners, which is useful for dirty model
544      * monitoring.
545      */

546     private abstract class ModelChangeAdapter implements ListDataListener JavaDoc,
547             DocumentListener JavaDoc, ItemListener JavaDoc, ChangeListener JavaDoc {
548         
549         public abstract void modelChanged();
550         
551         public void contentsChanged(ListDataEvent JavaDoc e) {
552             modelChanged();
553         }
554
555         public void intervalAdded(ListDataEvent JavaDoc e) {
556             modelChanged();
557         }
558
559         public void intervalRemoved(ListDataEvent JavaDoc e) {
560             modelChanged();
561         }
562
563         public void changedUpdate(DocumentEvent JavaDoc e) {
564             modelChanged();
565         }
566
567         public void removeUpdate(DocumentEvent JavaDoc e) {
568             modelChanged();
569         }
570
571         public void insertUpdate(DocumentEvent JavaDoc e) {
572             modelChanged();
573         }
574
575         public void itemStateChanged(ItemEvent JavaDoc e) {
576             modelChanged();
577         }
578
579         public void stateChanged(javax.swing.event.ChangeEvent JavaDoc e) {
580             modelChanged();
581         }
582     }
583     
584     /** Java platform combo box model helper */
585     private static class JavaPlatformAdapter implements Comparable JavaDoc {
586         private JavaPlatform platform;
587         
588         public JavaPlatformAdapter(JavaPlatform platform) {
589             this.platform = platform;
590         }
591         
592         public JavaPlatform getJavaPlatform() {
593             return platform;
594         }
595         
596         public String JavaDoc getName() {
597             return (String JavaDoc)platform.getProperties().get(TomcatProperties.PLAT_PROP_ANT_NAME);
598         }
599         
600         public String JavaDoc toString() {
601             return platform.getDisplayName();
602         }
603         
604         public int compareTo(Object JavaDoc o) {
605             return toString().compareTo(o.toString());
606         }
607     }
608 }
609
Popular Tags