1 13 package org.eclipse.ui.internal.dialogs; 14 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Set ; 22 23 import org.eclipse.jface.dialogs.IDialogConstants; 24 import org.eclipse.jface.window.Window; 25 import org.eclipse.jface.wizard.WizardDialog; 26 import org.eclipse.swt.SWT; 27 import org.eclipse.swt.events.SelectionAdapter; 28 import org.eclipse.swt.events.SelectionEvent; 29 import org.eclipse.swt.layout.GridData; 30 import org.eclipse.swt.layout.GridLayout; 31 import org.eclipse.swt.widgets.Button; 32 import org.eclipse.swt.widgets.Composite; 33 import org.eclipse.swt.widgets.Shell; 34 import org.eclipse.ui.IWorkingSet; 35 import org.eclipse.ui.IWorkingSetManager; 36 import org.eclipse.ui.PlatformUI; 37 import org.eclipse.ui.dialogs.IWorkingSetEditWizard; 38 import org.eclipse.ui.dialogs.IWorkingSetNewWizard; 39 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog; 40 import org.eclipse.ui.dialogs.SelectionDialog; 41 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 42 import org.eclipse.ui.internal.WorkbenchMessages; 43 import org.eclipse.ui.internal.WorkbenchPlugin; 44 import org.eclipse.ui.internal.WorkingSet; 45 import org.eclipse.ui.internal.registry.WorkingSetRegistry; 46 47 52 public abstract class AbstractWorkingSetDialog extends SelectionDialog 53 implements IWorkingSetSelectionDialog { 54 55 private static final int ID_NEW = IDialogConstants.CLIENT_ID + 1; 56 private static final int ID_DETAILS = ID_NEW + 1; 57 private static final int ID_REMOVE = ID_DETAILS + 1; 58 private static final int ID_SELECTALL = ID_REMOVE + 1; 59 private static final int ID_DESELECTALL = ID_SELECTALL + 1; 60 61 private Button newButton; 62 63 private Button detailsButton; 64 65 private Button removeButton; 66 67 private Button selectAllButton; 68 69 private Button deselectAllButton; 70 71 private IWorkingSet[] result; 72 73 private List addedWorkingSets; 74 75 private List removedWorkingSets; 76 77 private Map editedWorkingSets; 78 79 private List removedMRUWorkingSets; 80 81 private Set workingSetIds; 82 83 protected AbstractWorkingSetDialog(Shell parentShell, String [] workingSetIds) { 84 super(parentShell); 85 if (workingSetIds != null) { 86 this.workingSetIds = new HashSet (); 87 for (int i = 0; i < workingSetIds.length; i++) { 88 this.workingSetIds.add(workingSetIds[i]); 89 } 90 } 91 } 92 93 98 protected Set getSupportedWorkingSetIds() { 99 return workingSetIds; 100 } 101 102 108 protected void addModifyButtons(Composite composite) { 109 Composite buttonComposite = new Composite(composite, SWT.RIGHT); 110 GridLayout layout = new GridLayout(); 111 layout.marginHeight = layout.marginWidth = 0; 112 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 113 buttonComposite.setLayout(layout); 114 GridData data = new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.GRAB_VERTICAL); 115 buttonComposite.setLayoutData(data); 116 117 newButton = createButton(buttonComposite, ID_NEW, 118 WorkbenchMessages.WorkingSetSelectionDialog_newButton_label, 119 false); 120 newButton.addSelectionListener(new SelectionAdapter() { 121 public void widgetSelected(SelectionEvent e) { 122 createWorkingSet(); 123 } 124 }); 125 126 detailsButton = createButton( 127 buttonComposite, 128 ID_DETAILS, 129 WorkbenchMessages.WorkingSetSelectionDialog_detailsButton_label, 130 false); 131 detailsButton.setEnabled(false); 132 detailsButton.addSelectionListener(new SelectionAdapter() { 133 public void widgetSelected(SelectionEvent e) { 134 editSelectedWorkingSet(); 135 } 136 }); 137 138 removeButton = createButton(buttonComposite, ID_REMOVE, 139 WorkbenchMessages.WorkingSetSelectionDialog_removeButton_label, 140 false); 141 removeButton.setEnabled(false); 142 removeButton.addSelectionListener(new SelectionAdapter() { 143 public void widgetSelected(SelectionEvent e) { 144 removeSelectedWorkingSets(); 145 } 146 }); 147 148 layout.numColumns = 1; } 150 151 156 protected void addSelectionButtons(Composite composite) { 157 Composite buttonComposite = new Composite(composite, SWT.NONE); 158 GridLayout layout = new GridLayout(2, false); 159 layout.marginHeight = layout.marginWidth = 0; 160 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 161 buttonComposite.setLayout(layout); 162 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 163 buttonComposite.setLayoutData(data); 164 165 selectAllButton = createButton( 166 buttonComposite, 167 ID_SELECTALL, 168 WorkbenchMessages.SelectionDialog_selectLabel, 169 false); 170 selectAllButton.addSelectionListener(new SelectionAdapter() { 171 public void widgetSelected(SelectionEvent e) { 172 selectAllSets(); 173 } 174 }); 175 176 deselectAllButton = createButton( 177 buttonComposite, 178 ID_DESELECTALL, 179 WorkbenchMessages.SelectionDialog_deselectLabel, 180 false); 181 deselectAllButton.addSelectionListener(new SelectionAdapter() { 182 public void widgetSelected(SelectionEvent e) { 183 deselectAllSets(); 184 } 185 }); 186 } 187 188 191 protected abstract void selectAllSets(); 192 193 196 protected abstract void deselectAllSets(); 197 198 204 void editSelectedWorkingSet() { 205 IWorkingSetManager manager = WorkbenchPlugin.getDefault() 206 .getWorkingSetManager(); 207 IWorkingSet editWorkingSet = (IWorkingSet) getSelectedWorkingSets() 208 .get(0); 209 IWorkingSetEditWizard wizard = manager 210 .createWorkingSetEditWizard(editWorkingSet); 211 WizardDialog dialog = new WizardDialog(getShell(), wizard); 212 IWorkingSet originalWorkingSet = (IWorkingSet) editedWorkingSets 213 .get(editWorkingSet); 214 boolean firstEdit = originalWorkingSet == null; 215 216 if (firstEdit) { 219 originalWorkingSet = new WorkingSet(editWorkingSet.getName(), 220 editWorkingSet.getLabel(), editWorkingSet.getElements()); 221 } else { 222 editedWorkingSets.remove(editWorkingSet); 223 } 224 dialog.create(); 225 PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(), 226 IWorkbenchHelpContextIds.WORKING_SET_EDIT_WIZARD); 227 if (dialog.open() == Window.OK) { 228 editWorkingSet = wizard.getSelection(); 229 availableWorkingSetsChanged(); 230 updateButtonAvailability(); 233 } 234 editedWorkingSets.put(editWorkingSet, originalWorkingSet); 235 } 236 237 240 void createWorkingSet() { 241 IWorkingSetManager manager = WorkbenchPlugin.getDefault() 242 .getWorkingSetManager(); 243 String ids[] = null; 244 if (workingSetIds != null) { 245 ids = (String []) workingSetIds.toArray(new String [workingSetIds 246 .size()]); 247 } 248 IWorkingSetNewWizard wizard = manager.createWorkingSetNewWizard(ids); 249 WizardDialog dialog = new WizardDialog(getShell(), wizard); 253 254 dialog.create(); 255 PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(), 256 IWorkbenchHelpContextIds.WORKING_SET_NEW_WIZARD); 257 if (dialog.open() == Window.OK) { 258 IWorkingSet workingSet = wizard.getSelection(); 259 manager.addWorkingSet(workingSet); 260 addedWorkingSets.add(workingSet); 261 availableWorkingSetsChanged(); 262 } 263 } 264 265 protected abstract List getSelectedWorkingSets(); 266 267 276 protected void availableWorkingSetsChanged() { 277 boolean enable = PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets().length > 0; 278 if (!(selectAllButton == null || selectAllButton.isDisposed())){ 279 selectAllButton.setEnabled(enable); 280 } 281 if (!(deselectAllButton == null || deselectAllButton.isDisposed())){ 282 deselectAllButton.setEnabled(enable); 283 } 284 } 285 286 289 public IWorkingSet[] getSelection() { 290 return result; 291 } 292 293 296 public void setSelection(IWorkingSet[] selection) { 297 result = selection; 298 } 299 300 305 public int open() { 306 addedWorkingSets = new ArrayList (); 307 removedWorkingSets = new ArrayList (); 308 editedWorkingSets = new HashMap (); 309 removedMRUWorkingSets = new ArrayList (); 310 return super.open(); 311 } 312 313 319 protected final List getAddedWorkingSets() { 320 return addedWorkingSets; 321 } 322 323 329 protected final Map getEditedWorkingSets() { 330 return editedWorkingSets; 331 } 332 333 339 protected final List getRemovedMRUWorkingSets() { 340 return removedMRUWorkingSets; 341 } 342 343 349 protected final List getRemovedWorkingSets() { 350 return removedWorkingSets; 351 } 352 353 356 protected void updateButtonAvailability() { 357 List selection = getSelectedWorkingSets(); 358 boolean hasSelection = selection != null && !selection.isEmpty(); 359 boolean hasSingleSelection = hasSelection; 360 WorkingSetRegistry registry = WorkbenchPlugin.getDefault() 361 .getWorkingSetRegistry(); 362 363 newButton.setEnabled(registry.hasNewPageWorkingSetDescriptor()); 364 365 removeButton.setEnabled(hasSelection); 366 367 IWorkingSet selectedWorkingSet = null; 368 if (hasSelection) { 369 hasSingleSelection = selection.size() == 1; 370 if (hasSingleSelection) { 371 selectedWorkingSet = (IWorkingSet) selection 372 .get(0); 373 } 374 } 375 detailsButton.setEnabled(hasSingleSelection 376 && selectedWorkingSet.isEditable()); 377 378 getOkButton().setEnabled(true); 379 } 380 381 384 protected void removeSelectedWorkingSets() { 385 List selection = getSelectedWorkingSets(); 386 removeSelectedWorkingSets(selection); 387 } 388 389 396 protected void removeSelectedWorkingSets(List selection) { 397 IWorkingSetManager manager = WorkbenchPlugin.getDefault() 398 .getWorkingSetManager(); 399 Iterator iter = selection.iterator(); 400 while (iter.hasNext()) { 401 IWorkingSet workingSet = (IWorkingSet) iter.next(); 402 if (getAddedWorkingSets().contains(workingSet)) { 403 getAddedWorkingSets().remove(workingSet); 404 } else { 405 IWorkingSet[] recentWorkingSets = manager 406 .getRecentWorkingSets(); 407 for (int i = 0; i < recentWorkingSets.length; i++) { 408 if (workingSet.equals(recentWorkingSets[i])) { 409 getRemovedMRUWorkingSets().add(workingSet); 410 break; 411 } 412 } 413 getRemovedWorkingSets().add(workingSet); 414 } 415 manager.removeWorkingSet(workingSet); 416 } 417 availableWorkingSetsChanged(); 418 } 419 } 420 | Popular Tags |