KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > html > RewriteAction


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.netbeans.modules.tasklist.html;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import org.openide.text.Line;
25 import org.openide.loaders.DataObject;
26 import java.io.*;
27 import org.openide.ErrorManager;
28
29 import javax.swing.text.*;
30 import org.openide.DialogDescriptor;
31 import org.openide.DialogDisplayer;
32 import org.openide.NotifyDescriptor;
33 import org.openide.nodes.Node;
34 import org.openide.text.DataEditorSupport;
35 import org.openide.util.HelpCtx;
36 import org.openide.util.NbBundle;
37 import org.openide.util.actions.NodeAction;
38 import org.openide.cookies.EditorCookie;
39 import org.openide.cookies.OpenCookie;
40 import org.openide.cookies.EditCookie;
41
42 import org.netbeans.modules.html.*;
43 import org.netbeans.api.diff.*;
44
45 import org.netbeans.modules.tasklist.core.*;
46 import org.netbeans.modules.tasklist.client.Suggestion;
47
48 import org.w3c.tidy.*;
49
50 /**
51  * Rewrite the document
52  * <p>
53  * @todo Use a single button OK panel, not an OK/Cancel
54  * dialog for the preview dialog
55  *
56  * @author Tor Norbye
57  */

58
59 public class RewriteAction extends NodeAction
60      implements ErrorReporter {
61
62     private static final long serialVersionUID = 1;
63
64      protected boolean asynchronous() {
65          return false;
66      }
67          
68     public void reportError(int line, int col, boolean error, String JavaDoc message) {
69         //System.err.println("reportError(" + line + ", " + col + ", " + error + ", " + message + ")");
70
}
71
72     protected boolean enable(Node[] node) {
73         if ((node == null) || (node.length != 1)) {
74             return false;
75         }
76
77         DataObject dobj = (DataObject)node[0].getCookie(DataObject.class);
78         if (dobj == null) {
79             return false;
80         }
81         EditorCookie edit = (EditorCookie)dobj.getCookie(
82                                                      EditorCookie.class);
83         if (edit == null) {
84             return false;
85         }
86
87         if (TidySuggester.isHTML(dobj)) {
88             return true;
89         }
90         if (TidySuggester.isJSP(dobj)) {
91             return true;
92         }
93         if (TidySuggester.isXML(dobj)) {
94             return true;
95         }
96         return false;
97     }
98
99     private Tidy tidy = null;
100
101     protected void performAction(Node[] node) {
102         // Figure out which data object the node is associated
103
// with.
104
// XXX Later I could store this in the Suggestion
105
// rather than relying on the Line object (since
106
// for example category nodes don't have Line objects)
107
// (e.g. the suggestion manager would associate the
108
// data object with the node)
109
Suggestion item = (Suggestion)TaskNode.getTask(node[0]);
110         DataObject dobj;
111         if (item != null) {
112             Line l = item.getLine();
113             dobj = DataEditorSupport.findDataObject(l);
114         } else {
115             dobj = (DataObject)node[0].getCookie(DataObject.class);
116         }
117         if (dobj == null) {
118             return;
119         }
120         Document doc = TLUtils.getDocument(dobj);
121         if (doc == null) {
122             EditorCookie edit = (EditorCookie)dobj.getCookie(
123                                                      EditorCookie.class);
124             if (edit == null) {
125                 return; // Signal error?
126
//ErrorManager.getDefault().log(ErrorManager.USER,
127
// "no editor cookie!");
128
}
129         EditCookie ec = (EditCookie)dobj.getCookie(EditCookie.class);
130         if (ec == null) {
131             OpenCookie oc = (OpenCookie)dobj.getCookie(OpenCookie.class);
132             if (oc != null) oc.open();
133         } else {
134             ec.edit();
135         }
136             doc = TLUtils.getDocument(dobj);
137         if (doc == null) {
138             // HMMMMMM....
139
try {
140                     doc = edit.openDocument();
141                 } catch (java.io.IOException JavaDoc e) {
142                     ErrorManager.getDefault().notify(e);
143                     return;
144                 }
145         }
146         }
147
148         boolean isHTML = TidySuggester.isHTML(dobj);
149         boolean isJSP = false;
150         boolean isXML = false;
151         if (!isHTML) {
152             isJSP = TidySuggester.isJSP(dobj);
153             if (!isJSP) {
154                 isXML = TidySuggester.isXML(dobj);
155             }
156         }
157         if (!(isHTML || isJSP || isXML)) {
158             return;
159         }
160
161         // Set configuration settings
162
if (tidy == null) {
163             tidy = new Tidy();
164         }
165         tidy.setOnlyErrors(false);
166         tidy.setShowWarnings(false);
167         tidy.setQuiet(true);
168         
169         // XXX Apparently JSP pages (at least those involving
170
// JSF) need XML handling in order for JTidy not to choke on them
171
tidy.setXmlTags(isXML || isJSP);
172
173         RewritePanel panel = new RewritePanel(this, doc, dobj);
174         panel.setXHTML(tidy.getXHTML());
175
176         panel.setWrapCol(tidy.getWraplen());
177         panel.setOmit(tidy.getHideEndTags());
178         panel.setUpper(tidy.getUpperCaseTags());
179         panel.setIndent(tidy.getIndentContent());
180         panel.setReplace(tidy.getMakeClean());
181         panel.setXML(tidy.getXmlTags());
182
183         DialogDescriptor d = new DialogDescriptor(panel,
184                     NbBundle.getMessage(RewriteAction.class,
185                     "TITLE_rewrite")); // NOI18N
186
d.setModal(true);
187         d.setMessageType(NotifyDescriptor.PLAIN_MESSAGE);
188         d.setOptionType(NotifyDescriptor.OK_CANCEL_OPTION);
189         Dialog JavaDoc dlg = DialogDisplayer.getDefault().createDialog(d);
190         dlg.pack();
191         dlg.show();
192         if (d.getValue() != NotifyDescriptor.OK_OPTION) {
193             return;
194         }
195
196         tidy.setXHTML(panel.getXHTML());
197         tidy.setMakeClean(panel.getReplace());
198         tidy.setIndentContent(panel.getIndent());
199         tidy.setSmartIndent(panel.getIndent());
200         tidy.setUpperCaseTags(panel.getUpper());
201         tidy.setHideEndTags(panel.getOmit());
202         tidy.setWraplen(panel.getWrapCol());
203
204         String JavaDoc rewritten = rewrite(doc);
205
206         try {
207             // JDK14
208
/* Grrr ... turns out replace() is only available as of JDK 1.4...
209             if (doc instanceof AbstractDocument) {
210                 ((AbstractDocument)doc).replace(0, doc.getLength(), rewritten,
211                                                 null);
212             }
213             else {
214             */

215                 doc.remove(0, doc.getLength());
216                 doc.insertString(0, rewritten, null);
217             //}
218
} catch (BadLocationException e) {
219             ErrorManager.getDefault().notify(e);
220         }
221         Suggestion s = (Suggestion)node[0].getCookie(Suggestion.class);
222         if (s != null) {
223             Object JavaDoc seed = s.getSeed();
224             if (seed instanceof TidySuggester) {
225                 //TODO notify done ((TidySuggester)seed).rescan();
226
}
227         }
228     }
229     
230     private String JavaDoc rewrite(Document doc) {
231         InputStream input = null;
232         try {
233             String JavaDoc text = doc.getText(0, doc.getLength());
234             input = new StringBufferInputStream(text);
235         } catch (BadLocationException e) {
236             ErrorManager.getDefault().
237                 notify(ErrorManager.WARNING, e);
238             return "";
239         }
240         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(doc.getLength()+500);
241         OutputStream output = new StringBufferOutputStream(sb);
242         tidy.parse(input, output);
243         return sb.toString();
244     }
245
246     void preview(RewritePanel panel, Document doc, DataObject dobj) {
247         tidy.setXHTML(panel.getXHTML());
248         tidy.setMakeClean(panel.getReplace());
249         tidy.setIndentContent(panel.getIndent());
250         tidy.setSmartIndent(panel.getIndent());
251         tidy.setUpperCaseTags(panel.getUpper());
252         tidy.setHideEndTags(panel.getOmit());
253         tidy.setWraplen(panel.getWrapCol());
254
255         String JavaDoc before;
256         try {
257             before = doc.getText(0, doc.getLength());
258         } catch (BadLocationException e) {
259             ErrorManager.getDefault().
260                 notify(ErrorManager.WARNING, e);
261             return;
262         }
263         String JavaDoc rewritten = rewrite(doc);
264         String JavaDoc mime = dobj.getPrimaryFile().getMIMEType();
265         diff(before, rewritten, mime);
266
267     }
268
269     void diff(String JavaDoc before, String JavaDoc after, String JavaDoc mime) {
270         Diff diff = Diff.getDefault();
271         if (diff == null) {
272             // TODO Check for this condition and hide the Diff button
273
// if this is the case
274
return ;
275         }
276
277         String JavaDoc beforeDesc = NbBundle.getMessage(RewriteAction.class,
278                             "DiffBefore"); // NOI18N
279
String JavaDoc afterDesc = NbBundle.getMessage(RewriteAction.class,
280                             "DiffAfter"); // NOI18N
281
String JavaDoc beforeTitle = beforeDesc;
282         String JavaDoc afterTitle = afterDesc;
283
284         Component JavaDoc tp = null;
285         try {
286             tp = diff.createDiff(beforeDesc, beforeTitle,
287                                  new StringReader(before),
288                                  afterDesc, afterTitle,
289                                  new StringReader(after),
290                                  mime);
291         } catch (IOException ioex) {
292             ErrorManager.getDefault().notify(ioex);
293             return ;
294         }
295         if (tp == null) {
296             return;
297         }
298
299         //NotifyDescriptor d =
300
// new NotifyDescriptor.Message("Hello...", NotifyDescriptor.INFORMATION_MESSAGE);
301
// TopManager.getDefault().notify(d);
302

303         
304         DialogDescriptor d = new DialogDescriptor(tp,
305                     NbBundle.getMessage(RewriteAction.class,
306                     "TITLE_diff")); // NOI18N
307
d.setModal(true);
308         d.setMessageType(NotifyDescriptor.PLAIN_MESSAGE);
309         d.setOptionType(NotifyDescriptor.DEFAULT_OPTION);
310         Dialog JavaDoc dlg = DialogDisplayer.getDefault().createDialog(d);
311         dlg.pack();
312         dlg.show();
313     }
314
315
316     public String JavaDoc getName() {
317         return NbBundle.getMessage(RewriteAction.class,
318                                    "Rewrite"); // NOI18N
319
}
320
321     protected String JavaDoc iconResource() {
322         return "org/netbeans/modules/tasklist/html/rewrite.gif"; // NOI18N
323
}
324     
325     public HelpCtx getHelpCtx() {
326         return HelpCtx.DEFAULT_HELP;
327         // If you will provide context help then use:
328
// return new HelpCtx (NewTodoItemAction.class);
329
}
330
331     // Grr... tidy uses input/output stream instead of input/output writer
332
private class StringBufferOutputStream extends OutputStream {
333         private StringBuffer JavaDoc sb;
334         StringBufferOutputStream(StringBuffer JavaDoc sb) {
335             this.sb = sb;
336         }
337
338         public void write(int b) {
339             sb.append((char)b);
340         }
341     }
342
343     
344 }
345
Popular Tags