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: * Code 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.mbeanwizard;
042:
043: import java.awt.Component;
044: import java.util.Collections;
045: import java.util.HashSet;
046: import java.util.Iterator;
047: import java.util.Set;
048: import java.util.ResourceBundle;
049: import javax.swing.JComponent;
050: import javax.swing.event.ChangeEvent;
051: import javax.swing.event.ChangeListener;
052: import org.netbeans.api.java.source.JavaSource;
053: import org.netbeans.api.project.SourceGroup;
054: import org.netbeans.api.project.Project;
055: import org.netbeans.spi.project.ui.templates.support.Templates;
056: import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates;
057: import org.openide.loaders.TemplateWizard;
058: import org.openide.WizardDescriptor;
059: import org.openide.util.NbBundle;
060: import org.netbeans.modules.jmx.WizardConstants;
061: import org.netbeans.modules.jmx.WizardHelpers;
062: import org.netbeans.modules.jmx.FinishableDelegatedWizardPanel;
063: import org.netbeans.modules.jmx.WizardPanelWithoutReadSettings;
064: import org.netbeans.modules.jmx.mbeanwizard.generator.GeneratorControler;
065:
066: /**
067: *
068: * Main Wizard class for MXBean and Standard MBean
069: *
070: */
071: public abstract class MBeanIterator implements TemplateWizard.Iterator {
072: private static final long serialVersionUID = 1L;
073:
074: /** private variables */
075: private transient TemplateWizard wiz;
076:
077: private transient MBeanPanel.MBeanPanelWizardPanel mbeanTemplatePanel;
078: private transient FinishableDelegatedWizardPanel mbeanPanel;
079: private transient WizardDescriptor.Panel currentPanel;
080: private String[] steps;
081: private transient ResourceBundle bundle;
082: private Project lastSelectedProject = null;
083:
084: //****************************************************************
085: // Called with the menu new->file->Standard MBean
086: //****************************************************************
087:
088: //****************************************************************
089: // default constructor :
090: //*************************************JMXMBeanIterator_1***********
091: /**
092: * The default constructor
093: */
094: protected MBeanIterator() {
095: bundle = NbBundle.getBundle(MBeanIterator.class);
096: }
097:
098: //*********************************************************************
099: // Called to really start the wizard in
100: // case of a direct call from the menu
101: //*********************************************************************
102:
103: /**
104: * Initializing method, called to really start the wizard
105: * @param wiz a TemplateWizard
106: */
107: public void initialize(TemplateWizard wiz) {
108: // kludge to work around a netbeans bug :
109:
110: // create step description array
111: //String[] steps = initializeSteps(wiz);
112:
113: this .wiz = wiz;
114:
115: steps = new String[2];
116: steps[0] = new String("Choose File Type"); // NOI18N // should be added by netbeans
117: steps[1] = bundle.getString("LBL_Standard_Panel");// NOI18N
118:
119: // end of work around
120:
121: // Don't set the generated project as the new Netbeans "main project" !!
122: //
123: // cf ../projects/projectui/src/org/netbeans/modules/project/ui/actions/NewProject.java
124: //
125: //wiz.putProperty("setAsMain", false); // NOI18N
126:
127: try {
128:
129: // setup project location for the current project
130: WizardHelpers.setProjectValues(wiz);
131: // initialize each panel
132: initializeComponents(steps, 0);
133:
134: } catch (Exception ex) {
135: ex.printStackTrace();
136: WizardHelpers.logErrorMessage("initialize", ex);// NOI18N
137: }
138: }
139:
140: //*********************************************************************
141: // WizardIntegration method :
142: //
143: // Called when integrating this wizard within a higher level wizard.
144: //
145: //*********************************************************************
146:
147: /**
148: * Method which defines the different steps of our wizard;
149: * Called when integrating this wizard within a higher level
150: * wizard
151: * @param wiz a WizardDescriptor
152: * @return <CODE>String[]</CODE> step names
153: */
154:
155: public String[] initializeSteps(WizardDescriptor wiz) {
156: this .wiz = (TemplateWizard) wiz;
157:
158: steps = new String[1];
159: steps[0] = bundle.getString("LBL_Standard_Panel");// NOI18N
160: return steps;
161: }
162:
163: //*********************************************************************
164: // WizardIntegration method :
165: //
166: // Called when integrating this wizard within a higher level wizard.
167: //
168: // Parameters :
169: //
170: // Steps : Panels list to use
171: // panelOffset : number of the first panel of this wizard
172: //
173: //*********************************************************************
174:
175: protected abstract String getGeneratedMBeanType();
176:
177: /**
178: * Method which initialises the different components
179: * Called when integrating this wizard within a higher level wizard
180: * @param steps Panels list to use
181: * @param panelOffset number of the first panel of this wizard
182: */
183:
184: public void initializeComponents(String[] steps, int panelOffset) {
185: mbeanTemplatePanel = new MBeanPanel.MBeanPanelWizardPanel(
186: getGeneratedMBeanType());
187: initializeComponent(steps, panelOffset + 0,
188: (JComponent) mbeanTemplatePanel.getComponent());
189:
190: Project project = Templates.getProject(wiz);
191: SourceGroup[] mbeanSrcGroups = WizardHelpers
192: .getSourceGroups(project);
193: WizardDescriptor.Panel delegateMBeanPanel = JavaTemplates
194: .createPackageChooser(project, mbeanSrcGroups,
195: mbeanTemplatePanel);
196: mbeanPanel = new WizardPanelWithoutReadSettings(
197: delegateMBeanPanel, mbeanTemplatePanel);
198: mbeanPanel.getComponent().setName(
199: bundle.getString("LBL_Standard_Panel"));// NOI18N
200: initializeComponent(steps, panelOffset + 0,
201: (JComponent) mbeanPanel.getComponent());
202: ((MBeanPanel.MBeanPanelWizardPanel) mbeanTemplatePanel)
203: .setListenerEnabled(delegateMBeanPanel,
204: mbeanTemplatePanel, wiz);
205: mbeanPanel.readAllSettings(wiz);
206:
207: currentPanel = mbeanPanel;
208: }
209:
210: private void initializeComponent(String[] steps, int panelOffset,
211: JComponent jc) {
212: jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
213: jc.putClientProperty("WizardPanel_contentSelectedIndex",
214: panelOffset);// NOI18N
215: }
216:
217: /**
218: * Method which releases the wizard
219: * @param wiz a TemplateWizard
220: */
221: public void uninitialize(TemplateWizard wiz) {
222: this .wiz = null;
223: }
224:
225: //*********************************************************************
226: // real code / file generation
227: //*********************************************************************
228:
229: /**
230: * Method which recalls the stored data and generates the mbean
231: * @return <CODE>Set</CODE> set of generated files to open
232: * @param wizard the wizard which contains all the data
233: * @throws java.io.IOException <CODE>IOException</CODE>
234: */
235: public java.util.Set/*<FileObject>*/instantiate(
236: TemplateWizard wizard) throws java.io.IOException {
237: mbeanPanel.storeAllSettings(wizard);
238:
239: // mbean generation
240: try {
241:
242: return GeneratorControler.generate(wizard);
243:
244: } catch (Exception ex) {
245: WizardHelpers.logErrorMessage("MBean generation ", ex);// NOI18N
246: return Collections.EMPTY_SET;
247: }
248: }
249:
250: /**
251: * Method returning the name of a component contained in the current panel
252: * @return name the name of the component
253: */
254: public String name() {
255: Component c = currentPanel.getComponent();
256:
257: if (c != null)
258: return c.getName();
259:
260: return null;
261: }
262:
263: /**
264: * Method returning the current panel
265: * @return currentPanel the current panel
266: */
267: public org.openide.WizardDescriptor.Panel current() {
268: return currentPanel;
269: }
270:
271: /**
272: * Method returning if the current panel has a next panel or not
273: * Enables the next button
274: * @return next true if the current panel has a next one
275: */
276: public boolean hasNext() {
277:
278: if (currentPanel == mbeanPanel) {
279: return false;
280: } else
281: return true;
282:
283: }
284:
285: /**
286: * Method returning if the current panel has a previous panel or not
287: * Enables the back button
288: * @return next true if the current panel has a previous one
289: */
290: public boolean hasPrevious() {
291: return false;
292: }
293:
294: /**
295: * Method reaffecting the current panel variable to the next panel
296: */
297: public void nextPanel() {
298: }
299:
300: /**
301: * Method reaffecting the current panel variable to the previous panel
302: */
303: public void previousPanel() {
304: }
305:
306: private transient Set listeners = new HashSet(1); // Set<ChangeListener>
307:
308: public final void addChangeListener(ChangeListener l) {
309: synchronized (listeners) {
310: listeners.add(l);
311: }
312: }
313:
314: public final void removeChangeListener(ChangeListener l) {
315: synchronized (listeners) {
316: listeners.remove(l);
317: }
318: }
319:
320: /**
321: * Fire a change event.
322: */
323: protected final void fireChangeEvent() {
324: Iterator it;
325: synchronized (listeners) {
326: it = new HashSet(listeners).iterator();
327: }
328: ChangeEvent ev = new ChangeEvent(this );
329: while (it.hasNext()) {
330: ((ChangeListener) it.next()).stateChanged(ev);
331: }
332: }
333:
334: }
|