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:
042: package org.netbeans.modules.mercurial.ui.update;
043:
044: import java.io.File;
045: import java.util.*;
046: import javax.swing.*;
047: import java.awt.event.ActionEvent;
048: import org.netbeans.modules.versioning.spi.VCSContext;
049: import org.netbeans.modules.mercurial.Mercurial;
050: import org.netbeans.modules.mercurial.OutputLogger;
051: import org.netbeans.modules.mercurial.FileStatusCache;
052: import org.netbeans.modules.mercurial.FileInformation;
053: import org.netbeans.modules.mercurial.util.HgUtils;
054: import org.netbeans.modules.mercurial.HgProgressSupport;
055: import org.netbeans.modules.mercurial.HgException;
056: import org.netbeans.modules.mercurial.ui.actions.ContextAction;
057: import org.openide.DialogDisplayer;
058: import org.openide.NotifyDescriptor;
059: import org.openide.util.RequestProcessor;
060: import org.openide.util.NbBundle;
061: import org.netbeans.modules.mercurial.util.HgCommand;
062: import org.openide.filesystems.FileObject;
063: import org.openide.filesystems.FileUtil;
064:
065: /**
066: * Reverts local changes.
067: *
068: * @author Padraig O'Briain
069: */
070: public class RevertModificationsAction extends ContextAction {
071:
072: private final VCSContext context;
073:
074: public RevertModificationsAction(String name, VCSContext context) {
075: this .context = context;
076: putValue(Action.NAME, name);
077: }
078:
079: public void performAction(ActionEvent e) {
080: revert(context);
081: }
082:
083: public static void revert(final VCSContext ctx) {
084: final File[] files = ctx.getRootFiles().toArray(
085: new File[ctx.getRootFiles().size()]);
086: final File repository = HgUtils.getRootFile(ctx);
087: if (repository == null)
088: return;
089: String rev = null;
090:
091: final RevertModifications revertModifications = new RevertModifications(
092: repository, files);
093: if (!revertModifications.showDialog()) {
094: return;
095: }
096: rev = revertModifications.getSelectionRevision();
097: final String revStr = rev;
098: final boolean doBackup = revertModifications
099: .isBackupRequested();
100:
101: RequestProcessor rp = Mercurial.getInstance()
102: .getRequestProcessor(repository);
103: HgProgressSupport support = new HgProgressSupport() {
104: public void perform() {
105: performRevert(repository, revStr, files, doBackup, this
106: .getLogger());
107: }
108: };
109: support.start(rp, repository.getAbsolutePath(),
110: org.openide.util.NbBundle.getMessage(
111: UpdateAction.class, "MSG_Revert_Progress")); // NOI18N
112:
113: return;
114: }
115:
116: public static void performRevert(File repository, String revStr,
117: File file, boolean doBackup, OutputLogger logger) {
118: List<File> revertFiles = new ArrayList<File>();
119: revertFiles.add(file);
120:
121: performRevert(repository, revStr, revertFiles, doBackup, logger);
122: }
123:
124: public static void performRevert(File repository, String revStr,
125: File[] files, boolean doBackup, OutputLogger logger) {
126: List<File> revertFiles = new ArrayList<File>();
127: for (File file : files) {
128: revertFiles.add(file);
129: }
130: performRevert(repository, revStr, revertFiles, doBackup, logger);
131: }
132:
133: public static void performRevert(File repository, String revStr,
134: List<File> revertFiles, boolean doBackup,
135: OutputLogger logger) {
136: try {
137: logger.outputInRed(NbBundle
138: .getMessage(RevertModificationsAction.class,
139: "MSG_REVERT_TITLE")); // NOI18N
140: logger.outputInRed(NbBundle.getMessage(
141: RevertModificationsAction.class,
142: "MSG_REVERT_TITLE_SEP")); // NOI18N
143:
144: // revStr == null => no -r REV in hg revert command
145: // No revisions to revert too
146: if (revStr != null
147: && NbBundle.getMessage(
148: RevertModificationsAction.class,
149: "MSG_Revision_Default").startsWith(revStr)) {
150: logger.output(NbBundle.getMessage(
151: RevertModificationsAction.class,
152: "MSG_REVERT_NOTHING")); // NOI18N
153: logger.outputInRed(NbBundle.getMessage(
154: RevertModificationsAction.class,
155: "MSG_REVERT_DONE")); // NOI18N
156: logger.outputInRed(""); // NOI18N
157: return;
158: }
159:
160: logger.output(NbBundle.getMessage(
161: RevertModificationsAction.class,
162: "MSG_REVERT_REVISION_STR", revStr)); // NOI18N
163: for (File file : revertFiles) {
164: logger.output(file.getAbsolutePath());
165: }
166: logger.output(""); // NOI18N
167:
168: HgCommand.doRevert(repository, revertFiles, revStr,
169: doBackup, logger);
170: FileStatusCache cache = Mercurial.getInstance()
171: .getFileStatusCache();
172: File[] conflictFiles = cache.listFiles(revertFiles
173: .toArray(new File[0]),
174: FileInformation.STATUS_VERSIONED_CONFLICT);
175: if (conflictFiles.length != 0) {
176: ConflictResolvedAction.conflictResolved(repository,
177: conflictFiles);
178: }
179: } catch (HgException ex) {
180: NotifyDescriptor.Exception e = new NotifyDescriptor.Exception(
181: ex);
182: DialogDisplayer.getDefault().notifyLater(e);
183: }
184:
185: if (revStr == null) {
186: for (File file : revertFiles) {
187: HgUtils.forceStatusRefresh(file);
188: }
189: } else {
190: HgUtils.forceStatusRefresh(revertFiles.get(0));
191: }
192:
193: // refresh filesystem to take account of changes
194: FileObject rootObj = FileUtil.toFileObject(repository);
195: try {
196: rootObj.getFileSystem().refresh(true);
197: } catch (java.lang.Exception exc) {
198: }
199: logger.outputInRed(NbBundle.getMessage(
200: RevertModificationsAction.class, "MSG_REVERT_DONE")); // NOI18N
201: logger.outputInRed(""); // NOI18N
202:
203: }
204:
205: public boolean isEnabled() {
206: Set<File> ctxFiles = context != null ? context.getRootFiles()
207: : null;
208: if (HgUtils.getRootFile(context) == null || ctxFiles == null
209: || ctxFiles.size() == 0)
210: return false;
211: return true;
212: }
213: }
|