|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2003, 2011 IBM Corporation 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 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.ui.internal; |
| 12 |
|
| 13 |
import org.eclipse.jface.util.Util; |
| 14 |
import org.eclipse.jface.viewers.ILabelProvider; |
| 15 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 16 |
import org.eclipse.jface.viewers.StructuredSelection; |
| 17 |
import org.eclipse.jface.viewers.TableViewer; |
| 18 |
import org.eclipse.jface.viewers.Viewer; |
| 19 |
import org.eclipse.jface.viewers.ViewerFilter; |
| 20 |
import org.eclipse.swt.SWT; |
| 21 |
import org.eclipse.swt.events.KeyEvent; |
| 22 |
import org.eclipse.swt.events.KeyListener; |
| 23 |
import org.eclipse.swt.events.ModifyEvent; |
| 24 |
import org.eclipse.swt.events.ModifyListener; |
| 25 |
import org.eclipse.swt.events.MouseAdapter; |
| 26 |
import org.eclipse.swt.events.MouseEvent; |
| 27 |
import org.eclipse.swt.events.MouseMoveListener; |
| 28 |
import org.eclipse.swt.events.SelectionAdapter; |
| 29 |
import org.eclipse.swt.events.SelectionEvent; |
| 30 |
import org.eclipse.swt.events.SelectionListener; |
| 31 |
import org.eclipse.swt.events.TraverseEvent; |
| 32 |
import org.eclipse.swt.events.TraverseListener; |
| 33 |
import org.eclipse.swt.graphics.Color; |
| 34 |
import org.eclipse.swt.graphics.FontMetrics; |
| 35 |
import org.eclipse.swt.graphics.GC; |
| 36 |
import org.eclipse.swt.graphics.Point; |
| 37 |
import org.eclipse.swt.graphics.Rectangle; |
| 38 |
import org.eclipse.swt.layout.FillLayout; |
| 39 |
import org.eclipse.swt.layout.GridData; |
| 40 |
import org.eclipse.swt.layout.GridLayout; |
| 41 |
import org.eclipse.swt.widgets.Composite; |
| 42 |
import org.eclipse.swt.widgets.Display; |
| 43 |
import org.eclipse.swt.widgets.Item; |
| 44 |
import org.eclipse.swt.widgets.Label; |
| 45 |
import org.eclipse.swt.widgets.Menu; |
| 46 |
import org.eclipse.swt.widgets.MenuItem; |
| 47 |
import org.eclipse.swt.widgets.Shell; |
| 48 |
import org.eclipse.swt.widgets.Table; |
| 49 |
import org.eclipse.swt.widgets.TableItem; |
| 50 |
import org.eclipse.swt.widgets.Text; |
| 51 |
import org.eclipse.ui.internal.misc.StringMatcher; |
| 52 |
|
| 53 |
/** |
| 54 |
* @since 3.0 |
| 55 |
*/ |
| 56 |
public abstract class AbstractTableInformationControl { |
| 57 |
|
| 58 |
/** |
| 59 |
* The NamePatternFilter selects the elements which match the given string |
| 60 |
* patterns. |
| 61 |
*/ |
| 62 |
protected class NamePatternFilter extends ViewerFilter { |
| 63 |
|
| 64 |
/* |
| 65 |
* (non-Javadoc) Method declared on ViewerFilter. |
| 66 |
*/ |
| 67 |
public boolean select(Viewer viewer, Object parentElement, Object element) { |
| 68 |
StringMatcher matcher = getMatcher(); |
| 69 |
if (matcher == null || !(viewer instanceof TableViewer)) { |
| 70 |
return true; |
| 71 |
} |
| 72 |
TableViewer tableViewer = (TableViewer) viewer; |
| 73 |
|
| 74 |
String matchName = ((ILabelProvider) tableViewer.getLabelProvider()).getText(element); |
| 75 |
|
| 76 |
if (matchName == null) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
// A dirty editor's label will start with dirty prefix, this prefix |
| 80 |
// should not be taken in consideration when matching with a pattern |
| 81 |
if (matchName.startsWith("*")) { //$NON-NLS-1$ |
| 82 |
matchName = matchName.substring(1); |
| 83 |
} |
| 84 |
return matcher.match(matchName); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** The control's shell */ |
| 89 |
private Shell fShell; |
| 90 |
|
| 91 |
/** The composite */ |
| 92 |
private Composite fComposite; |
| 93 |
|
| 94 |
/** The control's text widget */ |
| 95 |
private Text fFilterText; |
| 96 |
|
| 97 |
/** The control's table widget */ |
| 98 |
private TableViewer fTableViewer; |
| 99 |
|
| 100 |
/** The current string matcher */ |
| 101 |
private StringMatcher fStringMatcher; |
| 102 |
|
| 103 |
/** |
| 104 |
* Creates an information control with the given shell as parent. The given |
| 105 |
* styles are applied to the shell and the table widget. |
| 106 |
* |
| 107 |
* @param parent |
| 108 |
* the parent shell |
| 109 |
* @param shellStyle |
| 110 |
* the additional styles for the shell |
| 111 |
* @param controlStyle |
| 112 |
* the additional styles for the control |
| 113 |
*/ |
| 114 |
public AbstractTableInformationControl(Shell parent, int shellStyle, int controlStyle) { |
| 115 |
fShell = new Shell(parent, shellStyle); |
| 116 |
fShell.setLayout(new FillLayout()); |
| 117 |
|
| 118 |
// Composite for filter text and viewer |
| 119 |
fComposite = new Composite(fShell, SWT.RESIZE); |
| 120 |
GridLayout layout = new GridLayout(1, false); |
| 121 |
fComposite.setLayout(layout); |
| 122 |
createFilterText(fComposite); |
| 123 |
|
| 124 |
fTableViewer = createTableViewer(fComposite, controlStyle); |
| 125 |
|
| 126 |
final Table table = fTableViewer.getTable(); |
| 127 |
table.addKeyListener(new KeyListener() { |
| 128 |
public void keyPressed(KeyEvent e) { |
| 129 |
switch (e.keyCode) { |
| 130 |
case SWT.ESC: |
| 131 |
dispose(); |
| 132 |
break; |
| 133 |
case SWT.DEL: |
| 134 |
removeSelectedItems(); |
| 135 |
e.character = SWT.NONE; |
| 136 |
e.doit = false; |
| 137 |
break; |
| 138 |
case SWT.ARROW_UP: |
| 139 |
if (table.getSelectionIndex() == 0) { |
| 140 |
// on the first item, going up should grant focus to |
| 141 |
// text field |
| 142 |
fFilterText.setFocus(); |
| 143 |
} |
| 144 |
break; |
| 145 |
case SWT.ARROW_DOWN: |
| 146 |
if (table.getSelectionIndex() == table.getItemCount() - 1) { |
| 147 |
// on the last item, going down should grant focus to |
| 148 |
// the text field |
| 149 |
fFilterText.setFocus(); |
| 150 |
} |
| 151 |
break; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
public void keyReleased(KeyEvent e) { |
| 156 |
// do nothing |
| 157 |
} |
| 158 |
}); |
| 159 |
|
| 160 |
table.addSelectionListener(new SelectionListener() { |
| 161 |
public void widgetSelected(SelectionEvent e) { |
| 162 |
// do nothing; |
| 163 |
} |
| 164 |
|
| 165 |
public void widgetDefaultSelected(SelectionEvent e) { |
| 166 |
gotoSelectedElement(); |
| 167 |
} |
| 168 |
}); |
| 169 |
|
| 170 |
/* |
| 171 |
* Bug in GTK, see SWT bug: 62405 Editor drop down performance slow on |
| 172 |
* Linux-GTK on mouse move. Rather then removing the support altogether |
| 173 |
* this feature has been worked around for GTK only as we expect that |
| 174 |
* newer versions of GTK will no longer exhibit this quality and we will |
| 175 |
* be able to have the desired support running on all platforms. See |
| 176 |
* comment https://bugs.eclipse.org/bugs/show_bug.cgi?id=62405#c22 TODO: |
| 177 |
* remove this code once bug 62405 is fixed for the mainstream GTK |
| 178 |
* version |
| 179 |
*/ |
| 180 |
final int ignoreEventCount = Util.isGtk() ? 4 : 1; |
| 181 |
|
| 182 |
table.addMouseMoveListener(new MouseMoveListener() { |
| 183 |
TableItem fLastItem = null; |
| 184 |
int lastY = 0; |
| 185 |
int itemHeightdiv4 = table.getItemHeight() / 4; |
| 186 |
int tableHeight = table.getBounds().height; |
| 187 |
Point tableLoc = table.toDisplay(0, 0); |
| 188 |
int divCount = 0; |
| 189 |
|
| 190 |
public void mouseMove(MouseEvent e) { |
| 191 |
if (divCount == ignoreEventCount) { |
| 192 |
divCount = 0; |
| 193 |
} |
| 194 |
if (table.equals(e.getSource()) & ++divCount == ignoreEventCount) { |
| 195 |
Object o = table.getItem(new Point(e.x, e.y)); |
| 196 |
if (fLastItem == null ^ o == null) { |
| 197 |
table.setCursor(o == null ? null : table.getDisplay().getSystemCursor( |
| 198 |
SWT.CURSOR_HAND)); |
| 199 |
} |
| 200 |
if (o instanceof TableItem && lastY != e.y) { |
| 201 |
lastY = e.y; |
| 202 |
if (!o.equals(fLastItem)) { |
| 203 |
fLastItem = (TableItem) o; |
| 204 |
table.setSelection(new TableItem[] { fLastItem }); |
| 205 |
} else if (e.y < itemHeightdiv4) { |
| 206 |
// Scroll up |
| 207 |
Item item = fTableViewer.scrollUp(e.x + tableLoc.x, e.y + tableLoc.y); |
| 208 |
if (item instanceof TableItem) { |
| 209 |
fLastItem = (TableItem) item; |
| 210 |
table.setSelection(new TableItem[] { fLastItem }); |
| 211 |
} |
| 212 |
} else if (e.y > tableHeight - itemHeightdiv4) { |
| 213 |
// Scroll down |
| 214 |
Item item = fTableViewer.scrollDown(e.x + tableLoc.x, e.y + tableLoc.y); |
| 215 |
if (item instanceof TableItem) { |
| 216 |
fLastItem = (TableItem) item; |
| 217 |
table.setSelection(new TableItem[] { fLastItem }); |
| 218 |
} |
| 219 |
} |
| 220 |
} else if (o == null) { |
| 221 |
fLastItem = null; |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
}); |
| 226 |
|
| 227 |
table.addMouseListener(new MouseAdapter() { |
| 228 |
public void mouseUp(MouseEvent e) { |
| 229 |
if (table.getSelectionCount() < 1) { |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
if (e.button == 1) { |
| 234 |
if (table.equals(e.getSource())) { |
| 235 |
Object o = table.getItem(new Point(e.x, e.y)); |
| 236 |
TableItem selection = table.getSelection()[0]; |
| 237 |
if (selection.equals(o)) { |
| 238 |
gotoSelectedElement(); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
if (e.button == 3) { |
| 243 |
TableItem tItem = fTableViewer.getTable().getItem(new Point(e.x, e.y)); |
| 244 |
if (tItem != null) { |
| 245 |
Menu menu = new Menu(fTableViewer.getTable()); |
| 246 |
MenuItem mItem = new MenuItem(menu, SWT.NONE); |
| 247 |
mItem.setText(WorkbenchMessages.PartPane_close); |
| 248 |
mItem.addSelectionListener(new SelectionAdapter() { |
| 249 |
public void widgetSelected(SelectionEvent selectionEvent) { |
| 250 |
removeSelectedItems(); |
| 251 |
} |
| 252 |
}); |
| 253 |
menu.setVisible(true); |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
}); |
| 258 |
|
| 259 |
fShell.addTraverseListener(new TraverseListener() { |
| 260 |
public void keyTraversed(TraverseEvent e) { |
| 261 |
switch (e.detail) { |
| 262 |
case SWT.TRAVERSE_PAGE_NEXT: |
| 263 |
e.detail = SWT.TRAVERSE_NONE; |
| 264 |
e.doit = true; |
| 265 |
{ |
| 266 |
int n = table.getItemCount(); |
| 267 |
if (n == 0) |
| 268 |
return; |
| 269 |
|
| 270 |
int i = table.getSelectionIndex() + 1; |
| 271 |
if (i >= n) |
| 272 |
i = 0; |
| 273 |
table.setSelection(i); |
| 274 |
} |
| 275 |
break; |
| 276 |
|
| 277 |
case SWT.TRAVERSE_PAGE_PREVIOUS: |
| 278 |
e.detail = SWT.TRAVERSE_NONE; |
| 279 |
e.doit = true; |
| 280 |
{ |
| 281 |
int n = table.getItemCount(); |
| 282 |
if (n == 0) |
| 283 |
return; |
| 284 |
|
| 285 |
int i = table.getSelectionIndex() - 1; |
| 286 |
if (i < 0) |
| 287 |
i = n - 1; |
| 288 |
table.setSelection(i); |
| 289 |
} |
| 290 |
break; |
| 291 |
} |
| 292 |
} |
| 293 |
}); |
| 294 |
|
| 295 |
setInfoSystemColor(); |
| 296 |
installFilter(); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Removes the selected items from the list and closes their corresponding |
| 301 |
* tabs Selects the next item in the list or disposes it if its presentation |
| 302 |
* is disposed |
| 303 |
*/ |
| 304 |
protected void removeSelectedItems() { |
| 305 |
int selInd = fTableViewer.getTable().getSelectionIndex(); |
| 306 |
if (deleteSelectedElements()) { |
| 307 |
return; |
| 308 |
} |
| 309 |
fTableViewer.refresh(); |
| 310 |
if (selInd >= fTableViewer.getTable().getItemCount()) { |
| 311 |
selInd = fTableViewer.getTable().getItemCount() - 1; |
| 312 |
} |
| 313 |
if (selInd >= 0) { |
| 314 |
fTableViewer.getTable().setSelection(selInd); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
protected abstract TableViewer createTableViewer(Composite parent, int style); |
| 319 |
|
| 320 |
public TableViewer getTableViewer() { |
| 321 |
return fTableViewer; |
| 322 |
} |
| 323 |
|
| 324 |
protected Text createFilterText(Composite parent) { |
| 325 |
fFilterText = new Text(parent, SWT.NONE); |
| 326 |
|
| 327 |
GridData data = new GridData(); |
| 328 |
GC gc = new GC(parent); |
| 329 |
gc.setFont(parent.getFont()); |
| 330 |
FontMetrics fontMetrics = gc.getFontMetrics(); |
| 331 |
gc.dispose(); |
| 332 |
|
| 333 |
data.heightHint = org.eclipse.jface.dialogs.Dialog.convertHeightInCharsToPixels( |
| 334 |
fontMetrics, 1); |
| 335 |
data.horizontalAlignment = GridData.FILL; |
| 336 |
data.verticalAlignment = GridData.BEGINNING; |
| 337 |
fFilterText.setLayoutData(data); |
| 338 |
|
| 339 |
fFilterText.addKeyListener(new KeyListener() { |
| 340 |
public void keyPressed(KeyEvent e) { |
| 341 |
switch (e.keyCode) { |
| 342 |
case SWT.CR: |
| 343 |
case SWT.KEYPAD_CR: |
| 344 |
gotoSelectedElement(); |
| 345 |
break; |
| 346 |
case SWT.ARROW_DOWN: |
| 347 |
fTableViewer.getTable().setFocus(); |
| 348 |
fTableViewer.getTable().setSelection(0); |
| 349 |
break; |
| 350 |
case SWT.ARROW_UP: |
| 351 |
fTableViewer.getTable().setFocus(); |
| 352 |
fTableViewer.getTable() |
| 353 |
.setSelection(fTableViewer.getTable().getItemCount() - 1); |
| 354 |
break; |
| 355 |
case SWT.ESC: |
| 356 |
dispose(); |
| 357 |
break; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
public void keyReleased(KeyEvent e) { |
| 362 |
// do nothing |
| 363 |
} |
| 364 |
}); |
| 365 |
|
| 366 |
// Horizontal separator line |
| 367 |
Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.LINE_DOT); |
| 368 |
separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
| 369 |
|
| 370 |
return fFilterText; |
| 371 |
} |
| 372 |
|
| 373 |
private void setInfoSystemColor() { |
| 374 |
Display display = fShell.getDisplay(); |
| 375 |
setForegroundColor(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND)); |
| 376 |
setBackgroundColor(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND)); |
| 377 |
} |
| 378 |
|
| 379 |
private void installFilter() { |
| 380 |
fFilterText.setText(""); //$NON-NLS-1$ |
| 381 |
|
| 382 |
fFilterText.addModifyListener(new ModifyListener() { |
| 383 |
public void modifyText(ModifyEvent e) { |
| 384 |
String text = ((Text) e.widget).getText(); |
| 385 |
int length = text.length(); |
| 386 |
if (length > 0 && text.charAt(length - 1) != '*') { |
| 387 |
text = text + '*'; |
| 388 |
} |
| 389 |
setMatcherString(text); |
| 390 |
} |
| 391 |
}); |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* The string matcher has been modified. The default implementation |
| 396 |
* refreshes the view and selects the first macthed element |
| 397 |
*/ |
| 398 |
private void stringMatcherUpdated() { |
| 399 |
// refresh viewer to refilter |
| 400 |
fTableViewer.getControl().setRedraw(false); |
| 401 |
fTableViewer.refresh(); |
| 402 |
selectFirstMatch(); |
| 403 |
fTableViewer.getControl().setRedraw(true); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Sets the patterns to filter out for the receiver. |
| 408 |
* <p> |
| 409 |
* The following characters have special meaning: ? => any character * => |
| 410 |
* any string |
| 411 |
* </p> |
| 412 |
*/ |
| 413 |
private void setMatcherString(String pattern) { |
| 414 |
if (pattern.length() == 0) { |
| 415 |
fStringMatcher = null; |
| 416 |
} else { |
| 417 |
boolean ignoreCase = pattern.toLowerCase().equals(pattern); |
| 418 |
fStringMatcher = new StringMatcher(pattern, ignoreCase, false); |
| 419 |
} |
| 420 |
stringMatcherUpdated(); |
| 421 |
} |
| 422 |
|
| 423 |
private StringMatcher getMatcher() { |
| 424 |
return fStringMatcher; |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* Implementers can modify |
| 429 |
*/ |
| 430 |
protected Object getSelectedElement() { |
| 431 |
return ((IStructuredSelection) fTableViewer.getSelection()).getFirstElement(); |
| 432 |
} |
| 433 |
|
| 434 |
protected abstract void gotoSelectedElement(); |
| 435 |
|
| 436 |
/** |
| 437 |
* Delete all selected elements. |
| 438 |
* |
| 439 |
* @return <code>true</code> if there are no elements left after deletion. |
| 440 |
*/ |
| 441 |
protected abstract boolean deleteSelectedElements(); |
| 442 |
|
| 443 |
/** |
| 444 |
* Selects the first element in the table which matches the current filter |
| 445 |
* pattern. |
| 446 |
*/ |
| 447 |
protected void selectFirstMatch() { |
| 448 |
Table table = fTableViewer.getTable(); |
| 449 |
Object element = findElement(table.getItems()); |
| 450 |
if (element != null) { |
| 451 |
fTableViewer.setSelection(new StructuredSelection(element), true); |
| 452 |
} else { |
| 453 |
fTableViewer.setSelection(StructuredSelection.EMPTY); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
private Object findElement(TableItem[] items) { |
| 458 |
ILabelProvider labelProvider = (ILabelProvider) fTableViewer.getLabelProvider(); |
| 459 |
for (int i = 0; i < items.length; i++) { |
| 460 |
Object element = items[i].getData(); |
| 461 |
if (fStringMatcher == null) { |
| 462 |
return element; |
| 463 |
} |
| 464 |
|
| 465 |
if (element != null) { |
| 466 |
String label = labelProvider.getText(element); |
| 467 |
if (label == null) { |
| 468 |
return null; |
| 469 |
} |
| 470 |
// remove the dirty prefix from the editor's label |
| 471 |
if (label.startsWith("*")) { //$NON-NLS-1$ |
| 472 |
label = label.substring(1); |
| 473 |
} |
| 474 |
if (fStringMatcher.match(label)) { |
| 475 |
return element; |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
return null; |
| 480 |
} |
| 481 |
|
| 482 |
public void setVisible(boolean visible) { |
| 483 |
fShell.setVisible(visible); |
| 484 |
} |
| 485 |
|
| 486 |
public void dispose() { |
| 487 |
if (fShell != null) { |
| 488 |
if (!fShell.isDisposed()) { |
| 489 |
fShell.dispose(); |
| 490 |
} |
| 491 |
fShell = null; |
| 492 |
fTableViewer = null; |
| 493 |
fComposite = null; |
| 494 |
fFilterText = null; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
public Point computeSizeHint() { |
| 499 |
return fShell.computeSize(SWT.DEFAULT, SWT.DEFAULT); |
| 500 |
} |
| 501 |
|
| 502 |
public void setLocation(Point location) { |
| 503 |
Rectangle trim = fShell.computeTrim(0, 0, 0, 0); |
| 504 |
Point textLocation = fComposite.getLocation(); |
| 505 |
location.x += trim.x - textLocation.x; |
| 506 |
location.y += trim.y - textLocation.y; |
| 507 |
fShell.setLocation(location); |
| 508 |
} |
| 509 |
|
| 510 |
public void setSize(int width, int height) { |
| 511 |
fShell.setSize(width, height); |
| 512 |
} |
| 513 |
|
| 514 |
public Shell getShell() { |
| 515 |
return fShell; |
| 516 |
} |
| 517 |
|
| 518 |
private void setForegroundColor(Color foreground) { |
| 519 |
fTableViewer.getTable().setForeground(foreground); |
| 520 |
fFilterText.setForeground(foreground); |
| 521 |
fComposite.setForeground(foreground); |
| 522 |
} |
| 523 |
|
| 524 |
private void setBackgroundColor(Color background) { |
| 525 |
fTableViewer.getTable().setBackground(background); |
| 526 |
fFilterText.setBackground(background); |
| 527 |
fComposite.setBackground(background); |
| 528 |
} |
| 529 |
|
| 530 |
public void setFocus() { |
| 531 |
fShell.forceFocus(); |
| 532 |
fFilterText.setFocus(); |
| 533 |
} |
| 534 |
} |