001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.localhistory.ui.actions;
042:
043: import org.netbeans.modules.localhistory.ui.view.ShowLocalHistoryAction;
044: import java.io.File;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.OutputStream;
048: import java.util.Set;
049: import java.util.logging.Level;
050: import org.netbeans.modules.localhistory.LocalHistory;
051: import org.netbeans.modules.localhistory.store.StoreEntry;
052: import org.netbeans.modules.versioning.spi.VCSContext;
053: import org.netbeans.modules.versioning.spi.VersioningSupport;
054: import org.openide.filesystems.FileAlreadyLockedException;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.nodes.Node;
058: import org.openide.util.HelpCtx;
059: import org.openide.util.NbBundle;
060: import org.openide.util.RequestProcessor;
061: import org.openide.util.actions.NodeAction;
062:
063: /**
064: * @author Tomas Stupka
065: */
066: public class RevertDeletedAction extends NodeAction {
067:
068: /** Creates a new instance of ShowLocalHistoryAction */
069: public RevertDeletedAction() {
070: setIcon(null);
071: putValue("noIconInMenu", Boolean.TRUE); // NOI18N
072: }
073:
074: protected void performAction(final Node[] activatedNodes) {
075:
076: RequestProcessor.getDefault().post(new Runnable() {
077: public void run() {
078: VCSContext ctx = VCSContext.forNodes(activatedNodes);
079: Set<File> rootSet = ctx.getRootFiles();
080: if (rootSet == null || rootSet.size() < 1) {
081: return;
082: }
083: for (File file : rootSet) {
084: if (VersioningSupport.isFlat(file)) {
085: revert(file);
086: } else {
087: revertRecursively(file);
088: }
089: }
090: }
091: });
092: }
093:
094: private void revertRecursively(File file) {
095: revert(file);
096: File[] files = file.listFiles();
097: for (File f : files) {
098: if (f.isDirectory()) {
099: revertRecursively(f);
100: }
101: }
102: }
103:
104: private void revert(File file) {
105: StoreEntry[] entries = LocalHistory.getInstance()
106: .getLocalHistoryStore().getDeletedFiles(file);
107: for (StoreEntry se : entries) {
108: revert(se);
109: }
110: }
111:
112: protected boolean enable(Node[] activatedNodes) {
113: VCSContext ctx = VCSContext.forNodes(activatedNodes);
114: Set<File> rootSet = ctx.getRootFiles();
115: if (rootSet == null || rootSet.size() < 1) {
116: return false;
117: }
118: for (File file : rootSet) {
119: if (file != null && !file.isDirectory()) {
120: return false;
121: }
122: }
123: return true;
124: }
125:
126: public String getName() {
127: return NbBundle.getMessage(this .getClass(),
128: "CTL_ShowRevertDeleted");
129: }
130:
131: public HelpCtx getHelpCtx() {
132: return new HelpCtx(ShowLocalHistoryAction.class);
133: }
134:
135: private static void revert(StoreEntry se) {
136: File file = se.getFile();
137: if (file.exists()) {
138: // created externaly?
139: if (file.isFile()) {
140: LocalHistory.LOG.warning("Skipping revert for file "
141: + file.getAbsolutePath()
142: + " which already exists.");
143: }
144: // fix history
145: // XXX create a new entry vs. fixing the entry timestamp and deleted flag?
146: LocalHistory.getInstance().getLocalHistoryStore()
147: .fileCreate(file, file.lastModified());
148: }
149: File storeFile = se.getStoreFile();
150:
151: InputStream is = null;
152: OutputStream os = null;
153: try {
154: if (!storeFile.isFile()) {
155: FileUtil.createFolder(file);
156: } else {
157: FileObject fo = FileUtil.createData(file);
158:
159: os = getOutputStream(fo);
160: is = se.getStoreFileInputStream();
161: FileUtil.copy(is, os);
162: }
163: } catch (Exception e) {
164: LocalHistory.LOG.log(Level.SEVERE, null, e);
165: return;
166: } finally {
167: try {
168: if (os != null) {
169: os.close();
170: }
171: if (is != null) {
172: is.close();
173: }
174: } catch (IOException e) {
175: }
176: }
177: }
178:
179: private static OutputStream getOutputStream(FileObject fo)
180: throws FileAlreadyLockedException, IOException,
181: InterruptedException {
182: int retry = 0;
183: while (true) {
184: try {
185: return fo.getOutputStream();
186: } catch (IOException ioe) {
187: retry++;
188: if (retry > 7) {
189: throw ioe;
190: }
191: Thread.sleep(retry * 30);
192: }
193: }
194: }
195:
196: }
|