Skip to main content
summaryrefslogtreecommitdiffstats
blob: ca2dc11fdcd3ce0afee2f1de89268b2c6995970e (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*******************************************************************************
 * Copyright (c) 2004, 2010 Tasktop Technologies 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
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *     Eugene Kuleshov - improvements
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui;

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.eclipse.jface.fieldassist.IContentProposal;
import org.eclipse.jface.fieldassist.IContentProposalProvider;
import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
import org.eclipse.mylyn.commons.net.AuthenticationType;
import org.eclipse.mylyn.internal.tasks.core.AbstractTask;
import org.eclipse.mylyn.tasks.core.IRepositoryPerson;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.TaskAttachmentMapper;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMetaData;
import org.eclipse.mylyn.tasks.core.data.TaskCommentMapper;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.ui.TasksUi;
import org.eclipse.osgi.util.NLS;

/**
 * @author Shawn Minto
 * @author Eugene Kuleshov
 * @author Steffen Pingel
 * @author David Shepherd
 * @author Sam Davis
 * @author Thomas Ehrnhoefer
 * @author Mike Wu
 */
public class PersonProposalProvider implements IContentProposalProvider {

	private final AbstractTask currentTask;

	private String currentUser;

	private SortedSet<String> addressSet;

	private String repositoryUrl;

	private String connectorKind;

	private TaskData currentTaskData;

	private final Map<String, String> proposals;

	private Map<String, String> errorProposals;

	public PersonProposalProvider(AbstractTask task, TaskData taskData) {
		this(task, taskData, new HashMap<String, String>(0));
	}

	public PersonProposalProvider(AbstractTask task, TaskData taskData, Map<String, String> proposals) {
		this.currentTask = task;
		this.currentTaskData = taskData;
		if (task != null) {
			repositoryUrl = task.getRepositoryUrl();
			connectorKind = task.getConnectorKind();
		} else if (taskData != null) {
			repositoryUrl = taskData.getRepositoryUrl();
			connectorKind = taskData.getConnectorKind();
		}
		this.proposals = new HashMap<String, String>(proposals);
	}

	public PersonProposalProvider(String repositoryUrl, String repositoryKind) {
		this(repositoryUrl, repositoryKind, new HashMap<String, String>(0));
	}

	public PersonProposalProvider(String repositoryUrl, String repositoryKind, Map<String, String> proposals) {
		this.currentTask = null;
		this.repositoryUrl = repositoryUrl;
		this.connectorKind = repositoryKind;
		this.proposals = new HashMap<String, String>(proposals);
	}

	protected String getRepositoryUrl() {
		return repositoryUrl;
	}

	protected String getConnectorKind() {
		return connectorKind;
	}

	public IContentProposal[] getProposals(String contents, int position) {
		if (contents == null) {
			throw new IllegalArgumentException();
		}

		int leftSeparator = getIndexOfLeftSeparator(contents, position);
		int rightSeparator = getIndexOfRightSeparator(contents, position);

		assert leftSeparator <= position;
		assert position <= rightSeparator;

		String searchText = contents.substring(leftSeparator + 1, position);
		String resultPrefix = contents.substring(0, leftSeparator + 1);
		String resultPostfix = contents.substring(rightSeparator);

		// retrieve subset of the tree set using key range
		SortedSet<String> addressSet = getAddressSet();
		if (errorProposals == null || errorProposals.isEmpty()) {
			if (!searchText.equals("")) { //$NON-NLS-1$
				// lower bounds
				searchText = searchText.toLowerCase();

				// compute the upper bound
				char[] nextWord = searchText.toCharArray();
				nextWord[searchText.length() - 1]++;

				// filter matching keys 
				addressSet = new TreeSet<String>(addressSet.subSet(searchText, new String(nextWord)));

				// add matching keys based on pretty names 
				addMatchingProposalsByPrettyName(addressSet, searchText);
			}
		}

		IContentProposal[] result = new IContentProposal[addressSet.size()];
		int i = 0;
		for (final String address : addressSet) {
			result[i++] = createPersonProposal(address, address.equalsIgnoreCase(currentUser), resultPrefix + address
					+ resultPostfix, resultPrefix.length() + address.length());
		}
		Arrays.sort(result);
		return result;
	}

	private void addMatchingProposalsByPrettyName(SortedSet<String> addressSet, String searchText) {
		if (proposals.size() > 0) {
			for (Map.Entry<String, String> entry : proposals.entrySet()) {
				if (matchesSubstring(entry.getValue(), searchText)) {
					addressSet.add(entry.getKey());
				}
			}
		}
	}

	private boolean matchesSubstring(String value, String searchText) {
		if (value != null) {
			String tokens[] = value.split("\\s"); //$NON-NLS-1$
			for (String token : tokens) {
				if (token.toLowerCase().startsWith(searchText)) {
					return true;
				}
			}
		}
		return false;
	}

	protected PersonContentProposal createPersonProposal(String address, boolean isCurrentUser, String replacementText,
			int cursorPosition) {
		return new PersonContentProposal(getPrettyName(address), isCurrentUser, replacementText, cursorPosition);
	}

	protected String getPrettyName(String address) {
		String value = proposals.get(address);
		if (value != null) {
			return NLS.bind("{0} <{1}>", value, address); //$NON-NLS-1$
		}
		return address;
	}

	private int getIndexOfLeftSeparator(String contents, int position) {
		int i = contents.lastIndexOf(' ', position - 1);
		i = Math.max(contents.lastIndexOf(',', position - 1), i);
		return i;
	}

	private int getIndexOfRightSeparator(String contents, int position) {
		int index = contents.length();
		int i = contents.indexOf(' ', position);
		if (i != -1) {
			index = Math.min(i, index);
		}
		i = contents.indexOf(',', position);
		if (i != -1) {
			index = Math.min(i, index);
		}
		return index;
	}

	private SortedSet<String> getAddressSet() {
		if (addressSet != null) {
			return addressSet;
		}

		addressSet = new TreeSet<String>(new Comparator<String>() {
			public int compare(String s1, String s2) {
				return s1.compareToIgnoreCase(s2);
			}
		});

		if (errorProposals != null && !errorProposals.isEmpty()) {
			for (String proposal : errorProposals.keySet()) {
				addAddress(addressSet, proposal);
			}
			return addressSet;
		}

		if (proposals.size() > 0) {
			if (repositoryUrl != null && connectorKind != null) {
				currentUser = getCurrentUser(repositoryUrl, connectorKind);
			}
			for (String proposal : proposals.keySet()) {
				addAddress(addressSet, proposal);
			}
			return addressSet;
		}

		if (currentTask != null) {
			addAddress(addressSet, currentTask.getOwner());
		}

		if (currentTaskData != null) {
			addAddresses(currentTaskData, addressSet);
		}

		if (repositoryUrl != null && connectorKind != null) {
			Set<AbstractTask> tasks = new HashSet<AbstractTask>();
			if (currentTask != null) {
				tasks.add(currentTask);
			}

			currentUser = getCurrentUser(repositoryUrl, connectorKind);
			if (currentUser != null) {
				addressSet.add(currentUser);
			}

			Collection<AbstractTask> allTasks = TasksUiPlugin.getTaskList().getAllTasks();
			for (AbstractTask task : allTasks) {
				if (repositoryUrl.equals(task.getRepositoryUrl())) {
					tasks.add(task);
				}
			}

			for (ITask task : tasks) {
				addAddresses(task, addressSet);
			}
		}

		return addressSet;
	}

	private String getCurrentUser(String repositoryUrl, String connectorKind) {
		TaskRepository repository = TasksUi.getRepositoryManager().getRepository(connectorKind, repositoryUrl);

		if (repository != null) {
			AuthenticationCredentials credentials = repository.getCredentials(AuthenticationType.REPOSITORY);
			if (credentials != null && credentials.getUserName().length() > 0) {
				return credentials.getUserName();
			}
		}
		return null;
	}

	private void addAddresses(ITask task, Set<String> addressSet) {
		addAddress(addressSet, task.getOwner());
	}

	private void addAddresses(TaskData data, Set<String> addressSet) {
		addPerson(data, addressSet, TaskAttribute.USER_REPORTER);
		addPerson(data, addressSet, TaskAttribute.USER_ASSIGNED);
		addPerson(data, addressSet, TaskAttribute.USER_CC);
		List<TaskAttribute> comments = data.getAttributeMapper().getAttributesByType(data, TaskAttribute.TYPE_COMMENT);
		for (TaskAttribute commentAttribute : comments) {
			addPerson(data, addressSet, commentAttribute);
		}
		List<TaskAttribute> attachments = data.getAttributeMapper().getAttributesByType(data,
				TaskAttribute.TYPE_ATTACHMENT);
		for (TaskAttribute attachmentAttribute : attachments) {
			addPerson(data, addressSet, attachmentAttribute);
		}
	}

	private void addPerson(TaskData data, Set<String> addresses, String key) {
		TaskAttribute attribute = data.getRoot().getMappedAttribute(key);
		if (attribute != null) {
			addPerson(data, addresses, attribute);
		}
	}

	private void addPerson(TaskData data, Set<String> addresses, TaskAttribute attribute) {
		TaskAttributeMetaData metaData = attribute.getMetaData();
		if (TaskAttribute.TYPE_COMMENT.equals(metaData.getType())) {
			TaskCommentMapper mapper = TaskCommentMapper.createFrom(attribute);
			addPerson(addresses, mapper.getAuthor());
		} else if (TaskAttribute.TYPE_ATTACHMENT.equals(metaData.getType())) {
			TaskAttachmentMapper mapper = TaskAttachmentMapper.createFrom(attribute);
			addPerson(addresses, mapper.getAuthor());
		} else if (TaskAttribute.TYPE_PERSON.equals(metaData.getType())) {
			addPerson(addresses, data.getAttributeMapper().getRepositoryPerson(attribute));
		} else {
			List<String> values = attribute.getValues();
			for (String value : values) {
				addAddress(addresses, value);
			}
		}
	}

	private void addPerson(Set<String> addresses, IRepositoryPerson repositoryPerson) {
		if (repositoryPerson != null) {
			addresses.add(repositoryPerson.getPersonId());
			if (repositoryPerson.getName() != null) {
				proposals.put(repositoryPerson.getPersonId(), repositoryPerson.getName());
			}
		}
	}

	private void addAddress(Set<String> addresses, String address) {
		if (address != null && address.trim().length() > 0) {
			addresses.add(address.trim());
		}
	}

	public Map<String, String> getProposals() {
		return proposals;
	}

	public Map<String, String> getErrorProposals() {
		return errorProposals;
	}

	public void setErrorProposals(Map<String, String> errorProposals) {
		this.errorProposals = errorProposals;
		addressSet = null;
	}
}

Back to the top