KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > translators > HtmlExportFormat


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.usertasks.translators;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import javax.xml.transform.Transformer JavaDoc;
31 import javax.xml.transform.TransformerConfigurationException JavaDoc;
32 import javax.xml.transform.TransformerException JavaDoc;
33 import javax.xml.transform.TransformerFactory JavaDoc;
34 import javax.xml.transform.stream.StreamSource JavaDoc;
35
36 import org.netbeans.modules.tasklist.core.export.ExportImportProvider;
37 import org.netbeans.modules.tasklist.core.export.SaveFilePanel;
38 import org.netbeans.modules.tasklist.core.util.ExtensionFileFilter;
39 import org.netbeans.modules.tasklist.core.util.SimpleWizardPanel;
40 import org.netbeans.modules.tasklist.usertasks.options.Settings;
41 import org.netbeans.modules.tasklist.usertasks.util.UTUtils;
42 import org.openide.DialogDisplayer;
43 import org.openide.NotifyDescriptor;
44 import org.openide.WizardDescriptor;
45 import org.openide.awt.HtmlBrowser;
46 import org.openide.util.NbBundle;
47
48 /**
49  * Creates HTML using XSL transformation
50  */

51 public class HtmlExportFormat extends XmlExportFormat {
52     private static String JavaDoc[] LAYOUTS = {
53         "usertasks-simple-html.xsl", // NOI18N
54
"usertasks-effort-html.xsl", // NOI18N
55
"usertasks-planning-html.xsl", // NOI18N
56
"usertasks-table-html.xsl", // NOI18N
57
"usertasks-tree-html.xsl" // NOI18N
58
};
59
60     /**
61      * Copies a resource to a file.
62      *
63      * @param from see Class.getResourceAsStream
64      * @param to destination
65      */

66     private static void copyResourceToFile(String JavaDoc from, File JavaDoc to)
67             throws IOException JavaDoc {
68         InputStream JavaDoc is = TextExportFormat.class.getResourceAsStream(from);
69         try {
70             OutputStream JavaDoc os = new FileOutputStream JavaDoc(to);
71             try {
72                 UTUtils.copyStream(is, os);
73             } finally {
74                 os.close();
75             }
76         } finally {
77             is.close();
78         }
79     }
80     
81     private String JavaDoc res = "usertasks-effort-html.xsl"; // NOI18N
82

83     /**
84      * Creates a new instance of HTMLTranslator
85      */

86     public HtmlExportFormat() {
87     }
88     
89     public String JavaDoc getName() {
90         return NbBundle.getMessage(HtmlExportFormat.class, "HTML"); // NOI18N
91
}
92     
93     public org.openide.WizardDescriptor getWizard() {
94         SaveFilePanel chooseFilePanel = new SaveFilePanel();
95         SimpleWizardPanel chooseFileWP = new SimpleWizardPanel(chooseFilePanel);
96         chooseFilePanel.setWizardPanel(chooseFileWP);
97         chooseFilePanel.getFileChooser().addChoosableFileFilter(
98             new ExtensionFileFilter(
99                 NbBundle.getMessage(XmlExportFormat.class,
100                     "HtmlFilter"), // NOI18N
101
new String JavaDoc[] {".html"})); // NOI18N
102
chooseFilePanel.setFile(new File JavaDoc(
103                 Settings.getDefault().getLastUsedExportFolder(),
104                 "tasklist.html")); // NOI18N
105
chooseFileWP.setContentHighlightedIndex(0);
106
107         XslTemplatesPanel templatesPanel = new XslTemplatesPanel();
108         templatesPanel.setAvailableLayouts(new String JavaDoc[] {
109             NbBundle.getMessage(
110                 XmlExportFormat.class, "Simple"), // NOI18N
111
NbBundle.getMessage(
112                 XmlExportFormat.class, "Effort"), // NOI18N
113
NbBundle.getMessage(
114                 XmlExportFormat.class, "Planning"), // NOI18N
115
NbBundle.getMessage(
116                 XmlExportFormat.class, "Table"), // NOI18N
117
NbBundle.getMessage(
118                 XmlExportFormat.class, "Tree") // NOI18N
119
});
120         SimpleWizardPanel templatesWP = new SimpleWizardPanel(templatesPanel);
121         templatesWP.setFinishPanel(true);
122         templatesWP.setContentHighlightedIndex(1);
123
124         // create wizard descriptor
125
WizardDescriptor.Iterator iterator =
126             new WizardDescriptor.ArrayIterator(
127                 new WizardDescriptor.Panel[] {chooseFileWP, templatesWP});
128         WizardDescriptor wd = new WizardDescriptor(iterator);
129         wd.putProperty("WizardPanel_contentData", // NOI18N
130
new String JavaDoc[] {
131                 NbBundle.getMessage(
132                     XmlExportFormat.class, "ChooseDestination"), // NOI18N
133
NbBundle.getMessage(
134                     XmlExportFormat.class, "ChooseLayout") // NOI18N
135
}
136         ); // NOI18N
137
wd.putProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); // NOI18N
138
wd.putProperty("WizardPanel_contentDisplayed", Boolean.TRUE); // NOI18N
139
wd.putProperty("WizardPanel_contentNumbered", Boolean.TRUE); // NOI18N
140
wd.setTitle(NbBundle.getMessage(XmlExportFormat.class,
141             "ExportHTML")); // NOI18N
142
wd.putProperty(getClass().getName() +
143             ".TemplatesPanel", templatesPanel); // NOI18N
144
wd.putProperty(CHOOSE_FILE_PANEL_PROP, chooseFilePanel);
145         wd.setTitleFormat(new java.text.MessageFormat JavaDoc("{0}")); // NOI18N todo
146

147         return wd;
148     }
149     
150     /**
151      * Opens the specified file in browser
152      *
153      * @param file file to be opened
154      */

155     private static void showFileInBrowser(File JavaDoc file) {
156         URL JavaDoc url;
157         try {
158             url = file.toURI().toURL();
159         } catch (MalformedURLException JavaDoc e) {
160             // Can't show URL
161
UTUtils.LOGGER.log(Level.SEVERE, "", e);
162             return;
163         }
164
165         HtmlBrowser.URLDisplayer.getDefault().showURL(url);
166     }
167    
168     protected Transformer JavaDoc createTransformer() {
169         TransformerFactory JavaDoc tf = TransformerFactory.newInstance();
170         try {
171             InputStream JavaDoc xsl = HtmlExportFormat.class.
172                 getResourceAsStream(res);
173             return tf.newTransformer(new StreamSource JavaDoc(xsl));
174         } catch (TransformerConfigurationException JavaDoc e) {
175             UTUtils.LOGGER.log(Level.WARNING, "", e);
176             return null;
177         } catch (TransformerException JavaDoc e) {
178             UTUtils.LOGGER.log(Level.WARNING, "", e);
179             return null;
180         }
181    }
182     
183     public void doExportImport(ExportImportProvider provider,
184     WizardDescriptor wd) {
185         SaveFilePanel chooseFilePanel = (SaveFilePanel)
186             wd.getProperty(CHOOSE_FILE_PANEL_PROP);
187         XslTemplatesPanel templatesPanel = (XslTemplatesPanel)
188             wd.getProperty(getClass().getName() +
189                 ".TemplatesPanel"); // NOI18N
190
this.res = LAYOUTS[templatesPanel.getLayoutIndex()];
191         File JavaDoc dir = chooseFilePanel.getFile().getParentFile();
192         super.doExportImport(provider, wd);
193         try {
194             copyResourceToFile(
195                     "/org/netbeans/modules/tasklist/core/task.gif", // NOI18N
196
new File JavaDoc(dir, "undone.gif")); // NOI18N
197
copyResourceToFile(
198                     "/org/netbeans/modules/tasklist/core/doneItem.gif", // NOI18N
199
new File JavaDoc(dir, "done.gif")); // NOI18N
200
} catch (IOException JavaDoc ex) {
201             NotifyDescriptor nd = new NotifyDescriptor.Message(ex.getMessage(),
202                     NotifyDescriptor.Message.ERROR_MESSAGE);
203             DialogDisplayer.getDefault().notify(nd);
204         }
205         if (templatesPanel.getOpenFile()) {
206             showFileInBrowser(chooseFilePanel.getFile());
207         }
208     }
209 }
210
Popular Tags