1 13 package org.eclipse.ui.dialogs; 14 15 import org.eclipse.core.runtime.Assert; 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.jface.dialogs.IDialogConstants; 19 import org.eclipse.jface.viewers.ILabelProvider; 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.custom.BusyIndicator; 22 import org.eclipse.swt.events.KeyEvent; 23 import org.eclipse.swt.events.KeyListener; 24 import org.eclipse.swt.events.SelectionEvent; 25 import org.eclipse.swt.events.SelectionListener; 26 import org.eclipse.swt.layout.GridData; 27 import org.eclipse.swt.widgets.Button; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Event; 30 import org.eclipse.swt.widgets.Label; 31 import org.eclipse.swt.widgets.Listener; 32 import org.eclipse.swt.widgets.Shell; 33 import org.eclipse.swt.widgets.Text; 34 import org.eclipse.ui.PlatformUI; 35 36 41 public abstract class AbstractElementListSelectionDialog extends 42 SelectionStatusDialog { 43 44 private ILabelProvider fRenderer; 45 46 private boolean fIgnoreCase = true; 47 48 private boolean fIsMultipleSelection = false; 49 50 private boolean fMatchEmptyString = true; 51 52 private boolean fAllowDuplicates = true; 53 54 private Label fMessage; 55 56 protected FilteredList fFilteredList; 57 58 private Text fFilterText; 59 60 private ISelectionStatusValidator fValidator; 61 62 private String fFilter = null; 63 64 private String fEmptyListMessage = ""; 66 private String fEmptySelectionMessage = ""; 68 private int fWidth = 60; 69 70 private int fHeight = 18; 71 72 private Object [] fSelection = new Object [0]; 73 74 79 protected AbstractElementListSelectionDialog(Shell parent, 80 ILabelProvider renderer) { 81 super(parent); 82 fRenderer = renderer; 83 84 int shellStyle = getShellStyle(); 85 setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE); 86 } 87 88 92 protected void handleDefaultSelected() { 93 if (validateCurrentSelection()) { 94 buttonPressed(IDialogConstants.OK_ID); 95 } 96 } 97 98 102 public void setIgnoreCase(boolean ignoreCase) { 103 fIgnoreCase = ignoreCase; 104 } 105 106 110 public boolean isCaseIgnored() { 111 return fIgnoreCase; 112 } 113 114 119 public void setMatchEmptyString(boolean matchEmptyString) { 120 fMatchEmptyString = matchEmptyString; 121 } 122 123 127 public void setMultipleSelection(boolean multipleSelection) { 128 fIsMultipleSelection = multipleSelection; 129 } 130 131 135 public void setAllowDuplicates(boolean allowDuplicates) { 136 fAllowDuplicates = allowDuplicates; 137 } 138 139 144 public void setSize(int width, int height) { 145 fWidth = width; 146 fHeight = height; 147 } 148 149 153 public void setEmptyListMessage(String message) { 154 fEmptyListMessage = message; 155 } 156 157 161 public void setEmptySelectionMessage(String message) { 162 fEmptySelectionMessage = message; 163 } 164 165 170 public void setValidator(ISelectionStatusValidator validator) { 171 fValidator = validator; 172 } 173 174 179 protected void setListElements(Object [] elements) { 180 Assert.isNotNull(fFilteredList); 181 fFilteredList.setElements(elements); 182 } 183 184 188 public void setFilter(String filter) { 189 if (fFilterText == null) { 190 fFilter = filter; 191 } else { 192 fFilterText.setText(filter); 193 } 194 } 195 196 200 public String getFilter() { 201 if (fFilteredList == null) { 202 return fFilter; 203 } else { 204 return fFilteredList.getFilter(); 205 } 206 } 207 208 213 protected int[] getSelectionIndices() { 214 Assert.isNotNull(fFilteredList); 215 return fFilteredList.getSelectionIndices(); 216 } 217 218 223 protected int getSelectionIndex() { 224 Assert.isNotNull(fFilteredList); 225 return fFilteredList.getSelectionIndex(); 226 } 227 228 234 protected void setSelection(Object [] selection) { 235 Assert.isNotNull(fFilteredList); 236 fFilteredList.setSelection(selection); 237 } 238 239 244 protected Object [] getSelectedElements() { 245 Assert.isNotNull(fFilteredList); 246 return fFilteredList.getSelection(); 247 } 248 249 254 public Object [] getFoldedElements(int index) { 255 Assert.isNotNull(fFilteredList); 256 return fFilteredList.getFoldedElements(index); 257 } 258 259 263 protected Label createMessageArea(Composite composite) { 264 Label label = super.createMessageArea(composite); 265 266 GridData data = new GridData(); 267 data.grabExcessVerticalSpace = false; 268 data.grabExcessHorizontalSpace = true; 269 data.horizontalAlignment = GridData.FILL; 270 data.verticalAlignment = GridData.BEGINNING; 271 label.setLayoutData(data); 272 273 fMessage = label; 274 275 return label; 276 } 277 278 282 protected void handleSelectionChanged() { 283 validateCurrentSelection(); 284 } 285 286 292 protected boolean validateCurrentSelection() { 293 Assert.isNotNull(fFilteredList); 294 295 IStatus status; 296 Object [] elements = getSelectedElements(); 297 298 if (elements.length > 0) { 299 if (fValidator != null) { 300 status = fValidator.validate(elements); 301 } else { 302 status = new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 303 IStatus.OK, "", null); 305 } 306 } else { 307 if (fFilteredList.isEmpty()) { 308 status = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 309 IStatus.ERROR, fEmptyListMessage, null); 310 } else { 311 status = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 312 IStatus.ERROR, fEmptySelectionMessage, null); 313 } 314 } 315 316 updateStatus(status); 317 318 return status.isOK(); 319 } 320 321 324 protected void cancelPressed() { 325 setResult(null); 326 super.cancelPressed(); 327 } 328 329 334 protected FilteredList createFilteredList(Composite parent) { 335 int flags = SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL 336 | (fIsMultipleSelection ? SWT.MULTI : SWT.SINGLE); 337 338 FilteredList list = new FilteredList(parent, flags, fRenderer, 339 fIgnoreCase, fAllowDuplicates, fMatchEmptyString); 340 341 GridData data = new GridData(); 342 data.widthHint = convertWidthInCharsToPixels(fWidth); 343 data.heightHint = convertHeightInCharsToPixels(fHeight); 344 data.grabExcessVerticalSpace = true; 345 data.grabExcessHorizontalSpace = true; 346 data.horizontalAlignment = GridData.FILL; 347 data.verticalAlignment = GridData.FILL; 348 list.setLayoutData(data); 349 list.setFont(parent.getFont()); 350 list.setFilter((fFilter == null ? "" : fFilter)); 352 list.addSelectionListener(new SelectionListener() { 353 public void widgetDefaultSelected(SelectionEvent e) { 354 handleDefaultSelected(); 355 } 356 357 public void widgetSelected(SelectionEvent e) { 358 handleWidgetSelected(); 359 } 360 }); 361 362 fFilteredList = list; 363 364 return list; 365 } 366 367 private void handleWidgetSelected() { 369 Object [] newSelection = fFilteredList.getSelection(); 370 371 if (newSelection.length != fSelection.length) { 372 fSelection = newSelection; 373 handleSelectionChanged(); 374 } else { 375 for (int i = 0; i != newSelection.length; i++) { 376 if (!newSelection[i].equals(fSelection[i])) { 377 fSelection = newSelection; 378 handleSelectionChanged(); 379 break; 380 } 381 } 382 } 383 } 384 385 protected Text createFilterText(Composite parent) { 386 Text text = new Text(parent, SWT.BORDER); 387 388 GridData data = new GridData(); 389 data.grabExcessVerticalSpace = false; 390 data.grabExcessHorizontalSpace = true; 391 data.horizontalAlignment = GridData.FILL; 392 data.verticalAlignment = GridData.BEGINNING; 393 text.setLayoutData(data); 394 text.setFont(parent.getFont()); 395 396 text.setText((fFilter == null ? "" : fFilter)); 398 Listener listener = new Listener() { 399 public void handleEvent(Event e) { 400 fFilteredList.setFilter(fFilterText.getText()); 401 } 402 }; 403 text.addListener(SWT.Modify, listener); 404 405 text.addKeyListener(new KeyListener() { 406 public void keyPressed(KeyEvent e) { 407 if (e.keyCode == SWT.ARROW_DOWN) { 408 fFilteredList.setFocus(); 409 } 410 } 411 412 public void keyReleased(KeyEvent e) { 413 } 414 }); 415 416 fFilterText = text; 417 418 return text; 419 } 420 421 425 public int open() { 426 super.open(); 427 return getReturnCode(); 428 } 429 430 private void access$superCreate() { 431 super.create(); 432 } 433 434 438 public void create() { 439 440 BusyIndicator.showWhile(null, new Runnable () { 441 public void run() { 442 access$superCreate(); 443 444 Assert.isNotNull(fFilteredList); 445 446 if (fFilteredList.isEmpty()) { 447 handleEmptyList(); 448 } else { 449 validateCurrentSelection(); 450 fFilterText.selectAll(); 451 fFilterText.setFocus(); 452 } 453 } 454 }); 455 456 } 457 458 461 protected void handleEmptyList() { 462 fMessage.setEnabled(false); 463 fFilterText.setEnabled(false); 464 fFilteredList.setEnabled(false); 465 updateOkState(); 466 } 467 468 473 protected void updateOkState() { 474 Button okButton = getOkButton(); 475 if (okButton != null) { 476 okButton.setEnabled(getSelectedElements().length != 0); 477 } 478 } 479 } 480 | Popular Tags |