1 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 ; 17 import java.io.FileWriter ; 18 import java.io.PrintWriter ; 19 import java.io.IOException ; 20 import java.util.Iterator ; 21 import javax.swing.JFileChooser ; 22 import javax.swing.filechooser.FileFilter ; 23 import xmlmodel.*; 24 28 public class GenerateTextAction extends NodeAction { 29 30 private static final String INDENT = " "; private static final String NL = "\n"; private static final String TXT_EXT = ".txt"; 34 35 public GenerateTextAction() { 36 } 37 38 39 public void performAction (Node[] nodes) { 40 xmlmodel.Node node = (xmlmodel.Node) ((LookNode)nodes[0]).getRepresentedObject(); 41 JFileChooser fc = new JFileChooser (); 42 fc.setFileFilter(new TextFileFilter ()); 43 if (fc.showOpenDialog(TopManager.getDefault().getWindowManager().getMainWindow()) == JFileChooser.APPROVE_OPTION) { 44 File file = fc.getSelectedFile (); 45 PrintWriter pw = null; 46 try { 47 pw = new PrintWriter ( new FileWriter (file)); 48 String result = processNode (node,0); 49 pw.println (result); 50 }catch (IOException 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 getName () { 72 return NbBundle.getMessage (GenerateTextAction.class,"TXT_GenerateTXT"); 73 } 74 75 76 private static String processNode (xmlmodel.Node node, int indent) { 77 String result = generateFill (indent) + node.getName () + NL; 78 if (node instanceof xmlmodel.ElementNode) { 79 for (Iterator 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 generateFill (int count) { 87 StringBuffer sb = new StringBuffer (); 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 { 95 96 public boolean accept (File file) { 97 return (file.isDirectory() || file.getName().toUpperCase().endsWith (TXT_EXT)); 98 } 99 100 public String getDescription () { 101 return NbBundle.getMessage (GenerateTextAction.class, "TXT_TXT"); 102 } 103 } 104 105 } 106 | Popular Tags |