KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tools > doclet > DocletAction


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 package org.netbeans.modules.xml.tools.doclet;
20
21 import java.util.*;
22 import java.awt.datatransfer.StringSelection JavaDoc;
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 /**
44  * Creates a documentation upon a standalone DTD. Stores it into html.
45  * It does work only with standalone DTD (it is feature).
46  *
47  * @author Petr Kuzel
48  * @version 1.0
49  */

50 public final class DocletAction extends CookieAction implements CollectDTDAction.DTDAction {
51
52     /** Stream serialVersionUID. */
53     private static final long serialVersionUID = -4037098165368211623L;
54     
55     
56     /** Creates new CSSStyleAction */
57     public DocletAction() {
58     }
59
60     public Class JavaDoc[] cookieClasses() {
61         return new Class JavaDoc[] { 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 JavaDoc text = new StringBuffer JavaDoc();
74         final Node dtd = nodes[0];
75
76         final DTDDataObject dtdo = (DTDDataObject) dtd.getCookie(DTDDataObject.class);
77
78         Thread JavaDoc 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"); // NOI18N
90
}
91             final TreeDTD treeDTD = (TreeDTD) result;
92
93             final DTDDoclet doclet = new DTDDoclet();
94
95             Runnable JavaDoc task = new Runnable JavaDoc() {
96                 public void run() {
97                     text.append(doclet.createDoclet (treeDTD));
98                 }
99             };
100
101             //start task in paralel with user input
102

103             thread = new Thread JavaDoc(task, "Creating XML doc..."); // NOI18N
104
thread.setPriority(Thread.MIN_PRIORITY);
105             thread.setDaemon(true);
106             thread.start();
107
108
109             try {
110
111                 // ask for file object location
112

113                 FileObject primFile = dtdo.getPrimaryFile();
114                 String JavaDoc name = primFile.getName() + Util.THIS.getString("NAME_SUFFIX_Documentation");
115                 FileObject folder = primFile.getParent();
116
117                 FileObject generFile = (new SelectFileDialog (folder, name, "html")).getFileObject(); // NOI18N
118
name = generFile.getName();
119
120                 // wait until documentation generated
121
thread.join();
122
123                 // fill result file
124

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"); //NOI18N
132
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                 // open results in a browser if exists
152

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                     // just do not show
160
}
161
162                 
163             } catch (UserCancelException ex) {
164                 //user cancelled do nothing
165

166             } catch (InterruptedException JavaDoc 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 JavaDoc text) {
189         StringSelection JavaDoc ss = new StringSelection JavaDoc(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 JavaDoc getName() {
200         return Util.THIS.getString("NAME_Generate_Documentation");
201     }
202
203     protected boolean asynchronous() {
204         return false;
205     }
206
207 }
208
Popular Tags