1 10 11 package org.netbeans.modules.piaget.user; 12 import java.io.File ; 13 import java.io.FileInputStream ; 14 import java.io.FileOutputStream ; 15 import java.nio.channels.FileChannel ; 16 import java.util.ArrayList ; 17 import javax.swing.AbstractAction ; 18 import javax.swing.Action ; 19 import org.netbeans.modules.piaget.analyze.Analyzer; 20 import org.netbeans.modules.piaget.unzip.Unzipper; 21 import org.openide.nodes.Node; 22 import org.openide.windows.WindowManager; 23 24 25 29 public class UserNodeAction extends AbstractAction { 30 31 private static final String PARSED_DIR = "parsed"; 32 private static final String ORIGINALS_DIR = "originals"; 33 private static final String REP_DIR = "piaget"; 34 private static final String SEP = System.getProperty("file.separator"); 35 37 private static UserNodeAction instance; 38 39 40 private UserNodeAction() { 41 putValue(Action.NAME, "process"); 43 } 44 45 public static synchronized UserNodeAction getInstance(){ 46 if(instance==null) instance = new UserNodeAction(); 47 return instance; 48 } 49 50 public Object clone() throws CloneNotSupportedException { 51 throw new CloneNotSupportedException (); 52 } 53 54 57 public void actionPerformed(java.awt.event.ActionEvent e) { 58 Node[] n = WindowManager.getDefault().getRegistry().getActivatedNodes(); 60 if(n==null || n.length<1) return; 61 62 ArrayList list = new ArrayList (); 63 for(int i=0; i<n.length; i++){ 64 UserDataNode node = (UserDataNode)n[i].getLookup().lookup(UserDataNode.class); 65 String workPath = organizeFiles(node); 66 if(workPath == null) return; 67 String dirPath = Unzipper.unzipUserFile(workPath); 68 File f = new File (workPath); 69 f.delete(); 70 71 f = new File (dirPath); 72 Analyzer.analyze(f); 73 74 75 try{ 76 node.fireDestroy(); 77 } catch(Exception ex){ 78 ex.printStackTrace(System.out); 79 } 80 81 try{ 82 node.destroy(); 83 } catch(Exception ex){ 84 ex.printStackTrace(System.out); 85 } 86 } 87 88 } 89 90 91 private String organizeFiles(UserDataNode node){ 92 String path = node.getPath(); 93 String name = node.getFilename(); 94 int index = path.lastIndexOf(name); 95 String directory = path.substring(0,index); 97 98 String parsedDir = directory + REP_DIR + SEP + PARSED_DIR; 99 String originalDir = directory + REP_DIR + SEP + ORIGINALS_DIR; 100 File original = new File (path); 101 102 try{ 103 copyFile(original, parsedDir, name); 104 copyFile(original, originalDir, name); 105 } catch(Exception ex){ 106 ex.printStackTrace(System.out); 107 return null; 108 } 109 110 return parsedDir + SEP + name; 111 } 112 113 private void copyFile(File in, String dirpath, String filename) throws Exception { 114 File dir = new File (dirpath); 115 dir.mkdirs(); 116 File out = new File (dirpath + SEP + filename); 117 FileChannel sourceChannel = new FileInputStream (in).getChannel(); 118 FileChannel destinationChannel = new FileOutputStream (out).getChannel(); 119 sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel); 120 121 sourceChannel.close(); 122 destinationChannel.close(); 123 } 124 125 public boolean isEnabled() { 126 return true; 127 } 128 129 } 130 | Popular Tags |