KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > localhistory > ui > actions > RevertDeletedAction


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.localhistory.ui.actions;
20
21 import org.netbeans.modules.localhistory.ui.view.ShowLocalHistoryAction;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.util.Set JavaDoc;
27 import org.netbeans.modules.localhistory.LocalHistory;
28 import org.netbeans.modules.localhistory.store.StoreEntry;
29 import org.netbeans.modules.versioning.spi.VCSContext;
30 import org.netbeans.modules.versioning.util.FlatFolder;
31 import org.openide.ErrorManager;
32 import org.openide.filesystems.FileAlreadyLockedException;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.nodes.Node;
36 import org.openide.util.HelpCtx;
37 import org.openide.util.NbBundle;
38 import org.openide.util.RequestProcessor;
39 import org.openide.util.actions.NodeAction;
40
41 /**
42  * @author Tomas Stupka
43  */

44 public class RevertDeletedAction extends NodeAction {
45     
46     /** Creates a new instance of ShowLocalHistoryAction */
47     public RevertDeletedAction() {
48         setIcon(null);
49         putValue("noIconInMenu", Boolean.TRUE); // NOI18N
50
}
51     
52     protected void performAction(final Node[] activatedNodes) {
53                                
54         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
55             public void run() {
56                 VCSContext ctx = VCSContext.forNodes(activatedNodes);
57                 Set JavaDoc<File JavaDoc> rootSet = ctx.getRootFiles();
58                 if(rootSet == null || rootSet.size() < 1) {
59                     return;
60                 }
61                 for (File JavaDoc file : rootSet) {
62                     if(file instanceof FlatFolder) {
63                         revert(file);
64                     } else {
65                         revertRecursively(file);
66                     }
67                 }
68             }
69         });
70     }
71
72     private void revertRecursively(File JavaDoc file) {
73         revert(file);
74         File JavaDoc[] files = file.listFiles();
75         for(File JavaDoc f : files) {
76             if(f.isDirectory()) {
77                 revertRecursively(f);
78             }
79         }
80     }
81     
82     private void revert(File JavaDoc file) {
83         StoreEntry[] entries = LocalHistory.getInstance().getLocalHistoryStore().getDeletedFiles(file);
84         for(StoreEntry se : entries) {
85             revert(se);
86         }
87     }
88     
89     protected boolean enable(Node[] activatedNodes) {
90         VCSContext ctx = VCSContext.forNodes(activatedNodes);
91         Set JavaDoc<File JavaDoc> rootSet = ctx.getRootFiles();
92         if(rootSet == null || rootSet.size() < 1) {
93             return false;
94         }
95         for (File JavaDoc file : rootSet) {
96             if(file != null && !file.isDirectory()) {
97                 return false;
98             }
99         }
100         return true;
101     }
102     
103     public String JavaDoc getName() {
104         return NbBundle.getMessage(this.getClass(), "CTL_ShowRevertDeleted");
105     }
106     
107     public HelpCtx getHelpCtx() {
108         return new HelpCtx(ShowLocalHistoryAction.class);
109     }
110
111     private static void revert(StoreEntry se) {
112         File JavaDoc file = se.getFile();
113         File JavaDoc storeFile = se.getStoreFile();
114                 
115         InputStream JavaDoc is = null;
116         OutputStream JavaDoc os = null;
117         try {
118             if(!storeFile.isFile()) {
119                 FileUtil.createFolder(file);
120             } else {
121                 FileObject fo = FileUtil.createData(file);
122
123                 os = getOutputStream(fo);
124                 is = se.getStoreFileInputStream();
125                 FileUtil.copy(is, os);
126             }
127         } catch (Exception JavaDoc e) {
128             ErrorManager.getDefault().notify(ErrorManager.ERROR, e);
129             return;
130         } finally {
131             try {
132                 if(os != null) { os.close(); }
133                 if(is != null) { is.close(); }
134             } catch (IOException JavaDoc e) {}
135         }
136     }
137     
138     private static OutputStream JavaDoc getOutputStream(FileObject fo) throws FileAlreadyLockedException, IOException JavaDoc, InterruptedException JavaDoc {
139         int retry = 0;
140         while (true) {
141             try {
142                 return fo.getOutputStream();
143             } catch (IOException JavaDoc ioe) {
144                 retry++;
145                 if (retry > 7) {
146                     throw ioe;
147                 }
148                 Thread.sleep(retry * 30);
149             }
150         }
151     }
152     
153 }
154
Popular Tags