KickJava   Java API By Example, From Geeks To Geeks.

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


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.mdrxml.looks.actions;
20
21 import org.openide.util.NbBundle;
22 import org.openide.TopManager;
23 import org.openide.NotifyDescriptor;
24 import org.openide.nodes.Node;
25 import org.openide.util.*;
26 import org.openide.util.actions.NodeAction;
27 import org.openide.filesystems.FileSystem;
28 import org.openide.filesystems.FileObject;
29 import org.openide.loaders.*;
30
31 import org.netbeans.api.looks.*;
32 import org.netbeans.api.mdr.*;
33 import org.netbeans.modules.mdrxml.util.XMLGenerator;
34
35 import javax.jmi.reflect.*;
36 import xmlmodel.*;
37 import java.util.*;
38 import java.io.*;
39 import javax.swing.JFileChooser JavaDoc;
40 import javax.swing.filechooser.FileFilter JavaDoc;
41
42 public class GenerateXMLAction extends NodeAction {
43     
44     private static ResourceBundle bundle;
45
46     protected void performAction (Node[] nodes) {
47         Node node = nodes[0];
48         LookNode lookNode = (LookNode) node;
49         if ( lookNode != null ) {
50             generateXML( lookNode );
51         }
52     }
53
54     private void generateXML( LookNode lookNode ) {
55         File temp = null;
56         final TopManager tm = TopManager.getDefault();
57         // selects one folder from data systems
58
JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
59         chooser.setFileFilter(new XMLFileFilter());
60     java.awt.Component JavaDoc parent = tm.getWindowManager().getMainWindow();
61         int returnVal = org.openide.util.Utilities.showJFileChooser(chooser, parent, getLocalizedString("TXT_GenerateXML"));
62         if(returnVal == JFileChooser.APPROVE_OPTION) {
63             temp = chooser.getSelectedFile();
64         }
65         
66         final File file = temp;
67         final ElementNode elementNode = (ElementNode) lookNode.getRepresentedObject();
68         
69         if (file != null) {
70             RequestProcessor.postRequest(new Runnable JavaDoc() {
71                 public void run() {
72                     tm.setStatusText (getLocalizedString("TXT_StartGenerate"));
73                     try {
74                         FileOutputStream fos = new FileOutputStream( file );
75                         new XMLGenerator ().generateXML (fos, elementNode);
76                         tm.setStatusText (getLocalizedString("TXT_FinishGeneration"));
77                     } catch (Exception JavaDoc e) {
78                         TopManager.getDefault().getErrorManager().notify (e);
79                     }
80                 } // run
81
});
82         } // if
83
}
84     
85     private static String JavaDoc getLocalizedString (String JavaDoc message) {
86         if (bundle == null)
87             bundle = NbBundle.getBundle (GenerateXMLAction.class);
88         return bundle.getString (message);
89     }
90
91     protected boolean enable (Node[] nodes) {
92         if (nodes.length != 1)
93             return false;
94         if (!(nodes[0] instanceof LookNode))
95             return false;
96         LookNode lookNode = (LookNode) nodes[0];
97         return lookNode.getRepresentedObject() instanceof ElementNode;
98     }
99
100     public String JavaDoc getName () {
101         return getLocalizedString ("TXT_GenerateAction");
102     }
103
104     protected String JavaDoc iconResource () {
105         return null;
106     }
107
108     public HelpCtx getHelpCtx () {
109         return HelpCtx.DEFAULT_HELP;
110     }
111
112     private static final class XMLFileFilter extends FileFilter JavaDoc {
113         public boolean accept(File f) {
114             return f.getName().toUpperCase().endsWith(".XML") || f.isDirectory(); // No I18N
115
}
116         
117         public String JavaDoc getDescription() {
118             return getLocalizedString ("TXT_XMLFiles");
119         }
120     }
121         
122 }
Popular Tags