Continued work to remove a bunch of Hg-related commands.
[nbgit.git] / src / org / netbeans / modules / git / ui / update / RevertModificationsAction.java
blob6b3ad69df6675d6ccfd01df050a687c16fe48ddc
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Contributor(s):
26 * The Original Software is NetBeans. The Initial Developer of the Original
27 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28 * Microsystems, Inc. All Rights Reserved.
29 * Portions Copyright 2008 Alexander Coles (Ikonoklastik Productions).
31 * If you wish your version of this file to be governed by only the CDDL
32 * or only the GPL Version 2, indicate your decision by adding
33 * "[Contributor] elects to include this software in this distribution
34 * under the [CDDL or GPL Version 2] license." If you do not indicate a
35 * single choice of license, a recipient has the option to distribute
36 * your version of this file under either the CDDL, the GPL Version 2 or
37 * to extend the choice of license to its licensees as provided above.
38 * However, if you add GPL Version 2 code and therefore, elected the GPL
39 * Version 2 license, then the option applies only if the new code is
40 * made subject to such option by the copyright holder.
42 package org.netbeans.modules.git.ui.update;
44 import java.awt.event.ActionEvent;
45 import java.io.File;
46 import java.util.ArrayList;
47 import java.util.List;
48 import java.util.Set;
49 import javax.swing.Action;
50 import org.netbeans.modules.git.FileInformation;
51 import org.netbeans.modules.git.FileStatusCache;
52 import org.netbeans.modules.git.Git;
53 import org.netbeans.modules.git.GitException;
54 import org.netbeans.modules.git.GitProgressSupport;
55 import org.netbeans.modules.git.OutputLogger;
56 import org.netbeans.modules.git.ui.actions.ContextAction;
57 import org.netbeans.modules.git.util.GitCommand;
58 import org.netbeans.modules.git.util.GitUtils;
59 import org.netbeans.modules.versioning.spi.VCSContext;
60 import org.openide.DialogDisplayer;
61 import org.openide.NotifyDescriptor;
62 import org.openide.filesystems.FileObject;
63 import org.openide.filesystems.FileUtil;
64 import org.openide.util.NbBundle;
65 import org.openide.util.RequestProcessor;
67 /**
68 * Reverts local changes.
70 * @author Padraig O'Briain
72 public class RevertModificationsAction extends ContextAction {
74 private final VCSContext context;
76 public RevertModificationsAction(String name, VCSContext context) {
77 this.context = context;
78 putValue(Action.NAME, name);
81 public void performAction(ActionEvent e) {
82 revert(context);
85 public static void revert(final VCSContext ctx) {
86 final File[] files = ctx.getRootFiles().toArray(new File[ctx.getRootFiles().size()]);
87 final File repository = GitUtils.getRootFile(ctx);
88 if (repository == null) return;
89 String rev = null;
91 final RevertModifications revertModifications = new RevertModifications(repository, files);
92 if (!revertModifications.showDialog()) {
93 return;
95 rev = revertModifications.getSelectionRevision();
96 final String revStr = rev;
97 final boolean doBackup = revertModifications.isBackupRequested();
99 RequestProcessor rp = Git.getInstance().getRequestProcessor(repository);
100 GitProgressSupport support = new GitProgressSupport() {
101 public void perform() {
102 performRevert(repository, revStr, files, doBackup, this.getLogger());
105 support.start(rp, repository.getAbsolutePath(), org.openide.util.NbBundle.getMessage(UpdateAction.class, "MSG_Revert_Progress")); // NOI18N
107 return;
110 public static void performRevert(File repository, String revStr, File file, boolean doBackup, OutputLogger logger) {
111 List<File> revertFiles = new ArrayList<File>();
112 revertFiles.add(file);
114 performRevert(repository, revStr, revertFiles, doBackup, logger);
117 public static void performRevert(File repository, String revStr, File[] files, boolean doBackup, OutputLogger logger) {
118 List<File> revertFiles = new ArrayList<File>();
119 for (File file : files) {
120 revertFiles.add(file);
122 performRevert(repository, revStr, revertFiles, doBackup, logger);
125 public static void performRevert(File repository, String revStr, List<File> revertFiles, boolean doBackup, OutputLogger logger) {
126 try{
127 logger.outputInRed(
128 NbBundle.getMessage(RevertModificationsAction.class,
129 "MSG_REVERT_TITLE")); // NOI18N
130 logger.outputInRed(
131 NbBundle.getMessage(RevertModificationsAction.class,
132 "MSG_REVERT_TITLE_SEP")); // NOI18N
134 // revStr == null => no -r REV in git revert command
135 // No revisions to revert too
136 if (revStr != null && NbBundle.getMessage(RevertModificationsAction.class,
137 "MSG_Revision_Default").startsWith(revStr)) {
138 logger.output(
139 NbBundle.getMessage(RevertModificationsAction.class,
140 "MSG_REVERT_NOTHING")); // NOI18N
141 logger.outputInRed(
142 NbBundle.getMessage(RevertModificationsAction.class,
143 "MSG_REVERT_DONE")); // NOI18N
144 logger.outputInRed(""); // NOI18N
145 return;
148 logger.output(
149 NbBundle.getMessage(RevertModificationsAction.class,
150 "MSG_REVERT_REVISION_STR", revStr)); // NOI18N
151 for (File file : revertFiles) {
152 logger.output(file.getAbsolutePath());
154 logger.output(""); // NOI18N
156 GitCommand.doCheckout(repository, revertFiles, revStr, doBackup, logger);
157 FileStatusCache cache = Git.getInstance().getFileStatusCache();
158 File[] conflictFiles = cache.listFiles(revertFiles.toArray(new File[0]), FileInformation.STATUS_VERSIONED_CONFLICT);
159 if (conflictFiles.length != 0) {
160 ConflictResolvedAction.conflictResolved(repository, conflictFiles);
162 } catch (GitException ex) {
163 NotifyDescriptor.Exception e = new NotifyDescriptor.Exception(ex);
164 DialogDisplayer.getDefault().notifyLater(e);
167 if (revStr == null) {
168 for (File file : revertFiles) {
169 GitUtils.forceStatusRefresh(file);
171 } else {
172 GitUtils.forceStatusRefresh(revertFiles.get(0));
175 // refresh filesystem to take account of changes
176 FileObject rootObj = FileUtil.toFileObject(repository);
177 try {
178 rootObj.getFileSystem().refresh(true);
179 } catch (java.lang.Exception exc) {
181 logger.outputInRed(
182 NbBundle.getMessage(RevertModificationsAction.class,
183 "MSG_REVERT_DONE")); // NOI18N
184 logger.outputInRed(""); // NOI18N
188 public boolean isEnabled() {
189 Set<File> ctxFiles = context != null? context.getRootFiles(): null;
190 if(GitUtils.getRootFile(context) == null || ctxFiles == null || ctxFiles.size() == 0)
191 return false;
192 return true;