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