1 13 package org.eclipse.jdt.internal.ui.workingsets; 14 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 17 import java.util.HashMap ; 18 import java.util.HashSet ; 19 import java.util.Hashtable ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 import org.eclipse.core.runtime.Assert; 26 import org.eclipse.core.runtime.IAdaptable; 27 28 import org.eclipse.core.resources.IProject; 29 30 import org.eclipse.swt.SWT; 31 import org.eclipse.swt.events.SelectionAdapter; 32 import org.eclipse.swt.events.SelectionEvent; 33 import org.eclipse.swt.graphics.Image; 34 import org.eclipse.swt.layout.GridData; 35 import org.eclipse.swt.layout.GridLayout; 36 import org.eclipse.swt.widgets.Button; 37 import org.eclipse.swt.widgets.Composite; 38 import org.eclipse.swt.widgets.Control; 39 import org.eclipse.swt.widgets.Shell; 40 41 import org.eclipse.jface.dialogs.IDialogConstants; 42 import org.eclipse.jface.resource.ImageDescriptor; 43 import org.eclipse.jface.viewers.CheckStateChangedEvent; 44 import org.eclipse.jface.viewers.CheckboxTableViewer; 45 import org.eclipse.jface.viewers.ICheckStateListener; 46 import org.eclipse.jface.viewers.ISelection; 47 import org.eclipse.jface.viewers.ISelectionChangedListener; 48 import org.eclipse.jface.viewers.IStructuredContentProvider; 49 import org.eclipse.jface.viewers.IStructuredSelection; 50 import org.eclipse.jface.viewers.LabelProvider; 51 import org.eclipse.jface.viewers.SelectionChangedEvent; 52 import org.eclipse.jface.viewers.StructuredSelection; 53 import org.eclipse.jface.viewers.Viewer; 54 import org.eclipse.jface.viewers.ViewerFilter; 55 import org.eclipse.jface.window.Window; 56 import org.eclipse.jface.wizard.WizardDialog; 57 58 import org.eclipse.ui.IWorkingSet; 59 import org.eclipse.ui.IWorkingSetManager; 60 import org.eclipse.ui.PlatformUI; 61 import org.eclipse.ui.dialogs.IWorkingSetEditWizard; 62 import org.eclipse.ui.dialogs.IWorkingSetNewWizard; 63 import org.eclipse.ui.dialogs.SelectionDialog; 64 65 public class WorkingSetConfigurationDialog extends SelectionDialog { 66 67 private static class WorkingSetLabelProvider extends LabelProvider { 68 private Map fIcons; 69 public WorkingSetLabelProvider() { 70 fIcons= new Hashtable (); 71 } 72 public void dispose() { 73 Iterator iterator= fIcons.values().iterator(); 74 while (iterator.hasNext()) { 75 Image icon= (Image)iterator.next(); 76 icon.dispose(); 77 } 78 super.dispose(); 79 } 80 public Image getImage(Object object) { 81 Assert.isTrue(object instanceof IWorkingSet); 82 IWorkingSet workingSet= (IWorkingSet)object; 83 ImageDescriptor imageDescriptor= workingSet.getImage(); 84 if (imageDescriptor == null) 85 return null; 86 Image icon= (Image)fIcons.get(imageDescriptor); 87 if (icon == null) { 88 icon= imageDescriptor.createImage(); 89 fIcons.put(imageDescriptor, icon); 90 } 91 return icon; 92 } 93 public String getText(Object object) { 94 Assert.isTrue(object instanceof IWorkingSet); 95 IWorkingSet workingSet= (IWorkingSet)object; 96 return workingSet.getName(); 97 } 98 } 99 100 private class Filter extends ViewerFilter { 101 public boolean select(Viewer viewer, Object parentElement, Object element) { 102 IWorkingSet ws= (IWorkingSet)element; 103 String id= ws.getId(); 104 return OthersWorkingSetUpdater.ID.equals(id) || 105 JavaWorkingSetUpdater.ID.equals(id) || isCompatible(ws) || isActive(ws); 106 } 107 private boolean isCompatible(IWorkingSet set) { 108 if (!set.isSelfUpdating() || set.isAggregateWorkingSet()) 109 return false; 110 IAdaptable[] elements= set.getElements(); 111 if (elements.length == 0) 112 return false; 113 for (int i= 0; i < elements.length; i++) { 114 IAdaptable element= elements[i]; 115 IProject p= (IProject)element.getAdapter(IProject.class); 116 if (p == null || !p.exists()) 117 return false; 118 } 119 return true; 120 } 121 private boolean isActive(IWorkingSet workingSet) { 122 return fActiveWorkingSets.contains(workingSet); 123 } 124 } 125 126 private List fAllWorkingSets; 127 private List fActiveWorkingSets; 128 private CheckboxTableViewer fTableViewer; 129 130 private Button fNewButton; 131 private Button fEditButton; 132 private Button fRemoveButton; 133 private Button fUpButton; 134 private Button fDownButton; 135 private Button fSelectAll; 136 private Button fDeselectAll; 137 138 private IWorkingSet[] fResult; 139 private List fAddedWorkingSets; 140 private List fRemovedWorkingSets; 141 private Map fEditedWorkingSets; 142 private List fRemovedMRUWorkingSets; 143 144 private int nextButtonId= IDialogConstants.CLIENT_ID + 1; 145 146 public WorkingSetConfigurationDialog(Shell parentShell, IWorkingSet[] allWorkingSets, IWorkingSet[] activeWorkingSets) { 147 super(parentShell); 148 setTitle(WorkingSetMessages.WorkingSetConfigurationDialog_title); 149 setMessage(WorkingSetMessages.WorkingSetConfigurationDialog_message); 150 fAllWorkingSets= new ArrayList (allWorkingSets.length); 151 fActiveWorkingSets= Arrays.asList(activeWorkingSets); 152 Filter filter= new Filter(); 153 for (int i= 0; i < allWorkingSets.length; i++) { 154 if (filter.select(null, null, allWorkingSets[i])) 155 fAllWorkingSets.add(allWorkingSets[i]); 156 } 157 setShellStyle(getShellStyle() | SWT.RESIZE); 158 } 159 160 165 public IWorkingSet[] getSelection() { 166 return fResult; 167 } 168 169 174 public void setSelection(IWorkingSet[] workingSets) { 175 fResult= workingSets; 176 setInitialSelections(workingSets); 177 } 178 179 182 protected Control createContents(Composite parent) { 183 Control control= super.createContents(parent); 184 setInitialSelection(); 185 updateButtonAvailability(); 186 return control; 187 } 188 189 192 protected Control createDialogArea(Composite parent) { 193 Composite composite= (Composite)super.createDialogArea(parent); 194 composite.setFont(parent.getFont()); 195 196 createMessageArea(composite); 197 Composite inner= new Composite(composite, SWT.NONE); 198 inner.setFont(composite.getFont()); 199 inner.setLayoutData(new GridData(GridData.FILL_BOTH)); 200 GridLayout layout= new GridLayout(); 201 layout.numColumns= 2; 202 layout.marginHeight= 0; 203 layout.marginWidth= 0; 204 inner.setLayout(layout); 205 createTableViewer(inner); 206 createOrderButtons(inner); 207 createModifyButtons(composite); 208 fTableViewer.setInput(fAllWorkingSets); 209 210 return composite; 211 } 212 213 private void createTableViewer(Composite parent) { 214 fTableViewer= CheckboxTableViewer.newCheckList(parent, SWT.BORDER | SWT.MULTI); 215 fTableViewer.addCheckStateListener(new ICheckStateListener() { 216 public void checkStateChanged(CheckStateChangedEvent event) { 217 updateButtonAvailability(); 218 } 219 }); 220 GridData data= new GridData(GridData.FILL_BOTH); 221 data.heightHint= convertHeightInCharsToPixels(20); 222 data.widthHint= convertWidthInCharsToPixels(50); 223 fTableViewer.getTable().setLayoutData(data); 224 fTableViewer.getTable().setFont(parent.getFont()); 225 226 fTableViewer.addFilter(new Filter()); 227 fTableViewer.setLabelProvider(new WorkingSetLabelProvider()); 228 fTableViewer.setContentProvider(new IStructuredContentProvider() { 229 public Object [] getElements(Object element) { 230 return ((List )element).toArray(); 231 } 232 public void dispose() { 233 } 234 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 235 } 236 }); 237 fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() { 238 public void selectionChanged(SelectionChangedEvent event) { 239 handleSelectionChanged(); 240 } 241 }); 242 } 243 244 private void createModifyButtons(Composite composite) { 245 Composite buttonComposite= new Composite(composite, SWT.RIGHT); 246 GridLayout layout= new GridLayout(); 247 layout.numColumns= 2; 248 buttonComposite.setLayout(layout); 249 GridData data= new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.GRAB_HORIZONTAL); 250 data.grabExcessHorizontalSpace= true; 251 composite.setData(data); 252 253 fNewButton= createButton(buttonComposite, nextButtonId++, 254 WorkingSetMessages.WorkingSetConfigurationDialog_new_label, false); 255 fNewButton.setFont(composite.getFont()); 256 fNewButton.addSelectionListener(new SelectionAdapter() { 257 public void widgetSelected(SelectionEvent e) { 258 createWorkingSet(); 259 } 260 }); 261 262 fEditButton= createButton(buttonComposite, nextButtonId++, 263 WorkingSetMessages.WorkingSetConfigurationDialog_edit_label, false); 264 fEditButton.setFont(composite.getFont()); 265 fEditButton.addSelectionListener(new SelectionAdapter() { 266 public void widgetSelected(SelectionEvent e) { 267 editSelectedWorkingSet(); 268 } 269 }); 270 271 fRemoveButton= createButton(buttonComposite, nextButtonId++, 272 WorkingSetMessages.WorkingSetConfigurationDialog_remove_label, false); 273 fRemoveButton.setFont(composite.getFont()); 274 fRemoveButton.addSelectionListener(new SelectionAdapter() { 275 public void widgetSelected(SelectionEvent e) { 276 removeSelectedWorkingSets(); 277 } 278 }); 279 } 280 281 private void createOrderButtons(Composite parent) { 282 Composite buttons= new Composite(parent, SWT.NONE); 283 buttons.setFont(parent.getFont()); 284 buttons.setLayoutData(new GridData(GridData.FILL_VERTICAL)); 285 GridLayout layout= new GridLayout(); 286 layout.marginHeight= 0; 287 layout.marginWidth= 0; 288 buttons.setLayout(layout); 289 290 fUpButton= new Button(buttons, SWT.PUSH); 291 fUpButton.setText(WorkingSetMessages.WorkingSetConfigurationDialog_up_label); 292 fUpButton.setFont(parent.getFont()); 293 setButtonLayoutData(fUpButton); 294 fUpButton.addSelectionListener(new SelectionAdapter() { 295 public void widgetSelected(SelectionEvent e) { 296 moveUp(((IStructuredSelection)fTableViewer.getSelection()).toList()); 297 } 298 }); 299 300 fDownButton= new Button(buttons, SWT.PUSH); 301 fDownButton.setText(WorkingSetMessages.WorkingSetConfigurationDialog_down_label); 302 fDownButton.setFont(parent.getFont()); 303 setButtonLayoutData(fDownButton); 304 fDownButton.addSelectionListener(new SelectionAdapter() { 305 public void widgetSelected(SelectionEvent e) { 306 moveDown(((IStructuredSelection)fTableViewer.getSelection()).toList()); 307 } 308 }); 309 310 fSelectAll= new Button(buttons, SWT.PUSH); 311 fSelectAll.setText(WorkingSetMessages.WorkingSetConfigurationDialog_selectAll_label); 312 fSelectAll.setFont(parent.getFont()); 313 setButtonLayoutData(fSelectAll); 314 fSelectAll.addSelectionListener(new SelectionAdapter() { 315 public void widgetSelected(SelectionEvent e) { 316 selectAll(); 317 } 318 }); 319 320 fDeselectAll= new Button(buttons, SWT.PUSH); 321 fDeselectAll.setText(WorkingSetMessages.WorkingSetConfigurationDialog_deselectAll_label); 322 fDeselectAll.setFont(parent.getFont()); 323 setButtonLayoutData(fDeselectAll); 324 fDeselectAll.addSelectionListener(new SelectionAdapter() { 325 public void widgetSelected(SelectionEvent e) { 326 deselectAll(); 327 } 328 }); 329 } 330 331 334 protected void okPressed() { 335 List newResult= getResultWorkingSets(); 336 fResult= (IWorkingSet[])newResult.toArray(new IWorkingSet[newResult.size()]); 337 setResult(newResult); 338 super.okPressed(); 339 } 340 341 private List getResultWorkingSets() { 342 Object [] checked= fTableViewer.getCheckedElements(); 343 return new ArrayList (Arrays.asList(checked)); 344 } 345 346 349 protected void cancelPressed() { 350 restoreAddedWorkingSets(); 351 restoreChangedWorkingSets(); 352 restoreRemovedWorkingSets(); 353 super.cancelPressed(); 354 } 355 356 private void setInitialSelection() { 357 List selections= getInitialElementSelections(); 358 if (!selections.isEmpty()) { 359 fTableViewer.setCheckedElements(selections.toArray()); 360 } 361 } 362 363 private void createWorkingSet() { 364 IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager(); 365 IWorkingSetNewWizard wizard= manager.createWorkingSetNewWizard(new String [] {JavaWorkingSetUpdater.ID}); 366 WizardDialog dialog= new WizardDialog(getShell(), wizard); 368 dialog.create(); 369 if (dialog.open() == Window.OK) { 370 IWorkingSet workingSet= wizard.getSelection(); 371 Filter filter= new Filter(); 372 if (filter.select(null, null, workingSet)) { 373 fAllWorkingSets.add(workingSet); 374 fTableViewer.add(workingSet); 375 fTableViewer.setSelection(new StructuredSelection(workingSet), true); 376 fTableViewer.setChecked(workingSet, true); 377 manager.addWorkingSet(workingSet); 378 fAddedWorkingSets.add(workingSet); 379 } 380 } 381 } 382 383 private void editSelectedWorkingSet() { 384 IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager(); 385 IWorkingSet editWorkingSet= (IWorkingSet)((IStructuredSelection)fTableViewer.getSelection()).getFirstElement(); 386 IWorkingSetEditWizard wizard= manager.createWorkingSetEditWizard(editWorkingSet); 387 WizardDialog dialog= new WizardDialog(getShell(), wizard); 388 IWorkingSet originalWorkingSet= (IWorkingSet)fEditedWorkingSets.get(editWorkingSet); 389 boolean firstEdit= originalWorkingSet == null; 390 391 if (firstEdit) { 394 originalWorkingSet= 395 PlatformUI.getWorkbench().getWorkingSetManager(). 396 createWorkingSet(editWorkingSet.getName(), editWorkingSet.getElements()); 397 } else { 398 fEditedWorkingSets.remove(editWorkingSet); 399 } 400 dialog.create(); 401 if (dialog.open() == Window.OK) { 402 editWorkingSet= wizard.getSelection(); 403 fTableViewer.update(editWorkingSet, null); 404 updateButtonAvailability(); 407 } 408 fEditedWorkingSets.put(editWorkingSet, originalWorkingSet); 409 } 410 411 414 void handleSelectionChanged() { 415 updateButtonAvailability(); 416 } 417 418 423 public int open() { 424 fAddedWorkingSets= new ArrayList (); 425 fRemovedWorkingSets= new ArrayList (); 426 fEditedWorkingSets= new HashMap (); 427 fRemovedMRUWorkingSets= new ArrayList (); 428 return super.open(); 429 } 430 431 434 private void removeSelectedWorkingSets() { 435 ISelection selection= fTableViewer.getSelection(); 436 if (selection instanceof IStructuredSelection) { 437 IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager(); 438 Iterator iter= ((IStructuredSelection)selection).iterator(); 439 while (iter.hasNext()) { 440 IWorkingSet workingSet= (IWorkingSet)iter.next(); 441 if (fAddedWorkingSets.contains(workingSet)) { 442 fAddedWorkingSets.remove(workingSet); 443 } else { 444 IWorkingSet[] recentWorkingSets= manager.getRecentWorkingSets(); 445 for (int i= 0; i < recentWorkingSets.length; i++) { 446 if (workingSet.equals(recentWorkingSets[i])) { 447 fRemovedMRUWorkingSets.add(workingSet); 448 break; 449 } 450 } 451 fRemovedWorkingSets.add(workingSet); 452 } 453 fAllWorkingSets.remove(workingSet); 454 manager.removeWorkingSet(workingSet); 455 } 456 fTableViewer.remove(((IStructuredSelection)selection).toArray()); 457 } 458 } 459 460 463 private void restoreAddedWorkingSets() { 464 IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager(); 465 Iterator iterator= fAddedWorkingSets.iterator(); 466 467 while (iterator.hasNext()) { 468 manager.removeWorkingSet(((IWorkingSet)iterator.next())); 469 } 470 } 471 472 475 private void restoreChangedWorkingSets() { 476 Iterator iterator= fEditedWorkingSets.keySet().iterator(); 477 478 while (iterator.hasNext()) { 479 IWorkingSet editedWorkingSet= (IWorkingSet)iterator.next(); 480 IWorkingSet originalWorkingSet= (IWorkingSet)fEditedWorkingSets.get(editedWorkingSet); 481 482 if (editedWorkingSet.getName().equals(originalWorkingSet.getName()) == false) { 483 editedWorkingSet.setName(originalWorkingSet.getName()); 484 } 485 if (editedWorkingSet.getElements().equals(originalWorkingSet.getElements()) == false) { 486 editedWorkingSet.setElements(originalWorkingSet.getElements()); 487 } 488 } 489 } 490 491 494 private void restoreRemovedWorkingSets() { 495 IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager(); 496 Iterator iterator= fRemovedWorkingSets.iterator(); 497 498 while (iterator.hasNext()) { 499 manager.addWorkingSet(((IWorkingSet)iterator.next())); 500 } 501 iterator= fRemovedMRUWorkingSets.iterator(); 502 while (iterator.hasNext()) { 503 manager.addRecentWorkingSet(((IWorkingSet)iterator.next())); 504 } 505 } 506 507 510 private void updateButtonAvailability() { 511 IStructuredSelection selection= (IStructuredSelection) fTableViewer.getSelection(); 512 boolean hasSelection= !selection.isEmpty(); 513 boolean hasSingleSelection= selection.size() == 1; 514 515 fRemoveButton.setEnabled(hasSelection && areAllGlobalWorkingSets(selection)); 516 fEditButton.setEnabled(hasSingleSelection && ((IWorkingSet)selection.getFirstElement()).isEditable()); 517 if (fUpButton != null) { 518 fUpButton.setEnabled(canMoveUp()); 519 } 520 if (fDownButton != null) { 521 fDownButton.setEnabled(canMoveDown()); 522 } 523 } 524 525 private boolean areAllGlobalWorkingSets(IStructuredSelection selection) { 526 Set globals= new HashSet (Arrays.asList(PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets())); 527 for (Iterator iter= selection.iterator(); iter.hasNext();) { 528 if (!globals.contains(iter.next())) 529 return false; 530 } 531 return true; 532 } 533 534 private void moveUp(List toMoveUp) { 535 if (toMoveUp.size() > 0) { 536 setElements(moveUp(fAllWorkingSets, toMoveUp)); 537 fTableViewer.reveal(toMoveUp.get(0)); 538 } 539 } 540 541 private void moveDown(List toMoveDown) { 542 if (toMoveDown.size() > 0) { 543 setElements(reverse(moveUp(reverse(fAllWorkingSets), toMoveDown))); 544 fTableViewer.reveal(toMoveDown.get(toMoveDown.size() - 1)); 545 } 546 } 547 548 private void setElements(List elements) { 549 fAllWorkingSets= elements; 550 fTableViewer.setInput(fAllWorkingSets); 551 updateButtonAvailability(); 552 } 553 554 private List moveUp(List elements, List move) { 555 int nElements= elements.size(); 556 List res= new ArrayList (nElements); 557 Object floating= null; 558 for (int i= 0; i < nElements; i++) { 559 Object curr= elements.get(i); 560 if (move.contains(curr)) { 561 res.add(curr); 562 } else { 563 if (floating != null) { 564 res.add(floating); 565 } 566 floating= curr; 567 } 568 } 569 if (floating != null) { 570 res.add(floating); 571 } 572 return res; 573 } 574 575 private List reverse(List p) { 576 List reverse= new ArrayList (p.size()); 577 for (int i= p.size() - 1; i >= 0; i--) { 578 reverse.add(p.get(i)); 579 } 580 return reverse; 581 } 582 583 private boolean canMoveUp() { 584 int[] indc= fTableViewer.getTable().getSelectionIndices(); 585 for (int i= 0; i < indc.length; i++) { 586 if (indc[i] != i) { 587 return true; 588 } 589 } 590 return false; 591 } 592 593 private boolean canMoveDown() { 594 int[] indc= fTableViewer.getTable().getSelectionIndices(); 595 int k= fAllWorkingSets.size() - 1; 596 for (int i= indc.length - 1; i >= 0; i--, k--) { 597 if (indc[i] != k) { 598 return true; 599 } 600 } 601 return false; 602 } 603 604 606 private void selectAll() { 607 fTableViewer.setAllChecked(true); 608 } 609 610 private void deselectAll() { 611 fTableViewer.setAllChecked(false); 612 } 613 } 614 | Popular Tags |