Skip to main content
summaryrefslogtreecommitdiffstats
blob: d670d077017bbfdd7b30146db94722f2b91285e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*******************************************************************************
 * Copyright (c) 2003, 2007 Mylyn project committers and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.mylyn.internal.tasks.ui.search;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.internal.tasks.core.AbstractRepositoryQuery;
import org.eclipse.mylyn.internal.tasks.core.AbstractTask;
import org.eclipse.mylyn.internal.tasks.core.deprecated.AbstractLegacyRepositoryConnector;
import org.eclipse.mylyn.internal.tasks.core.deprecated.LegacyTaskDataCollector;
import org.eclipse.mylyn.internal.tasks.core.deprecated.RepositoryTaskData;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.ITaskList;
import org.eclipse.mylyn.tasks.core.RepositoryStatus;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.TaskDataCollector;
import org.eclipse.mylyn.tasks.ui.TasksUi;
import org.eclipse.search.ui.ISearchQuery;
import org.eclipse.search.ui.ISearchResult;
import org.eclipse.search.ui.NewSearchUI;
import org.eclipse.search.ui.text.Match;
import org.eclipse.ui.PlatformUI;

/**
 * Used for returning results from Eclipse Search view. Collects results of a repository search.
 * 
 * @author Rob Elves
 * @since 2.0
 */
public class SearchHitCollector extends LegacyTaskDataCollector implements ISearchQuery {

	private static final String LABEL_MAX_HITS_REACHED = "Max allowed number of hits returned exceeded. Some hits may not be displayed. Please narrow query scope.";

	private static final String QUERYING_REPOSITORY = "Querying Repository...";

	private final Set<AbstractTask> taskResults = new HashSet<AbstractTask>();

//	/** The string to display to the user when we have 1 match */
//	private static final String MATCH = "1 match";
//
//	/** The string to display to the user when we have multiple or no matches */
//	private static final String MATCHES = "{0} matches";

	private final ITaskList taskList;

	private final TaskRepository repository;

	private final AbstractRepositoryQuery repositoryQuery;

	private final RepositorySearchResult searchResult;

	private final AbstractRepositoryConnector connector;

	/**
	 * @since 3.0
	 */
	public SearchHitCollector(ITaskList tasklist, TaskRepository repository, AbstractRepositoryQuery repositoryQuery) {
		this.taskList = tasklist;
		this.repository = repository;
		this.repositoryQuery = repositoryQuery;
		this.connector = TasksUi.getRepositoryManager().getRepositoryConnector(repository.getConnectorKind());
		this.searchResult = new RepositorySearchResult(this);
	}

	public void aboutToStart() {
		taskResults.clear();

		searchResult.removeAll();
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				NewSearchUI.activateSearchResultView();
			}
		});
	}

//	public void accept(AbstractTask task) {
//		if (task == null) {
//			throw new IllegalArgumentException();
//		}
//
//		AbstractTask hitTask = taskList.getTask(task.getHandleIdentifier());
//		if (hitTask == null) {
//			hitTask = task;
//		}
//
//		taskResults.add(hitTask);
//		this.searchResult.addMatch(new Match(hitTask, 0, 0));
//	}

	@Override
	public void accept(RepositoryTaskData taskData) {
		AbstractTask task = taskList.getTask(repository.getRepositoryUrl(), taskData.getTaskId());
		if (task == null) {
			task = ((AbstractLegacyRepositoryConnector) connector).createTask(taskData.getRepositoryUrl(),
					taskData.getTaskId(), "");
			((AbstractLegacyRepositoryConnector) connector).updateTaskFromTaskData(repository, task, taskData);
		}
		taskResults.add(task);
		this.searchResult.addMatch(new Match(task, 0, 0));
	}

	public String getLabel() {
		return QUERYING_REPOSITORY;
	}

	public boolean canRerun() {
		return true;
	}

	public boolean canRunInBackground() {
		return true;
	}

	public ISearchResult getSearchResult() {
		if (searchResult.getMatchCount() >= TaskDataCollector.MAX_HITS) {
			TasksUiInternal.displayStatus("Maximum hits reached", RepositoryStatus.createStatus(
					repository.getRepositoryUrl(), IStatus.WARNING, TasksUiPlugin.ID_PLUGIN, LABEL_MAX_HITS_REACHED));
		}
		return searchResult;
	}

	public IStatus run(IProgressMonitor monitor) throws OperationCanceledException {
		if (monitor == null) {
			monitor = new NullProgressMonitor();
		}

		//monitor.beginTask(QUERYING_REPOSITORY, IProgressMonitor.UNKNOWN);
		aboutToStart();

		if (monitor.isCanceled()) {
			throw new OperationCanceledException("Search cancelled");
		}
		AbstractRepositoryConnector connector = TasksUi.getRepositoryManager().getRepositoryConnector(
				repositoryQuery.getConnectorKind());
		if (connector != null) {
			IStatus status = connector.performQuery(repository, repositoryQuery, this, null, monitor);
			if (!status.isOK()) {
				TasksUiInternal.displayStatus("Search failed", status);
			}
		} else {
			return new Status(IStatus.ERROR, TasksUiPlugin.ID_PLUGIN, IStatus.OK,
					"repository connector could not be found", null);
		}

		return Status.OK_STATUS;
	}

//	protected String getFormattedMatchesString(int count) {
//		if (count == 1) {
//			return MATCH;
//		}
//		Object[] messageFormatArgs = { new Integer(count) };
//		return MessageFormat.format(MATCHES, messageFormatArgs);
//	}

	public Set<AbstractTask> getTasks() {
		return taskResults;
	}

	public AbstractRepositoryQuery getRepositoryQuery() {
		return repositoryQuery;
	}

}

Back to the top