KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > wizards > shortcut > ShortcutWizard


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.apache.tools.ant.module.wizards.shortcut;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import javax.swing.KeyStroke JavaDoc;
26 import org.apache.tools.ant.module.AntModule;
27 import org.apache.tools.ant.module.api.AntProjectCookie;
28 import org.openide.DialogDisplayer;
29 import org.openide.WizardDescriptor;
30 import org.openide.filesystems.FileLock;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileSystem;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.filesystems.Repository;
35 import org.openide.loaders.DataFolder;
36 import org.openide.util.NbBundle;
37 import org.openide.util.Utilities;
38 import org.openide.xml.XMLUtil;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41
42 /**
43  * The shortcut wizard itself.
44  * @author Jesse Glick
45  */

46 public final class ShortcutWizard extends WizardDescriptor {
47     
48     /**
49      * Show the shortcut wizard for a given Ant target.
50      * @param project the Ant script to make a target to
51      * @param target the particular target in it
52      */

53     public static void show(AntProjectCookie project, Element JavaDoc target) {
54         final ShortcutWizard wiz = new ShortcutWizard(project, target, new ShortcutIterator());
55         DialogDisplayer.getDefault().createDialog(wiz).setVisible(true);
56         if (wiz.getValue().equals(WizardDescriptor.FINISH_OPTION)) {
57             try {
58                 wiz.finish();
59             } catch (IOException JavaDoc ioe) {
60                 AntModule.err.notify(ioe);
61             }
62         }
63     }
64     
65     // Attributes stored on the template wizard:
66

67     /** type String */
68     private static final String JavaDoc PROP_CONTENTS = "wizdata.contents"; // NOI18N
69
/** type String */
70     static final String JavaDoc PROP_DISPLAY_NAME = "wizdata.displayName"; // NOI18N
71
/** type Boolean */
72     static final String JavaDoc PROP_SHOW_CUST = "wizdata.show.cust"; // NOI18N
73
/** type Boolean */
74     static final String JavaDoc PROP_SHOW_MENU = "wizdata.show.menu"; // NOI18N
75
/** type Boolean */
76     static final String JavaDoc PROP_SHOW_TOOL = "wizdata.show.tool"; // NOI18N
77
/** type Boolean */
78     static final String JavaDoc PROP_SHOW_KEYB = "wizdata.show.keyb"; // NOI18N
79
/** type DataFolder */
80     static final String JavaDoc PROP_FOLDER_MENU = "wizdata.folder.menu"; // NOI18N
81
/** type DataFolder */
82     static final String JavaDoc PROP_FOLDER_TOOL = "wizdata.folder.tool"; // NOI18N
83
/** type KeyStroke */
84     static final String JavaDoc PROP_STROKE = "wizdata.stroke"; // NOI18N
85

86     private final AntProjectCookie project;
87     private final Element JavaDoc target;
88     private final ShortcutIterator it;
89
90     ShortcutWizard(AntProjectCookie project, Element JavaDoc target, ShortcutIterator it) {
91         super(it);
92         this.project = project;
93         this.target = target;
94         this.it = it;
95         it.initialize(this);
96         setTitle(NbBundle.getMessage(ShortcutWizard.class, "TITLE_wizard"));
97         putProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); // NOI18N
98
putProperty("WizardPanel_contentDisplayed", Boolean.TRUE); // NOI18N
99
putProperty("WizardPanel_contentNumbered", Boolean.TRUE); // NOI18N
100
String JavaDoc desc = target.getAttribute("description"); // NOI18n
101
putProperty(PROP_DISPLAY_NAME, desc);
102         // XXX deal with toolbar short desc somehow: #39985
103
// Need to have another field in toolbar panel, and also patch AntActionInstance
104
// to respond to Action.SHORT_DESCRIPTION, presumably as the <description>.
105
}
106     
107     /**
108      * Get the current XML contents of the shortcut.
109      */

110     String JavaDoc getContents() {
111         String JavaDoc c = (String JavaDoc)getProperty(PROP_CONTENTS);
112         if (c == null) {
113             c = generateContents();
114             putContents(c);
115         }
116         return c;
117     }
118     
119     /**
120      * Put the XML contents.
121      */

122     void putContents(String JavaDoc c) {
123         putProperty(PROP_CONTENTS, c);
124     }
125     
126     /**
127      * Create XML contents of the shortcut to be generated, based on current data.
128      */

129     private String JavaDoc generateContents() {
130         try {
131             Document JavaDoc doc = XMLUtil.createDocument("project", null, null, null); // NOI18N
132
Element JavaDoc pel = doc.getDocumentElement();
133             String JavaDoc displayName = (String JavaDoc)getProperty(PROP_DISPLAY_NAME);
134             if (displayName != null && displayName.length() > 0) {
135                 pel.setAttribute("name", displayName); // NOI18N
136
}
137             pel.setAttribute("default", "run"); // NOI18N
138
Element JavaDoc tel = doc.createElement("target"); // NOI18N
139
tel.setAttribute("name", "run"); // NOI18N
140
Element JavaDoc ael = doc.createElement("ant"); // NOI18N
141
ael.setAttribute("antfile", project.getFile().getAbsolutePath()); // NOI18N
142
// #34802: let the child project decide on the basedir:
143
ael.setAttribute("inheritall", "false"); // NOI18N
144
ael.setAttribute("target", target.getAttribute("name")); // NOI18N
145
tel.appendChild(ael);
146             pel.appendChild(tel);
147             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(1000);
148             XMLUtil.write(doc, baos, "UTF-8"); // NOI18N
149
return baos.toString("UTF-8"); // NOI18N
150
} catch (IOException JavaDoc e) {
151             AntModule.err.notify(e);
152             return ""; // NOI18N
153
}
154     }
155
156     void finish() throws IOException JavaDoc {
157         if (it.showing(PROP_SHOW_MENU)) {
158             create((DataFolder) getProperty(PROP_FOLDER_MENU), null);
159         }
160         if (it.showing(PROP_SHOW_TOOL)) {
161             create((DataFolder) getProperty(PROP_FOLDER_TOOL), null);
162         }
163         if (it.showing(PROP_SHOW_KEYB)) {
164             FileObject shortcutsFolder = Repository.getDefault().getDefaultFileSystem().findResource("Shortcuts"); // NOI18N
165
KeyStroke JavaDoc stroke = (KeyStroke JavaDoc) getProperty(PROP_STROKE);
166             create(DataFolder.findFolder(shortcutsFolder), Utilities.keyToString(stroke));
167         }
168     }
169     
170     private void create(DataFolder f, String JavaDoc name) throws IOException JavaDoc {
171         assert f != null;
172         final String JavaDoc fname;
173         if (name != null) {
174             fname = name + ".xml"; // NOI18N
175
} else {
176             fname = FileUtil.findFreeFileName(f.getPrimaryFile(), getTargetBaseName(), "xml") + ".xml"; // NOI18N
177
}
178         final String JavaDoc contents = getContents();
179         final FileObject folder = f.getPrimaryFile();
180         final FileObject[] shortcut = new FileObject[1];
181         folder.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
182             public void run() throws IOException JavaDoc {
183                 shortcut[0] = folder.createData(fname); // NOI18N
184
FileLock lock = shortcut[0].lock();
185                 try {
186                     OutputStream JavaDoc os = shortcut[0].getOutputStream(lock);
187                     try {
188                         os.write(contents.getBytes("UTF-8")); // NOI18N
189
} finally {
190                         os.close();
191                     }
192                 } finally {
193                     lock.releaseLock();
194                 }
195             }
196         });
197     }
198     
199     String JavaDoc getTargetBaseName() {
200         String JavaDoc projname = ""; // NOI18N
201
Document JavaDoc doc = project.getDocument();
202         if (doc != null) {
203             projname = doc.getDocumentElement().getAttribute("name"); // NOI18N
204
}
205         return (projname + '-' + target.getAttribute("name")).replaceAll("[^a-zA-Z0-9_-]", "-"); // NOI18N
206
}
207
208 }
209
Popular Tags