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