1 19 package org.netbeans.modules.xml.tools.doclet; 20 21 import java.util.*; 22 import java.awt.datatransfer.StringSelection ; 23 import java.io.*; 24 25 import javax.swing.text.*; 26 27 import org.openide.*; 28 import org.openide.awt.StatusDisplayer; 29 import org.openide.nodes.*; 30 import org.openide.cookies.*; 31 import org.openide.loaders.*; 32 import org.openide.filesystems.*; 33 import org.openide.util.*; 34 import org.openide.util.datatransfer.ExClipboard; 35 import org.openide.util.actions.*; 36 37 import org.netbeans.tax.*; 38 import org.netbeans.modules.xml.core.*; 39 import org.netbeans.modules.xml.core.actions.CollectDTDAction; 40 import org.netbeans.modules.xml.tools.generator.*; 41 import org.netbeans.modules.xml.tax.cookies.TreeEditorCookie; 42 43 50 public final class DocletAction extends CookieAction implements CollectDTDAction.DTDAction { 51 52 53 private static final long serialVersionUID = -4037098165368211623L; 54 55 56 57 public DocletAction() { 58 } 59 60 public Class [] cookieClasses() { 61 return new Class [] { DTDDataObject.class }; 62 } 63 64 public int mode() { 65 return MODE_ONE; 66 } 67 68 public void performAction(Node[] nodes) { 69 70 if (nodes == null) return; 71 if (nodes.length != 1) return; 72 73 final StringBuffer text = new StringBuffer (); 74 final Node dtd = nodes[0]; 75 76 final DTDDataObject dtdo = (DTDDataObject) dtd.getCookie(DTDDataObject.class); 77 78 Thread thread = null; 79 ErrorManager emgr = ErrorManager.getDefault(); 80 81 try { 82 83 TreeDocumentRoot result; 84 85 TreeEditorCookie cake = (TreeEditorCookie) dtdo.getCookie(TreeEditorCookie.class); 86 if (cake != null) { 87 result = cake.openDocumentRoot(); 88 } else { 89 throw new TreeException("DTDDataObject:INTERNAL ERROR"); } 91 final TreeDTD treeDTD = (TreeDTD) result; 92 93 final DTDDoclet doclet = new DTDDoclet(); 94 95 Runnable task = new Runnable () { 96 public void run() { 97 text.append(doclet.createDoclet (treeDTD)); 98 } 99 }; 100 101 103 thread = new Thread (task, "Creating XML doc..."); thread.setPriority(Thread.MIN_PRIORITY); 105 thread.setDaemon(true); 106 thread.start(); 107 108 109 try { 110 111 113 FileObject primFile = dtdo.getPrimaryFile(); 114 String name = primFile.getName() + Util.THIS.getString("NAME_SUFFIX_Documentation"); 115 FileObject folder = primFile.getParent(); 116 117 FileObject generFile = (new SelectFileDialog (folder, name, "html")).getFileObject(); name = generFile.getName(); 119 120 thread.join(); 122 123 125 FileLock lock = null; 126 try { 127 lock = generFile.lock(); 128 OutputStream fout = generFile.getOutputStream(lock); 129 try { 130 OutputStream out = new BufferedOutputStream(fout); 131 Writer writer = new OutputStreamWriter(out, "UTF8"); writer.write(text.toString()); 133 writer.flush(); 134 } finally { 135 if (fout != null) fout.close(); 136 } 137 138 } catch (IOException ex) { 139 emgr.annotate(ex, Util.THIS.getString("MSG_error_leaving_in_clipboard")); 140 emgr.notify(ex); 141 142 leaveInClipboard(text.toString()); 143 return; 144 145 } finally { 146 if (lock != null) { 147 lock.releaseLock(); 148 } 149 } 150 151 153 try { 154 DataObject html = DataObject.find(generFile); 155 156 ViewCookie vc = (ViewCookie) html.getCookie(ViewCookie.class); 157 if (vc != null) vc.view(); 158 } catch (DataObjectNotFoundException dex) { 159 } 161 162 163 } catch (UserCancelException ex) { 164 166 } catch (InterruptedException ex) { 167 168 emgr.annotate(ex, Util.THIS.getString("MSG_generating_interrupted")); 169 emgr.notify(ex); 170 } 171 172 } catch (IOException ioex) { 173 174 emgr.annotate(ioex, Util.THIS.getString("MSG_IO_ex_docwriting")); 175 emgr.notify(ioex); 176 177 } catch (TreeException tex) { 178 179 StatusDisplayer.getDefault().setStatusText(Util.THIS.getString("MSG_doclet_fatal_error")); 180 181 } finally { 182 if (thread != null) thread.interrupt(); 183 } 184 185 } 186 187 188 private void leaveInClipboard(String text) { 189 StringSelection ss = new StringSelection (text); 190 ExClipboard clipboard = (ExClipboard) Lookup.getDefault().lookup(ExClipboard.class); 191 clipboard.setContents(ss, null); 192 StatusDisplayer.getDefault().setStatusText(Util.THIS.getString("MSG_documentation_in_clipboard")); 193 } 194 195 public HelpCtx getHelpCtx() { 196 return new HelpCtx(getClass()); 197 } 198 199 public String getName() { 200 return Util.THIS.getString("NAME_Generate_Documentation"); 201 } 202 203 protected boolean asynchronous() { 204 return false; 205 } 206 207 } 208 | Popular Tags |