Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2010 Frank Becker 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 |
* Frank Becker - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
|
12 |
package org.eclipse.mylyn.internal.tasks.ui.views; |
13 |
|
14 |
import org.eclipse.jface.action.IMenuManager; |
15 |
import org.eclipse.jface.action.Separator; |
16 |
import org.eclipse.jface.dialogs.Dialog; |
17 |
import org.eclipse.jface.dialogs.PopupDialog; |
18 |
import org.eclipse.jface.text.IInformationControl; |
19 |
import org.eclipse.jface.text.IInformationControlExtension; |
20 |
import org.eclipse.jface.text.IInformationControlExtension2; |
21 |
import org.eclipse.jface.viewers.ILabelProvider; |
22 |
import org.eclipse.jface.viewers.IStructuredSelection; |
23 |
import org.eclipse.jface.viewers.StructuredSelection; |
24 |
import org.eclipse.mylyn.internal.tasks.ui.CommonViewerTaskDataFilter; |
25 |
import org.eclipse.mylyn.internal.tasks.ui.CommonViewerTaskDataOpenListener; |
26 |
import org.eclipse.mylyn.internal.tasks.ui.ITasksUiPreferenceConstants; |
27 |
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin; |
28 |
import org.eclipse.mylyn.tasks.core.data.TaskData; |
29 |
import org.eclipse.mylyn.tasks.ui.editors.AbstractTaskEditorPage; |
30 |
import org.eclipse.mylyn.tasks.ui.editors.TaskEditor; |
31 |
import org.eclipse.swt.SWT; |
32 |
import org.eclipse.swt.events.DisposeEvent; |
33 |
import org.eclipse.swt.events.DisposeListener; |
34 |
import org.eclipse.swt.events.FocusListener; |
35 |
import org.eclipse.swt.events.KeyEvent; |
36 |
import org.eclipse.swt.events.KeyListener; |
37 |
import org.eclipse.swt.events.ModifyEvent; |
38 |
import org.eclipse.swt.events.ModifyListener; |
39 |
import org.eclipse.swt.events.MouseAdapter; |
40 |
import org.eclipse.swt.events.MouseEvent; |
41 |
import org.eclipse.swt.events.SelectionEvent; |
42 |
import org.eclipse.swt.events.SelectionListener; |
43 |
import org.eclipse.swt.graphics.Color; |
44 |
import org.eclipse.swt.graphics.FontMetrics; |
45 |
import org.eclipse.swt.graphics.GC; |
46 |
import org.eclipse.swt.graphics.Point; |
47 |
import org.eclipse.swt.layout.GridData; |
48 |
import org.eclipse.swt.layout.GridLayout; |
49 |
import org.eclipse.swt.widgets.Button; |
50 |
import org.eclipse.swt.widgets.Composite; |
51 |
import org.eclipse.swt.widgets.Control; |
52 |
import org.eclipse.swt.widgets.Shell; |
53 |
import org.eclipse.swt.widgets.Text; |
54 |
import org.eclipse.swt.widgets.Tree; |
55 |
import org.eclipse.swt.widgets.TreeItem; |
56 |
import org.eclipse.ui.IEditorPart; |
57 |
import org.eclipse.ui.PlatformUI; |
58 |
import org.eclipse.ui.forms.editor.IFormPage; |
59 |
import org.eclipse.ui.internal.misc.StringMatcher; |
60 |
import org.eclipse.ui.navigator.CommonViewer; |
61 |
|
62 |
public class QuickOutlineTaskDataDialog extends PopupDialog implements IInformationControl, |
63 |
IInformationControlExtension, IInformationControlExtension2, DisposeListener { |
64 |
|
65 |
public static final String ID_VIEWER = "org.eclipse.mylyn.internal.tasks.ui.taskdata.quick"; //$NON-NLS-1$ |
66 |
|
67 |
private CommonViewer commonViewer; |
68 |
|
69 |
private Text fFilterText; |
70 |
|
71 |
private StringMatcher fStringMatcher; |
72 |
|
73 |
private CommonViewerTaskDataFilter namePatternFilter; |
74 |
|
75 |
private CommonViewerTaskDataOpenListener openListener; |
76 |
|
77 |
public QuickOutlineTaskDataDialog(Shell parent) { |
78 |
super(parent, SWT.RESIZE, true, true, true, true, true, null, Messages.QuickOutlineTaskDataDialog_InfoText); |
79 |
create(); |
80 |
} |
81 |
|
82 |
@Override |
83 |
public boolean close() { |
84 |
// nothing additional to dispose |
85 |
return super.close(); |
86 |
} |
87 |
|
88 |
@Override |
89 |
protected Control createDialogArea(Composite parent) { |
90 |
createViewer(parent); |
91 |
createUIListenersTreeViewer(); |
92 |
addDisposeListener(this); |
93 |
|
94 |
return commonViewer.getControl(); |
95 |
} |
96 |
|
97 |
private void createViewer(Composite parent) { |
98 |
Control composite = super.createDialogArea(parent); |
99 |
commonViewer = createCommonViewer((Composite) composite); |
100 |
openListener = new CommonViewerTaskDataOpenListener(commonViewer); |
101 |
|
102 |
commonViewer.addOpenListener(openListener); |
103 |
commonViewer.getTree().addMouseListener(openListener); |
104 |
|
105 |
namePatternFilter = new CommonViewerTaskDataFilter(); |
106 |
commonViewer.addFilter(namePatternFilter); |
107 |
try { |
108 |
commonViewer.getControl().setRedraw(false); |
109 |
IEditorPart activeEditor = PlatformUI.getWorkbench() |
110 |
.getActiveWorkbenchWindow() |
111 |
.getActivePage() |
112 |
.getActiveEditor(); |
113 |
if (activeEditor instanceof TaskEditor) { |
114 |
TaskEditor taskEditor = (TaskEditor) activeEditor; |
115 |
IFormPage pageInstance = taskEditor.getActivePageInstance(); |
116 |
if (pageInstance instanceof AbstractTaskEditorPage) { |
117 |
AbstractTaskEditorPage taskEditorPage = (AbstractTaskEditorPage) pageInstance; |
118 |
TaskData taskData = taskEditorPage.getModel().getTaskData(); |
119 |
commonViewer.setInput(taskData); |
120 |
} |
121 |
} |
122 |
commonViewer.expandAll(); |
123 |
} finally { |
124 |
commonViewer.getControl().setRedraw(true); |
125 |
} |
126 |
} |
127 |
|
128 |
protected CommonViewer createCommonViewer(Composite parent) { |
129 |
CommonViewer viewer = new CommonViewer(ID_VIEWER, parent, SWT.H_SCROLL | SWT.V_SCROLL); |
130 |
viewer.setUseHashlookup(true); |
131 |
viewer.getControl().setLayoutData(new GridData(500, 400)); |
132 |
return viewer; |
133 |
} |
134 |
|
135 |
@Override |
136 |
protected void fillDialogMenu(IMenuManager dialogMenu) { |
137 |
dialogMenu.add(new Separator()); |
138 |
super.fillDialogMenu(dialogMenu); |
139 |
} |
140 |
|
141 |
private void createUIListenersTreeViewer() { |
142 |
final Tree tree = commonViewer.getTree(); |
143 |
tree.addKeyListener(new KeyListener() { |
144 |
public void keyPressed(KeyEvent e) { |
145 |
if (e.character == 0x1B) { |
146 |
// Dispose on ESC key press |
147 |
dispose(); |
148 |
} |
149 |
} |
150 |
|
151 |
public void keyReleased(KeyEvent e) { |
152 |
// ignore |
153 |
} |
154 |
}); |
155 |
|
156 |
tree.addMouseListener(new MouseAdapter() { |
157 |
@Override |
158 |
public void mouseUp(MouseEvent e) { |
159 |
handleTreeViewerMouseUp(tree, e); |
160 |
} |
161 |
}); |
162 |
|
163 |
tree.addSelectionListener(new SelectionListener() { |
164 |
public void widgetSelected(SelectionEvent e) { |
165 |
// ignore |
166 |
} |
167 |
|
168 |
public void widgetDefaultSelected(SelectionEvent e) { |
169 |
gotoSelectedElement(); |
170 |
} |
171 |
}); |
172 |
} |
173 |
|
174 |
private void handleTreeViewerMouseUp(final Tree tree, MouseEvent e) { |
175 |
if ((tree.getSelectionCount() < 1) || (e.button != 1) || (tree.equals(e.getSource()) == false)) { |
176 |
return; |
177 |
} |
178 |
// Selection is made in the selection changed listener |
179 |
Object object = tree.getItem(new Point(e.x, e.y)); |
180 |
TreeItem selection = tree.getSelection()[0]; |
181 |
if (selection.equals(object)) { |
182 |
gotoSelectedElement(); |
183 |
} |
184 |
} |
185 |
|
186 |
private Object getSelectedElement() { |
187 |
if (commonViewer == null) { |
188 |
return null; |
189 |
} |
190 |
return ((IStructuredSelection) commonViewer.getSelection()).getFirstElement(); |
191 |
} |
192 |
|
193 |
public void addDisposeListener(DisposeListener listener) { |
194 |
getShell().addDisposeListener(listener); |
195 |
} |
196 |
|
197 |
public void addFocusListener(FocusListener listener) { |
198 |
getShell().addFocusListener(listener); |
199 |
} |
200 |
|
201 |
public Point computeSizeHint() { |
202 |
// Note that it already has the persisted size if persisting is enabled. |
203 |
return getShell().getSize(); |
204 |
} |
205 |
|
206 |
public void dispose() { |
207 |
close(); |
208 |
} |
209 |
|
210 |
public boolean isFocusControl() { |
211 |
if (commonViewer.getControl().isFocusControl() || fFilterText.isFocusControl()) { |
212 |
return true; |
213 |
} |
214 |
return false; |
215 |
} |
216 |
|
217 |
public void removeDisposeListener(DisposeListener listener) { |
218 |
getShell().removeDisposeListener(listener); |
219 |
} |
220 |
|
221 |
public void removeFocusListener(FocusListener listener) { |
222 |
getShell().removeFocusListener(listener); |
223 |
} |
224 |
|
225 |
public void setBackgroundColor(Color background) { |
226 |
applyBackgroundColor(background, getContents()); |
227 |
} |
228 |
|
229 |
public void setFocus() { |
230 |
getShell().forceFocus(); |
231 |
fFilterText.setFocus(); |
232 |
} |
233 |
|
234 |
public void setForegroundColor(Color foreground) { |
235 |
applyForegroundColor(foreground, getContents()); |
236 |
} |
237 |
|
238 |
public void setInformation(String information) { |
239 |
// See IInformationControlExtension2 |
240 |
} |
241 |
|
242 |
public void setLocation(Point location) { |
243 |
/* |
244 |
* If the location is persisted, it gets managed by PopupDialog - fine. Otherwise, the location is |
245 |
* computed in Window#getInitialLocation, which will center it in the parent shell / main |
246 |
* monitor, which is wrong for two reasons: |
247 |
* - we want to center over the editor / subject control, not the parent shell |
248 |
* - the center is computed via the initalSize, which may be also wrong since the size may |
249 |
* have been updated since via min/max sizing of AbstractInformationControlManager. |
250 |
* In that case, override the location with the one computed by the manager. Note that |
251 |
* the call to constrainShellSize in PopupDialog.open will still ensure that the shell is |
252 |
* entirely visible. |
253 |
*/ |
254 |
if (getPersistLocation() == false || getDialogSettings() == null) { |
255 |
getShell().setLocation(location); |
256 |
} |
257 |
} |
258 |
|
259 |
public void setSize(int width, int height) { |
260 |
getShell().setSize(width, height); |
261 |
} |
262 |
|
263 |
public void setSizeConstraints(int maxWidth, int maxHeight) { |
264 |
// Ignore |
265 |
} |
266 |
|
267 |
public void setVisible(boolean visible) { |
268 |
if (visible) { |
269 |
open(); |
270 |
} else { |
271 |
saveDialogBounds(getShell()); |
272 |
getShell().setVisible(false); |
273 |
} |
274 |
} |
275 |
|
276 |
public boolean hasContents() { |
277 |
if ((commonViewer == null) || (commonViewer.getInput() == null)) { |
278 |
return false; |
279 |
} |
280 |
return true; |
281 |
} |
282 |
|
283 |
public void setInput(Object input) { |
284 |
// Input comes from PDESourceInfoProvider.getInformation2() |
285 |
// The input should be a model object of some sort |
286 |
// Turn it into a structured selection and set the selection in the tree |
287 |
if (input != null) { |
288 |
commonViewer.setSelection(new StructuredSelection(input)); |
289 |
} |
290 |
} |
291 |
|
292 |
public void widgetDisposed(DisposeEvent e) { |
293 |
// Note: We do not reuse the dialog |
294 |
commonViewer = null; |
295 |
fFilterText = null; |
296 |
} |
297 |
|
298 |
@Override |
299 |
protected Control createTitleControl(Composite parent) { |
300 |
Composite control = new Composite(parent, SWT.NONE); |
301 |
GridLayout layout = new GridLayout(1, false); |
302 |
layout.marginHeight = 0; |
303 |
control.setLayout(layout); |
304 |
control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
305 |
|
306 |
// Applies only to dialog title - not body. See createDialogArea |
307 |
// Create the text widget |
308 |
createUIWidgetFilterText(control); |
309 |
// Add listeners to the text widget |
310 |
createUIListenersFilterText(); |
311 |
final Button showTaskDataButton = new Button(control, SWT.CHECK); |
312 |
showTaskDataButton.setText(Messages.QuickOutlineTaskDataDialog_ShowRawData); |
313 |
showTaskDataButton.setSelection(TasksUiPlugin.getDefault() |
314 |
.getPreferenceStore() |
315 |
.getBoolean(ITasksUiPreferenceConstants.QUICKOUTLINE_TASKMODE_TASKDATA)); |
316 |
showTaskDataButton.addSelectionListener(new SelectionListener() { |
317 |
|
318 |
public void widgetSelected(SelectionEvent e) { |
319 |
// ignore |
320 |
boolean selection = showTaskDataButton.getSelection(); |
321 |
TasksUiPlugin.getDefault() |
322 |
.getPreferenceStore() |
323 |
.setValue(ITasksUiPreferenceConstants.QUICKOUTLINE_TASKMODE_TASKDATA, selection); |
324 |
IEditorPart activeEditor = PlatformUI.getWorkbench() |
325 |
.getActiveWorkbenchWindow() |
326 |
.getActivePage() |
327 |
.getActiveEditor(); |
328 |
if (activeEditor instanceof TaskEditor) { |
329 |
TaskEditor taskEditor = (TaskEditor) activeEditor; |
330 |
IFormPage pageInstance = taskEditor.getActivePageInstance(); |
331 |
if (pageInstance instanceof AbstractTaskEditorPage) { |
332 |
AbstractTaskEditorPage taskEditorPage = (AbstractTaskEditorPage) pageInstance; |
333 |
TaskData taskData = taskEditorPage.getModel().getTaskData(); |
334 |
commonViewer.setInput(taskData); |
335 |
if (!selection) { |
336 |
commonViewer.expandAll(); |
337 |
} |
338 |
} |
339 |
} |
340 |
|
341 |
} |
342 |
|
343 |
public void widgetDefaultSelected(SelectionEvent e) { |
344 |
// ignore |
345 |
|
346 |
} |
347 |
}); |
348 |
|
349 |
// Return the text widget |
350 |
return control; |
351 |
} |
352 |
|
353 |
private void createUIWidgetFilterText(Composite parent) { |
354 |
// Create the widget |
355 |
fFilterText = new Text(parent, SWT.NONE); |
356 |
// Set the font |
357 |
GC gc = new GC(parent); |
358 |
gc.setFont(parent.getFont()); |
359 |
FontMetrics fontMetrics = gc.getFontMetrics(); |
360 |
gc.dispose(); |
361 |
// Create the layout |
362 |
GridData data = new GridData(GridData.FILL_HORIZONTAL); |
363 |
data.heightHint = Dialog.convertHeightInCharsToPixels(fontMetrics, 1); |
364 |
data.horizontalAlignment = GridData.FILL; |
365 |
data.verticalAlignment = GridData.CENTER; |
366 |
fFilterText.setLayoutData(data); |
367 |
} |
368 |
|
369 |
/** |
370 |
* |
371 |
*/ |
372 |
private void gotoSelectedElement() { |
373 |
Object selectedElement = getSelectedElement(); |
374 |
if (selectedElement == null) { |
375 |
return; |
376 |
} |
377 |
dispose(); |
378 |
} |
379 |
|
380 |
private void createUIListenersFilterText() { |
381 |
fFilterText.addKeyListener(new KeyListener() { |
382 |
public void keyPressed(KeyEvent e) { |
383 |
if (e.keyCode == 0x0D) { |
384 |
// Return key was pressed |
385 |
gotoSelectedElement(); |
386 |
} else if (e.keyCode == SWT.ARROW_DOWN) { |
387 |
// Down key was pressed |
388 |
commonViewer.getTree().setFocus(); |
389 |
} else if (e.keyCode == SWT.ARROW_UP) { |
390 |
// Up key was pressed |
391 |
commonViewer.getTree().setFocus(); |
392 |
} else if (e.character == 0x1B) { |
393 |
// Escape key was pressed |
394 |
dispose(); |
395 |
} |
396 |
} |
397 |
|
398 |
public void keyReleased(KeyEvent e) { |
399 |
// NO-OP |
400 |
} |
401 |
}); |
402 |
// Handle text modify events |
403 |
fFilterText.addModifyListener(new ModifyListener() { |
404 |
public void modifyText(ModifyEvent e) { |
405 |
String text = ((Text) e.widget).getText(); |
406 |
int length = text.length(); |
407 |
if (length > 0) { |
408 |
// Append a '*' pattern to the end of the text value if it |
409 |
// does not have one already |
410 |
if (text.charAt(length - 1) != '*') { |
411 |
text = text + '*'; |
412 |
} |
413 |
// Prepend a '*' pattern to the beginning of the text value |
414 |
// if it does not have one already |
415 |
if (text.charAt(0) != '*') { |
416 |
text = '*' + text; |
417 |
} |
418 |
} |
419 |
// Set and update the pattern |
420 |
setMatcherString(text, true); |
421 |
} |
422 |
}); |
423 |
} |
424 |
|
425 |
/** |
426 |
* Sets the patterns to filter out for the receiver. |
427 |
* <p> |
428 |
* The following characters have special meaning: ? => any character * => any string |
429 |
* </p> |
430 |
* |
431 |
* @param pattern |
432 |
* the pattern |
433 |
* @param update |
434 |
* <code>true</code> if the viewer should be updated |
435 |
*/ |
436 |
private void setMatcherString(String pattern, boolean update) { |
437 |
if (pattern.length() == 0) { |
438 |
fStringMatcher = null; |
439 |
} else { |
440 |
fStringMatcher = new StringMatcher(pattern, true, false); |
441 |
} |
442 |
// Update the name pattern filter on the tree viewer |
443 |
namePatternFilter.setStringMatcher(fStringMatcher); |
444 |
// Update the tree viewer according to the pattern |
445 |
if (update) { |
446 |
stringMatcherUpdated(); |
447 |
} |
448 |
} |
449 |
|
450 |
/** |
451 |
* The string matcher has been modified. The default implementation refreshes the view and selects the first matched |
452 |
* element |
453 |
*/ |
454 |
private void stringMatcherUpdated() { |
455 |
// Refresh the tree viewer to re-filter |
456 |
commonViewer.getControl().setRedraw(false); |
457 |
commonViewer.refresh(); |
458 |
commonViewer.expandAll(); |
459 |
selectFirstMatch(); |
460 |
commonViewer.getControl().setRedraw(true); |
461 |
} |
462 |
|
463 |
/** |
464 |
* Selects the first element in the tree which matches the current filter pattern. |
465 |
*/ |
466 |
private void selectFirstMatch() { |
467 |
Tree tree = commonViewer.getTree(); |
468 |
Object element = findFirstMatchToPattern(tree.getItems()); |
469 |
if (element != null) { |
470 |
commonViewer.setSelection(new StructuredSelection(element), true); |
471 |
} else { |
472 |
commonViewer.setSelection(StructuredSelection.EMPTY); |
473 |
} |
474 |
} |
475 |
|
476 |
/** |
477 |
* @param items |
478 |
* @return |
479 |
*/ |
480 |
private Object findFirstMatchToPattern(TreeItem[] items) { |
481 |
// Match the string pattern against labels |
482 |
ILabelProvider labelProvider = (ILabelProvider) commonViewer.getLabelProvider(); |
483 |
// Process each item in the tree |
484 |
for (TreeItem item : items) { |
485 |
Object element = item.getData(); |
486 |
// Return the first element if no pattern is set |
487 |
if (fStringMatcher == null) { |
488 |
return element; |
489 |
} |
490 |
// Return the element if it matches the pattern |
491 |
if (element != null) { |
492 |
String label = labelProvider.getText(element); |
493 |
if (fStringMatcher.match(label)) { |
494 |
return element; |
495 |
} |
496 |
} |
497 |
// Recursively check the elements children for a match |
498 |
element = findFirstMatchToPattern(item.getItems()); |
499 |
// Return the child element match if found |
500 |
if (element != null) { |
501 |
return element; |
502 |
} |
503 |
} |
504 |
// No match found |
505 |
return null; |
506 |
} |
507 |
|
508 |
} |