KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > mdrxml > looks > actions > GenerateTextAction


1 /*
2  * GenerateTextAction.java
3  *
4  * Created on July 25, 2002, 6:21 PM
5  */

6
7 package org.netbeans.modules.mdrxml.looks.actions;
8
9
10 import org.openide.TopManager;
11 import org.openide.nodes.Node;
12 import org.openide.util.actions.NodeAction;
13 import org.openide.util.NbBundle;
14 import org.openide.util.HelpCtx;
15 import org.netbeans.api.looks.LookNode;
16 import java.io.File JavaDoc;
17 import java.io.FileWriter JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import javax.swing.JFileChooser JavaDoc;
22 import javax.swing.filechooser.FileFilter JavaDoc;
23 import xmlmodel.*;
24 /**
25  *
26  * @author xtom
27  */

28 public class GenerateTextAction extends NodeAction {
29     
30     private static final String JavaDoc INDENT = " "; //NOI18N
31
private static final String JavaDoc NL = "\n"; //NOI18N
32
private static final String JavaDoc TXT_EXT = ".txt"; //NOI18N
33

34     /** Creates a new instance of GenerateTextAction */
35     public GenerateTextAction() {
36     }
37     
38     
39     public void performAction (Node[] nodes) {
40         xmlmodel.Node node = (xmlmodel.Node) ((LookNode)nodes[0]).getRepresentedObject();
41         JFileChooser JavaDoc fc = new JFileChooser JavaDoc ();
42         fc.setFileFilter(new TextFileFilter ());
43         if (fc.showOpenDialog(TopManager.getDefault().getWindowManager().getMainWindow()) == JFileChooser.APPROVE_OPTION) {
44             File JavaDoc file = fc.getSelectedFile ();
45             PrintWriter JavaDoc pw = null;
46             try {
47                 pw = new PrintWriter JavaDoc ( new FileWriter JavaDoc (file));
48                 String JavaDoc result = processNode (node,0);
49                 pw.println (result);
50             }catch (IOException JavaDoc ioe) {
51                 TopManager.getDefault().notifyException (ioe);
52             }
53             finally {
54         if (pw != null)
55                     pw.close();
56             }
57             
58         }
59     }
60     
61     public boolean enable (Node[] nodes) {
62         if (nodes == null || nodes.length != 1 || (!(nodes[0] instanceof LookNode)))
63             return false;
64         return (((LookNode)nodes[0]).getRepresentedObject () instanceof xmlmodel.Node);
65     }
66     
67     public HelpCtx getHelpCtx () {
68         return HelpCtx.DEFAULT_HELP;
69     }
70     
71     public String JavaDoc getName () {
72         return NbBundle.getMessage (GenerateTextAction.class,"TXT_GenerateTXT");
73     }
74     
75     
76     private static String JavaDoc processNode (xmlmodel.Node node, int indent) {
77         String JavaDoc result = generateFill (indent) + node.getName () + NL;
78         if (node instanceof xmlmodel.ElementNode) {
79             for (Iterator JavaDoc it = ((xmlmodel.ElementNode)node).getNodes().iterator(); it.hasNext();) {
80                 result = result + processNode ((xmlmodel.Node)it.next(), indent+1);
81             }
82         }
83     return result;
84     }
85     
86     private static String JavaDoc generateFill (int count) {
87         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
88         for (int i=0; i< count; i++)
89             sb.append (INDENT);
90         return sb.toString ();
91     }
92     
93     
94     private static class TextFileFilter extends FileFilter JavaDoc {
95     
96     public boolean accept (File JavaDoc file) {
97         return (file.isDirectory() || file.getName().toUpperCase().endsWith (TXT_EXT));
98     }
99     
100     public String JavaDoc getDescription () {
101         return NbBundle.getMessage (GenerateTextAction.class, "TXT_TXT");
102     }
103     }
104     
105 }
106
Popular Tags