KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > ignore > IgnoreAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.subversion.ui.ignore;
21
22 import java.util.*;
23 import org.netbeans.modules.subversion.*;
24 import org.netbeans.modules.subversion.client.SvnClient;
25 import org.netbeans.modules.subversion.ui.actions.*;
26 import org.netbeans.modules.subversion.util.*;
27 import org.openide.*;
28 import org.openide.nodes.Node;
29
30 import java.io.File JavaDoc;
31 import java.lang.String JavaDoc;
32 import org.tigris.subversion.svnclientadapter.*;
33
34 /**
35  * Adds/removes files to svn:ignore property.
36  * It does not support patterns.
37  *
38  * @author Maros Sandor
39  */

40 public class IgnoreAction extends ContextAction {
41     
42     public static final int UNDEFINED = 0;
43     public static final int IGNORING = 1;
44     public static final int UNIGNORING = 2;
45     
46     protected String JavaDoc getBaseName(Node [] activatedNodes) {
47         int actionStatus = getActionStatus(activatedNodes);
48         switch (actionStatus) {
49         case UNDEFINED:
50         case IGNORING:
51             return "CTL_MenuItem_Ignore"; // NOI18N
52
case UNIGNORING:
53             return "CTL_MenuItem_Unignore"; // NOI18N
54
default:
55             throw new RuntimeException JavaDoc("Invalid action status: " + actionStatus); // NOI18N
56
}
57     }
58
59     protected int getFileEnabledStatus() {
60         return FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY | FileInformation.STATUS_NOTVERSIONED_EXCLUDED;
61     }
62
63     protected int getDirectoryEnabledStatus() {
64         return FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY | FileInformation.STATUS_NOTVERSIONED_EXCLUDED;
65     }
66     
67     public int getActionStatus(Node [] nodes) {
68         return getActionStatus(SvnUtils.getCurrentContext(nodes).getFiles());
69     }
70
71     public int getActionStatus(File JavaDoc [] files) {
72         int actionStatus = -1;
73         if (files.length == 0) return UNDEFINED;
74         FileStatusCache cache = Subversion.getInstance().getStatusCache();
75         for (int i = 0; i < files.length; i++) {
76             if (files[i].getName().equals(".svn") || files[i].getName().equals("_svn")) { // NOI18N
77
actionStatus = UNDEFINED;
78                 break;
79             }
80             FileInformation info = cache.getStatus(files[i]);
81             if (info.getStatus() == FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY) {
82                 if (actionStatus == UNIGNORING) {
83                     actionStatus = UNDEFINED;
84                     break;
85                 }
86                 actionStatus = IGNORING;
87             } else if (info.getStatus() == FileInformation.STATUS_NOTVERSIONED_EXCLUDED) {
88                 if (actionStatus == IGNORING) {
89                     actionStatus = UNDEFINED;
90                     break;
91                 }
92                 actionStatus = UNIGNORING;
93             } else {
94                 actionStatus = UNDEFINED;
95                 break;
96             }
97         }
98         return actionStatus == -1 ? UNDEFINED : actionStatus;
99     }
100     
101     protected boolean enable(Node[] nodes) {
102         return getActionStatus(nodes) != UNDEFINED;
103     }
104
105     public void performContextAction(final Node[] nodes) {
106
107         final int actionStatus = getActionStatus(nodes);
108         if (actionStatus != IGNORING && actionStatus != UNIGNORING) {
109             throw new RuntimeException JavaDoc("Invalid action status: " + actionStatus); // NOI18N
110
}
111         
112         final File JavaDoc files[] = SvnUtils.getCurrentContext(nodes).getRootFiles();
113
114         ContextAction.ProgressSupport support = new ContextAction.ProgressSupport(this, nodes) {
115             public void perform() {
116                 Map<File JavaDoc, Set<String JavaDoc>> names = splitByParent(files);
117                 // do not attach onNotify listeners because the ignore command forcefully fires change events on ALL files
118
// in the parent directory and NONE of them interests us, see #89516
119
SvnClient client = Subversion.getInstance().getClient(false);
120                 for (File JavaDoc parent : names.keySet()) {
121                     Set<String JavaDoc> patterns = names.get(parent);
122                     if(isCanceled()) {
123                         return;
124                     }
125                     try {
126                         Set<String JavaDoc> currentPatterns = new HashSet<String JavaDoc>(client.getIgnoredPatterns(parent));
127                         if (actionStatus == IGNORING) {
128                             ensureVersioned(parent);
129                             currentPatterns.addAll(patterns);
130                         } else if (actionStatus == UNIGNORING) {
131                             currentPatterns.removeAll(patterns);
132                         }
133                         client.setIgnoredPatterns(parent, new ArrayList<String JavaDoc>(currentPatterns));
134                         
135                     } catch (SVNClientException e) {
136                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
137                     }
138                 }
139                 // refresh files manually, we do not suppport wildcards in ignore patterns so this is sufficient
140
for (File JavaDoc file : files) {
141                     Subversion.getInstance().getStatusCache().refresh(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN);
142                 }
143                 // refresh also the parents
144
for (File JavaDoc parent : names.keySet()) {
145                     Subversion.getInstance().getStatusCache().refresh(parent, FileStatusCache.REPOSITORY_STATUS_UNKNOWN);
146                 }
147             }
148         };
149         support.start(createRequestProcessor(nodes));
150     }
151
152     private Map<File JavaDoc, Set<String JavaDoc>> splitByParent(File JavaDoc[] files) {
153         Map<File JavaDoc, Set<String JavaDoc>> map = new HashMap<File JavaDoc, Set<String JavaDoc>>(2);
154         for (File JavaDoc file : files) {
155             File JavaDoc parent = file.getParentFile();
156             if (parent == null) continue;
157             Set<String JavaDoc> names = map.get(parent);
158             if (names == null) {
159                 names = new HashSet<String JavaDoc>(5);
160                 map.put(parent, names);
161             }
162             names.add(file.getName());
163         }
164         return map;
165     }
166     
167     /**
168      * Adds this file and all its parent folders to repository if they are not yet added.
169      *
170      * @param file file to add
171      * @throws SVNClientException if something goes wrong in subversion
172      */

173     private static void ensureVersioned(File JavaDoc file) throws SVNClientException {
174         FileStatusCache cache = Subversion.getInstance().getStatusCache();
175         if ((cache.getStatus(file).getStatus() & FileInformation.STATUS_VERSIONED) != 0) return;
176         ensureVersioned(file.getParentFile());
177         add(file);
178         cache.refresh(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN);
179     }
180
181     /**
182      * Adds the file to repository with 'svn add', non-recursively.
183      *
184      * @param file file to add
185      */

186     private static void add(File JavaDoc file) throws SVNClientException {
187         SVNUrl repositoryUrl = SvnUtils.getRepositoryRootUrl(file);
188         SvnClient client = Subversion.getInstance().getClient(repositoryUrl);
189         client.addFile(file);
190     }
191
192     protected boolean asynchronous() {
193         return false;
194     }
195
196     public static void ignore(File JavaDoc file) throws SVNClientException {
197         // technically, this block need not be synchronized but we want to have svn:ignore property set correctly at all times
198
synchronized(IgnoreAction.class) {
199             File JavaDoc parent = file.getParentFile();
200             ensureVersioned(parent);
201             List<String JavaDoc> patterns = Subversion.getInstance().getClient(true).getIgnoredPatterns(parent);
202             if (patterns.contains(file.getName()) == false) {
203                 patterns.add(file.getName());
204                 Subversion.getInstance().getClient(true).setIgnoredPatterns(parent, patterns);
205             }
206             Subversion.getInstance().getStatusCache().refresh(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN);
207         }
208     }
209 }
210
Popular Tags