001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.tomcat5.customizer;
043:
044: import java.awt.event.ItemEvent;
045: import java.awt.event.ItemListener;
046: import java.util.Arrays;
047: import javax.swing.ButtonGroup;
048: import javax.swing.ButtonModel;
049: import javax.swing.DefaultComboBoxModel;
050: import javax.swing.JToggleButton;
051: import javax.swing.SpinnerNumberModel;
052: import javax.swing.event.ChangeListener;
053: import javax.swing.event.DocumentEvent;
054: import javax.swing.event.DocumentListener;
055: import javax.swing.event.ListDataEvent;
056: import javax.swing.event.ListDataListener;
057: import javax.swing.text.BadLocationException;
058: import javax.swing.text.Document;
059: import javax.swing.text.PlainDocument;
060: import org.netbeans.api.java.platform.JavaPlatform;
061: import org.netbeans.api.java.platform.JavaPlatformManager;
062: import org.netbeans.api.java.platform.Specification;
063: import org.netbeans.modules.tomcat5.TomcatManager;
064: import org.netbeans.modules.tomcat5.util.TomcatProperties;
065: import org.openide.util.Exceptions;
066:
067: /**
068: * Customizer data support keeps models for all the customizer components,
069: * initializes them, tracks model changes and performs save.
070: *
071: * @author sherold
072: */
073: public class CustomizerDataSupport {
074:
075: // models
076: private DefaultComboBoxModel jvmModel;
077: private Document javaOptsModel;
078: private ButtonModel secManagerModel;
079: private Document catalinaHomeModel;
080: private Document catalinaBaseModel;
081: private Document usernameModel;
082: private Document passwordModel;
083: private Document scriptPathModel;
084: private ButtonModel customScriptModel;
085: private ButtonModel forceStopModel;
086: private ButtonModel sharedMemModel;
087: private ButtonModel socketModel;
088: private ButtonModel monitorModel;
089: private ButtonModel proxyModel;
090: private Document sharedMemNameModel;
091: private CustomizerSupport.PathModel sourceModel;
092: private CustomizerSupport.PathModel classModel;
093: private CustomizerSupport.PathModel javadocModel;
094: private SpinnerNumberModel serverPortModel;
095: private SpinnerNumberModel shutdownPortModel;
096: private SpinnerNumberModel debugPortModel;
097: private SpinnerNumberModel deploymentTimeoutModel;
098: private ButtonModel driverDeploymentModel;
099:
100: // model dirty flags
101: private boolean jvmModelFlag;
102: private boolean javaOptsModelFlag;
103: private boolean secManagerModelFlag;
104: private boolean usernameModelFlag;
105: private boolean passwordModelFlag;
106: private boolean scriptPathModelFlag;
107: private boolean customScriptModelFlag;
108: private boolean forceStopModelFlag;
109: private boolean sharedMemModelFlag;
110: private boolean socketModelFlag;
111: private boolean monitorModelFlag;
112: private boolean proxyModelFlag;
113: private boolean sharedMemNameModelFlag;
114: private boolean sourceModelFlag;
115: private boolean javadocModelFlag;
116: private boolean serverPortModelFlag;
117: private boolean shutdownPortModelFlag;
118: private boolean debugPortModelFlag;
119: private boolean deploymentTimeoutModelFlag;
120: private boolean driverDeploymentModelFlag;
121:
122: private TomcatProperties tp;
123: private TomcatManager tm;
124:
125: /**
126: * Creates a new instance of CustomizerDataSupport
127: */
128: public CustomizerDataSupport(TomcatManager tm) {
129: this .tm = tm;
130: tp = tm.getTomcatProperties();
131: init();
132: }
133:
134: /** Initialize the customizer models. */
135: private void init() {
136:
137: // jvmModel
138: jvmModel = new DefaultComboBoxModel();
139: loadJvmModel();
140: jvmModel.addListDataListener(new ListDataListener() {
141: public void contentsChanged(ListDataEvent e) {
142: jvmModelFlag = true;
143: store(); // This is just temporary until the server manager has OK and Cancel buttons
144: }
145:
146: public void intervalAdded(ListDataEvent e) {
147: }
148:
149: public void intervalRemoved(ListDataEvent e) {
150: }
151: });
152:
153: // javaOptions
154: javaOptsModel = createDocument(tp.getJavaOpts());
155: javaOptsModel.addDocumentListener(new ModelChangeAdapter() {
156: public void modelChanged() {
157: javaOptsModelFlag = true;
158: store(); // This is just temporary until the server manager has OK and Cancel buttons
159: }
160: });
161:
162: // catalinaHomeModel
163: catalinaHomeModel = createDocument(tp.getCatalinaHome()
164: .toString());
165:
166: // catalinaBaseModel
167: catalinaBaseModel = createDocument(tp.getCatalinaDir()
168: .toString());
169:
170: // usernameModel
171: usernameModel = createDocument(tp.getUsername());
172: usernameModel.addDocumentListener(new ModelChangeAdapter() {
173: public void modelChanged() {
174: usernameModelFlag = true;
175: store(); // This is just temporary until the server manager has OK and Cancel buttons
176: }
177: });
178:
179: // passwordModel
180: passwordModel = createDocument(tp.getPassword());
181: passwordModel.addDocumentListener(new ModelChangeAdapter() {
182: public void modelChanged() {
183: passwordModelFlag = true;
184: store(); // This is just temporary until the server manager has OK and Cancel buttons
185: }
186: });
187:
188: // sharedMemNameModel
189: sharedMemNameModel = createDocument(tp.getSharedMem());
190: sharedMemNameModel
191: .addDocumentListener(new ModelChangeAdapter() {
192: public void modelChanged() {
193: sharedMemNameModelFlag = true;
194: store(); // This is just temporary until the server manager has OK and Cancel buttons
195: }
196: });
197:
198: // scriptPathModel
199: scriptPathModel = createDocument(tp.getScriptPath());
200: scriptPathModel.addDocumentListener(new ModelChangeAdapter() {
201: public void modelChanged() {
202: scriptPathModelFlag = true;
203: store(); // This is just temporary until the server manager has OK and Cancel buttons
204: }
205: });
206:
207: // secManagerModel
208: secManagerModel = createToggleButtonModel(tp.getSecManager());
209: secManagerModel.addItemListener(new ModelChangeAdapter() {
210: public void modelChanged() {
211: secManagerModelFlag = true;
212: store(); // This is just temporary until the server manager has OK and Cancel buttons
213: }
214: });
215:
216: // customScriptModel
217: customScriptModel = createToggleButtonModel(tp
218: .getCustomScript());
219: customScriptModel.addItemListener(new ModelChangeAdapter() {
220: public void modelChanged() {
221: customScriptModelFlag = true;
222: store(); // This is just temporary until the server manager has OK and Cancel buttons
223: }
224: });
225:
226: // forceStopModel
227: forceStopModel = createToggleButtonModel(tp.getForceStop());
228: forceStopModel.addItemListener(new ModelChangeAdapter() {
229: public void modelChanged() {
230: forceStopModelFlag = true;
231: store(); // This is just temporary until the server manager has OK and Cancel buttons
232: }
233: });
234:
235: // monitorModel
236: monitorModel = createToggleButtonModel(tp.getMonitor());
237: monitorModel.addItemListener(new ModelChangeAdapter() {
238: public void modelChanged() {
239: monitorModelFlag = true;
240: store(); // This is just temporary until the server manager has OK and Cancel buttons
241: }
242: });
243:
244: // proxyModel
245: proxyModel = createToggleButtonModel(tp.getProxyEnabled());
246: proxyModel.addItemListener(new ModelChangeAdapter() {
247: public void modelChanged() {
248: proxyModelFlag = true;
249: store(); // This is just temporary until the server manager has OK and Cancel buttons
250: }
251: });
252:
253: // classModel
254: classModel = new CustomizerSupport.PathModel(tp.getClasses());
255:
256: // sourceModel
257: sourceModel = new CustomizerSupport.PathModel(tp.getSources());
258: sourceModel.addListDataListener(new ModelChangeAdapter() {
259: public void modelChanged() {
260: sourceModelFlag = true;
261: store(); // This is just temporary until the server manager has OK and Cancel buttons
262: }
263: });
264:
265: // javadocModel
266: javadocModel = new CustomizerSupport.PathModel(tp.getJavadocs());
267: javadocModel.addListDataListener(new ModelChangeAdapter() {
268: public void modelChanged() {
269: javadocModelFlag = true;
270: store(); // This is just temporary until the server manager has OK and Cancel buttons
271: }
272: });
273:
274: // serverPortModel
275: serverPortModel = new SpinnerNumberModel(tm.getServerPort(), 0,
276: 65535, 1);
277: serverPortModel.addChangeListener(new ModelChangeAdapter() {
278: public void modelChanged() {
279: serverPortModelFlag = true;
280: store(); // This is just temporary until the server manager has OK and Cancel buttons
281: }
282: });
283:
284: // shutdownPortModel
285: shutdownPortModel = new SpinnerNumberModel(
286: tm.getShutdownPort(), 0, 65535, 1);
287: shutdownPortModel.addChangeListener(new ModelChangeAdapter() {
288: public void modelChanged() {
289: shutdownPortModelFlag = true;
290: store(); // This is just temporary until the server manager has OK and Cancel buttons
291: }
292: });
293:
294: // debugPortModel
295: debugPortModel = new SpinnerNumberModel(tp.getDebugPort(), 0,
296: 65535, 1);
297: debugPortModel.addChangeListener(new ModelChangeAdapter() {
298: public void modelChanged() {
299: debugPortModelFlag = true;
300: store(); // This is just temporary until the server manager has OK and Cancel buttons
301: }
302: });
303:
304: ButtonGroup debugButtonGroup = new ButtonGroup();
305:
306: // socketModel
307: socketModel = new JToggleButton.ToggleButtonModel();
308: socketModel.setGroup(debugButtonGroup);
309: socketModel.addItemListener(new ModelChangeAdapter() {
310: public void modelChanged() {
311: socketModelFlag = true;
312: store(); // This is just temporary until the server manager has OK and Cancel buttons
313: }
314: });
315:
316: // sharedMemModel
317: sharedMemModel = new JToggleButton.ToggleButtonModel();
318: sharedMemModel.setGroup(debugButtonGroup);
319: sharedMemModel.addItemListener(new ModelChangeAdapter() {
320: public void modelChanged() {
321: sharedMemModelFlag = true;
322: store(); // This is just temporary until the server manager has OK and Cancel buttons
323: }
324: });
325:
326: boolean socketEnabled = TomcatProperties.DEBUG_TYPE_SOCKET
327: .equalsIgnoreCase(tp.getDebugType());
328: debugButtonGroup.setSelected(socketEnabled ? socketModel
329: : sharedMemModel, true);
330:
331: // deploymentTimeoutModel
332: deploymentTimeoutModel = new SpinnerNumberModel(tp
333: .getDeploymentTimeout(), 1, Integer.MAX_VALUE, 1);
334: deploymentTimeoutModel
335: .addChangeListener(new ModelChangeAdapter() {
336: public void modelChanged() {
337: deploymentTimeoutModelFlag = true;
338: store(); // This is just temporary until the server manager has OK and Cancel buttons
339: }
340: });
341:
342: // driverDeploymentModel
343: driverDeploymentModel = createToggleButtonModel(tp
344: .getDriverDeployment());
345: driverDeploymentModel.addItemListener(new ModelChangeAdapter() {
346: public void modelChanged() {
347: driverDeploymentModelFlag = true;
348: store(); // This is just temporary until the server manager has OK and Cancel buttons
349: }
350: });
351: }
352:
353: /** Update the jvm model */
354: public void loadJvmModel() {
355: JavaPlatformManager jpm = JavaPlatformManager.getDefault();
356: JavaPlatformAdapter curJvm = (JavaPlatformAdapter) jvmModel
357: .getSelectedItem();
358: String curPlatformName = null;
359: if (curJvm != null) {
360: curPlatformName = curJvm.getName();
361: } else {
362: curPlatformName = (String) tp.getJavaPlatform()
363: .getProperties().get(
364: TomcatProperties.PLAT_PROP_ANT_NAME);
365: }
366:
367: jvmModel.removeAllElements();
368:
369: // feed the combo with sorted platform list
370: JavaPlatform[] j2sePlatforms = jpm.getPlatforms(null,
371: new Specification("J2SE", null)); // NOI18N
372: JavaPlatformAdapter[] platformAdapters = new JavaPlatformAdapter[j2sePlatforms.length];
373: for (int i = 0; i < platformAdapters.length; i++) {
374: platformAdapters[i] = new JavaPlatformAdapter(
375: j2sePlatforms[i]);
376: }
377: Arrays.sort(platformAdapters);
378: for (int i = 0; i < platformAdapters.length; i++) {
379: JavaPlatformAdapter platformAdapter = platformAdapters[i];
380: jvmModel.addElement(platformAdapter);
381: // try to set selected item
382: if (curPlatformName != null) {
383: if (curPlatformName.equals(platformAdapter.getName())) {
384: jvmModel.setSelectedItem(platformAdapter);
385: }
386: }
387: }
388: }
389:
390: // model getters ----------------------------------------------------------
391:
392: public DefaultComboBoxModel getJvmModel() {
393: return jvmModel;
394: }
395:
396: public Document getJavaOptsModel() {
397: return javaOptsModel;
398: }
399:
400: public Document getCatalinaHomeModel() {
401: return catalinaHomeModel;
402: }
403:
404: public Document getCatalinaBaseModel() {
405: return catalinaBaseModel;
406: }
407:
408: public Document getUsernameModel() {
409: return usernameModel;
410: }
411:
412: public Document getPasswordModel() {
413: return passwordModel;
414: }
415:
416: public ButtonModel getCustomScriptModel() {
417: return customScriptModel;
418: }
419:
420: public ButtonModel getForceStopModel() {
421: return forceStopModel;
422: }
423:
424: public Document getScriptPathModel() {
425: return scriptPathModel;
426: }
427:
428: public ButtonModel getSharedMemModel() {
429: return sharedMemModel;
430: }
431:
432: public ButtonModel getSocketModel() {
433: return socketModel;
434: }
435:
436: public ButtonModel getMonitorModel() {
437: return monitorModel;
438: }
439:
440: public ButtonModel getProxyModel() {
441: return proxyModel;
442: }
443:
444: public ButtonModel getSecManagerModel() {
445: return secManagerModel;
446: }
447:
448: public Document getSharedMemNameModel() {
449: return sharedMemNameModel;
450: }
451:
452: public CustomizerSupport.PathModel getClassModel() {
453: return classModel;
454: }
455:
456: public CustomizerSupport.PathModel getSourceModel() {
457: return sourceModel;
458: }
459:
460: public CustomizerSupport.PathModel getJavadocsModel() {
461: return javadocModel;
462: }
463:
464: public SpinnerNumberModel getServerPortModel() {
465: return serverPortModel;
466: }
467:
468: public SpinnerNumberModel getShutdownPortModel() {
469: return shutdownPortModel;
470: }
471:
472: public SpinnerNumberModel getDebugPortModel() {
473: return debugPortModel;
474: }
475:
476: public SpinnerNumberModel getDeploymentTimeoutModel() {
477: return deploymentTimeoutModel;
478: }
479:
480: public ButtonModel getDriverDeploymentModel() {
481: return driverDeploymentModel;
482: }
483:
484: // private helper methods -------------------------------------------------
485:
486: /** Save all changes */
487: private void store() {
488:
489: if (jvmModelFlag) {
490: JavaPlatformAdapter platformAdapter = (JavaPlatformAdapter) jvmModel
491: .getSelectedItem();
492: tp.setJavaPlatform(platformAdapter.getJavaPlatform());
493: jvmModelFlag = false;
494: }
495:
496: if (javaOptsModelFlag) {
497: tp.setJavaOpts(getText(javaOptsModel));
498: javaOptsModelFlag = false;
499: }
500:
501: if (secManagerModelFlag) {
502: tp.setSecManager(secManagerModel.isSelected());
503: secManagerModelFlag = false;
504: }
505:
506: if (usernameModelFlag) {
507: tp.setUsername(getText(usernameModel));
508: usernameModelFlag = false;
509: }
510:
511: if (passwordModelFlag) {
512: tp.setPassword(getText(passwordModel));
513: passwordModelFlag = false;
514: }
515:
516: if (scriptPathModelFlag) {
517: tp.setScriptPath(getText(scriptPathModel));
518: scriptPathModelFlag = false;
519: }
520:
521: if (customScriptModelFlag) {
522: tp.setCustomScript(customScriptModel.isSelected());
523: customScriptModelFlag = false;
524: }
525:
526: if (forceStopModelFlag) {
527: tp.setForceStop(forceStopModel.isSelected());
528: forceStopModelFlag = false;
529: }
530:
531: if (sharedMemModelFlag || socketModelFlag) {
532: tp
533: .setDebugType(sharedMemModel.isSelected() ? TomcatProperties.DEBUG_TYPE_SHARED
534: : TomcatProperties.DEBUG_TYPE_SOCKET);
535: sharedMemModelFlag = false;
536: socketModelFlag = false;
537: }
538:
539: if (monitorModelFlag) {
540: tp.setMonitor(monitorModel.isSelected());
541: monitorModelFlag = false;
542: }
543:
544: if (proxyModelFlag) {
545: tp.setProxyEnabled(proxyModel.isSelected());
546: proxyModelFlag = false;
547: }
548:
549: if (sharedMemNameModelFlag) {
550: tp.setSharedMem(getText(sharedMemNameModel));
551: sharedMemNameModelFlag = false;
552: }
553:
554: if (sourceModelFlag) {
555: tp.setSources(sourceModel.getData());
556: sourceModelFlag = false;
557: }
558:
559: if (javadocModelFlag) {
560: tp.setJavadocs(javadocModel.getData());
561: javadocModelFlag = false;
562: }
563:
564: if (serverPortModelFlag) {
565: tm.setServerPort(((Integer) serverPortModel.getValue())
566: .intValue());
567: serverPortModelFlag = false;
568: }
569:
570: if (shutdownPortModelFlag) {
571: tm.setShutdownPort(((Integer) shutdownPortModel.getValue())
572: .intValue());
573: shutdownPortModelFlag = false;
574: }
575:
576: if (debugPortModelFlag) {
577: tp.setDebugPort(((Integer) debugPortModel.getValue())
578: .intValue());
579: debugPortModelFlag = false;
580: }
581:
582: if (deploymentTimeoutModelFlag) {
583: tp.setDeploymentTimeout(((Integer) deploymentTimeoutModel
584: .getValue()).intValue());
585: deploymentTimeoutModelFlag = false;
586: }
587:
588: if (driverDeploymentModelFlag) {
589: tp.setDriverDeployment(driverDeploymentModel.isSelected());
590: driverDeploymentModelFlag = false;
591: }
592: }
593:
594: /** Create a Document initialized by the specified text parameter, which may be null */
595: private Document createDocument(String text) {
596: PlainDocument doc = new PlainDocument();
597: if (text != null) {
598: try {
599: doc.insertString(0, text, null);
600: } catch (BadLocationException e) {
601: Exceptions.printStackTrace(e);
602: }
603: }
604: return doc;
605: }
606:
607: /** Create a ToggleButtonModel inilialized by the specified selected parameter. */
608: private JToggleButton.ToggleButtonModel createToggleButtonModel(
609: boolean selected) {
610: JToggleButton.ToggleButtonModel model = new JToggleButton.ToggleButtonModel();
611: model.setSelected(selected);
612: return model;
613: }
614:
615: /** Get the text value from the document */
616: private String getText(Document doc) {
617: try {
618: return doc.getText(0, doc.getLength());
619: } catch (BadLocationException e) {
620: Exceptions.printStackTrace(e);
621: return null;
622: }
623: }
624:
625: // private helper class ---------------------------------------------------
626:
627: /**
628: * Adapter that implements several listeners, which is useful for dirty model
629: * monitoring.
630: */
631: private abstract class ModelChangeAdapter implements
632: ListDataListener, DocumentListener, ItemListener,
633: ChangeListener {
634:
635: public abstract void modelChanged();
636:
637: public void contentsChanged(ListDataEvent e) {
638: modelChanged();
639: }
640:
641: public void intervalAdded(ListDataEvent e) {
642: modelChanged();
643: }
644:
645: public void intervalRemoved(ListDataEvent e) {
646: modelChanged();
647: }
648:
649: public void changedUpdate(DocumentEvent e) {
650: modelChanged();
651: }
652:
653: public void removeUpdate(DocumentEvent e) {
654: modelChanged();
655: }
656:
657: public void insertUpdate(DocumentEvent e) {
658: modelChanged();
659: }
660:
661: public void itemStateChanged(ItemEvent e) {
662: modelChanged();
663: }
664:
665: public void stateChanged(javax.swing.event.ChangeEvent e) {
666: modelChanged();
667: }
668: }
669:
670: /** Java platform combo box model helper */
671: private static class JavaPlatformAdapter implements Comparable {
672: private JavaPlatform platform;
673:
674: public JavaPlatformAdapter(JavaPlatform platform) {
675: this .platform = platform;
676: }
677:
678: public JavaPlatform getJavaPlatform() {
679: return platform;
680: }
681:
682: public String getName() {
683: return (String) platform.getProperties().get(
684: TomcatProperties.PLAT_PROP_ANT_NAME);
685: }
686:
687: public String toString() {
688: return platform.getDisplayName();
689: }
690:
691: public int compareTo(Object o) {
692: return toString().compareTo(o.toString());
693: }
694: }
695: }
|