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.update;
042:
043: import java.io.File;
044: import java.util.List;
045: import org.netbeans.modules.mercurial.HgException;
046: import org.netbeans.modules.mercurial.HgProgressSupport;
047: import org.netbeans.modules.mercurial.Mercurial;
048: import org.netbeans.modules.mercurial.OutputLogger;
049: import org.netbeans.modules.mercurial.util.HgUtils;
050: import org.netbeans.modules.mercurial.ui.actions.ContextAction;
051: import org.netbeans.modules.versioning.spi.VCSContext;
052: import javax.swing.*;
053: import java.awt.event.ActionEvent;
054: import org.netbeans.modules.mercurial.util.HgCommand;
055: import org.openide.util.RequestProcessor;
056: import org.openide.DialogDisplayer;
057: import org.openide.NotifyDescriptor;
058: import org.openide.util.NbBundle;
059: import org.openide.filesystems.FileObject;
060: import org.openide.filesystems.FileUtil;
061:
062: /**
063: * Update action for mercurial:
064: * hg update - update or merge working directory
065: *
066: * @author John Rice
067: */
068: public class UpdateAction extends ContextAction {
069:
070: private final VCSContext context;
071:
072: public UpdateAction(String name, VCSContext context) {
073: this .context = context;
074: putValue(Action.NAME, name);
075: }
076:
077: public void performAction(ActionEvent e) {
078: update(context);
079: }
080:
081: public static void update(final VCSContext ctx) {
082: final File root = HgUtils.getRootFile(ctx);
083: if (root == null)
084: return;
085: String repository = root.getAbsolutePath();
086: String rev = null;
087:
088: final Update update = new Update(root);
089: if (!update.showDialog()) {
090: return;
091: }
092: rev = update.getSelectionRevision();
093: final boolean doForcedUpdate = update.isForcedUpdateRequested();
094: final String revStr = rev;
095:
096: RequestProcessor rp = Mercurial.getInstance()
097: .getRequestProcessor(repository);
098: HgProgressSupport support = new HgProgressSupport() {
099: public void perform() {
100: boolean bNoUpdates = true;
101: OutputLogger logger = getLogger();
102: try {
103: logger.outputInRed(NbBundle.getMessage(
104: UpdateAction.class, "MSG_UPDATE_TITLE")); // NOI18N
105: logger
106: .outputInRed(NbBundle.getMessage(
107: UpdateAction.class,
108: "MSG_UPDATE_TITLE_SEP")); // NOI18N
109: logger.output(NbBundle.getMessage(
110: UpdateAction.class, "MSG_UPDATE_INFO_SEP",
111: revStr, root.getAbsolutePath())); // NOI18N
112: List<String> list = HgCommand.doUpdateAll(root,
113: doForcedUpdate, revStr);
114:
115: if (list != null && !list.isEmpty()) {
116: bNoUpdates = HgCommand.isNoUpdates(list.get(0));
117: //logger.clearOutput();
118: logger.output(list);
119: logger.output(""); // NOI18N
120: }
121: // refresh filesystem to take account of changes
122: FileObject rootObj = FileUtil.toFileObject(root);
123: try {
124: rootObj.getFileSystem().refresh(true);
125: } catch (Exception ex) {
126: }
127:
128: } catch (HgException ex) {
129: NotifyDescriptor.Exception e = new NotifyDescriptor.Exception(
130: ex);
131: DialogDisplayer.getDefault().notifyLater(e);
132: }
133:
134: // Force Status Refresh from this dir and below
135: if (!bNoUpdates)
136: HgUtils.forceStatusRefreshProject(ctx);
137:
138: logger.outputInRed(NbBundle.getMessage(
139: UpdateAction.class, "MSG_UPDATE_DONE")); // NOI18N
140: logger.output(""); // NOI18N
141: }
142: };
143: support.start(rp, repository, org.openide.util.NbBundle
144: .getMessage(UpdateAction.class, "MSG_Update_Progress")); // NOI18N
145: }
146:
147: public boolean isEnabled() {
148: return HgUtils.getRootFile(context) != null;
149: }
150: }
|