1 19 20 package org.apache.tools.ant.module.wizards.shortcut; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import javax.swing.KeyStroke ; 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 ; 40 import org.w3c.dom.Element ; 41 42 46 public final class ShortcutWizard extends WizardDescriptor { 47 48 53 public static void show(AntProjectCookie project, Element 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 ioe) { 60 AntModule.err.notify(ioe); 61 } 62 } 63 } 64 65 67 68 private static final String PROP_CONTENTS = "wizdata.contents"; 70 static final String PROP_DISPLAY_NAME = "wizdata.displayName"; 72 static final String PROP_SHOW_CUST = "wizdata.show.cust"; 74 static final String PROP_SHOW_MENU = "wizdata.show.menu"; 76 static final String PROP_SHOW_TOOL = "wizdata.show.tool"; 78 static final String PROP_SHOW_KEYB = "wizdata.show.keyb"; 80 static final String PROP_FOLDER_MENU = "wizdata.folder.menu"; 82 static final String PROP_FOLDER_TOOL = "wizdata.folder.tool"; 84 static final String PROP_STROKE = "wizdata.stroke"; 86 private final AntProjectCookie project; 87 private final Element target; 88 private final ShortcutIterator it; 89 90 ShortcutWizard(AntProjectCookie project, Element 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); putProperty("WizardPanel_contentDisplayed", Boolean.TRUE); putProperty("WizardPanel_contentNumbered", Boolean.TRUE); String desc = target.getAttribute("description"); putProperty(PROP_DISPLAY_NAME, desc); 102 } 106 107 110 String getContents() { 111 String c = (String )getProperty(PROP_CONTENTS); 112 if (c == null) { 113 c = generateContents(); 114 putContents(c); 115 } 116 return c; 117 } 118 119 122 void putContents(String c) { 123 putProperty(PROP_CONTENTS, c); 124 } 125 126 129 private String generateContents() { 130 try { 131 Document doc = XMLUtil.createDocument("project", null, null, null); Element pel = doc.getDocumentElement(); 133 String displayName = (String )getProperty(PROP_DISPLAY_NAME); 134 if (displayName != null && displayName.length() > 0) { 135 pel.setAttribute("name", displayName); } 137 pel.setAttribute("default", "run"); Element tel = doc.createElement("target"); tel.setAttribute("name", "run"); Element ael = doc.createElement("ant"); ael.setAttribute("antfile", project.getFile().getAbsolutePath()); ael.setAttribute("inheritall", "false"); ael.setAttribute("target", target.getAttribute("name")); tel.appendChild(ael); 146 pel.appendChild(tel); 147 ByteArrayOutputStream baos = new ByteArrayOutputStream (1000); 148 XMLUtil.write(doc, baos, "UTF-8"); return baos.toString("UTF-8"); } catch (IOException e) { 151 AntModule.err.notify(e); 152 return ""; } 154 } 155 156 void finish() throws IOException { 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"); KeyStroke stroke = (KeyStroke ) getProperty(PROP_STROKE); 166 create(DataFolder.findFolder(shortcutsFolder), Utilities.keyToString(stroke)); 167 } 168 } 169 170 private void create(DataFolder f, String name) throws IOException { 171 assert f != null; 172 final String fname; 173 if (name != null) { 174 fname = name + ".xml"; } else { 176 fname = FileUtil.findFreeFileName(f.getPrimaryFile(), getTargetBaseName(), "xml") + ".xml"; } 178 final String 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 { 183 shortcut[0] = folder.createData(fname); FileLock lock = shortcut[0].lock(); 185 try { 186 OutputStream os = shortcut[0].getOutputStream(lock); 187 try { 188 os.write(contents.getBytes("UTF-8")); } finally { 190 os.close(); 191 } 192 } finally { 193 lock.releaseLock(); 194 } 195 } 196 }); 197 } 198 199 String getTargetBaseName() { 200 String projname = ""; Document doc = project.getDocument(); 202 if (doc != null) { 203 projname = doc.getDocumentElement().getAttribute("name"); } 205 return (projname + '-' + target.getAttribute("name")).replaceAll("[^a-zA-Z0-9_-]", "-"); } 207 208 } 209 | Popular Tags |