2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
26 * The Original Software is NetBeans. The Initial Developer of the Original
27 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28 * Microsystems, Inc. All Rights Reserved.
29 * Portions Copyright 2008 Alexander Coles (Ikonoklastik Productions).
31 * If you wish your version of this file to be governed by only the CDDL
32 * or only the GPL Version 2, indicate your decision by adding
33 * "[Contributor] elects to include this software in this distribution
34 * under the [CDDL or GPL Version 2] license." If you do not indicate a
35 * single choice of license, a recipient has the option to distribute
36 * your version of this file under either the CDDL, the GPL Version 2 or
37 * to extend the choice of license to its licensees as provided above.
38 * However, if you add GPL Version 2 code and therefore, elected the GPL
39 * Version 2 license, then the option applies only if the new code is
40 * made subject to such option by the copyright holder.
42 package org
.netbeans
.modules
.git
.ui
.ignore
;
45 import java
.awt
.event
.ActionEvent
;
47 import java
.io
.IOException
;
49 import java
.util
.logging
.Level
;
50 import javax
.swing
.Action
;
51 import org
.netbeans
.api
.queries
.SharabilityQuery
;
52 import org
.netbeans
.modules
.git
.FileInformation
;
53 import org
.netbeans
.modules
.git
.FileStatusCache
;
54 import org
.netbeans
.modules
.git
.Git
;
55 import org
.netbeans
.modules
.git
.GitProgressSupport
;
56 import org
.netbeans
.modules
.git
.OutputLogger
;
57 import org
.netbeans
.modules
.git
.ui
.actions
.ContextAction
;
58 import org
.netbeans
.modules
.git
.util
.GitUtils
;
59 import org
.netbeans
.modules
.versioning
.spi
.VCSContext
;
60 import org
.openide
.util
.NbBundle
;
61 import org
.openide
.util
.RequestProcessor
;
65 * Adds/removes files to repository .gitignore.
67 * @author Maros Sandor
69 public class IgnoreAction
extends ContextAction
{
71 private final VCSContext context
;
72 private int mActionStatus
;
73 public static final int UNDEFINED
= 0;
74 public static final int IGNORING
= 1;
75 public static final int UNIGNORING
= 2;
77 public IgnoreAction(String name
, VCSContext context
) {
78 this.context
= context
;
79 putValue(Action
.NAME
, name
);
82 protected int getFileEnabledStatus() {
83 return FileInformation
.STATUS_NOTVERSIONED_NEWLOCALLY
| FileInformation
.STATUS_NOTVERSIONED_EXCLUDED
;
86 protected int getDirectoryEnabledStatus() {
87 return FileInformation
.STATUS_NOTVERSIONED_NEWLOCALLY
| FileInformation
.STATUS_NOTVERSIONED_EXCLUDED
;
90 public int getActionStatus(File
[] files
) {
91 int actionStatus
= -1;
92 if (files
.length
== 0) return UNDEFINED
;
93 FileStatusCache cache
= Git
.getInstance().getFileStatusCache();
94 for (int i
= 0; i
< files
.length
; i
++) {
95 if (files
[i
].getName().equals(".git") || // NOI18N
96 files
[i
].isDirectory() ||
97 SharabilityQuery
.getSharability(files
[i
])== SharabilityQuery
.NOT_SHARABLE
) {
98 actionStatus
= UNDEFINED
;
101 FileInformation info
= cache
.getStatus(files
[i
]);
102 if (info
.getStatus() == FileInformation
.STATUS_NOTVERSIONED_NEWLOCALLY
) {
103 if (actionStatus
== UNIGNORING
) {
104 actionStatus
= UNDEFINED
;
107 actionStatus
= IGNORING
;
108 } else if (info
.getStatus() == FileInformation
.STATUS_NOTVERSIONED_EXCLUDED
) {
109 if (actionStatus
== IGNORING
) {
110 actionStatus
= UNDEFINED
;
113 actionStatus
= UNIGNORING
;
115 actionStatus
= UNDEFINED
;
119 return actionStatus
== -1 ? UNDEFINED
: actionStatus
;
122 public boolean isEnabled() {
123 Set
<File
> ctxFiles
= context
!= null? context
.getRootFiles(): null;
124 final File repository
= GitUtils
.getRootFile(context
);
125 if(repository
== null || ctxFiles
== null || ctxFiles
.size() == 0)
130 public void performAction(ActionEvent e
) {
131 final File repository
= GitUtils
.getRootFile(context
);
132 if(repository
== null) return;
133 Set
<File
> ctxFiles
= context
!= null? context
.getRootFiles(): null;
134 if(ctxFiles
== null || ctxFiles
.size() == 0) return;
135 final File
[] files
= ctxFiles
.toArray(new File
[context
.getRootFiles().size()]);
137 RequestProcessor rp
= Git
.getInstance().getRequestProcessor(repository
.getAbsolutePath());
138 GitProgressSupport support
= new GitProgressSupport() {
139 public void perform() {
140 OutputLogger logger
= getLogger();
142 mActionStatus
= getActionStatus(files
);
143 if (mActionStatus
== UNDEFINED
) {
145 NbBundle
.getMessage(IgnoreAction
.class, "MSG_IGNORE_TITLE")); // NOI18N
147 NbBundle
.getMessage(IgnoreAction
.class, "MSG_IGNORE_TITLE_SEP")); // NOI18N
149 NbBundle
.getMessage(IgnoreAction
.class, "MSG_IGNORE_ONLY_LOCALLY_NEW")); // NOI18N
151 NbBundle
.getMessage(IgnoreAction
.class, "MSG_IGNORE_DONE")); // NOI18N
152 logger
.output(""); // NOI18N
156 if (mActionStatus
== IGNORING
) {
157 GitUtils
.addIgnored(repository
, files
);
159 NbBundle
.getMessage(IgnoreAction
.class,
160 "MSG_IGNORE_TITLE")); // NOI18N
162 NbBundle
.getMessage(IgnoreAction
.class,
163 "MSG_IGNORE_TITLE_SEP")); // NOI18N
165 NbBundle
.getMessage(IgnoreAction
.class,
166 "MSG_IGNORE_INIT_SEP", repository
.getName())); // NOI18N
168 GitUtils
.removeIgnored(repository
, files
);
170 NbBundle
.getMessage(IgnoreAction
.class,
171 "MSG_UNIGNORE_TITLE")); // NOI18N
173 NbBundle
.getMessage(IgnoreAction
.class,
174 "MSG_UNIGNORE_TITLE_SEP")); // NOI18N
176 NbBundle
.getMessage(IgnoreAction
.class,
177 "MSG_UNIGNORE_INIT_SEP", repository
.getName())); // NOI18N
179 } catch (IOException ex
) {
180 Git
.LOG
.log(Level
.FINE
, "IgnoreAction(): File {0} - {1}", // NOI18N
181 new Object
[] {repository
.getAbsolutePath(), ex
.toString()});
183 // refresh files manually
184 for (File file
: files
) {
185 Git
.getInstance().getFileStatusCache().refresh(file
, FileStatusCache
.REPOSITORY_STATUS_UNKNOWN
);
186 logger
.output("\t" + file
.getAbsolutePath()); // NOI18N
188 if (mActionStatus
== IGNORING
) {
190 NbBundle
.getMessage(IgnoreAction
.class,
191 "MSG_IGNORE_DONE")); // NOI18N
194 NbBundle
.getMessage(IgnoreAction
.class,
195 "MSG_UNIGNORE_DONE")); // NOI18N
197 logger
.output(""); // NOI18N
200 support
.start(rp
, repository
.getAbsolutePath(), org
.openide
.util
.NbBundle
.getMessage(IgnoreAction
.class, "LBL_Ignore_Progress")); // NOI18N