1 11 package org.eclipse.team.internal.core.subscribers; 12 13 import java.util.*; 14 15 import org.eclipse.core.resources.*; 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.team.core.diff.IDiff; 18 import org.eclipse.team.core.mapping.provider.ResourceDiffTree; 19 import org.eclipse.team.internal.core.TeamPlugin; 20 import org.osgi.service.prefs.Preferences; 21 22 27 public class ActiveChangeSet extends DiffChangeSet { 28 29 private static final String CTX_TITLE = "title"; private static final String CTX_COMMENT = "comment"; private static final String CTX_RESOURCES = "resources"; private static final String CTX_USER_CREATED = "userCreated"; 34 private final ActiveChangeSetManager manager; 35 private String comment; 36 private boolean userCreated = true; 37 38 43 public ActiveChangeSet(ActiveChangeSetManager manager, String title) { 44 super(title); 45 this.manager = manager; 46 } 47 48 54 public String getTitle() { 55 return getName(); 56 } 57 58 64 public void setTitle(String title) { 65 setName(title); 66 getManager().fireNameChangedEvent(this); 67 } 68 69 74 public String getComment() { 75 if (comment == null) { 76 return getTitle(); 77 } 78 return comment; 79 } 80 81 88 public void setComment(String comment) { 89 if (comment != null && comment.equals(getTitle())) { 90 this.comment = null; 91 } else { 92 this.comment = comment; 93 } 94 } 95 96 99 protected boolean isValidChange(IDiff diff) { 100 return getManager().isModified(diff); 101 } 102 103 private void addResource(IResource resource) throws CoreException { 104 IDiff diff = getManager().getDiff(resource); 105 if (diff != null) { 106 add(diff); 107 } 108 } 109 110 private ActiveChangeSetManager getManager() { 111 return manager; 112 } 113 114 118 public boolean hasComment() { 119 return comment != null; 120 } 121 122 public void save(Preferences prefs) { 123 prefs.put(CTX_TITLE, getTitle()); 124 if (comment != null) { 125 prefs.put(CTX_COMMENT, comment); 126 } 127 if (!isEmpty()) { 128 StringBuffer buffer = new StringBuffer (); 129 IResource[] resources = getResources(); 130 for (int i = 0; i < resources.length; i++) { 131 IResource resource = resources[i]; 132 buffer.append(resource.getFullPath().toString()); 133 buffer.append('\n'); 134 } 135 prefs.put(CTX_RESOURCES, buffer.toString()); 136 } 137 prefs.putBoolean(CTX_USER_CREATED, isUserCreated()); 138 } 139 140 public void init(Preferences prefs) { 141 setName(prefs.get(CTX_TITLE, "")); comment = prefs.get(CTX_COMMENT, null); 143 String resourcePaths = prefs.get(CTX_RESOURCES, null); 144 if (resourcePaths != null) { 145 ResourceDiffTree tree = internalGetDiffTree(); 146 try { 147 tree.beginInput(); 148 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 149 StringTokenizer tokenizer = new StringTokenizer(resourcePaths, "\n"); while (tokenizer.hasMoreTokens()) { 151 String next = tokenizer.nextToken(); 152 if (next.trim().length() > 0) { 153 IResource resource = getResource(root, next); 154 try { 156 if (resource != null && getManager().getDiff(resource) != null) { 157 addResource(resource); 158 } 159 } catch (CoreException e) { 160 TeamPlugin.log(e); 161 } 162 } 163 } 164 } finally { 165 tree.endInput(null); 166 } 167 } 168 userCreated = prefs.getBoolean(CTX_USER_CREATED, true); 169 } 170 171 private IResource getResource(IWorkspaceRoot root, String next) { 172 IResource resource = root.findMember(next); 173 if (resource == null) { 174 Path path = new Path(null, next); 176 if (next.charAt(next.length()-1) == IPath.SEPARATOR) { 177 if (path.segmentCount() == 1) { 178 resource = root.getProject(path.lastSegment()); 180 } else { 181 resource = root.getFolder(path); 183 } 184 } else { 185 resource = root.getFile(path); 187 } 188 } 189 return resource; 190 } 191 192 197 public void add(IResource[] resources) throws CoreException { 198 List toAdd = new ArrayList(); 199 for (int i = 0; i < resources.length; i++) { 200 IResource resource = resources[i]; 201 IDiff diff = getManager().getDiff(resource); 202 if (diff != null) { 203 toAdd.add(diff); 204 } 205 } 206 if (!toAdd.isEmpty()) { 207 add((IDiff[]) toAdd.toArray(new IDiff[toAdd.size()])); 208 } 209 } 210 211 215 public void setUserCreated(boolean userCreated) { 216 this.userCreated = userCreated; 217 } 218 219 223 public boolean isUserCreated() { 224 return userCreated; 225 } 226 } 227 | Popular Tags |