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 2004-2005 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: package org.netbeans.modules.jmx.configwizard;
042:
043: import org.netbeans.modules.jmx.FinishableDelegatedWizardPanel;
044: import org.netbeans.modules.jmx.GenericWizardPanel;
045: import java.io.IOException;
046: import java.util.Collections;
047: import java.util.List;
048: import java.util.ArrayList;
049: import java.util.Iterator;
050: import java.util.NoSuchElementException;
051: import java.util.Set;
052: import java.util.ResourceBundle;
053: import javax.swing.JComponent;
054: import javax.swing.event.ChangeEvent;
055: import javax.swing.event.ChangeListener;
056:
057: import org.openide.WizardDescriptor;
058: import org.openide.util.NbBundle;
059: import org.openide.filesystems.FileObject;
060: import org.openide.loaders.TemplateWizard;
061:
062: import org.netbeans.api.project.SourceGroup;
063: import org.netbeans.spi.project.ui.templates.support.Templates;
064: import org.netbeans.api.project.Project;
065:
066: import org.netbeans.modules.jmx.WizardConstants;
067: import org.netbeans.modules.jmx.WizardHelpers;
068: import org.netbeans.modules.jmx.configwizard.generator.ConfigGenerator;
069: import org.netbeans.modules.jmx.WizardPanelWithoutReadSettings;
070:
071: /**
072: *
073: * Main Wizard class : manage the panel navigation and the code generation.
074: *
075: */
076: public class JMXConfigWizardIterator implements TemplateWizard.Iterator {
077: /** */
078: private static JMXConfigWizardIterator instance;
079:
080: private transient ResourceBundle bundle;
081: /** */
082: private TemplateWizard wizard;
083:
084: /** index of step "Name & Location" */
085: private static final int INDEX_TARGET = 2;
086: private static final int INDEX_RMI = 3;
087: private static final int INDEX_SNMP = 4;
088:
089: /** name of panel "Name & Location" */
090: private final String nameTarget = NbBundle.getMessage(
091: JMXConfigWizardIterator.class, "LBL_panel_Target"); // NOI18N
092:
093: private final String nameRMI = NbBundle.getMessage(
094: JMXConfigWizardIterator.class, "LBL_RMI_Panel"); // NOI18N
095:
096: private final String nameSNMP = NbBundle.getMessage(
097: JMXConfigWizardIterator.class, "LBL_SNMP_Panel"); // NOI18N
098:
099: /** index of the current panel */
100: private int current;
101: /** registered change listeners */
102: private List changeListeners; //PENDING - what is this useful for?
103: /** panel for choosing name and target location of the management files */
104: private FinishableDelegatedWizardPanel targetPanel;
105: private Project lastSelectedProject = null;
106: /** */
107: private WizardDescriptor.Panel rmiPanel;
108: private WizardDescriptor.Panel snmpPanel;
109: private WizardDescriptor.Panel otherPanel;
110: private GenericWizardPanel configPanel;
111:
112: /**
113: */
114: public void addChangeListener(ChangeListener l) {
115: if (changeListeners == null) {
116: changeListeners = new ArrayList(2);
117: }
118: changeListeners.add(l);
119: }
120:
121: /**
122: */
123: public void removeChangeListener(ChangeListener l) {
124: if (changeListeners != null) {
125: changeListeners.remove(l);
126: if (changeListeners.isEmpty()) {
127: changeListeners = null;
128: }
129: }
130: }
131:
132: /**
133: * Notifies all registered listeners about a change.
134: *
135: * @see #addChangeListener
136: * @see #removeChangeListener
137: */
138: private void fireChange() {
139: if (changeListeners != null) {
140: ChangeEvent e = new ChangeEvent(this );
141: Iterator i = changeListeners.iterator();
142: while (i.hasNext()) {
143: ((ChangeListener) i.next()).stateChanged(e);
144: }
145: }
146: }
147:
148: /**
149: */
150: public boolean hasPrevious() {
151: return current > INDEX_TARGET;
152: }
153:
154: /**
155: */
156: public boolean hasNext() {
157: return current < INDEX_SNMP;
158: }
159:
160: /**
161: */
162: public void previousPanel() {
163: if (!hasPrevious()) {
164: throw new NoSuchElementException();
165: }
166: current--;
167: }
168:
169: /**
170: */
171: public void nextPanel() {
172: if (!hasNext()) {
173: throw new NoSuchElementException();
174: }
175:
176: current++;
177: }
178:
179: /**
180: */
181: public WizardDescriptor.Panel current() {
182: switch (current) {
183: case INDEX_TARGET:
184: return getTargetPanel();
185: case INDEX_RMI:
186: return getRMIPanel();
187: case INDEX_SNMP:
188: return getSNMPPanel();
189: default:
190: throw new IllegalStateException();
191: }
192: }
193:
194: private FinishableDelegatedWizardPanel getTargetPanel() {
195: final Project project = Templates.getProject(wizard);
196: if (targetPanel == null || project != lastSelectedProject) {
197: SourceGroup[] propSrcGroups = WizardHelpers
198: .getPropSourceGroups(project);
199: if (configPanel == null) {
200: configPanel = new ConfigPanel.ConfigWizardPanel();
201: }
202: WizardDescriptor.Panel targetChooserPanel = Templates
203: .createSimpleTargetChooser(project, propSrcGroups,
204: configPanel);
205: targetPanel = new WizardPanelWithoutReadSettings(
206: targetChooserPanel, configPanel);
207: targetPanel.getComponent().setName(nameTarget);
208: ((ConfigPanel.ConfigWizardPanel) configPanel)
209: .setListenerEnabled(targetPanel, configPanel,
210: wizard);
211: lastSelectedProject = project;
212: targetPanel.readAllSettings(wizard);
213: }
214:
215: return targetPanel;
216: }
217:
218: private WizardDescriptor.Panel getRMIPanel() {
219: if (rmiPanel == null) {
220: rmiPanel = new RMIPanel.RMIWizardPanel();
221: }
222: return rmiPanel;
223: }
224:
225: private WizardDescriptor.Panel getSNMPPanel() {
226: if (snmpPanel == null) {
227: snmpPanel = new SNMPPanel.SNMPWizardPanel();
228: }
229: return snmpPanel;
230: }
231:
232: /**
233: */
234: public String name() {
235: switch (current) {
236: case INDEX_TARGET:
237: return nameTarget;
238: case INDEX_RMI:
239: return nameRMI;
240: case INDEX_SNMP:
241: return nameSNMP;
242: default:
243: throw new AssertionError(current);
244: }
245: }
246:
247: private void loadSettings(TemplateWizard wizard) {
248:
249: }
250:
251: private void saveSettings(TemplateWizard wizard) {
252:
253: }
254:
255: /**
256: * <!-- PENDING -->
257: */
258: public void initialize(TemplateWizard wiz) {
259: this .wizard = wiz;
260: bundle = NbBundle.getBundle(JMXConfigWizardIterator.class);
261: current = INDEX_TARGET;
262: loadSettings(wiz);
263:
264: String[] panelNames = new String[] {
265: NbBundle.getMessage(JMXConfigWizardIterator.class,
266: "LBL_panel_chooseFileType"),// NOI18N
267: NbBundle.getMessage(JMXConfigWizardIterator.class,
268: "LBL_panel_Target"),// NOI18N
269: NbBundle.getMessage(JMXConfigWizardIterator.class,
270: "LBL_RMI_Panel"),// NOI18N
271: NbBundle.getMessage(JMXConfigWizardIterator.class,
272: "LBL_SNMP_Panel"),// NOI18N
273: };
274:
275: ((javax.swing.JComponent) getTargetPanel().getComponent())
276: .putClientProperty("WizardPanel_contentData",
277: panelNames); // NOI18N
278: ((javax.swing.JComponent) getTargetPanel().getComponent())
279: .putClientProperty("WizardPanel_contentSelectedIndex",
280: new Integer(0)); // NOI18N
281: ((javax.swing.JComponent) getRMIPanel().getComponent())
282: .putClientProperty("WizardPanel_contentData",
283: panelNames); // NOI18N
284: ((javax.swing.JComponent) getRMIPanel().getComponent())
285: .putClientProperty("WizardPanel_contentSelectedIndex",
286: new Integer(1)); // NOI18N
287: ((javax.swing.JComponent) getSNMPPanel().getComponent())
288: .putClientProperty("WizardPanel_contentData",
289: panelNames); // NOI18N
290: ((javax.swing.JComponent) getSNMPPanel().getComponent())
291: .putClientProperty("WizardPanel_contentSelectedIndex",
292: new Integer(2)); // NOI18N
293:
294: wiz
295: .putProperty(
296: WizardConstants.RMI_PORT,
297: Integer
298: .valueOf(NbBundle
299: .getBundle(
300: "org.netbeans.modules.jmx.configwizard.Bundle_noi18n")
301: .getString("RMI_Port_Default")));// NOI18N
302: wiz
303: .putProperty(
304: WizardConstants.SNMP_PORT,
305: Integer
306: .valueOf(NbBundle
307: .getBundle(
308: "org.netbeans.modules.jmx.configwizard.Bundle_noi18n")
309: .getString("SNMP_Port_Default")));// NOI18N
310: wiz
311: .putProperty(
312: WizardConstants.SNMP_TRAP_PORT,
313: Integer
314: .valueOf(NbBundle
315: .getBundle(
316: "org.netbeans.modules.jmx.configwizard.Bundle_noi18n")
317: .getString(
318: "SNMP_Trap_Port_Default")));// NOI18N
319: wiz
320: .putProperty(
321: WizardConstants.SNMP_INTERFACES,
322: NbBundle
323: .getBundle(
324: "org.netbeans.modules.jmx.configwizard.Bundle_noi18n")
325: .getString("SNMP_Interface_Default"));// NOI18N
326:
327: }
328:
329: /**
330: * <!-- PENDING -->
331: */
332: public void uninitialize(TemplateWizard wiz) {
333: this .wizard = null;
334:
335: targetPanel = null;
336: lastSelectedProject = null;
337: configPanel = null;
338: rmiPanel = null;
339: snmpPanel = null;
340: otherPanel = null;
341:
342: changeListeners = null;
343: }
344:
345: public Set instantiate(TemplateWizard wiz) throws IOException {
346: saveSettings(wiz);
347: targetPanel.storeAllSettings(wiz);
348: FileObject createdFile = null;
349: try {
350: ConfigGenerator gen = new ConfigGenerator();
351:
352: createdFile = gen.generateConfig(wiz);
353:
354: } catch (Exception ex) {
355: WizardHelpers.logErrorMessage(
356: "Management Configuration generation ", ex);// NOI18N
357: }
358:
359: return Collections.singleton(createdFile);
360: }
361:
362: /**
363: * Returns a singleton of this class.
364: * @return <CODE>JMXConfigWizardIterator</CODE>
365: */
366: public static JMXConfigWizardIterator singleton() {
367: instance = new JMXConfigWizardIterator();
368: return instance;
369: }
370:
371: }
|