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-2007 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.bpel.core.wizard;
042:
043: import java.awt.Component;
044: import java.awt.Dimension;
045: import java.io.BufferedReader;
046: import java.io.BufferedWriter;
047: import java.io.IOException;
048: import java.io.InputStreamReader;
049: import java.io.OutputStreamWriter;
050: import java.io.Writer;
051: import java.util.Collections;
052: import java.util.NoSuchElementException;
053: import java.util.Set;
054: import javax.swing.JComponent;
055: import javax.swing.JTextField;
056: import javax.swing.event.ChangeListener;
057: import org.netbeans.api.java.project.JavaProjectConstants;
058:
059: import org.openide.filesystems.FileObject;
060: import org.openide.WizardDescriptor;
061: import org.openide.loaders.*;
062: import org.openide.util.NbBundle;
063:
064: import org.netbeans.spi.project.ui.templates.support.Templates;
065: import org.netbeans.api.project.Project;
066: import org.netbeans.api.project.Sources;
067: import org.netbeans.api.project.SourceGroup;
068: import org.netbeans.modules.bpel.project.BpelproProject;
069: import org.openide.ErrorManager;
070: import org.netbeans.api.queries.FileEncodingQuery;
071: import org.netbeans.modules.soa.ui.SoaUiUtil;
072:
073: /**
074: * A template wizard iterator (sequence of panels).
075: * Used to fill in the second and subsequent panels in the New wizard.
076: * Associate this to a template inside a layer using the
077: * Sequence of Panels extra property.
078: * Create one or more panels from template as needed too.
079: */
080: public class NewBpelFileIterator implements TemplateWizard.Iterator {
081:
082: private static final long serialVersionUID = 1L;
083: public static final Dimension PREF_SIZE = new Dimension(560, 350);
084:
085: protected WizardDescriptor.Panel[] createPanels(Project project,
086: TemplateWizard wizard) {
087: Sources sources = (Sources) project.getLookup().lookup(
088: Sources.class);
089: sourceGroups = sources
090: .getSourceGroups(BpelproProject.SOURCES_TYPE_BPELPRO);
091:
092: if (sourceGroups.length == 0) {
093: sourceGroups = sources
094: .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
095: }
096: folderPanel = new NewBpelFilePanel(project, sourceGroups);
097:
098: if (sourceGroups.length > 0 && sourceGroups[0] != null) {
099: DataFolder folder = DataFolder.findFolder(sourceGroups[0]
100: .getRootFolder());
101: DataFolder projectFolder = DataFolder.findFolder(project
102: .getProjectDirectory());
103: try {
104: if (wizard.getTargetFolder().equals(projectFolder)) {
105: wizard.setTargetFolder(folder);
106: }
107: } catch (IOException ioe) {
108: ErrorManager.getDefault().notify(
109: ErrorManager.INFORMATIONAL, ioe);
110: }
111: }
112: WizardDescriptor.Panel firstPanel = Templates
113: .createSimpleTargetChooser(project, sourceGroups,
114: folderPanel);
115: JComponent c = (JComponent) firstPanel.getComponent();
116: String fileNameLabel = NbBundle.getMessage(
117: NewBpelFileIterator.class,
118: "LBL_SimpleTargetChooserPanel_FileName_Label"); // NOI18N
119: ((NewBpelFilePanel) folderPanel)
120: .setNameTF((JTextField) Utilities
121: .findTextFieldForLabel(c, fileNameLabel));
122:
123: return new WizardDescriptor.Panel[] { firstPanel };
124: }
125:
126: public Set instantiate(TemplateWizard wiz) throws IOException {
127: NewBpelFilePanel panel = (NewBpelFilePanel) folderPanel;
128: org.openide.filesystems.FileObject dir = Templates
129: .getTargetFolder(wiz);
130: DataObject data = createBpelFile(Templates.getTargetName(wiz),
131: dir, panel.getNS());
132:
133: if (data == null) {
134: return Collections.emptySet();
135: }
136: SoaUiUtil.fixEncoding(data, dir);
137:
138: return Collections.singleton(data);
139: }
140:
141: public void initialize(TemplateWizard wiz) {
142: this .wiz = wiz;
143: index = 0;
144: Project project = Templates.getProject(wiz);
145: panels = createPanels(project, wiz);
146:
147: // Creating steps.
148: Object prop = wiz.getProperty("WizardPanel_contentData"); // NOI18N
149: String[] beforeSteps = null;
150: if (prop != null && prop instanceof String[]) {
151: beforeSteps = (String[]) prop;
152: }
153: String[] steps = Utilities.createSteps(beforeSteps, panels);
154:
155: for (int i = 0; i < panels.length; i++) {
156: Component c = panels[i].getComponent();
157: if (steps[i] == null) {
158: // Default step name to component name of panel.
159: // Mainly useful for getting the name of the target
160: // chooser to appear in the list of steps.
161: steps[i] = c.getName();
162: }
163: if (c instanceof JComponent) { // assume Swing components
164: JComponent jc = (JComponent) c;
165: // Step #.
166: jc.putClientProperty(
167: "WizardPanel_contentSelectedIndex", // NOI18N
168: Integer.valueOf(i));
169: // Step name (actually the whole list for reference).
170: jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
171: }
172: }
173: }
174:
175: public void uninitialize(TemplateWizard wiz) {
176: this .wiz = null;
177: panels = null;
178: }
179:
180: public String name() {
181: return NbBundle.getMessage(NewBpelFileIterator.class,
182: "TITLE_x_of_y", index + 1, panels.length);
183: }
184:
185: public boolean hasNext() {
186: return index < panels.length - 1;
187: }
188:
189: public boolean hasPrevious() {
190: return index > 0;
191: }
192:
193: public void nextPanel() {
194: if (!hasNext()) {
195: throw new NoSuchElementException();
196: }
197: index++;
198: }
199:
200: public void previousPanel() {
201: if (!hasPrevious()) {
202: throw new NoSuchElementException();
203: }
204: index--;
205: }
206:
207: public WizardDescriptor.Panel current() {
208: return panels[index];
209: }
210:
211: public final void addChangeListener(ChangeListener l) {
212: }
213:
214: public final void removeChangeListener(ChangeListener l) {
215: }
216:
217: private DataObject createBpelFile(String bpelFileName,
218: FileObject srcFolder, String namespace) throws IOException {
219:
220: DataFolder df = DataFolder.findFolder(srcFolder);
221: FileObject template = Templates.getTemplate(wiz);
222:
223: boolean importSchemas = false;
224:
225: DataObject dTemplate = DataObject.find(template);
226: DataObject dobj = dTemplate.createFromTemplate(df, Templates
227: .getTargetName(wiz));
228:
229: initialiseNames(dobj.getPrimaryFile(), bpelFileName, namespace,
230: "url1");
231:
232: return dobj;
233: }
234:
235: /**
236: * Basically acts like a xslt tranformer by
237: * replacing _PROCNAME_ in fileObject contents with 'name'.
238: * replaceing _NS_ in fileObject contents with 'namespace'
239: */
240: private void initialiseNames(FileObject fileObject, String name,
241: String namespace, String url) {
242: String line;
243: StringBuffer buffer = new StringBuffer();
244: String separator = System.getProperty("line.separator"); // NOI18N
245:
246: try {
247: BufferedReader reader = new BufferedReader(
248: new InputStreamReader(fileObject.getInputStream(),
249: "UTF-8")); // NOI18N
250:
251: try {
252: while ((line = reader.readLine()) != null) {
253: line = line.replace("_PROCNAME_", name); // NOI18N
254: line = line.replace("_NS_", namespace); // NOI18N
255: line = line.replace("_URL_", url); // NOI18N
256: buffer.append(line);
257: buffer.append(separator);
258: }
259: } finally {
260: reader.close();
261: }
262:
263: Writer writer = new BufferedWriter(new OutputStreamWriter(
264: fileObject.getOutputStream(), FileEncodingQuery
265: .getDefaultEncoding())); //NOI18N
266: try {
267: writer.write(buffer.toString());
268: } finally {
269: writer.close();
270: }
271: } catch (IOException ex) {
272: ErrorManager.getDefault().notify(
273: ErrorManager.INFORMATIONAL, ex);
274: }
275: }
276:
277: private transient int index;
278: private transient WizardDescriptor.Panel[] panels;
279: private transient TemplateWizard wiz;
280: private WizardDescriptor.Panel folderPanel;
281: private transient SourceGroup[] sourceGroups;
282: }
|