1 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 ; 31 import java.lang.String ; 32 import org.tigris.subversion.svnclientadapter.*; 33 34 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 getBaseName(Node [] activatedNodes) { 47 int actionStatus = getActionStatus(activatedNodes); 48 switch (actionStatus) { 49 case UNDEFINED: 50 case IGNORING: 51 return "CTL_MenuItem_Ignore"; case UNIGNORING: 53 return "CTL_MenuItem_Unignore"; default: 55 throw new RuntimeException ("Invalid action status: " + actionStatus); } 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 [] 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")) { 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 ("Invalid action status: " + actionStatus); } 111 112 final File files[] = SvnUtils.getCurrentContext(nodes).getRootFiles(); 113 114 ContextAction.ProgressSupport support = new ContextAction.ProgressSupport(this, nodes) { 115 public void perform() { 116 Map<File , Set<String >> names = splitByParent(files); 117 SvnClient client = Subversion.getInstance().getClient(false); 120 for (File parent : names.keySet()) { 121 Set<String > patterns = names.get(parent); 122 if(isCanceled()) { 123 return; 124 } 125 try { 126 Set<String > currentPatterns = new HashSet<String >(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 >(currentPatterns)); 134 135 } catch (SVNClientException e) { 136 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 137 } 138 } 139 for (File file : files) { 141 Subversion.getInstance().getStatusCache().refresh(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN); 142 } 143 for (File 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 , Set<String >> splitByParent(File [] files) { 153 Map<File , Set<String >> map = new HashMap<File , Set<String >>(2); 154 for (File file : files) { 155 File parent = file.getParentFile(); 156 if (parent == null) continue; 157 Set<String > names = map.get(parent); 158 if (names == null) { 159 names = new HashSet<String >(5); 160 map.put(parent, names); 161 } 162 names.add(file.getName()); 163 } 164 return map; 165 } 166 167 173 private static void ensureVersioned(File 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 186 private static void add(File 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 file) throws SVNClientException { 197 synchronized(IgnoreAction.class) { 199 File parent = file.getParentFile(); 200 ensureVersioned(parent); 201 List<String > 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 |