1 12 package org.eclipse.team.internal.ccvs.core.client; 13 14 import java.util.*; 15 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.jobs.Job; 20 import org.eclipse.osgi.util.NLS; 21 import org.eclipse.team.internal.ccvs.core.*; 22 import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption; 23 import org.eclipse.team.internal.ccvs.core.resources.CVSEntryLineTag; 24 import org.eclipse.team.internal.ccvs.core.syncinfo.*; 25 import org.eclipse.team.internal.ccvs.core.util.Util; 26 27 36 abstract class AbstractStructureVisitor implements ICVSResourceVisitor { 37 38 protected Session session; 39 private ICVSFolder lastFolderSent; 40 protected IProgressMonitor monitor; 41 protected boolean sendQuestionable; 42 protected boolean sendModifiedContents; 43 private boolean sendBinary; 44 45 private boolean recurse = true; 46 47 public AbstractStructureVisitor(Session session, LocalOption[] localOptions, boolean sendQuestionable, boolean sendModifiedContents) { 48 this(session, localOptions, sendQuestionable, sendModifiedContents, true); 49 } 50 51 public AbstractStructureVisitor(Session session, LocalOption[] localOptions, boolean sendQuestionable, boolean sendModifiedContents, boolean sendBinary) { 52 this.session = session; 53 this.sendQuestionable = sendQuestionable; 54 this.sendModifiedContents = sendModifiedContents; 55 this.sendBinary = sendBinary; 56 if (Command.DO_NOT_RECURSE.isElementOf(localOptions)) 57 recurse = false; 58 } 59 60 63 protected boolean isLastSent(ICVSFolder folder) { 64 return folder.equals(lastFolderSent); 65 } 66 67 70 protected void recordLastSent(ICVSFolder folder) { 71 lastFolderSent = folder; 72 } 73 74 81 protected boolean isOrphanedSubtree(ICVSFolder mFolder) throws CVSException { 82 return mFolder.isCVSFolder() && ! mFolder.isManaged() && ! mFolder.equals(session.getLocalRoot()) && mFolder.getParent().isCVSFolder(); 83 } 84 85 91 protected void sendFolder(ICVSFolder mFolder) throws CVSException { 92 93 Policy.checkCanceled(monitor); 94 95 boolean exists = mFolder.exists(); 96 FolderSyncInfo info = mFolder.getFolderSyncInfo(); 97 boolean isCVSFolder = info != null; 98 99 if ( ! exists && ! isCVSFolder) return; 102 103 if (isLastSent(mFolder)) return; 105 106 if (isCVSFolder && info.isVirtualDirectory()) { 108 return; 109 } 110 111 String localPath = mFolder.getRelativePath(session.getLocalRoot()); 112 113 monitor.subTask(NLS.bind(CVSMessages.AbstractStructureVisitor_sendingFolder, new String [] { Util.toTruncatedPath(mFolder, session.getLocalRoot(), 3) })); 114 115 boolean isQuestionable = exists && (! isCVSFolder || isOrphanedSubtree(mFolder)); 117 if (isQuestionable) { 118 if (sendQuestionable) { 119 sendFolder(mFolder.getParent()); 121 session.sendQuestionable(mFolder); 122 } 123 return; 124 } 125 126 String remotePath = mFolder.getRemoteLocation(session.getLocalRoot()); 128 if (remotePath == null) { 129 IStatus status = new CVSStatus(IStatus.ERROR,CVSStatus.ERROR, CVSMessages.AbstractStructureVisitor_noRemote, session.getLocalRoot()); 130 throw new CVSException(status); 131 } 132 session.sendDirectory(localPath, remotePath); 133 134 if (info != null) { 136 137 if (info.getIsStatic()) { 138 session.sendStaticDirectory(); 139 } 140 141 CVSEntryLineTag tag = info.getTag(); 142 143 if (tag != null && tag.getType() != CVSTag.HEAD) { 144 session.sendSticky(tag.toEntryLineFormat(false)); 145 } 146 } 147 148 recordLastSent(mFolder); 150 151 monitor.worked(1); 152 } 153 154 159 protected void sendFile(ICVSFile mFile) throws CVSException { 160 161 Policy.checkCanceled(monitor); 162 163 sendFolder(mFile.getParent()); 165 166 byte[] syncBytes = mFile.getSyncBytes(); 168 boolean isManaged = syncBytes != null; 169 170 if (isManaged) { 171 sendPendingNotification(mFile); 172 } else { 173 if (sendQuestionable) { 176 if (mFile.exists()) { 177 session.sendQuestionable(mFile); 178 } 179 return; 180 } 181 } 183 184 boolean sendContents = mFile.exists() && mFile.isModified(monitor) 188 && !mFile.getSyncInfo().isNeedsMerge(mFile.getTimeStamp()); 189 if (ResourceSyncInfo.isDeletion(syncBytes)) { 190 sendEntryLineToServer(mFile, syncBytes); 191 } else if (sendContents) { 192 final IResource resource = mFile.getIResource(); 195 try { 196 if (resource != null) 197 Job.getJobManager().beginRule(resource, monitor); 198 199 sendEntryLineToServer(mFile, syncBytes); 200 if (mFile.exists() && mFile.isModified(null)) { 201 boolean binary = ResourceSyncInfo.isBinary(syncBytes); 202 if (sendModifiedContents) { 203 session.sendModified(mFile, binary, sendBinary, monitor); 204 } else { 205 session.sendIsModified(mFile, binary, monitor); 206 } 207 } else { 208 session.sendUnchanged(mFile); 209 } 210 } finally { 211 if (resource != null) 212 Job.getJobManager().endRule(resource); 213 } 214 } else { 215 sendEntryLineToServer(mFile, syncBytes); 216 session.sendUnchanged(mFile); 217 } 218 219 monitor.worked(1); 220 } 221 222 private void sendEntryLineToServer(ICVSFile mFile, byte[] syncBytes) throws CVSException { 223 if (syncBytes != null) { 224 String syncBytesToServer = ResourceSyncInfo.getTimestampToServer(syncBytes, mFile.getTimeStamp()); 225 session.sendEntry(syncBytes, syncBytesToServer); 226 } 227 } 228 229 protected void sendPendingNotification(ICVSFile mFile) throws CVSException { 230 NotifyInfo notify = mFile.getPendingNotification(); 231 if (notify != null) { 232 sendFolder(mFile.getParent()); 233 session.sendNotify(mFile.getParent(), notify); 234 } 235 } 236 237 241 public void visit(Session session, ICVSResource[] resources, IProgressMonitor monitor) throws CVSException { 242 243 List resourceList = new ArrayList(resources.length); 245 resourceList.addAll(Arrays.asList(resources)); 246 final ICVSFolder localRoot = session.getLocalRoot(); 247 Collections.sort(resourceList, new Comparator() { 248 public int compare(Object object1, Object object2) { 249 ICVSResource resource1 = (ICVSResource)object1; 250 ICVSResource resource2 = (ICVSResource)object2; 251 try { 252 String path1 = resource1.getParent().getRelativePath(localRoot); 253 String path2 = resource2.getParent().getRelativePath(localRoot); 254 int pathCompare = path1.compareTo(path2); 255 if (pathCompare == 0) { 256 if (resource1.isFolder() == resource2.isFolder()) { 257 return resource1.getName().compareTo(resource2.getName()); 258 } else if (resource1.isFolder()) { 259 return 1; 260 } else { 261 return -1; 262 } 263 } else { 264 return pathCompare; 265 } 266 } catch (CVSException e) { 267 return resource1.getName().compareTo(resource2.getName()); 268 } 269 } 270 }); 271 272 int resourceHint = 64; 274 monitor.beginTask(null, resourceHint); 275 this.monitor = Policy.infiniteSubMonitorFor(monitor, resourceHint); 276 try { 277 this.monitor.beginTask(null, resourceHint); 279 session.setSendFileTitleKey(getSendFileMessage()); 280 for (int i = 0; i < resourceList.size(); i++) { 281 ((ICVSResource)resourceList.get(i)).accept(this); 282 } 283 } finally { 284 monitor.done(); 285 } 286 } 287 288 294 protected String getSendFileMessage() { 295 return CVSMessages.AbstractStructureVisitor_sendingFile; 296 } 297 public boolean isRecurse() { 298 return recurse; 299 } 300 } 301 | Popular Tags |