diff options
author | Brandon Dong | 2016-01-27 00:00:37 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2016-02-26 23:03:25 +0000 |
commit | 3965aae570d82102f6befe14dac0d56d99f74c1c (patch) | |
tree | 728d6d49050a71b7886a55a7435247242d053f6a | |
parent | cc7f16234f4428da36783b2b3debcc7a450bb17d (diff) | |
download | org.eclipse.mylyn.tasks-3965aae570d82102f6befe14dac0d56d99f74c1c.tar.gz org.eclipse.mylyn.tasks-3965aae570d82102f6befe14dac0d56d99f74c1c.tar.xz org.eclipse.mylyn.tasks-3965aae570d82102f6befe14dac0d56d99f74c1c.zip |
487166: Task search only uses id if not found in tasklist
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=487166
Change-Id: I3f94959523bf7366abdeeb6ee6f9224184ec8b1f
Signed-off-by: Brandon Dong <brandon.dong@tasktop.com>
3 files changed, 149 insertions, 50 deletions
diff --git a/org.eclipse.mylyn.tasks.search/src/org/eclipse/mylyn/internal/tasks/ui/TaskSearchPage.java b/org.eclipse.mylyn.tasks.search/src/org/eclipse/mylyn/internal/tasks/ui/TaskSearchPage.java index b0e4ed941..75cbcf9ba 100644 --- a/org.eclipse.mylyn.tasks.search/src/org/eclipse/mylyn/internal/tasks/ui/TaskSearchPage.java +++ b/org.eclipse.mylyn.tasks.search/src/org/eclipse/mylyn/internal/tasks/ui/TaskSearchPage.java @@ -28,6 +28,7 @@ import org.eclipse.jface.operation.IRunnableContext; import org.eclipse.mylyn.commons.core.StatusHandler; import org.eclipse.mylyn.commons.ui.CommonImages; import org.eclipse.mylyn.internal.tasks.ui.search.SearchHitCollector; +import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal; import org.eclipse.mylyn.internal.tasks.ui.views.TaskListView; import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector; import org.eclipse.mylyn.tasks.core.IRepositoryQuery; @@ -110,7 +111,7 @@ public class TaskSearchPage extends DialogPage implements ISearchPage { String key = keyText.getText(); if (key != null && key.trim().length() > 0) { key = key.trim(); - boolean openSuccessful = TasksUiUtil.openTask(repository, key); + boolean openSuccessful = TasksUiInternal.openTaskByIdOrKey(repository, key, null); if (!openSuccessful) { MessageDialog.openInformation(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), Messages.TaskSearchPage_Task_Search, Messages.TaskSearchPage_No_task_found_matching_key_ + key); diff --git a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/OpenRepositoryTaskJob.java b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/OpenRepositoryTaskJob.java index a7ae7f4e9..a88ec5386 100644 --- a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/OpenRepositoryTaskJob.java +++ b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/OpenRepositoryTaskJob.java @@ -32,6 +32,7 @@ import org.eclipse.mylyn.tasks.core.data.TaskData; import org.eclipse.mylyn.tasks.ui.TasksUi; import org.eclipse.mylyn.tasks.ui.TasksUiUtil; import org.eclipse.mylyn.tasks.ui.editors.TaskEditor; +import org.eclipse.osgi.util.NLS; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PlatformUI; @@ -97,8 +98,17 @@ public class OpenRepositoryTaskJob extends Job { } /** - * Returns the task if it was created when openeing - * + * Creates a job that fetches a task with the given task id or key and opens it. + */ + public OpenRepositoryTaskJob(IWorkbenchPage page, String repositoryKind, String repositoryUrl, String taskIdOrKey, + String taskUrl) { + this(repositoryKind, repositoryUrl, taskIdOrKey, taskUrl, page); + taskKey = taskIdOrKey; + } + + /** + * Returns the task if it was created when opening + * * @return */ public ITask getTask() { @@ -118,15 +128,12 @@ public class OpenRepositoryTaskJob extends Job { if (repository == null) { PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() { public void run() { - MessageDialog.openError( - null, - Messages.OpenRepositoryTaskJob_Repository_Not_Found, + MessageDialog.openError(null, Messages.OpenRepositoryTaskJob_Repository_Not_Found, MessageFormat.format( Messages.OpenRepositoryTaskJob_Could_not_find_repository_configuration_for_X, - repositoryUrl) - + "\n" + //$NON-NLS-1$ - MessageFormat.format(Messages.OpenRepositoryTaskJob_Please_set_up_repository_via_X, - Messages.TasksUiPlugin_Task_Repositories)); + repositoryUrl) + "\n" + //$NON-NLS-1$ + MessageFormat.format(Messages.OpenRepositoryTaskJob_Please_set_up_repository_via_X, + Messages.TasksUiPlugin_Task_Repositories)); TasksUiUtil.openUrl(taskUrl); } @@ -141,23 +148,26 @@ public class OpenRepositoryTaskJob extends Job { task = TasksUi.getRepositoryModel().createTask(repository, taskData.getTaskId()); TasksUiPlugin.getTaskDataManager().putUpdatedTaskData(task, taskData, true); PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() { + public void run() { TaskOpenEvent event = TasksUiInternal.openTask(task, taskId); if (listener != null && event != null) { listener.taskOpened(event); } if (timestamp != 0 && event != null) { - List<TaskAttribute> commentAttributes = taskData.getAttributeMapper().getAttributesByType( - taskData, TaskAttribute.TYPE_COMMENT); + List<TaskAttribute> commentAttributes = taskData.getAttributeMapper() + .getAttributesByType(taskData, TaskAttribute.TYPE_COMMENT); if (commentAttributes.size() > 0) { for (TaskAttribute commentAttribute : commentAttributes) { - TaskAttribute commentCreateDate = commentAttribute.getMappedAttribute(TaskAttribute.COMMENT_DATE); + TaskAttribute commentCreateDate = commentAttribute + .getMappedAttribute(TaskAttribute.COMMENT_DATE); if (commentCreateDate != null) { Date dateValue = taskData.getAttributeMapper().getDateValue(commentCreateDate); if (dateValue.getTime() < timestamp) { continue; } - TaskAttribute dn = commentAttribute.getMappedAttribute(TaskAttribute.COMMENT_NUMBER); + TaskAttribute dn = commentAttribute + .getMappedAttribute(TaskAttribute.COMMENT_NUMBER); TaskEditor editor = (TaskEditor) event.getEditor(); if (dn != null) { editor.selectReveal(TaskAttribute.PREFIX_COMMENT + dn.getValue()); @@ -189,17 +199,36 @@ public class OpenRepositoryTaskJob extends Job { } TaskData getTaskData(AbstractRepositoryConnector connector, IProgressMonitor monitor) throws CoreException { - if (taskId != null) { + if (taskId != null && taskKey != null && connector.supportsSearchByTaskKey(repository)) { + try { + TaskData data = getTaskDataByKey(connector, monitor); + if (data != null) { + return data; + } + } catch (CoreException | RuntimeException e) { + } + try { + return connector.getTaskData(repository, taskId, monitor); + } catch (CoreException | RuntimeException e) { + // do not display connector's message because it may be about using a task key as an ID which is not the real error + throw new CoreException(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, + NLS.bind("Could not find task with ID \"{0}\" on repository {1}.", taskId, repository), e)); //$NON-NLS-1$ + } + } else if (taskId != null) { return connector.getTaskData(repository, taskId, monitor); } else if (taskKey != null && connector.supportsSearchByTaskKey(repository)) { - TaskData searchTaskData = connector.searchByTaskKey(repository, taskKey, monitor); - if (searchTaskData.isPartial()) { - return connector.getTaskData(repository, searchTaskData.getTaskId(), monitor); - } else { - return searchTaskData; - } + return getTaskDataByKey(connector, monitor); } return null; } + private TaskData getTaskDataByKey(AbstractRepositoryConnector connector, IProgressMonitor monitor) + throws CoreException { + TaskData searchTaskData = connector.searchByTaskKey(repository, taskKey, monitor); + if (searchTaskData != null && searchTaskData.isPartial()) { + return connector.getTaskData(repository, searchTaskData.getTaskId(), monitor); + } + return searchTaskData; + } + } diff --git a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/util/TasksUiInternal.java b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/util/TasksUiInternal.java index 24f270846..777e0ec3e 100644 --- a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/util/TasksUiInternal.java +++ b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/internal/tasks/ui/util/TasksUiInternal.java @@ -880,27 +880,38 @@ public class TasksUiInternal { } public static boolean openTask(TaskRepository repository, String taskId, TaskOpenListener listener) { - Assert.isNotNull(repository); - Assert.isNotNull(taskId); - - AbstractTask task = (AbstractTask) TasksUiInternal.getTaskList().getTask(repository.getRepositoryUrl(), taskId); - if (task == null) { - task = TasksUiPlugin.getTaskList().getTaskByKey(repository.getRepositoryUrl(), taskId); - } + AbstractTask task = getTaskFromTaskList(repository, taskId); if (task != null) { // task is known, open in task editor - TaskOpenEvent event = TasksUiInternal.openTask(task, taskId); - if (listener != null && event != null) { - listener.taskOpened(event); + return openKnownTaskInEditor(task, taskId, listener); + } else { + // search for task + AbstractRepositoryConnectorUi connectorUi = TasksUiPlugin.getConnectorUi(repository.getConnectorKind()); + if (connectorUi != null) { + try { + return openRepositoryTask(connectorUi.getConnectorKind(), repository.getRepositoryUrl(), taskId, + listener); + } catch (Exception e) { + StatusHandler.log(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, + "Internal error while opening repository task", e)); //$NON-NLS-1$ + } } - return event != null; + } + return false; + } + + public static boolean openTaskByIdOrKey(TaskRepository repository, String taskIdOrKey, TaskOpenListener listener) { + AbstractTask task = getTaskFromTaskList(repository, taskIdOrKey); + if (task != null) { + // task is known, open in task editor + return openKnownTaskInEditor(task, taskIdOrKey, listener); } else { // search for task AbstractRepositoryConnectorUi connectorUi = TasksUiPlugin.getConnectorUi(repository.getConnectorKind()); if (connectorUi != null) { try { - return TasksUiInternal.openRepositoryTask(connectorUi.getConnectorKind(), - repository.getRepositoryUrl(), taskId, listener); + return openRepositoryTaskByIdOrKey(connectorUi.getConnectorKind(), repository.getRepositoryUrl(), + taskIdOrKey, listener); } catch (Exception e) { StatusHandler.log(new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, "Internal error while opening repository task", e)); //$NON-NLS-1$ @@ -910,6 +921,26 @@ public class TasksUiInternal { return false; } + private static boolean openKnownTaskInEditor(AbstractTask task, String taskIdOrKey, TaskOpenListener listener) { + TaskOpenEvent event = TasksUiInternal.openTask(task, taskIdOrKey); + if (listener != null && event != null) { + listener.taskOpened(event); + } + return event != null; + } + + private static AbstractTask getTaskFromTaskList(TaskRepository repository, String taskIdOrKey) { + Assert.isNotNull(repository); + Assert.isNotNull(taskIdOrKey); + + AbstractTask task = (AbstractTask) TasksUiInternal.getTaskList().getTask(repository.getRepositoryUrl(), + taskIdOrKey); + if (task == null) { + task = TasksUiPlugin.getTaskList().getTaskByKey(repository.getRepositoryUrl(), taskIdOrKey); + } + return task; + } + public static TaskOpenEvent openTask(ITask task, String taskId) { Assert.isNotNull(task); IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); @@ -1024,20 +1055,12 @@ public class TasksUiInternal { */ public static boolean openRepositoryTask(String connectorKind, String repositoryUrl, String id, TaskOpenListener listener, long timestamp) { - IRepositoryManager repositoryManager = TasksUi.getRepositoryManager(); - AbstractRepositoryConnector connector = repositoryManager.getRepositoryConnector(connectorKind); - String taskUrl = connector.getTaskUrl(repositoryUrl, id); + String taskUrl = getTaskUrl(connectorKind, repositoryUrl, id); if (taskUrl == null) { return false; } - IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); - if (window == null) { - IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows(); - if (windows != null && windows.length > 0) { - window = windows[0]; - } - } + IWorkbenchWindow window = getWorkbenchWindow(); if (window == null) { return false; } @@ -1052,6 +1075,48 @@ public class TasksUiInternal { } /** + * Opens a task with a given search string that can be a task id or task key. + * + * @return true if the task was successfully opened + */ + public static boolean openRepositoryTaskByIdOrKey(String connectorKind, String repositoryUrl, String idOrKey, + TaskOpenListener listener) { + String taskUrl = getTaskUrl(connectorKind, repositoryUrl, idOrKey); + if (taskUrl == null) { + return false; + } + + IWorkbenchWindow window = getWorkbenchWindow(); + if (window == null) { + return false; + } + IWorkbenchPage page = window.getActivePage(); + + OpenRepositoryTaskJob job = new OpenRepositoryTaskJob(page, connectorKind, repositoryUrl, idOrKey, taskUrl); + job.setListener(listener); + job.schedule(); + + return true; + } + + private static IWorkbenchWindow getWorkbenchWindow() { + IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); + if (window == null) { + IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows(); + if (windows != null && windows.length > 0) { + window = windows[0]; + } + } + return window; + } + + private static String getTaskUrl(String connectorKind, String repositoryUrl, String idOrKey) { + IRepositoryManager repositoryManager = TasksUi.getRepositoryManager(); + AbstractRepositoryConnector connector = repositoryManager.getRepositoryConnector(connectorKind); + return connector.getTaskUrl(repositoryUrl, idOrKey); + } + + /** * @since 3.0 */ public static boolean openTaskInBackground(ITask task, boolean bringToTop) { @@ -1255,14 +1320,18 @@ public class TasksUiInternal { NLS.bind("The command with the id ''{0}'' does not exist.", commandId))); //$NON-NLS-1$ } } else { - TasksUiInternal.displayStatus(title, new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, - NLS.bind("Command service is not available to execute command with the id ''{0}''.", commandId), //$NON-NLS-1$ - new Exception())); + TasksUiInternal + .displayStatus(title, + new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, NLS.bind( + "Command service is not available to execute command with the id ''{0}''.", //$NON-NLS-1$ + commandId), new Exception())); } } else { - TasksUiInternal.displayStatus(title, new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, - NLS.bind("Handler service is not available to execute command with the id ''{0}''.", commandId), //$NON-NLS-1$ - new Exception())); + TasksUiInternal.displayStatus(title, + new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, + NLS.bind("Handler service is not available to execute command with the id ''{0}''.", //$NON-NLS-1$ + commandId), + new Exception())); } } |