|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004 - 2006 University Of British Columbia and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Eugene Kuleshov - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.mylar.internal.team; |
| 13 |
|
| 14 |
import org.eclipse.compare.structuremergeviewer.DiffNode; |
| 15 |
import org.eclipse.compare.structuremergeviewer.IDiffElement; |
| 16 |
import org.eclipse.core.resources.IProject; |
| 17 |
import org.eclipse.core.resources.IResource; |
| 18 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 19 |
import org.eclipse.core.runtime.IAdaptable; |
| 20 |
import org.eclipse.core.runtime.IAdapterFactory; |
| 21 |
import org.eclipse.core.runtime.IAdapterManager; |
| 22 |
import org.eclipse.core.runtime.Platform; |
| 23 |
import org.eclipse.mylar.internal.team.template.CommitTemplateManager; |
| 24 |
import org.eclipse.mylar.tasks.core.TaskRepository; |
| 25 |
import org.eclipse.mylar.tasks.ui.TasksUiPlugin; |
| 26 |
import org.eclipse.mylar.team.MylarTeamPlugin; |
| 27 |
import org.eclipse.team.core.TeamException; |
| 28 |
import org.eclipse.team.core.history.IFileRevision; |
| 29 |
import org.eclipse.team.core.variants.IResourceVariant; |
| 30 |
import org.eclipse.team.internal.ccvs.core.CVSException; |
| 31 |
import org.eclipse.team.internal.ccvs.core.ICVSResource; |
| 32 |
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; |
| 33 |
import org.eclipse.team.internal.ccvs.core.resources.RemoteResource; |
| 34 |
import org.eclipse.team.internal.core.subscribers.ChangeSet; |
| 35 |
import org.eclipse.team.internal.core.subscribers.DiffChangeSet; |
| 36 |
import org.eclipse.team.internal.ui.synchronize.ChangeSetDiffNode; |
| 37 |
import org.eclipse.team.internal.ui.synchronize.SyncInfoModelElement; |
| 38 |
import org.eclipse.team.internal.ui.synchronize.SynchronizeModelElement; |
| 39 |
|
| 40 |
import org.tigris.subversion.subclipse.core.ISVNRemoteResource; |
| 41 |
import org.tigris.subversion.subclipse.core.sync.SVNStatusSyncInfo; |
| 42 |
import org.tigris.subversion.subclipse.ui.settings.ProjectProperties; |
| 43 |
import org.tigris.subversion.svnclientadapter.ISVNLogMessage; |
| 44 |
import org.tigris.subversion.svnclientadapter.SVNRevision; |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* Adapter factory used to create adapters for <code>LinkedTaskInfo</code> |
| 49 |
* |
| 50 |
* @author Eugene Kuleshov |
| 51 |
*/ |
| 52 |
public class LinkedTaskInfoAdapterFactory implements IAdapterFactory { |
| 53 |
|
| 54 |
private static final Class[] ADAPTER_TYPES = new Class[] { ILinkedTaskInfo.class }; |
| 55 |
|
| 56 |
private static IAdapterFactory FACTORY = new LinkedTaskInfoAdapterFactory(); |
| 57 |
|
| 58 |
private static final String PREFIX_HTTP = "http://"; |
| 59 |
|
| 60 |
private static final String PREFIX_HTTPS = "https://"; |
| 61 |
|
| 62 |
private static boolean haveSubclipse; |
| 63 |
|
| 64 |
|
| 65 |
public static void registerAdapters() { |
| 66 |
IAdapterManager adapterManager = Platform.getAdapterManager(); |
| 67 |
|
| 68 |
// Mylar |
| 69 |
adapterManager.registerAdapters(FACTORY, ContextChangeSet.class); |
| 70 |
|
| 71 |
// Team public |
| 72 |
adapterManager.registerAdapters(FACTORY, IFileRevision.class); |
| 73 |
adapterManager.registerAdapters(FACTORY, DiffNode.class); |
| 74 |
|
| 75 |
// Team internal |
| 76 |
adapterManager.registerAdapters(FACTORY, DiffChangeSet.class); // CVSCheckedInChangeSet ??? |
| 77 |
adapterManager.registerAdapters(FACTORY, ChangeSetDiffNode.class); |
| 78 |
adapterManager.registerAdapters(FACTORY, SynchronizeModelElement.class); |
| 79 |
|
| 80 |
// Team CVS internal; is it used? Maybe CVS History view in Eclipse 3.1? |
| 81 |
adapterManager.registerAdapters(FACTORY, org.eclipse.team.internal.ccvs.core.client.listeners.LogEntry.class); |
| 82 |
|
| 83 |
// Subclipse |
| 84 |
try { |
| 85 |
SubclipseWrapper.init(adapterManager, FACTORY); |
| 86 |
haveSubclipse = true; |
| 87 |
} catch (Throwable ex) { |
| 88 |
// ignore |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
public static void unregisterAdapters() { |
| 93 |
Platform.getAdapterManager().unregisterAdapters(FACTORY); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
private LinkedTaskInfoAdapterFactory() { |
| 98 |
} |
| 99 |
|
| 100 |
public Object getAdapter(Object object, Class adapterType) { |
| 101 |
if(!ILinkedTaskInfo.class.equals(adapterType)) { |
| 102 |
return null; |
| 103 |
} |
| 104 |
|
| 105 |
if(object instanceof ChangeSetDiffNode) { |
| 106 |
return adaptChangeSetDiffNode(object); |
| 107 |
} |
| 108 |
|
| 109 |
if(object instanceof DiffNode) { |
| 110 |
return getAdapter(((DiffNode) object).getParent(), adapterType); |
| 111 |
} |
| 112 |
|
| 113 |
if(haveSubclipse && |
| 114 |
"org.tigris.subversion.subclipse.core.history.LogEntry".equals(object.getClass().getName())) { |
| 115 |
ILinkedTaskInfo info = SubclipseWrapper.adaptSubclipseLogEntry(object); |
| 116 |
if(info!=null) { |
| 117 |
return info; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
// TODO add other adapted types |
| 122 |
|
| 123 |
return adaptFromComment(object); |
| 124 |
} |
| 125 |
|
| 126 |
public Class[] getAdapterList() { |
| 127 |
return ADAPTER_TYPES; |
| 128 |
} |
| 129 |
|
| 130 |
private ILinkedTaskInfo adaptChangeSetDiffNode(Object object) { |
| 131 |
ChangeSetDiffNode diffNode = (ChangeSetDiffNode) object; |
| 132 |
ChangeSet set = diffNode.getSet(); |
| 133 |
if (set instanceof ContextChangeSet) { |
| 134 |
return new LinkedTaskInfo(((ContextChangeSet) set).getTask()); |
| 135 |
} else if(haveSubclipse && set.getClass().getName().startsWith("org.tigris")) { |
| 136 |
ILinkedTaskInfo info = SubclipseWrapper.adaptSubclipseChangeset(diffNode, set); |
| 137 |
if(info!=null) { |
| 138 |
return info; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return adaptFromComment(object); |
| 143 |
} |
| 144 |
|
| 145 |
private ILinkedTaskInfo adaptFromComment(Object object) { |
| 146 |
String comment = getCommentFromElement(object); |
| 147 |
|
| 148 |
CommitTemplateManager commitTemplateManager = MylarTeamPlugin.getDefault().getCommitTemplateManager(); |
| 149 |
String taskId = commitTemplateManager.getTaskIdFromCommentOrLabel(comment); |
| 150 |
if (taskId == null) { |
| 151 |
taskId = getTaskIdFromLegacy07Label(comment); |
| 152 |
} |
| 153 |
|
| 154 |
IProject project = findCorrespondingProject(object); |
| 155 |
if (project != null) { |
| 156 |
TaskRepository repository = TasksUiPlugin.getDefault().getRepositoryForResource(project, false); |
| 157 |
if (repository != null && taskId!=null) { |
| 158 |
return new LinkedTaskInfo(repository.getUrl(), taskId, null); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
String fullTaskUrl = getUrlFromComment(comment); |
| 163 |
if(fullTaskUrl!=null) { |
| 164 |
return new LinkedTaskInfo(fullTaskUrl); |
| 165 |
} |
| 166 |
|
| 167 |
return null; |
| 168 |
} |
| 169 |
|
| 170 |
private String getCommentFromElement(Object element) { |
| 171 |
if (element instanceof DiffChangeSet) { |
| 172 |
return ((DiffChangeSet) element).getComment(); |
| 173 |
} else if (element instanceof ChangeSetDiffNode) { |
| 174 |
return ((ChangeSetDiffNode) element).getName(); |
| 175 |
} else if (element instanceof IFileRevision) { |
| 176 |
return ((IFileRevision) element).getComment(); |
| 177 |
} else if (element instanceof org.eclipse.team.internal.ccvs.core.client.listeners.LogEntry) { |
| 178 |
return ((org.eclipse.team.internal.ccvs.core.client.listeners.LogEntry) element).getComment(); |
| 179 |
} else if (element instanceof org.tigris.subversion.subclipse.core.history.LogEntry) { |
| 180 |
return ((org.tigris.subversion.subclipse.core.history.LogEntry) element).getComment(); |
| 181 |
} |
| 182 |
return null; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
// |
| 187 |
|
| 188 |
public static String getUrlFromComment(String comment) { |
| 189 |
int httpIndex = comment.indexOf(PREFIX_HTTP); |
| 190 |
int httpsIndex = comment.indexOf(PREFIX_HTTPS); |
| 191 |
int idStart = -1; |
| 192 |
if (httpIndex != -1) { |
| 193 |
idStart = httpIndex; |
| 194 |
} else if (httpsIndex != -1) { |
| 195 |
idStart = httpsIndex; |
| 196 |
} |
| 197 |
if (idStart != -1) { |
| 198 |
int idEnd = comment.indexOf(' ', idStart); |
| 199 |
if (idEnd == -1) { |
| 200 |
return comment.substring(idStart); |
| 201 |
} else if (idEnd != -1 && idStart < idEnd) { |
| 202 |
return comment.substring(idStart, idEnd); |
| 203 |
} |
| 204 |
} |
| 205 |
return null; |
| 206 |
} |
| 207 |
|
| 208 |
public static String getTaskIdFromLegacy07Label(String comment) { |
| 209 |
String PREFIX_DELIM = ":"; |
| 210 |
String PREFIX_START_1 = "Progress on:"; |
| 211 |
String PREFIX_START_2 = "Completed:"; |
| 212 |
String usedPrefix = PREFIX_START_1; |
| 213 |
int firstDelimIndex = comment.indexOf(PREFIX_START_1); |
| 214 |
if (firstDelimIndex == -1) { |
| 215 |
firstDelimIndex = comment.indexOf(PREFIX_START_2); |
| 216 |
usedPrefix = PREFIX_START_2; |
| 217 |
} |
| 218 |
if (firstDelimIndex != -1) { |
| 219 |
int idStart = firstDelimIndex + usedPrefix.length(); |
| 220 |
int idEnd = comment.indexOf(PREFIX_DELIM, firstDelimIndex + usedPrefix.length());// comment.indexOf(PREFIX_DELIM); |
| 221 |
if (idEnd != -1 && idStart < idEnd) { |
| 222 |
String id = comment.substring(idStart, idEnd); |
| 223 |
if (id != null) { |
| 224 |
return id.trim(); |
| 225 |
} |
| 226 |
} else { |
| 227 |
return comment.substring(0, firstDelimIndex); |
| 228 |
} |
| 229 |
} |
| 230 |
return null; |
| 231 |
} |
| 232 |
|
| 233 |
private static IProject findCorrespondingProject(Object element) { |
| 234 |
if (element instanceof DiffChangeSet) { |
| 235 |
IResource[] resources = ((DiffChangeSet) element).getResources(); |
| 236 |
if (resources.length > 0) { |
| 237 |
// TODO: only checks first resource |
| 238 |
return resources[0].getProject(); |
| 239 |
} |
| 240 |
} else if (element instanceof SynchronizeModelElement) { |
| 241 |
SynchronizeModelElement modelElement = (SynchronizeModelElement)element; |
| 242 |
IResource resource = modelElement.getResource(); |
| 243 |
if (resource != null) { |
| 244 |
return resource.getProject(); |
| 245 |
} else { |
| 246 |
IDiffElement[] elements = modelElement.getChildren(); |
| 247 |
if (elements.length > 0) { |
| 248 |
// TODO: only checks first diff |
| 249 |
if (elements[0] instanceof SynchronizeModelElement) { |
| 250 |
return ((SynchronizeModelElement)elements[0]).getResource().getProject(); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
} else if (element instanceof IAdaptable) { |
| 255 |
IAdaptable adaptable = (IAdaptable) element; |
| 256 |
IResourceVariant resourceVariant = (IResourceVariant) adaptable.getAdapter(IResourceVariant.class); |
| 257 |
if (resourceVariant != null && resourceVariant instanceof RemoteResource) { |
| 258 |
RemoteResource remoteResource = (RemoteResource) resourceVariant; |
| 259 |
// TODO is there a better way then iterating trough all projects? |
| 260 |
String path = remoteResource.getRepositoryRelativePath(); |
| 261 |
for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) { |
| 262 |
if(project.isAccessible()) { |
| 263 |
ICVSResource cvsResource = CVSWorkspaceRoot.getCVSFolderFor(project); |
| 264 |
try { |
| 265 |
if(cvsResource!=null && path.startsWith(cvsResource.getRepositoryRelativePath())) { |
| 266 |
return project; |
| 267 |
} |
| 268 |
} catch (CVSException ex) { |
| 269 |
// ignore |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
return null; |
| 275 |
} |
| 276 |
} else { |
| 277 |
// TODO |
| 278 |
|
| 279 |
} |
| 280 |
return null; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
/** |
| 285 |
* Wrapper class used to isolate Subclibse dependencies |
| 286 |
*/ |
| 287 |
private static class SubclipseWrapper { |
| 288 |
|
| 289 |
public static void init(IAdapterManager adapterManager, IAdapterFactory factory) { |
| 290 |
Class logEntryClass = org.tigris.subversion.subclipse.core.history.LogEntry.class; |
| 291 |
adapterManager.registerAdapters(FACTORY, logEntryClass); |
| 292 |
} |
| 293 |
|
| 294 |
private static ILinkedTaskInfo adaptSubclipseChangeset(ChangeSetDiffNode diff, ChangeSet set) { |
| 295 |
SynchronizeModelElement diffContainer = (SynchronizeModelElement) diff.getChildren()[0]; |
| 296 |
|
| 297 |
IResource res = diffContainer.getResource(); |
| 298 |
|
| 299 |
SyncInfoModelElement melement = (SyncInfoModelElement) diffContainer.getChildren()[0]; |
| 300 |
|
| 301 |
// Specific to Subclipse |
| 302 |
SVNStatusSyncInfo info = (SVNStatusSyncInfo) melement.getSyncInfo(); |
| 303 |
|
| 304 |
ISVNRemoteResource remoteResource = (ISVNRemoteResource) info.getRemote(); |
| 305 |
SVNRevision rev = remoteResource.getLastChangedRevision(); |
| 306 |
|
| 307 |
String comment; |
| 308 |
try { |
| 309 |
ISVNLogMessage[] messages = remoteResource.getLogMessages(rev, rev, SVNRevision.START, false, false, 1); |
| 310 |
comment = messages[0].getMessage(); |
| 311 |
} catch (TeamException ex) { |
| 312 |
comment = diff.getSet().getComment(); |
| 313 |
} |
| 314 |
return adaptFromComment(diff, res, comment); |
| 315 |
} |
| 316 |
|
| 317 |
private static ILinkedTaskInfo adaptSubclipseLogEntry(Object object) { |
| 318 |
org.tigris.subversion.subclipse.core.history.LogEntry logEntry = |
| 319 |
(org.tigris.subversion.subclipse.core.history.LogEntry) object; |
| 320 |
|
| 321 |
String comment = logEntry.getComment(); |
| 322 |
IResource res = logEntry.getResource().getResource(); |
| 323 |
|
| 324 |
return adaptFromComment(object, res, comment); |
| 325 |
} |
| 326 |
|
| 327 |
private static ILinkedTaskInfo adaptFromComment(Object object, IResource res, String comment) { |
| 328 |
ProjectProperties props = null; |
| 329 |
try { |
| 330 |
props = ProjectProperties.getProjectProperties(res); |
| 331 |
} catch (TeamException ex) { |
| 332 |
// ignore? |
| 333 |
} |
| 334 |
|
| 335 |
String[] urls = null; |
| 336 |
String repositoryUrl = null; |
| 337 |
if(props!=null) { |
| 338 |
repositoryUrl = getRepositoryUrl(props.getUrl()); |
| 339 |
urls = props.getLinkList(comment).getUrls(); |
| 340 |
} |
| 341 |
if (urls == null || urls.length == 0) { |
| 342 |
// we can do better job then this method |
| 343 |
urls = ProjectProperties.getUrls(comment).getUrls(); |
| 344 |
} |
| 345 |
if (urls != null && urls.length > 0) { |
| 346 |
return new LinkedTaskInfo(repositoryUrl, null, urls[0]); |
| 347 |
} |
| 348 |
return null; |
| 349 |
} |
| 350 |
|
| 351 |
private static String getRepositoryUrl(String url) { |
| 352 |
for (TaskRepository repository : TasksUiPlugin.getRepositoryManager().getAllRepositories()) { |
| 353 |
if(url.startsWith(repository.getUrl())) { |
| 354 |
return repository.getUrl(); |
| 355 |
} |
| 356 |
} |
| 357 |
return null; |
| 358 |
} |
| 359 |
|
| 360 |
} |
| 361 |
|
| 362 |
} |
| 363 |
|