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.agentwizard;
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.NoSuchElementException;
048: import java.util.Set;
049: import java.util.ResourceBundle;
050: import javax.swing.JComponent;
051: import javax.swing.event.ChangeEvent;
052: import javax.swing.event.ChangeListener;
053:
054: import org.openide.WizardDescriptor;
055: import org.openide.util.NbBundle;
056: import org.openide.filesystems.FileObject;
057: import org.netbeans.spi.project.ui.templates.support.Templates;
058: import org.netbeans.api.project.Project;
059: import org.openide.loaders.TemplateWizard;
060: import org.netbeans.api.project.SourceGroup;
061: import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates;
062:
063: import org.netbeans.modules.jmx.WizardConstants;
064: import org.netbeans.modules.jmx.WizardHelpers;
065:
066: import org.netbeans.modules.jmx.runtime.J2SEProjectType;
067:
068: /**
069: *
070: * Main Wizard class : manage the panel navigation and the code generation.
071: *
072: */
073: public class JMXAgentIterator implements TemplateWizard.Iterator {
074: private static final long serialVersionUID = 1L;
075:
076: /** private variables */
077: private transient TemplateWizard wizard;
078: private transient ResourceBundle bundle;
079:
080: // all the panels of the wizard
081: private transient AgentPanel.AgentWizardPanel agentOptionsPanel;
082: private transient TemplateWizard.Panel agentPanel;
083: private transient WizardDescriptor.Panel currentPanel;
084:
085: /**
086: * Returns an agent wizard. Called with the menu new->file->JMX Agent
087: * @return <CODE>JMXAgentIterator</CODE>
088: */
089: public static JMXAgentIterator createAgentIterator() {
090: return new JMXAgentIterator();
091: }
092:
093: /**
094: * Contruct an agent wizard.
095: */
096: public JMXAgentIterator() {
097: bundle = NbBundle.getBundle(JMXAgentIterator.class);
098: }
099:
100: /**
101: * Called to really start the wizard in
102: * case of a direct call from the menu
103: * @param wiz <CODE>WizardDescriptor</CODE> a wizard
104: */
105: public void initialize(TemplateWizard wiz) {
106: this .wizard = wiz;
107:
108: String[] steps = initSteps(false);
109:
110: wiz.putProperty("setAsMain", false); // NOI18N
111:
112: try {
113: // setup project location for the current project
114: WizardHelpers.setProjectValues(wiz);
115:
116: // initialize each panel
117: initializeComponents(steps, 0);
118:
119: } catch (Exception ex) {
120: WizardHelpers.logErrorMessage("initialize", ex);// NOI18N
121: }
122: }
123:
124: /**
125: * set the step names of this wizard
126: *
127: *@param wizardIntegrated true if this wizard is integrated in another
128: */
129: private String[] initSteps(boolean wizardIntegrated) {
130: int size = 2;
131: if (wizardIntegrated)
132: size--;
133: String[] steps = new String[size];
134: if (!wizardIntegrated) {
135: steps[0] = new String("Choose File Type"); // NOI18N // should be added by netbeans
136: }
137: steps[size - 1] = bundle.getString("LBL_Agent");// NOI18N
138: return steps;
139: }
140:
141: /**
142: * WizardIntegration method :
143: * Called when integrating this wizard within a higher level wizard.
144: * @param wiz <CODE>WizardDescriptor</CODE> a wizard
145: * @return <CODE>String[]</CODE> step names
146: */
147: public String[] initializeSteps(WizardDescriptor wiz) {
148: this .wizard = (TemplateWizard) wiz;
149: return initSteps(true);
150: }
151:
152: /**
153: * WizardIntegration method :
154: * Called when integrating this wizard within a higher level wizard.
155: * @param steps Panels list to use
156: * @param panelOffset number of the first panel of this wizard
157: */
158: public void initializeComponents(String[] steps, int panelOffset) {
159: JComponent jc = null;
160:
161: agentOptionsPanel = new AgentPanel.AgentWizardPanel();
162: initializeComponent(steps, panelOffset + 0,
163: (JComponent) agentOptionsPanel.getComponent());
164: Project project = Templates.getProject(wizard);
165: SourceGroup[] agentSrcGroups = WizardHelpers
166: .getSourceGroups(project);
167: agentPanel = JavaTemplates.createPackageChooser(project,
168: agentSrcGroups, agentOptionsPanel);
169: initializeComponent(steps, panelOffset + 0,
170: (JComponent) agentPanel.getComponent());
171: currentPanel = agentPanel;
172: }
173:
174: /**
175: *
176: */
177: private void initializeComponent(String[] steps, int panelOffset,
178: JComponent jc) {
179: jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
180: jc.putClientProperty("WizardPanel_contentSelectedIndex",
181: panelOffset);// NOI18N
182: }
183:
184: public void uninitialize(TemplateWizard wiz) {
185: this .wizard = null;
186: }
187:
188: public java.util.Set/*<FileObject>*/instantiate(TemplateWizard wiz)
189: throws java.io.IOException {
190: // agent generation
191: try {
192: AgentGenerator gen = new AgentGenerator();
193:
194: java.util.Set set = gen.generateAgent(wizard);
195: try {
196: //Set project main class
197: Boolean mainMethodSelected = (Boolean) wiz
198: .getProperty(WizardConstants.PROP_AGENT_MAIN_METHOD_SELECTED);
199: Boolean mainProjectClassSelected = (Boolean) wiz
200: .getProperty(WizardConstants.PROP_AGENT_MAIN_CLASS_SELECTED);
201:
202: Project project = Templates.getProject(wizard);
203: if (J2SEProjectType.isProjectTypeSupported(project)) {
204: if (((mainMethodSelected != null) && (mainMethodSelected))
205: && ((mainProjectClassSelected != null) && (mainProjectClassSelected))) {
206: final String agentName = Templates
207: .getTargetName(wiz);
208: J2SEProjectType.overwriteProperty(project,
209: "main.class", agentName);// NOI18N
210: }
211: }
212: } catch (Exception ex) {
213: WizardHelpers.logErrorMessage(
214: "Setting project Main Class failure : ", ex);// NOI18N
215: }
216:
217: return set;
218:
219: } catch (Exception ex) {
220: WizardHelpers.logErrorMessage(
221: "Agent generation failure : ", ex);// NOI18N
222: return Collections.EMPTY_SET;
223: }
224:
225: }
226:
227: public String name() {
228: Component c = currentPanel.getComponent();
229:
230: if (c != null)
231: return c.getName();
232:
233: return null;
234: }
235:
236: public org.openide.WizardDescriptor.Panel current() {
237: return currentPanel;
238: }
239:
240: public boolean hasNext() {
241: return false;
242: }
243:
244: public boolean hasPrevious() {
245: return false;
246: }
247:
248: public void nextPanel() {
249: if (!hasNext()) {
250: throw new NoSuchElementException();
251: }
252: }
253:
254: public void previousPanel() {
255: if (!hasPrevious()) {
256: throw new NoSuchElementException();
257: }
258: }
259:
260: private transient Set listeners = new HashSet(1); // Set<ChangeListener>
261:
262: public final void addChangeListener(ChangeListener l) {
263: synchronized (listeners) {
264: listeners.add(l);
265: }
266: }
267:
268: public final void removeChangeListener(ChangeListener l) {
269: synchronized (listeners) {
270: listeners.remove(l);
271: }
272: }
273:
274: /**
275: * Fire a ChangeEvent.
276: */
277: protected final void fireChangeEvent() {
278: Iterator it;
279: synchronized (listeners) {
280: it = new HashSet(listeners).iterator();
281: }
282: ChangeEvent ev = new ChangeEvent(this );
283: while (it.hasNext()) {
284: ((ChangeListener) it.next()).stateChanged(ev);
285: }
286: }
287:
288: }
|