KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > ExportHtmlAction


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.editor;
20
21 import org.openide.util.actions.CookieAction;
22 import org.openide.util.HelpCtx;
23 import org.openide.util.NbBundle;
24 import org.openide.util.RequestProcessor;
25 import org.openide.nodes.Node;
26 import org.openide.cookies.EditorCookie;
27 import org.openide.loaders.DataObject;
28 import org.openide.DialogDisplayer;
29 import org.openide.DialogDescriptor;
30 import org.openide.ErrorManager;
31 import org.openide.NotifyDescriptor;
32 import org.openide.filesystems.FileObject;
33 import org.openide.awt.HtmlBrowser;
34 import org.netbeans.editor.*;
35
36 import javax.swing.text.StyledDocument JavaDoc;
37 import javax.swing.text.Document JavaDoc;
38 import javax.swing.text.JTextComponent JavaDoc;
39 import javax.swing.*;
40 import java.io.*;
41 import java.awt.*;
42 import java.awt.event.ActionListener JavaDoc;
43 import java.awt.event.ActionEvent JavaDoc;
44 import java.net.MalformedURLException JavaDoc;
45
46 public class ExportHtmlAction extends CookieAction {
47
48     private static final String JavaDoc HTML_EXT = ".html"; //NOI18N
49
private static final String JavaDoc OPEN_HTML_HIST = "ExportHtmlAction_open_html_history"; //NOI18N
50
private static final String JavaDoc SHOW_LINES_HIST = "ExportHtmlAction_show_lines_history"; //NOI18N
51
private static final String JavaDoc SELECTION_HIST = "ExportHtmlAction_selection_history"; //NOI18N
52
private static final String JavaDoc FOLDER_NAME_HIST = "ExportHtmlAction_folder_name_history"; //NOI18N
53
private static final String JavaDoc CHARSET = "UTF-8"; //NOI18N
54

55     private Dialog dlg;
56
57     public ExportHtmlAction () {
58     }
59
60     protected final int mode() {
61         return CookieAction.MODE_EXACTLY_ONE;
62     }
63
64
65
66     protected final Class JavaDoc[] cookieClasses() {
67         return new Class JavaDoc[] {EditorCookie.class};
68     }
69
70     protected final void performAction(Node[] activatedNodes) {
71         EditorCookie ec = (EditorCookie) activatedNodes[0].getCookie (EditorCookie.class);
72         if (ec==null) return;
73         StyledDocument JavaDoc doc = null;
74         try {
75             doc = ec.openDocument();
76         } catch (IOException ioe) {
77         }
78         if (doc instanceof BaseDocument) {
79             final BaseDocument bdoc = (BaseDocument) doc;
80             final JTextComponent JavaDoc jtc = Utilities.getLastActiveComponent();
81             Presenter p = new Presenter ();
82             String JavaDoc folderName = (String JavaDoc)EditorState.get(FOLDER_NAME_HIST);
83             if (folderName == null)
84                 folderName = System.getProperty("user.home"); //NOI18N
85
p.setFileName (folderName+File.separatorChar+
86                     ((DataObject)bdoc.getProperty (Document.StreamDescriptionProperty)).getPrimaryFile().getName()+HTML_EXT);
87             Boolean JavaDoc bool = (Boolean JavaDoc)EditorState.get(SHOW_LINES_HIST);
88             boolean showLineNumbers = (bool != null ? bool : (Boolean JavaDoc)SettingsUtil.getValue (bdoc.getKitClass(),SettingsNames.LINE_NUMBER_VISIBLE,
89                     Boolean.FALSE)).booleanValue();
90             p.setShowLines (showLineNumbers);
91             bool = (Boolean JavaDoc)EditorState.get(SELECTION_HIST);
92             p.setSelectionActive (jtc != null && jtc.getSelectionStart()!=jtc.getSelectionEnd());
93             boolean selection = (jtc != null && jtc.getSelectionStart()!=jtc.getSelectionEnd()) && (bool != null ? bool.booleanValue() : true);
94             p.setSelection (selection);
95             bool = (Boolean JavaDoc)EditorState.get(OPEN_HTML_HIST);
96             boolean setOpen = bool != null ? bool.booleanValue() : false;
97             p.setOpenHtml(setOpen);
98             DialogDescriptor dd = new DialogDescriptor (p, NbBundle.getMessage(ExportHtmlAction.class, "CTL_ExportHtml"));
99             boolean overwrite = true;
100             dlg = DialogDisplayer.getDefault().createDialog (dd);
101             do{
102                 dlg.setVisible (true);
103                 overwrite = true;
104                 if ( dd.getValue() == DialogDescriptor.OK_OPTION && new File(p.getFileName()).exists()){
105                     NotifyDescriptor NDConfirm = new NotifyDescriptor.Confirmation(
106                     NbBundle.getMessage( org.netbeans.modules.editor.ExportHtmlAction.class, "MSG_FileExists", p.getFileName()),
107                     NotifyDescriptor.YES_NO_OPTION,
108                     NotifyDescriptor.WARNING_MESSAGE
109                     );
110
111                     org.openide.DialogDisplayer.getDefault().notify(NDConfirm);
112                     if (NDConfirm.getValue()!=NDConfirm.YES_OPTION){
113                         overwrite = false;
114                     }
115                 }
116             }while(!overwrite);
117             
118             dlg.dispose();
119             dlg = null;
120             if (dd.getValue() == DialogDescriptor.OK_OPTION) {
121                 if (selection != p.isSelection()) {
122                     selection = p.isSelection();
123                     EditorState.put(SELECTION_HIST, selection ? Boolean.TRUE : Boolean.FALSE);
124                 }
125                 final String JavaDoc file = p.getFileName();
126                 int idx = file.lastIndexOf(File.separatorChar);
127                 if (idx != -1)
128                     EditorState.put(FOLDER_NAME_HIST, file.substring(0, idx));
129                 final boolean lineNumbers = p.isShowLines();
130                 if (showLineNumbers != lineNumbers) {
131                     EditorState.put(SHOW_LINES_HIST, lineNumbers ? Boolean.TRUE : Boolean.FALSE);
132                 }
133                 final boolean open = p.isOpenHtml();
134                 if (setOpen != open) {
135                     EditorState.put(OPEN_HTML_HIST, open ? Boolean.TRUE : Boolean.FALSE);
136                 }
137                 final int selectionStart = selection ? jtc.getSelectionStart() : 0;
138                 final int selectionEnd = selection ? jtc.getSelectionEnd() : bdoc.getLength();
139                 RequestProcessor.getDefault().post(
140                         new Runnable JavaDoc () {
141                             public void run () {
142                                 try {
143                                     if (jtc!=null)
144                                         this.setCursor (org.openide.util.Utilities.createProgressCursor (jtc));
145                                     export (bdoc, file, lineNumbers, selectionStart, selectionEnd);
146                                     if (open) {
147                                         HtmlBrowser.URLDisplayer.getDefault().showURL(new File(file).toURI().toURL());
148                                     }
149                                 } catch (MalformedURLException JavaDoc mue) {
150                                         ErrorManager.getDefault().notify (mue);
151                                 } catch (IOException ioe) {
152                                     NotifyDescriptor nd = new NotifyDescriptor.Message (
153                                             NbBundle.getMessage(ExportHtmlAction.class,"ERR_IOError",
154                                                     new Object JavaDoc[]{((DataObject)bdoc.getProperty(Document.StreamDescriptionProperty)).getPrimaryFile().getNameExt()
155                                             +HTML_EXT,file}), //NOI18N
156
NotifyDescriptor.ERROR_MESSAGE);
157                                     DialogDisplayer.getDefault().notify (nd);
158                                     return;
159                                 }
160                                 finally {
161                                     if (jtc != null) {
162                                         this.setCursor (null);
163                                     }
164                                 }
165                             }
166
167
168                             private void setCursor (final Cursor c) {
169                                 SwingUtilities.invokeLater (new Runnable JavaDoc () {
170                                         public void run() {
171                                             jtc.setCursor (c);
172                                         }
173                                     });
174                             }
175                         }
176                 );
177             }
178         }
179         else {
180             ErrorManager.getDefault().log (NbBundle.getMessage(ExportHtmlAction.class,"MSG_DocError"));
181         }
182     }
183
184     public final String JavaDoc getName() {
185         return NbBundle.getMessage (ExportHtmlAction.class, "CTL_ExportHtmlAction");
186     }
187
188     public final HelpCtx getHelpCtx() {
189         return HelpCtx.DEFAULT_HELP;
190     }
191
192     protected final boolean asynchronous() {
193         return false;
194     }
195
196     private void export (final BaseDocument bdoc, String JavaDoc fileName, boolean lineNumbers, int selectionStart, int selectionEnd) throws IOException {
197         Coloring coloring = SettingsUtil.getColoring (bdoc.getKitClass(), SettingsNames.DEFAULT_COLORING, false);
198         Color bgColor = coloring.getBackColor();
199         Color fgColor = coloring.getForeColor();
200         Font font = coloring.getFont();
201         coloring = SettingsUtil.getColoring (bdoc.getKitClass(), SettingsNames.LINE_NUMBER_COLORING, false);
202         Color lnbgColor = coloring.getBackColor();
203         Color lnfgColor = coloring.getForeColor();
204         FileObject fo = ((DataObject)bdoc.getProperty (Document.StreamDescriptionProperty)).getPrimaryFile();
205         HtmlPrintContainer htmlPrintContainer = new HtmlPrintContainer();
206         htmlPrintContainer.begin (fo, font, fgColor, bgColor,lnfgColor,lnbgColor, bdoc.getKitClass(), CHARSET);
207         bdoc.print (htmlPrintContainer,false, Boolean.valueOf(lineNumbers), selectionStart, selectionEnd);
208         String JavaDoc result = htmlPrintContainer.end();
209         PrintWriter out = null;
210         try {
211             out = new PrintWriter (new OutputStreamWriter (new FileOutputStream (fileName), CHARSET));
212             out.print (result);
213         } finally {
214             if (out != null)
215                 out.close();
216         }
217     }
218
219
220     private class Presenter extends JPanel {
221
222         private JTextField fileName;
223         private JCheckBox showLineNumbers;
224         private JCheckBox openHtml;
225         private JCheckBox selection;
226
227         public Presenter () {
228             this.initGUI ();
229         }
230
231         public final String JavaDoc getFileName () {
232             return this.fileName.getText();
233         }
234
235         public final void setFileName (String JavaDoc name) {
236             this.fileName.setText (name);
237         }
238
239         public final boolean isShowLines () {
240             return this.showLineNumbers.isSelected();
241         }
242
243         public final void setShowLines (boolean value) {
244             this.showLineNumbers.setSelected (value);
245         }
246
247         public final boolean isSelection () {
248             return this.selection.isSelected();
249         }
250
251         public final void setSelection(boolean value) {
252             this.selection.setSelected(value);
253         }
254
255         public final boolean isOpenHtml () {
256             return this.openHtml.isSelected();
257         }
258
259         public final void setOpenHtml (boolean value) {
260             this.openHtml.setSelected (value);
261         }
262
263         public final void setSelectionActive (boolean value) {
264             this.selection.setEnabled (value);
265         }
266
267         private void initGUI () {
268             this.setLayout ( new GridBagLayout ());
269             getAccessibleContext().setAccessibleName(NbBundle.getMessage (ExportHtmlAction.class, "ACSN_ExportToHTML")); // NOI18N
270
getAccessibleContext().setAccessibleDescription(NbBundle.getMessage (ExportHtmlAction.class, "ACSD_ExportToHTML")); // NOI18N
271

272             JLabel label = new JLabel (NbBundle.getMessage (ExportHtmlAction.class, "CTL_OutputDir"));
273             label.setDisplayedMnemonic (NbBundle.getMessage (ExportHtmlAction.class, "MNE_OutputDir").charAt(0));
274             label.getAccessibleContext().setAccessibleName(NbBundle.getMessage (ExportHtmlAction.class, "AN_OutputDir"));
275             label.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage (ExportHtmlAction.class, "AD_OutputDir"));
276             GridBagConstraints c = new GridBagConstraints ();
277             c.gridx = 0;
278             c.gridy = 0;
279             c.gridwidth = 1;
280             c.gridheight = 1;
281             c.anchor = GridBagConstraints.WEST;
282             c.insets = new Insets (12,12,6,6);
283             ((GridBagLayout)this.getLayout()).setConstraints (label, c);
284             this.add (label);
285             fileName = new JTextField ();
286             fileName.setColumns (25);
287             c = new GridBagConstraints ();
288             c.gridx = 1;
289             c.gridy = 0;
290             c.gridwidth = 1;
291             c.gridheight = 1;
292             c.fill = GridBagConstraints.HORIZONTAL;
293             c.anchor = GridBagConstraints.WEST;
294             c.insets = new Insets (12,6,6,6);
295             c.weightx = 1.0;
296             ((GridBagLayout)this.getLayout()).setConstraints (fileName, c);
297             this.add (this.fileName);
298             label.setLabelFor (this.fileName);
299             JButton button = new JButton (NbBundle.getMessage(ExportHtmlAction.class,"CTL_Select"));
300             button.setMnemonic (NbBundle.getMessage (ExportHtmlAction.class, "MNE_Select").charAt(0));
301             button.getAccessibleContext().setAccessibleName(NbBundle.getMessage(ExportHtmlAction.class,"AN_Select"));
302             button.getAccessibleContext().setAccessibleDescription (NbBundle.getMessage(ExportHtmlAction.class,"AD_Select"));
303             button.addActionListener (new ActionListener JavaDoc () {
304                 public void actionPerformed(ActionEvent JavaDoc e) {
305                     selectFile ();
306                 }
307             });
308             c = new GridBagConstraints ();
309             c.gridx = 2;
310             c.gridy = 0;
311             c.gridwidth = 1;
312             c.gridheight = 1;
313             c.anchor = GridBagConstraints.WEST;
314             c.insets = new Insets (12,6,6,12);
315             ((GridBagLayout)this.getLayout()).setConstraints (button,c);
316             this.add (button);
317             selection = new JCheckBox (NbBundle.getMessage(ExportHtmlAction.class, "CTL_Selection"));
318             selection.setMnemonic(NbBundle.getMessage(ExportHtmlAction.class,"MNE_Selection").charAt(0));
319             selection.getAccessibleContext().setAccessibleName(NbBundle.getMessage(ExportHtmlAction.class,"AN_Selection"));
320             selection.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ExportHtmlAction.class,"AD_Selection"));
321             c = new GridBagConstraints ();
322             c.gridx = 1;
323             c.gridy = 1;
324             c.gridwidth = GridBagConstraints.REMAINDER;
325             c.gridheight = 1;
326             c.anchor = GridBagConstraints.WEST;
327             c.fill = GridBagConstraints.HORIZONTAL;
328             c.insets = new Insets (6,6,6,12);
329             c.weightx = 1.0;
330             ((GridBagLayout)this.getLayout()).setConstraints (this.selection,c);
331             this.add (this.selection);
332             showLineNumbers = new JCheckBox (NbBundle.getMessage(ExportHtmlAction.class,"CTL_ShowLineNumbers"));
333             showLineNumbers.setMnemonic(NbBundle.getMessage(ExportHtmlAction.class,"MNE_ShowLineNumbers").charAt(0));
334             showLineNumbers.getAccessibleContext().setAccessibleName(NbBundle.getMessage(ExportHtmlAction.class,"AN_ShowLineNumbers"));
335             showLineNumbers.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ExportHtmlAction.class,"AD_ShowLineNumbers"));
336             c = new GridBagConstraints();
337             c.gridx = 1;
338             c.gridy = 2;
339             c.gridwidth = GridBagConstraints.REMAINDER;
340             c.gridheight = 1;
341             c.anchor = GridBagConstraints.WEST;
342             c.fill = GridBagConstraints.HORIZONTAL;
343             c.insets = new Insets (6,6,6,12);
344             c.weightx = 1.0;
345             ((GridBagLayout)this.getLayout()).setConstraints (this.showLineNumbers,c);
346             this.add (this.showLineNumbers);
347             openHtml = new JCheckBox (NbBundle.getMessage(ExportHtmlAction.class,"CTL_OpenHTML"));
348             openHtml.setMnemonic(NbBundle.getMessage(ExportHtmlAction.class,"MNE_OpenHTML").charAt(0));
349             openHtml.getAccessibleContext().setAccessibleName(NbBundle.getMessage(ExportHtmlAction.class,"AN_OpenHTML"));
350             openHtml.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ExportHtmlAction.class,"AD_OpenHTML"));
351             c = new GridBagConstraints ();
352             c.gridx = 1;
353             c.gridy = 3;
354             c.gridwidth = GridBagConstraints.REMAINDER;
355             c.gridheight = 1;
356             c.anchor = GridBagConstraints.WEST;
357             c.fill = GridBagConstraints.HORIZONTAL;
358             c.insets = new Insets (6,6,12,12);
359             c.weightx = 1.0;
360             ((GridBagLayout)this.getLayout()).setConstraints (this.openHtml,c);
361             this.add (this.openHtml);
362         }
363
364
365         private void selectFile () {
366             JFileChooser chooser = new JFileChooser();
367             chooser.setFileFilter (new javax.swing.filechooser.FileFilter JavaDoc () {
368                 public boolean accept(File f) {
369                     if (f.isFile() && f.getName().endsWith (HTML_EXT) || f.isDirectory()) {
370                         return true;
371                     }
372                     else
373                       return false;
374                 }
375
376                 public String JavaDoc getDescription() {
377                     return NbBundle.getMessage (ExportHtmlAction.class, "TXT_HTMLFileType");
378                 }
379             });
380             chooser.setSelectedFile (new File (this.fileName.getText()));
381             if (chooser.showOpenDialog (dlg) == JFileChooser.APPROVE_OPTION) {
382                 this.fileName.setText (chooser.getSelectedFile().getAbsolutePath());
383             }
384         }
385     }
386
387 }
388
Popular Tags