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.ignore;
043:
044: import java.util.*;
045: import java.util.logging.Level;
046: import org.netbeans.modules.mercurial.*;
047: import org.netbeans.modules.mercurial.ui.actions.ContextAction;
048: import org.netbeans.modules.mercurial.util.HgUtils;
049: import org.netbeans.modules.versioning.spi.VCSContext;
050: import org.openide.*;
051: import org.openide.util.RequestProcessor;
052: import org.openide.util.NbBundle;
053: import java.io.File;
054: import java.io.IOException;
055: import java.awt.event.ActionEvent;
056: import javax.swing.*;
057: import org.netbeans.api.project.ProjectManager;
058: import org.netbeans.api.queries.SharabilityQuery;
059: import org.openide.filesystems.FileUtil;
060:
061: /**
062: * Adds/removes files to repository .hgignore.
063: *
064: * @author Maros Sandor
065: */
066: public class IgnoreAction extends ContextAction {
067:
068: private final VCSContext context;
069: private int mActionStatus;
070: public static final int UNDEFINED = 0;
071: public static final int IGNORING = 1;
072: public static final int UNIGNORING = 2;
073:
074: public IgnoreAction(String name, VCSContext context) {
075: this .context = context;
076: putValue(Action.NAME, name);
077: }
078:
079: protected int getFileEnabledStatus() {
080: return FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY
081: | FileInformation.STATUS_NOTVERSIONED_EXCLUDED;
082: }
083:
084: protected int getDirectoryEnabledStatus() {
085: return FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY
086: | FileInformation.STATUS_NOTVERSIONED_EXCLUDED;
087: }
088:
089: public int getActionStatus(File[] files) {
090: int actionStatus = -1;
091: if (files.length == 0)
092: return UNDEFINED;
093: FileStatusCache cache = Mercurial.getInstance()
094: .getFileStatusCache();
095: for (int i = 0; i < files.length; i++) {
096: if (files[i].getName().equals(".hg")
097: || // NOI18N
098: files[i].isDirectory()
099: || SharabilityQuery.getSharability(files[i]) == SharabilityQuery.NOT_SHARABLE) {
100: actionStatus = UNDEFINED;
101: break;
102: }
103: FileInformation info = cache.getStatus(files[i]);
104: if (info.getStatus() == FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY) {
105: if (actionStatus == UNIGNORING) {
106: actionStatus = UNDEFINED;
107: break;
108: }
109: actionStatus = IGNORING;
110: } else if (info.getStatus() == FileInformation.STATUS_NOTVERSIONED_EXCLUDED) {
111: if (actionStatus == IGNORING) {
112: actionStatus = UNDEFINED;
113: break;
114: }
115: actionStatus = UNIGNORING;
116: } else {
117: actionStatus = UNDEFINED;
118: break;
119: }
120: }
121: return actionStatus == -1 ? UNDEFINED : actionStatus;
122: }
123:
124: public boolean isEnabled() {
125: Set<File> ctxFiles = context != null ? context.getRootFiles()
126: : null;
127: final File repository = HgUtils.getRootFile(context);
128: if (repository == null || ctxFiles == null
129: || ctxFiles.size() == 0)
130: return false;
131: return true;
132: }
133:
134: public void performAction(ActionEvent e) {
135: final File repository = HgUtils.getRootFile(context);
136: if (repository == null)
137: return;
138: Set<File> ctxFiles = context != null ? context.getRootFiles()
139: : null;
140: if (ctxFiles == null || ctxFiles.size() == 0)
141: return;
142: final File[] files = ctxFiles.toArray(new File[context
143: .getRootFiles().size()]);
144:
145: RequestProcessor rp = Mercurial.getInstance()
146: .getRequestProcessor(repository.getAbsolutePath());
147: HgProgressSupport support = new HgProgressSupport() {
148: public void perform() {
149: OutputLogger logger = getLogger();
150: try {
151: mActionStatus = getActionStatus(files);
152: if (mActionStatus == UNDEFINED) {
153: logger
154: .outputInRed(NbBundle.getMessage(
155: IgnoreAction.class,
156: "MSG_IGNORE_TITLE")); // NOI18N
157: logger.outputInRed(NbBundle.getMessage(
158: IgnoreAction.class,
159: "MSG_IGNORE_TITLE_SEP")); // NOI18N
160: logger.output(NbBundle.getMessage(
161: IgnoreAction.class,
162: "MSG_IGNORE_ONLY_LOCALLY_NEW")); // NOI18N
163: logger.outputInRed(NbBundle.getMessage(
164: IgnoreAction.class, "MSG_IGNORE_DONE")); // NOI18N
165: logger.output(""); // NOI18N
166: return;
167: }
168:
169: if (mActionStatus == IGNORING) {
170: HgUtils.addIgnored(repository, files);
171: logger
172: .outputInRed(NbBundle.getMessage(
173: IgnoreAction.class,
174: "MSG_IGNORE_TITLE")); // NOI18N
175: logger.outputInRed(NbBundle.getMessage(
176: IgnoreAction.class,
177: "MSG_IGNORE_TITLE_SEP")); // NOI18N
178: logger.output(NbBundle.getMessage(
179: IgnoreAction.class,
180: "MSG_IGNORE_INIT_SEP", repository
181: .getName())); // NOI18N
182: } else {
183: HgUtils.removeIgnored(repository, files);
184: logger.outputInRed(NbBundle.getMessage(
185: IgnoreAction.class,
186: "MSG_UNIGNORE_TITLE")); // NOI18N
187: logger.outputInRed(NbBundle.getMessage(
188: IgnoreAction.class,
189: "MSG_UNIGNORE_TITLE_SEP")); // NOI18N
190: logger.output(NbBundle.getMessage(
191: IgnoreAction.class,
192: "MSG_UNIGNORE_INIT_SEP", repository
193: .getName())); // NOI18N
194: }
195: } catch (IOException ex) {
196: Mercurial.LOG.log(Level.FINE,
197: "IgnoreAction(): File {0} - {1}", // NOI18N
198: new Object[] {
199: repository.getAbsolutePath(),
200: ex.toString() });
201: }
202: // refresh files manually
203: for (File file : files) {
204: Mercurial
205: .getInstance()
206: .getFileStatusCache()
207: .refresh(
208: file,
209: FileStatusCache.REPOSITORY_STATUS_UNKNOWN);
210: logger.output("\t" + file.getAbsolutePath()); // NOI18N
211: }
212: if (mActionStatus == IGNORING) {
213: logger.outputInRed(NbBundle.getMessage(
214: IgnoreAction.class, "MSG_IGNORE_DONE")); // NOI18N
215: } else {
216: logger.outputInRed(NbBundle.getMessage(
217: IgnoreAction.class, "MSG_UNIGNORE_DONE")); // NOI18N
218: }
219: logger.output(""); // NOI18N
220: }
221: };
222: support.start(rp, repository.getAbsolutePath(),
223: org.openide.util.NbBundle.getMessage(
224: IgnoreAction.class, "LBL_Ignore_Progress")); // NOI18N
225: }
226: }
|