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.status;
042:
043: import java.io.File;
044: import java.util.Map;
045: import java.util.Calendar;
046: import java.util.Collection;
047: import java.util.logging.Level;
048: import org.netbeans.modules.mercurial.FileInformation;
049: import org.netbeans.modules.mercurial.FileStatusCache;
050: import org.netbeans.modules.mercurial.HgException;
051: import org.netbeans.modules.mercurial.Mercurial;
052: import org.netbeans.modules.versioning.util.Utils;
053: import org.netbeans.modules.versioning.spi.VCSContext;
054: import javax.swing.*;
055: import java.awt.event.ActionEvent;
056: import org.netbeans.modules.mercurial.util.HgCommand;
057: import org.netbeans.modules.mercurial.HgProgressSupport;
058: import org.netbeans.modules.mercurial.util.HgUtils;
059: import org.netbeans.modules.mercurial.ui.actions.ContextAction;
060:
061: /**
062: * Status action for mercurial:
063: * hg status - show changed files in the working directory
064: *
065: * @author John Rice
066: */
067: public class StatusAction extends ContextAction {
068:
069: private final VCSContext context;
070:
071: public StatusAction(String name, VCSContext context) {
072: this .context = context;
073: putValue(Action.NAME, name);
074: }
075:
076: public void performAction(ActionEvent ev) {
077: File[] files = context.getRootFiles().toArray(
078: new File[context.getRootFiles().size()]);
079: if (files == null || files.length == 0)
080: return;
081:
082: final HgVersioningTopComponent stc = HgVersioningTopComponent
083: .findInstance();
084: stc.setContentTitle(Utils.getContextDisplayName(context));
085: stc.setContext(context);
086: stc.open();
087: stc.requestActive();
088: stc.performRefreshAction();
089: }
090:
091: public boolean isEnabled() {
092: return HgUtils.getRootFile(context) != null;
093: }
094:
095: /**
096: * Connects to repository and gets recent status.
097: */
098: public static void executeStatus(final VCSContext context,
099: HgProgressSupport support) {
100:
101: if (context == null || context.getRootFiles().size() == 0) {
102: return;
103: }
104: File repository = HgUtils.getRootFile(context);
105: if (repository == null) {
106: return;
107: }
108:
109: try {
110: FileStatusCache cache = Mercurial.getInstance()
111: .getFileStatusCache();
112: Calendar start = Calendar.getInstance();
113: cache.refreshCached(context);
114: Calendar end = Calendar.getInstance();
115: Mercurial.LOG.log(Level.FINE,
116: "executeStatus: refreshCached took {0} millisecs",
117: end.getTimeInMillis() - start.getTimeInMillis()); // NOI18N
118:
119: for (File root : context.getRootFiles()) {
120: if (support.isCanceled()) {
121: return;
122: }
123: if (root.isDirectory()) {
124: Map<File, FileInformation> interestingFiles;
125: interestingFiles = HgCommand.getInterestingStatus(
126: repository, root);
127: if (!interestingFiles.isEmpty()) {
128: Collection<File> files = interestingFiles
129: .keySet();
130:
131: Map<File, Map<File, FileInformation>> interestingDirs = HgUtils
132: .getInterestingDirs(interestingFiles,
133: files);
134:
135: start = Calendar.getInstance();
136: for (File file : files) {
137: if (support.isCanceled()) {
138: return;
139: }
140: FileInformation fi = interestingFiles
141: .get(file);
142:
143: cache.refreshFileStatus(file, fi,
144: interestingDirs.get(file
145: .isDirectory() ? file
146: : file.getParentFile()));
147: }
148: end = Calendar.getInstance();
149: Mercurial.LOG
150: .log(
151: Level.FINE,
152: "executeStatus: process interesting files took {0} millisecs",
153: end.getTimeInMillis()
154: - start
155: .getTimeInMillis()); // NOI18N
156: }
157: } else {
158: cache.refresh(root,
159: FileStatusCache.REPOSITORY_STATUS_UNKNOWN);
160: }
161: }
162: } catch (HgException ex) {
163: support.annotate(ex);
164: }
165: }
166: }
|