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.xsl.wizard;
042:
043: //java imports
044: import java.awt.Component;
045: import java.io.IOException;
046: import java.util.Collections;
047: import java.util.HashSet;
048: import java.util.Iterator;
049: import java.util.NoSuchElementException;
050: import java.util.Set;
051: import javax.swing.JComponent;
052: import javax.swing.event.ChangeListener;
053: import javax.swing.event.ChangeEvent;
054: import javax.swing.text.BadLocationException;
055: import javax.swing.text.Document;
056: import org.netbeans.api.project.Project;
057: import org.netbeans.api.project.ProjectUtils;
058: import org.netbeans.api.project.SourceGroup;
059: import org.netbeans.api.project.Sources;
060: import org.netbeans.modules.xml.api.EncodingUtil;
061: import org.openide.WizardDescriptor;
062: import org.openide.filesystems.FileObject;
063: import org.openide.loaders.DataFolder;
064: import org.openide.loaders.DataObject;
065: import org.openide.util.Exceptions;
066: import org.openide.util.NbBundle;
067: import org.openide.cookies.EditCookie;
068: import org.openide.loaders.TemplateWizard;
069: import org.netbeans.spi.project.ui.templates.support.Templates;
070: import org.openide.cookies.EditorCookie;
071: import org.openide.cookies.SaveCookie;
072:
073: /**
074: * Sample schema wizard iterator. See layer.xml for template declaration.
075: *
076: * @author Samaresh (Samaresh.Panda@Sun.Com)
077: */
078: public class XSLWizardIterator extends Object implements
079: TemplateWizard.Iterator {
080:
081: private static final long serialVersionUID = 1L;
082: private int index;
083: private final Set<ChangeListener> changeListeners = new HashSet<ChangeListener>();
084: protected transient WizardDescriptor.Panel[] panels;
085: protected String encoding; //project's encoding
086:
087: /**
088: * You should define what panels you want to use here:
089: */
090: protected WizardDescriptor.Panel[] createPanels(Project project,
091: final TemplateWizard wizard) {
092: DataFolder df = null;
093: Sources sources = ProjectUtils.getSources(project);
094: SourceGroup[] folders = sources.getSourceGroups("java"); //NOI18N
095: if (folders == null || folders.length == 0) {
096: folders = sources.getSourceGroups(Sources.TYPE_GENERIC);
097: }
098: try {
099: df = wizard.getTargetFolder();
100: } catch (IOException ex) {
101: //just catch
102: }
103: if (df != null) {
104: wizard.setTargetFolder(df);
105: org.openide.WizardDescriptor.Panel panel = Templates
106: .createSimpleTargetChooser(project, folders);
107: return new org.openide.WizardDescriptor.Panel[] { panel };
108: }
109:
110: //make the first one as the default target folder. IZ: 98643
111: if (folders != null && folders.length != 0) {
112: df = DataFolder.findFolder(folders[0].getRootFolder());
113: wizard.setTargetFolder(df);
114: }
115: WizardDescriptor.Panel panel = Templates
116: .createSimpleTargetChooser(project, folders);
117: return new WizardDescriptor.Panel[] { panel };
118: }
119:
120: /**
121: * Initialization of the wizard iterator.
122: */
123: public void initialize(TemplateWizard wizard) {
124: index = 0;
125: Project project = Templates.getProject(wizard);
126: panels = createPanels(project, wizard);
127:
128: // Creating steps.
129: Object prop = wizard.getProperty("WizardPanel_contentData"); // NOI18N
130: String[] beforeSteps = null;
131: if (prop instanceof String[]) {
132: beforeSteps = (String[]) prop;
133: }
134: String[] steps = createSteps(beforeSteps, panels);
135:
136: for (int i = 0; i < panels.length; i++) {
137: Component c = panels[i].getComponent();
138: if (steps[i] == null) {
139: // Default step name to component name of panel.
140: // Mainly useful for getting the name of the target
141: // chooser to appear in the list of steps.
142: steps[i] = c.getName();
143: }
144: if (c instanceof JComponent) { // assume Swing components
145: JComponent jc = (JComponent) c;
146: // Step #.
147: jc.putClientProperty(
148: "WizardPanel_contentSelectedIndex", Integer
149: .valueOf(i)); // NOI18N
150: // Step name (actually the whole list for reference).
151: jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
152: }
153: }
154: }
155:
156: /**
157: * Cleanup.
158: */
159: public void uninitialize(TemplateWizard wiz) {
160: panels = null;
161: }
162:
163: /**
164: * This is where, the schema gets instantiated from the template.
165: */
166: public Set instantiate(TemplateWizard wizard) throws IOException {
167: FileObject dir = Templates.getTargetFolder(wizard);
168: DataFolder df = DataFolder.findFolder(dir);
169: FileObject template = Templates.getTemplate(wizard);
170: DataObject dTemplate = DataObject.find(template);
171: DataObject dobj = dTemplate.createFromTemplate(df, Templates
172: .getTargetName(wizard));
173: if (dobj == null)
174: return Collections.emptySet();
175:
176: encoding = EncodingUtil.getProjectEncoding(df.getPrimaryFile());
177: if (!EncodingUtil.isValidEncoding(encoding))
178: encoding = "UTF-8"; //NOI18N
179: EditCookie edit = dobj.getCookie(EditCookie.class);
180: if (edit != null) {
181: EditorCookie editorCookie = dobj
182: .getCookie(EditorCookie.class);
183: Document doc = (Document) editorCookie.openDocument();
184: fixEncoding(doc, encoding);
185: SaveCookie save = dobj.getCookie(SaveCookie.class);
186: if (save != null)
187: save.save();
188: }
189:
190: return Collections.singleton(dobj.getPrimaryFile());
191: }
192:
193: /**
194: *
195: */
196: public void addChangeListener(ChangeListener listener) {
197: changeListeners.add(listener);
198: }
199:
200: /**
201: *
202: *
203: */
204: public void removeChangeListener(ChangeListener listener) {
205: changeListeners.remove(listener);
206: }
207:
208: /**
209: *
210: */
211: public void fireStateChanged() {
212: ChangeEvent event = new ChangeEvent(this );
213:
214: Iterator<ChangeListener> i = changeListeners.iterator();
215: while (i.hasNext()) {
216: try {
217: i.next().stateChanged(event);
218: } catch (Exception e) {
219: //Debug.debugNotify(e);
220: }
221: }
222: }
223:
224: /**
225: *
226: */
227: public String name() {
228: return NbBundle.getMessage(XSLWizardIterator.class,
229: "TITLE_x_of_y", Integer.valueOf(index + 1), Integer
230: .valueOf(panels.length));
231: }
232:
233: /**
234: *
235: */
236: public boolean hasNext() {
237: return index < panels.length - 1;
238: }
239:
240: /**
241: *
242: */
243: public boolean hasPrevious() {
244: return index > 0;
245: }
246:
247: /**
248: *
249: */
250: public void nextPanel() {
251: if (!hasNext())
252: throw new NoSuchElementException();
253: index++;
254: }
255:
256: /**
257: *
258: */
259: public void previousPanel() {
260: if (!hasPrevious())
261: throw new NoSuchElementException();
262: index--;
263: }
264:
265: /**
266: * Returns the current panel.
267: */
268: public WizardDescriptor.Panel current() {
269: return panels[index];
270: }
271:
272: /**
273: * Create steps.
274: */
275: private String[] createSteps(String[] before,
276: WizardDescriptor.Panel[] panels) {
277: //assert panels != null;
278: // hack to use the steps set before this panel processed
279: int diff = 0;
280: if (before == null) {
281: before = new String[0];
282: } else if (before.length > 0) {
283: diff = ("...".equals(before[before.length - 1])) ? 1 : 0; // NOI18N
284: }
285: String[] res = new String[(before.length - diff)
286: + panels.length];
287: for (int i = 0; i < res.length; i++) {
288: if (i < (before.length - diff)) {
289: res[i] = before[i];
290: } else {
291: res[i] = panels[i - before.length + diff]
292: .getComponent().getName();
293: }
294: }
295: return res;
296: }
297:
298: /**
299: * Utility method to replace text in document.
300: */
301: void replaceInDocument(javax.swing.text.Document document,
302: String replaceFrom, String replaceTo) {
303: javax.swing.text.AbstractDocument doc = (javax.swing.text.AbstractDocument) document;
304: int len = replaceFrom.length();
305: try {
306: String content = doc.getText(0, doc.getLength());
307: int index = content.lastIndexOf(replaceFrom);
308: while (index >= 0) {
309: doc.replace(index, len, replaceTo, null);
310: content = content.substring(0, index);
311: index = content.lastIndexOf(replaceFrom);
312: }
313: } catch (javax.swing.text.BadLocationException ex) {
314: }
315: }
316:
317: void fixEncoding(javax.swing.text.Document document, String encoding) {
318: if (encoding == null)
319: encoding = "UTF-8"; //NOI18N
320: try {
321: document.insertString(19, " encoding=\"" + encoding + "\"",
322: null);
323: } catch (BadLocationException ex) {
324: Exceptions.printStackTrace(ex);
325: }
326: }
327:
328: }
|