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.mercurial.ui.rollback;
042:
043: import org.netbeans.modules.versioning.spi.VCSContext;
044:
045: import javax.swing.*;
046: import java.awt.event.ActionEvent;
047: import java.io.File;
048: import java.util.List;
049: import org.netbeans.modules.mercurial.HgException;
050: import org.netbeans.modules.mercurial.HgProgressSupport;
051: import org.netbeans.modules.mercurial.Mercurial;
052: import org.netbeans.modules.mercurial.OutputLogger;
053: import org.netbeans.modules.mercurial.util.HgCommand;
054: import org.netbeans.modules.mercurial.util.HgUtils;
055: import org.netbeans.modules.mercurial.ui.actions.ContextAction;
056: import org.netbeans.modules.mercurial.ui.log.RepositoryRevision;
057: import org.netbeans.modules.mercurial.ui.merge.MergeAction;
058: import org.openide.util.NbBundle;
059: import org.openide.util.RequestProcessor;
060: import org.openide.DialogDisplayer;
061: import org.openide.NotifyDescriptor;
062: import org.openide.filesystems.FileObject;
063: import org.openide.filesystems.FileUtil;
064:
065: /**
066: * Pull action for mercurial:
067: * hg pull - pull changes from the specified source
068: *
069: * @author John Rice
070: */
071: public class BackoutAction extends ContextAction {
072:
073: private final VCSContext context;
074: private static final String HG_BACKOUT_REVISION_REPLACE = "\\{revision}";
075: public static final String HG_BACKOUT_REVISION = " {revision}";
076:
077: public BackoutAction(String name, VCSContext context) {
078: this .context = context;
079: putValue(Action.NAME, name);
080: }
081:
082: public void performAction(ActionEvent e) {
083: backout(context);
084: }
085:
086: public static void backout(final VCSContext ctx) {
087: backout(ctx, null);
088: }
089:
090: public static void backout(final RepositoryRevision repoRev) {
091: backout(null, repoRev);
092: }
093:
094: public static void backout(final VCSContext ctx,
095: final RepositoryRevision repoRev) {
096: final File root;
097: if (repoRev != null) {
098: if (repoRev.getRepositoryRootUrl() == null
099: || repoRev.getRepositoryRootUrl().equals("")) {
100: return;
101: }
102: root = new File(repoRev.getRepositoryRootUrl());
103: } else {
104: root = HgUtils.getRootFile(ctx);
105: }
106: if (root == null)
107: return;
108: final String repository = root.getAbsolutePath();
109:
110: String rev = null;
111: String commitMsg = null;
112:
113: final Backout backout = new Backout(root, repoRev);
114: if (!backout.showDialog()) {
115: return;
116: }
117: rev = backout.getSelectionRevision();
118: commitMsg = backout.getCommitMessage();
119: final boolean doMerge = false; // Now handling this using our own merge mechanism, not backout's
120: final String revStr = rev;
121: commitMsg = commitMsg.replaceAll(HG_BACKOUT_REVISION_REPLACE,
122: revStr); //NOI18N
123: final String commitMsgStr = commitMsg;
124:
125: RequestProcessor rp = Mercurial.getInstance()
126: .getRequestProcessor(repository);
127: HgProgressSupport support = new HgProgressSupport() {
128: public void perform() {
129:
130: OutputLogger logger = getLogger();
131: try {
132: logger.outputInRed(NbBundle.getMessage(
133: BackoutAction.class, "MSG_BACKOUT_TITLE")); // NOI18N
134: logger.outputInRed(NbBundle.getMessage(
135: BackoutAction.class,
136: "MSG_BACKOUT_TITLE_SEP")); // NOI18N
137: logger.output(NbBundle.getMessage(
138: BackoutAction.class,
139: "MSG_BACKOUT_INFO_SEP", revStr, root
140: .getAbsolutePath())); // NOI18N
141: List<String> list = HgCommand.doBackout(root,
142: revStr, doMerge, commitMsgStr, logger);
143:
144: if (list != null && !list.isEmpty()) {
145: boolean bMergeNeededDueToBackout = HgCommand
146: .isBackoutMergeNeededMsg(list.get(list
147: .size() - 1));
148: if (bMergeNeededDueToBackout) {
149: list.remove(list.size() - 1);
150: list.remove(list.size() - 1);
151: }
152: logger.output(list);
153:
154: if (HgCommand.isUncommittedChangesBackout(list
155: .get(0))) {
156: logger.outputInRed(NbBundle.getMessage(
157: BackoutAction.class,
158: "MSG_UNCOMMITTED_CHANGES_BACKOUT")); // NOI18N
159: return;
160: } else if (HgCommand
161: .isMergeChangesetBackout(list.get(0))) {
162: logger.outputInRed(NbBundle.getMessage(
163: BackoutAction.class,
164: "MSG_MERGE_CSET_BACKOUT", revStr)); // NOI18N
165: return;
166: } else if (HgCommand.isNoRevStrip(list.get(0))) {
167: logger.outputInRed(NbBundle.getMessage(
168: BackoutAction.class,
169: "MSG_NO_REV_BACKOUT", revStr)); // NOI18N
170: return;
171: }
172:
173: // Handle Merge - both automatic and merge with conflicts
174: boolean bConfirmMerge = false;
175: if (bMergeNeededDueToBackout) {
176: bConfirmMerge = HgUtils.confirmDialog(
177: BackoutAction.class,
178: "MSG_BACKOUT_MERGE_CONFIRM_TITLE",
179: "MSG_BACKOUT_MERGE_CONFIRM_QUERY"); // NOI18N
180: }
181: if (bConfirmMerge) {
182: logger.output(""); // NOI18N
183: logger.outputInRed(NbBundle.getMessage(
184: BackoutAction.class,
185: "MSG_BACKOUT_MERGE_DO")); // NOI18N
186: MergeAction.doMergeAction(root, null,
187: logger);
188: } else {
189: List<String> headRevList = HgCommand
190: .getHeadRevisions(root);
191: if (headRevList != null
192: && headRevList.size() > 1) {
193: MergeAction.printMergeWarning(
194: headRevList, logger);
195: }
196: }
197: if (ctx != null) {
198: HgUtils.forceStatusRefreshProject(ctx);
199: } else if (repoRev != null) {
200: HgUtils.forceStatusRefresh(root);
201: }
202: // refresh filesystem to take account of deleted files.
203: FileObject rootObj = FileUtil
204: .toFileObject(root);
205: try {
206: rootObj.getFileSystem().refresh(true);
207: } catch (java.lang.Exception ex) {
208: }
209: }
210: } catch (HgException ex) {
211: NotifyDescriptor.Exception e = new NotifyDescriptor.Exception(
212: ex);
213: DialogDisplayer.getDefault().notifyLater(e);
214: } finally {
215: logger.outputInRed(NbBundle.getMessage(
216: BackoutAction.class, "MSG_BACKOUT_DONE")); // NOI18N
217: logger.output(""); // NOI18N
218: }
219: }
220: };
221: support.start(rp, repository,
222: org.openide.util.NbBundle.getMessage(
223: BackoutAction.class, "MSG_BACKOUT_PROGRESS")); // NOI18N
224: }
225:
226: public boolean isEnabled() {
227: return HgUtils.getRootFile(context) != null;
228: }
229: }
|