1 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 ; 40 import javax.swing.filechooser.FileFilter ; 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 JFileChooser chooser = new JFileChooser (); 59 chooser.setFileFilter(new XMLFileFilter()); 60 java.awt.Component 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 () { 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 e) { 78 TopManager.getDefault().getErrorManager().notify (e); 79 } 80 } }); 82 } } 84 85 private static String getLocalizedString (String 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 getName () { 101 return getLocalizedString ("TXT_GenerateAction"); 102 } 103 104 protected String 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 { 113 public boolean accept(File f) { 114 return f.getName().toUpperCase().endsWith(".XML") || f.isDirectory(); } 116 117 public String getDescription() { 118 return getLocalizedString ("TXT_XMLFiles"); 119 } 120 } 121 122 } | Popular Tags |