KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.logging.Level JavaDoc;
28 import javax.xml.transform.Transformer JavaDoc;
29 import javax.xml.transform.TransformerConfigurationException JavaDoc;
30 import javax.xml.transform.TransformerException JavaDoc;
31 import javax.xml.transform.TransformerFactory JavaDoc;
32 import javax.xml.transform.stream.StreamSource JavaDoc;
33
34 import org.netbeans.modules.tasklist.core.export.ExportImportProvider;
35 import org.netbeans.modules.tasklist.core.export.SaveFilePanel;
36 import org.netbeans.modules.tasklist.core.util.ExtensionFileFilter;
37 import org.netbeans.modules.tasklist.core.util.SimpleWizardPanel;
38 import org.netbeans.modules.tasklist.usertasks.options.Settings;
39 import org.netbeans.modules.tasklist.usertasks.util.UTUtils;
40 import org.openide.DialogDisplayer;
41 import org.openide.NotifyDescriptor;
42 import org.openide.WizardDescriptor;
43 import org.openide.cookies.OpenCookie;
44 import org.openide.filesystems.FileObject;
45 import org.openide.filesystems.FileUtil;
46 import org.openide.loaders.DataObject;
47 import org.openide.loaders.DataObjectNotFoundException;
48 import org.openide.util.NbBundle;
49
50 /**
51  * Creates plain text file using XSL transformation.
52  *
53  * @author tl
54  */

55 public class TextExportFormat extends XmlExportFormat {
56     private String JavaDoc res = "usertasks-effort-text.xsl"; // NOI18N
57

58     /**
59      * Creates a new instance of HTMLTranslator
60      */

61     public TextExportFormat() {
62     }
63     
64     public String JavaDoc getName() {
65         return NbBundle.getMessage(TextExportFormat.class, "Text"); // NOI18N
66
}
67     
68     public org.openide.WizardDescriptor getWizard() {
69         SaveFilePanel chooseFilePanel = new SaveFilePanel();
70         SimpleWizardPanel chooseFileWP = new SimpleWizardPanel(chooseFilePanel);
71         chooseFilePanel.setWizardPanel(chooseFileWP);
72         chooseFilePanel.getFileChooser().addChoosableFileFilter(
73             new ExtensionFileFilter(
74                 NbBundle.getMessage(XmlExportFormat.class,
75                     "TextFilter"), // NOI18N
76
new String JavaDoc[] {".txt"})); // NOI18N
77
chooseFilePanel.setFile(new File JavaDoc(
78                 Settings.getDefault().getLastUsedExportFolder(),
79                 "tasklist.txt")); // NOI18N
80
chooseFileWP.setContentHighlightedIndex(0);
81         chooseFilePanel.setOpenFileCheckBoxVisible(true);
82
83         // create wizard descriptor
84
WizardDescriptor.Iterator iterator =
85             new WizardDescriptor.ArrayIterator(
86                 new WizardDescriptor.Panel[] {chooseFileWP});
87         WizardDescriptor wd = new WizardDescriptor(iterator);
88         wd.putProperty("WizardPanel_contentData", // NOI18N
89
new String JavaDoc[] {
90                 NbBundle.getMessage(
91                     TextExportFormat.class, "TextChooseDestination"), // NOI18N
92
}
93         ); // NOI18N
94
wd.putProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); // NOI18N
95
wd.putProperty("WizardPanel_contentDisplayed", Boolean.TRUE); // NOI18N
96
wd.putProperty("WizardPanel_contentNumbered", Boolean.TRUE); // NOI18N
97
wd.setTitle(NbBundle.getMessage(TextExportFormat.class,
98             "ExportText")); // NOI18N
99
wd.putProperty(CHOOSE_FILE_PANEL_PROP, chooseFilePanel);
100         wd.setTitleFormat(new java.text.MessageFormat JavaDoc("{0}")); // NOI18N
101

102         return wd;
103     }
104     
105     /**
106      * Opens the specified file in the IDE.
107      *
108      * @param file file to be opened
109      */

110     private static void openFileInIde(File JavaDoc file) {
111         try {
112             FileObject fo = FileUtil.toFileObject(file);
113             if (fo != null) {
114                 DataObject do_ = DataObject.find(fo);
115                 OpenCookie oc = (OpenCookie) do_.getCookie(OpenCookie.class);
116                 if (oc != null) {
117                     oc.open();
118                 } else {
119                     String JavaDoc msg = NbBundle.getMessage(TextExportFormat.class,
120                             "CannotOpenFile"); // NOI18N
121
NotifyDescriptor nd = new NotifyDescriptor.Message(
122                             msg, NotifyDescriptor.ERROR_MESSAGE);
123                     DialogDisplayer.getDefault().notify(nd);
124                 }
125             } else {
126                 String JavaDoc msg = NbBundle.getMessage(TextExportFormat.class,
127                         "CannotFindFile"); // NOI18N
128
NotifyDescriptor nd = new NotifyDescriptor.Message(
129                         msg, NotifyDescriptor.ERROR_MESSAGE);
130                 DialogDisplayer.getDefault().notify(nd);
131             }
132         } catch (DataObjectNotFoundException e) {
133             UTUtils.LOGGER.log(Level.WARNING, "", e); // NOI18N
134
}
135     }
136    
137     protected Transformer JavaDoc createTransformer() {
138         TransformerFactory JavaDoc tf = TransformerFactory.newInstance();
139         try {
140             InputStream JavaDoc xsl = TextExportFormat.class.
141                 getResourceAsStream(res);
142             return tf.newTransformer(new StreamSource JavaDoc(xsl));
143         } catch (TransformerConfigurationException JavaDoc e) {
144             UTUtils.LOGGER.log(Level.WARNING,
145                     "XSL-Transformer not found", e); // NOI18N
146
return null;
147         } catch (TransformerException JavaDoc e) {
148             UTUtils.LOGGER.log(Level.WARNING,
149                     "XSL-Transformer cannot be created", e); // NOI18N
150
return null;
151         }
152     }
153     
154     public void doExportImport(ExportImportProvider provider,
155     WizardDescriptor wd) {
156         SaveFilePanel panel = (SaveFilePanel)
157                 wd.getProperty(CHOOSE_FILE_PANEL_PROP);
158         super.doExportImport(provider, wd);
159         File JavaDoc dir = panel.getFile().getParentFile();
160         Settings.getDefault().setLastUsedExportFolder(dir);
161         if (panel.getOpenExportedFile()) {
162             openFileInIde(panel.getFile());
163         }
164     }
165     
166 }
167
Popular Tags