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.commit;
043:
044: import javax.swing.AbstractAction;
045: import javax.swing.Action;
046: import java.awt.event.ActionEvent;
047: import java.io.*;
048: import java.util.Set;
049: import java.util.List;
050: import java.util.ArrayList;
051:
052: import org.netbeans.modules.mercurial.FileInformation;
053: import org.netbeans.modules.mercurial.HgModuleConfig;
054: import org.netbeans.modules.mercurial.HgProgressSupport;
055: import org.netbeans.modules.mercurial.Mercurial;
056: import org.netbeans.modules.mercurial.ui.actions.ContextAction;
057: import org.openide.util.RequestProcessor;
058: import org.netbeans.modules.versioning.spi.VCSContext;
059: import org.openide.nodes.*;
060:
061: /**
062: *
063: * @author Petr Kuzel
064: */
065: public final class ExcludeFromCommitAction extends ContextAction {
066:
067: public static final int UNDEFINED = -1;
068: public static final int EXCLUDING = 1;
069: public static final int INCLUDING = 2;
070:
071: private final VCSContext context;
072:
073: public ExcludeFromCommitAction(String name, VCSContext context) {
074: this .context = context;
075: putValue(Action.NAME, name);
076: }
077:
078: protected boolean enable(VCSContext ctx) {
079: return getActionStatus(ctx) != UNDEFINED;
080: }
081:
082: protected int getFileEnabledStatus() {
083: return FileInformation.STATUS_LOCAL_CHANGE;
084: }
085:
086: protected int getDirectoryEnabledStatus() {
087: return FileInformation.STATUS_LOCAL_CHANGE;
088: }
089:
090: protected String getBaseName(VCSContext ctx) {
091: int actionStatus = getActionStatus(ctx);
092: switch (actionStatus) {
093: case UNDEFINED:
094: case EXCLUDING:
095: return "popup_commit_exclude"; // NOI18N
096: case INCLUDING:
097: return "popup_commit_include"; // NOI18N
098: default:
099: throw new RuntimeException("Invalid action status: "
100: + actionStatus); // NOI18N
101: }
102: }
103:
104: public int getActionStatus(VCSContext ctx) {
105: HgModuleConfig config = HgModuleConfig.getDefault();
106: int status = UNDEFINED;
107: if (ctx == null)
108: ctx = context;
109: Set<File> files = ctx.getRootFiles();
110: for (File file : files) {
111: if (config.isExcludedFromCommit(file.getAbsolutePath())) {
112: if (status == EXCLUDING) {
113: return UNDEFINED;
114: }
115: status = INCLUDING;
116: } else {
117: if (status == INCLUDING) {
118: return UNDEFINED;
119: }
120: status = EXCLUDING;
121: }
122: }
123: return status;
124: }
125:
126: public void performAction(ActionEvent e) {
127: final VCSContext ctx = context;
128: RequestProcessor rp = Mercurial.getInstance()
129: .getRequestProcessor();
130: HgProgressSupport support = new HgProgressSupport() {
131: public void perform() {
132: HgModuleConfig config = HgModuleConfig.getDefault();
133: int status = getActionStatus(ctx);
134: Set<File> files = ctx.getRootFiles();
135: List<String> paths = new ArrayList<String>(files.size());
136: for (File file : files) {
137: paths.add(file.getAbsolutePath());
138: }
139: if (isCanceled())
140: return;
141: if (status == EXCLUDING) {
142: config.addExclusionPaths(paths);
143: } else if (status == INCLUDING) {
144: config.removeExclusionPaths(paths);
145: }
146: }
147: };
148: support.start(rp, "", ""); // NOI18N
149: }
150: }
|