KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > piaget > user > UserNodeAction


1 /*
2  * UserAction.java
3  *
4  * Created on June 8, 2005, 6:18 PM
5  *
6  * To change this template, choose Tools | Options and locate the template under
7  * the Source Creation and Management node. Right-click the template and choose
8  * Open. You can then make changes to the template in the Source Editor.
9  */

10
11 package org.netbeans.modules.piaget.user;
12 import java.io.File JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.FileOutputStream JavaDoc;
15 import java.nio.channels.FileChannel JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import javax.swing.AbstractAction JavaDoc;
18 import javax.swing.Action JavaDoc;
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 /**
26  *
27  * @author loicsegapelli
28  */

29 public class UserNodeAction extends AbstractAction JavaDoc {
30     
31     private static final String JavaDoc PARSED_DIR = "parsed";
32     private static final String JavaDoc ORIGINALS_DIR = "originals";
33     private static final String JavaDoc REP_DIR = "piaget";
34     private static final String JavaDoc SEP = System.getProperty("file.separator");
35     //UserDataNode oldNodeToDestroyThisBlah;
36

37     private static UserNodeAction instance;
38     
39     /** Creates a new instance of UserAction */
40     private UserNodeAction() {
41         //this.oldNodeToDestroyThisBlah = node;
42
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 JavaDoc clone() throws CloneNotSupportedException JavaDoc {
51         throw new CloneNotSupportedException JavaDoc();
52     }
53     
54     /**
55      * Invoked when an action occurs.
56      */

57     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
58         // .user(s) files selected: get the associated path(s)
59
Node[] n = WindowManager.getDefault().getRegistry().getActivatedNodes();
60         if(n==null || n.length<1) return;
61         
62         ArrayList JavaDoc list = new ArrayList JavaDoc();
63         for(int i=0; i<n.length; i++){
64             UserDataNode node = (UserDataNode)n[i].getLookup().lookup(UserDataNode.class);
65             String JavaDoc workPath = organizeFiles(node);
66             if(workPath == null) return;
67             String JavaDoc dirPath = Unzipper.unzipUserFile(workPath);
68             File JavaDoc f = new File JavaDoc(workPath);
69             f.delete();
70             
71             f = new File JavaDoc(dirPath);
72             Analyzer.analyze(f);
73             
74             
75             try{
76                 node.fireDestroy();
77             } catch(Exception JavaDoc ex){
78                 ex.printStackTrace(System.out);
79             }
80             
81             try{
82                 node.destroy();
83             } catch(Exception JavaDoc ex){
84                 ex.printStackTrace(System.out);
85             }
86         }
87         
88     }
89     
90     
91     private String JavaDoc organizeFiles(UserDataNode node){
92         String JavaDoc path = node.getPath();
93         String JavaDoc name = node.getFilename();
94         int index = path.lastIndexOf(name);
95         // directory where the file is located
96
String JavaDoc directory = path.substring(0,index);
97         
98         String JavaDoc parsedDir = directory + REP_DIR + SEP + PARSED_DIR;
99         String JavaDoc originalDir = directory + REP_DIR + SEP + ORIGINALS_DIR;
100         File JavaDoc original = new File JavaDoc(path);
101         
102         try{
103             copyFile(original, parsedDir, name);
104             copyFile(original, originalDir, name);
105         } catch(Exception JavaDoc ex){
106             ex.printStackTrace(System.out);
107             return null;
108         }
109         
110         return parsedDir + SEP + name;
111     }
112     
113     private void copyFile(File JavaDoc in, String JavaDoc dirpath, String JavaDoc filename) throws Exception JavaDoc {
114         File JavaDoc dir = new File JavaDoc(dirpath);
115         dir.mkdirs();
116         File JavaDoc out = new File JavaDoc(dirpath + SEP + filename);
117         FileChannel JavaDoc sourceChannel = new FileInputStream JavaDoc(in).getChannel();
118         FileChannel JavaDoc destinationChannel = new FileOutputStream JavaDoc(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