1 13 package org.eclipse.ui.dialogs; 14 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 17 import java.util.List ; 18 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 import org.eclipse.jface.dialogs.IDialogConstants; 22 import org.eclipse.jface.viewers.CheckStateChangedEvent; 23 import org.eclipse.jface.viewers.CheckboxTreeViewer; 24 import org.eclipse.jface.viewers.ICheckStateListener; 25 import org.eclipse.jface.viewers.ILabelProvider; 26 import org.eclipse.jface.viewers.ITreeContentProvider; 27 import org.eclipse.jface.viewers.ViewerComparator; 28 import org.eclipse.jface.viewers.ViewerFilter; 29 import org.eclipse.jface.viewers.ViewerSorter; 30 import org.eclipse.swt.SWT; 31 import org.eclipse.swt.custom.BusyIndicator; 32 import org.eclipse.swt.events.SelectionAdapter; 33 import org.eclipse.swt.events.SelectionEvent; 34 import org.eclipse.swt.events.SelectionListener; 35 import org.eclipse.swt.layout.GridData; 36 import org.eclipse.swt.layout.GridLayout; 37 import org.eclipse.swt.widgets.Button; 38 import org.eclipse.swt.widgets.Composite; 39 import org.eclipse.swt.widgets.Control; 40 import org.eclipse.swt.widgets.Label; 41 import org.eclipse.swt.widgets.Shell; 42 import org.eclipse.swt.widgets.Tree; 43 import org.eclipse.ui.PlatformUI; 44 import org.eclipse.ui.internal.WorkbenchMessages; 45 46 51 public class CheckedTreeSelectionDialog extends SelectionStatusDialog { 52 private CheckboxTreeViewer fViewer; 53 54 private ILabelProvider fLabelProvider; 55 56 private ITreeContentProvider fContentProvider; 57 58 private ISelectionStatusValidator fValidator = null; 59 60 private ViewerComparator fComparator; 61 62 private String fEmptyListMessage = WorkbenchMessages.CheckedTreeSelectionDialog_nothing_available; 63 64 private IStatus fCurrStatus = new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 65 0, "", null); 67 private List fFilters; 68 69 private Object fInput; 70 71 private boolean fIsEmpty; 72 73 private int fWidth = 60; 74 75 private int fHeight = 18; 76 77 private boolean fContainerMode; 78 79 private Object [] fExpandedElements; 80 81 91 public CheckedTreeSelectionDialog(Shell parent, 92 ILabelProvider labelProvider, ITreeContentProvider contentProvider) { 93 super(parent); 94 fLabelProvider = labelProvider; 95 fContentProvider = contentProvider; 96 setResult(new ArrayList (0)); 97 setStatusLineAboveButtons(true); 98 fContainerMode = false; 99 fExpandedElements = null; 100 int shellStyle = getShellStyle(); 101 setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE); 102 } 103 104 111 public void setContainerMode(boolean containerMode) { 112 fContainerMode = containerMode; 113 } 114 115 121 public void setInitialSelection(Object selection) { 122 setInitialSelections(new Object [] { selection }); 123 } 124 125 131 public void setEmptyListMessage(String message) { 132 fEmptyListMessage = message; 133 } 134 135 143 public void setSorter(ViewerSorter sorter) { 144 fComparator = sorter; 145 } 146 147 153 public void setComparator(ViewerComparator comparator){ 154 fComparator = comparator; 155 } 156 157 163 public void addFilter(ViewerFilter filter) { 164 if (fFilters == null) { 165 fFilters = new ArrayList (4); 166 } 167 fFilters.add(filter); 168 } 169 170 177 public void setValidator(ISelectionStatusValidator validator) { 178 fValidator = validator; 179 } 180 181 187 public void setInput(Object input) { 188 fInput = input; 189 } 190 191 197 public void setExpandedElements(Object [] elements) { 198 fExpandedElements = elements; 199 } 200 201 209 public void setSize(int width, int height) { 210 fWidth = width; 211 fHeight = height; 212 } 213 214 218 protected void updateOKStatus() { 219 if (!fIsEmpty) { 220 if (fValidator != null) { 221 fCurrStatus = fValidator.validate(fViewer.getCheckedElements()); 222 updateStatus(fCurrStatus); 223 } else if (!fCurrStatus.isOK()) { 224 fCurrStatus = new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 225 IStatus.OK, "", null); 227 } 228 } else { 229 fCurrStatus = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, 230 IStatus.OK, fEmptyListMessage, null); 231 } 232 updateStatus(fCurrStatus); 233 } 234 235 239 public int open() { 240 fIsEmpty = evaluateIfTreeEmpty(fInput); 241 super.open(); 242 return getReturnCode(); 243 } 244 245 private void access$superCreate() { 246 super.create(); 247 } 248 249 252 protected void cancelPressed() { 253 setResult(null); 254 super.cancelPressed(); 255 } 256 257 260 protected void computeResult() { 261 setResult(Arrays.asList(fViewer.getCheckedElements())); 262 } 263 264 268 public void create() { 269 BusyIndicator.showWhile(null, new Runnable () { 270 public void run() { 271 access$superCreate(); 272 fViewer.setCheckedElements(getInitialElementSelections() 273 .toArray()); 274 if (fExpandedElements != null) { 275 fViewer.setExpandedElements(fExpandedElements); 276 } 277 updateOKStatus(); 278 } 279 }); 280 } 281 282 286 protected Control createDialogArea(Composite parent) { 287 Composite composite = (Composite) super.createDialogArea(parent); 288 Label messageLabel = createMessageArea(composite); 289 CheckboxTreeViewer treeViewer = createTreeViewer(composite); 290 Control buttonComposite = createSelectionButtons(composite); 291 GridData data = new GridData(GridData.FILL_BOTH); 292 data.widthHint = convertWidthInCharsToPixels(fWidth); 293 data.heightHint = convertHeightInCharsToPixels(fHeight); 294 Tree treeWidget = treeViewer.getTree(); 295 treeWidget.setLayoutData(data); 296 treeWidget.setFont(parent.getFont()); 297 if (fIsEmpty) { 298 messageLabel.setEnabled(false); 299 treeWidget.setEnabled(false); 300 buttonComposite.setEnabled(false); 301 } 302 return composite; 303 } 304 305 312 protected CheckboxTreeViewer createTreeViewer(Composite parent) { 313 if (fContainerMode) { 314 fViewer = new ContainerCheckedTreeViewer(parent, SWT.BORDER); 315 } else { 316 fViewer = new CheckboxTreeViewer(parent, SWT.BORDER); 317 } 318 fViewer.setContentProvider(fContentProvider); 319 fViewer.setLabelProvider(fLabelProvider); 320 fViewer.addCheckStateListener(new ICheckStateListener() { 321 public void checkStateChanged(CheckStateChangedEvent event) { 322 updateOKStatus(); 323 } 324 }); 325 fViewer.setComparator(fComparator); 326 if (fFilters != null) { 327 for (int i = 0; i != fFilters.size(); i++) { 328 fViewer.addFilter((ViewerFilter) fFilters.get(i)); 329 } 330 } 331 fViewer.setInput(fInput); 332 return fViewer; 333 } 334 335 340 protected CheckboxTreeViewer getTreeViewer() { 341 return fViewer; 342 } 343 344 351 protected Composite createSelectionButtons(Composite composite) { 352 Composite buttonComposite = new Composite(composite, SWT.RIGHT); 353 GridLayout layout = new GridLayout(); 354 layout.numColumns = 2; 355 buttonComposite.setLayout(layout); 356 buttonComposite.setFont(composite.getFont()); 357 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END 358 | GridData.GRAB_HORIZONTAL); 359 data.grabExcessHorizontalSpace = true; 360 composite.setData(data); 361 Button selectButton = createButton(buttonComposite, 362 IDialogConstants.SELECT_ALL_ID, WorkbenchMessages.CheckedTreeSelectionDialog_select_all, 363 false); 364 SelectionListener listener = new SelectionAdapter() { 365 public void widgetSelected(SelectionEvent e) { 366 Object [] viewerElements = fContentProvider.getElements(fInput); 367 if (fContainerMode) { 368 fViewer.setCheckedElements(viewerElements); 369 } else { 370 for (int i = 0; i < viewerElements.length; i++) { 371 fViewer.setSubtreeChecked(viewerElements[i], true); 372 } 373 } 374 updateOKStatus(); 375 } 376 }; 377 selectButton.addSelectionListener(listener); 378 Button deselectButton = createButton(buttonComposite, 379 IDialogConstants.DESELECT_ALL_ID, WorkbenchMessages.CheckedTreeSelectionDialog_deselect_all, 380 false); 381 listener = new SelectionAdapter() { 382 public void widgetSelected(SelectionEvent e) { 383 fViewer.setCheckedElements(new Object [0]); 384 updateOKStatus(); 385 } 386 }; 387 deselectButton.addSelectionListener(listener); 388 return buttonComposite; 389 } 390 391 private boolean evaluateIfTreeEmpty(Object input) { 392 Object [] elements = fContentProvider.getElements(input); 393 if (elements.length > 0) { 394 if (fFilters != null) { 395 for (int i = 0; i < fFilters.size(); i++) { 396 ViewerFilter curr = (ViewerFilter) fFilters.get(i); 397 elements = curr.filter(fViewer, input, elements); 398 } 399 } 400 } 401 return elements.length == 0; 402 } 403 } 404 | Popular Tags |