1 13 package org.eclipse.ui.dialogs; 14 15 import java.util.ArrayList ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Status; 20 import org.eclipse.jface.dialogs.IDialogConstants; 21 import org.eclipse.jface.viewers.DoubleClickEvent; 22 import org.eclipse.jface.viewers.IDoubleClickListener; 23 import org.eclipse.jface.viewers.ILabelProvider; 24 import org.eclipse.jface.viewers.ISelection; 25 import org.eclipse.jface.viewers.ISelectionChangedListener; 26 import org.eclipse.jface.viewers.IStructuredSelection; 27 import org.eclipse.jface.viewers.ITreeContentProvider; 28 import org.eclipse.jface.viewers.SelectionChangedEvent; 29 import org.eclipse.jface.viewers.StructuredSelection; 30 import org.eclipse.jface.viewers.TreeViewer; 31 import org.eclipse.jface.viewers.ViewerComparator; 32 import org.eclipse.jface.viewers.ViewerFilter; 33 import org.eclipse.jface.viewers.ViewerSorter; 34 import org.eclipse.swt.SWT; 35 import org.eclipse.swt.custom.BusyIndicator; 36 import org.eclipse.swt.events.SelectionAdapter; 37 import org.eclipse.swt.events.SelectionEvent; 38 import org.eclipse.swt.layout.GridData; 39 import org.eclipse.swt.widgets.Composite; 40 import org.eclipse.swt.widgets.Control; 41 import org.eclipse.swt.widgets.Label; 42 import org.eclipse.swt.widgets.Shell; 43 import org.eclipse.swt.widgets.Tree; 44 import org.eclipse.ui.PlatformUI; 45 import org.eclipse.ui.internal.WorkbenchMessages; 46 47 52 public class ElementTreeSelectionDialog extends SelectionStatusDialog { 53 54 private TreeViewer fViewer; 55 56 private ILabelProvider fLabelProvider; 57 58 private ITreeContentProvider fContentProvider; 59 60 private ISelectionStatusValidator fValidator = null; 61 62 private ViewerComparator fComparator; 63 64 private boolean fAllowMultiple = true; 65 66 private boolean fDoubleClickSelects = true; 67 68 private String fEmptyListMessage = WorkbenchMessages.ElementTreeSelectionDialog_nothing_available; 69 70 private IStatus fCurrStatus = new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 71 IStatus.OK, "", null); 73 private List fFilters; 74 75 private Object fInput; 76 77 private boolean fIsEmpty; 78 79 private int fWidth = 60; 80 81 private int fHeight = 18; 82 83 89 public ElementTreeSelectionDialog(Shell parent, 90 ILabelProvider labelProvider, ITreeContentProvider contentProvider) { 91 super(parent); 92 93 fLabelProvider = labelProvider; 94 fContentProvider = contentProvider; 95 96 setResult(new ArrayList (0)); 97 setStatusLineAboveButtons(true); 98 99 int shellStyle = getShellStyle(); 100 setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE); 101 } 102 103 108 public void setInitialSelection(Object selection) { 109 setInitialSelections(new Object [] { selection }); 110 } 111 112 116 public void setEmptyListMessage(String message) { 117 fEmptyListMessage = message; 118 } 119 120 124 public void setAllowMultiple(boolean allowMultiple) { 125 fAllowMultiple = allowMultiple; 126 } 127 128 132 public void setDoubleClickSelects(boolean doubleClickSelects) { 133 fDoubleClickSelects = doubleClickSelects; 134 } 135 136 141 public void setSorter(ViewerSorter sorter) { 142 fComparator = sorter; 143 } 144 145 150 public void setComparator(ViewerComparator comparator){ 151 fComparator = comparator; 152 } 153 154 158 public void addFilter(ViewerFilter filter) { 159 if (fFilters == null) { 160 fFilters = new ArrayList (4); 161 } 162 163 fFilters.add(filter); 164 } 165 166 171 public void setValidator(ISelectionStatusValidator validator) { 172 fValidator = validator; 173 } 174 175 179 public void setInput(Object input) { 180 fInput = input; 181 } 182 183 188 public void setSize(int width, int height) { 189 fWidth = width; 190 fHeight = height; 191 } 192 193 197 protected void updateOKStatus() { 198 if (!fIsEmpty) { 199 if (fValidator != null) { 200 fCurrStatus = fValidator.validate(getResult()); 201 updateStatus(fCurrStatus); 202 } else { 203 fCurrStatus = new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 204 IStatus.OK, "", null); 206 } 207 } else { 208 fCurrStatus = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 209 IStatus.ERROR, fEmptyListMessage, null); 210 } 211 updateStatus(fCurrStatus); 212 } 213 214 218 public int open() { 219 fIsEmpty = evaluateIfTreeEmpty(fInput); 220 super.open(); 221 return getReturnCode(); 222 } 223 224 private void access$superCreate() { 225 super.create(); 226 } 227 228 231 protected void cancelPressed() { 232 setResult(null); 233 super.cancelPressed(); 234 } 235 236 239 protected void computeResult() { 240 setResult(((IStructuredSelection) fViewer.getSelection()).toList()); 241 } 242 243 247 public void create() { 248 BusyIndicator.showWhile(null, new Runnable () { 249 public void run() { 250 access$superCreate(); 251 fViewer.setSelection(new StructuredSelection( 252 getInitialElementSelections()), true); 253 updateOKStatus(); 254 } 255 }); 256 } 257 258 261 protected Control createDialogArea(Composite parent) { 262 Composite composite = (Composite) super.createDialogArea(parent); 263 264 Label messageLabel = createMessageArea(composite); 265 TreeViewer treeViewer = createTreeViewer(composite); 266 267 GridData data = new GridData(GridData.FILL_BOTH); 268 data.widthHint = convertWidthInCharsToPixels(fWidth); 269 data.heightHint = convertHeightInCharsToPixels(fHeight); 270 271 Tree treeWidget = treeViewer.getTree(); 272 treeWidget.setLayoutData(data); 273 treeWidget.setFont(parent.getFont()); 274 275 if (fIsEmpty) { 276 messageLabel.setEnabled(false); 277 treeWidget.setEnabled(false); 278 } 279 280 return composite; 281 } 282 283 289 protected TreeViewer createTreeViewer(Composite parent) { 290 int style = SWT.BORDER | (fAllowMultiple ? SWT.MULTI : SWT.SINGLE); 291 292 fViewer = new TreeViewer(new Tree(parent, style)); 293 fViewer.setContentProvider(fContentProvider); 294 fViewer.setLabelProvider(fLabelProvider); 295 fViewer.addSelectionChangedListener(new ISelectionChangedListener() { 296 public void selectionChanged(SelectionChangedEvent event) { 297 access$setResult(((IStructuredSelection) event.getSelection()) 298 .toList()); 299 updateOKStatus(); 300 } 301 }); 302 303 fViewer.setComparator(fComparator); 304 if (fFilters != null) { 305 for (int i = 0; i != fFilters.size(); i++) { 306 fViewer.addFilter((ViewerFilter) fFilters.get(i)); 307 } 308 } 309 310 if (fDoubleClickSelects) { 311 Tree tree = fViewer.getTree(); 312 tree.addSelectionListener(new SelectionAdapter() { 313 public void widgetDefaultSelected(SelectionEvent e) { 314 updateOKStatus(); 315 if (fCurrStatus.isOK()) { 316 access$superButtonPressed(IDialogConstants.OK_ID); 317 } 318 } 319 }); 320 } 321 fViewer.addDoubleClickListener(new IDoubleClickListener() { 322 public void doubleClick(DoubleClickEvent event) { 323 updateOKStatus(); 324 325 if (!(fDoubleClickSelects && fCurrStatus.isOK())) { 328 ISelection selection = event.getSelection(); 329 if (selection instanceof IStructuredSelection) { 330 Object item = ((IStructuredSelection) selection) 331 .getFirstElement(); 332 if (fViewer.getExpandedState(item)) { 333 fViewer.collapseToLevel(item, 1); 334 } else { 335 fViewer.expandToLevel(item, 1); 336 } 337 } 338 } 339 } 340 }); 341 342 fViewer.setInput(fInput); 343 344 return fViewer; 345 } 346 347 352 protected TreeViewer getTreeViewer() { 353 return fViewer; 354 } 355 356 private boolean evaluateIfTreeEmpty(Object input) { 357 Object [] elements = fContentProvider.getElements(input); 358 if (elements.length > 0) { 359 if (fFilters != null) { 360 for (int i = 0; i < fFilters.size(); i++) { 361 ViewerFilter curr = (ViewerFilter) fFilters.get(i); 362 elements = curr.filter(fViewer, input, elements); 363 } 364 } 365 } 366 return elements.length == 0; 367 } 368 369 375 protected void access$superButtonPressed(int id) { 376 super.buttonPressed(id); 377 } 378 379 385 protected void access$setResult(List result) { 386 super.setResult(result); 387 } 388 389 392 protected void handleShellCloseEvent() { 393 super.handleShellCloseEvent(); 394 395 if (getReturnCode() == CANCEL) { 397 setResult(null); 398 } 399 } 400 401 } 402 | Popular Tags |