1 13 package org.eclipse.ui.internal.dialogs; 14 15 import java.util.Arrays ; 16 import java.util.Collections ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 import org.eclipse.jface.dialogs.Dialog; 21 import org.eclipse.jface.dialogs.IDialogConstants; 22 import org.eclipse.jface.viewers.ArrayContentProvider; 23 import org.eclipse.jface.viewers.CheckStateChangedEvent; 24 import org.eclipse.jface.viewers.CheckboxTableViewer; 25 import org.eclipse.jface.viewers.DoubleClickEvent; 26 import org.eclipse.jface.viewers.ICheckStateListener; 27 import org.eclipse.jface.viewers.IDoubleClickListener; 28 import org.eclipse.jface.viewers.ILabelProvider; 29 import org.eclipse.jface.viewers.ISelection; 30 import org.eclipse.jface.viewers.ISelectionChangedListener; 31 import org.eclipse.jface.viewers.IStructuredContentProvider; 32 import org.eclipse.jface.viewers.IStructuredSelection; 33 import org.eclipse.jface.viewers.SelectionChangedEvent; 34 import org.eclipse.jface.viewers.StructuredSelection; 35 import org.eclipse.swt.SWT; 36 import org.eclipse.swt.events.SelectionAdapter; 37 import org.eclipse.swt.events.SelectionEvent; 38 import org.eclipse.swt.events.SelectionListener; 39 import org.eclipse.swt.layout.GridData; 40 import org.eclipse.swt.layout.GridLayout; 41 import org.eclipse.swt.widgets.Button; 42 import org.eclipse.swt.widgets.Composite; 43 import org.eclipse.swt.widgets.Control; 44 import org.eclipse.swt.widgets.Shell; 45 import org.eclipse.ui.IWorkbenchWindow; 46 import org.eclipse.ui.IWorkingSet; 47 import org.eclipse.ui.IWorkingSetManager; 48 import org.eclipse.ui.PlatformUI; 49 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog; 50 import org.eclipse.ui.internal.AggregateWorkingSet; 51 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 52 import org.eclipse.ui.internal.WorkbenchMessages; 53 import org.eclipse.ui.internal.WorkbenchPlugin; 54 import org.eclipse.ui.internal.util.Util; 55 import org.eclipse.ui.model.WorkbenchViewerComparator; 56 57 64 public class WorkingSetSelectionDialog extends AbstractWorkingSetDialog { 65 private final static int SIZING_SELECTION_WIDGET_HEIGHT = 200; 66 67 private final static int SIZING_SELECTION_WIDGET_WIDTH = 50; 68 69 private ILabelProvider labelProvider; 70 71 private IStructuredContentProvider contentProvider; 72 73 private CheckboxTableViewer listViewer; 74 75 private boolean multiSelect; 76 77 private IWorkbenchWindow workbenchWindow; 78 79 private Button buttonWindowSet; 80 81 private Button buttonNoSet; 82 83 private Button buttonSelectedSets; 84 85 97 public WorkingSetSelectionDialog(Shell parentShell, boolean multi, String [] workingSetIds) { 98 super(parentShell, workingSetIds); 99 setShellStyle(getShellStyle() | SWT.RESIZE); 100 initWorkbenchWindow(); 101 102 contentProvider = new ArrayContentProvider(); 103 labelProvider = new WorkingSetLabelProvider(); 104 multiSelect = multi; 105 if (multiSelect) { 106 setTitle(WorkbenchMessages.WorkingSetSelectionDialog_title_multiSelect); 107 setMessage(WorkbenchMessages.WorkingSetSelectionDialog_message_multiSelect); 108 } else { 109 setTitle(WorkbenchMessages.WorkingSetSelectionDialog_title); 110 setMessage(WorkbenchMessages.WorkingSetSelectionDialog_message); 111 } 112 113 } 114 115 121 private void initWorkbenchWindow() { 122 Shell shellToCheck = getShell(); 123 124 workbenchWindow = Util.getWorkbenchWindowForShell(shellToCheck); 125 } 126 127 132 protected void cancelPressed() { 133 restoreAddedWorkingSets(); 134 restoreChangedWorkingSets(); 135 restoreRemovedWorkingSets(); 136 setSelection(null); 137 super.cancelPressed(); 138 } 139 140 145 protected void configureShell(Shell shell) { 146 super.configureShell(shell); 147 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, 148 IWorkbenchHelpContextIds.WORKING_SET_SELECTION_DIALOG); 149 } 150 151 157 protected Control createDialogArea(Composite parent) { 158 initializeDialogUnits(parent); 159 160 Composite composite = (Composite) super.createDialogArea(parent); 161 162 createMessageArea(composite); 163 164 SelectionListener listener = new SelectionAdapter() { 165 public void widgetSelected(SelectionEvent e) { 166 updateButtonAvailability(); 167 } 168 }; 169 170 buttonWindowSet = new Button(composite, SWT.RADIO); 171 buttonWindowSet.setText(WorkbenchMessages.WindowWorkingSets); 172 buttonWindowSet.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 173 buttonWindowSet.addSelectionListener(listener); 174 175 buttonNoSet = new Button(composite, SWT.RADIO); 176 buttonNoSet.setText(WorkbenchMessages.NoWorkingSet); 177 buttonNoSet.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 178 buttonNoSet.addSelectionListener(listener); 179 180 buttonSelectedSets = new Button(composite, SWT.RADIO); 181 buttonSelectedSets.setText(WorkbenchMessages.SelectedWorkingSets); 182 buttonSelectedSets.addSelectionListener(listener); 183 184 switch (getInitialRadioSelection()) { 185 case 0: 186 buttonWindowSet.setSelection(true); 187 break; 188 case 1: 189 buttonNoSet.setSelection(true); 190 break; 191 case 2: 192 buttonSelectedSets.setSelection(true); 193 break; 194 } 195 buttonSelectedSets 196 .setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 197 198 Composite viewerComposite = new Composite(composite, SWT.NONE); 199 GridLayout layout = new GridLayout(2, false); 200 layout.marginHeight = layout.marginWidth = 0; 201 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 202 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 203 viewerComposite.setLayout(layout); 204 205 GridData data = new GridData(GridData.FILL_BOTH); 206 data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT; 207 data.widthHint = SIZING_SELECTION_WIDGET_WIDTH + 300; viewerComposite.setLayoutData(data); 209 210 listViewer = CheckboxTableViewer.newCheckList(viewerComposite, 211 SWT.BORDER | SWT.MULTI); 212 data = new GridData(GridData.FILL_BOTH); 213 data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT; 214 data.widthHint = SIZING_SELECTION_WIDGET_WIDTH; 215 listViewer.getTable().setLayoutData(data); 216 217 listViewer.setLabelProvider(labelProvider); 218 listViewer.setContentProvider(contentProvider); 219 listViewer.setComparator(new WorkbenchViewerComparator()); 220 221 listViewer.addFilter(new WorkingSetFilter(getSupportedWorkingSetIds())); 222 223 listViewer.addSelectionChangedListener(new ISelectionChangedListener() { 224 public void selectionChanged(SelectionChangedEvent event) { 225 handleSelectionChanged(); 226 } 227 }); 228 listViewer.addDoubleClickListener(new IDoubleClickListener() { 229 public void doubleClick(DoubleClickEvent event) { 230 Object obj = ((IStructuredSelection) listViewer.getSelection()) 231 .getFirstElement(); 232 listViewer.setCheckedElements(new Object [] {obj}); 233 buttonWindowSet.setSelection(false); 234 buttonNoSet.setSelection(false); 235 buttonSelectedSets.setSelection(true); 236 okPressed(); 237 } 238 }); 239 listViewer.addCheckStateListener(new ICheckStateListener() { 240 public void checkStateChanged(CheckStateChangedEvent event) { 241 buttonWindowSet.setSelection(false); 243 buttonNoSet.setSelection(false); 244 buttonSelectedSets.setSelection(true); 245 } 246 }); 247 248 addModifyButtons(viewerComposite); 249 250 addSelectionButtons(composite); 251 252 253 listViewer.setInput(Arrays.asList(WorkbenchPlugin.getDefault() 254 .getWorkingSetManager().getWorkingSets())); 255 List initialElementSelections = getInitialElementSelections(); 256 if (multiSelect) { 257 listViewer.setCheckedElements(initialElementSelections.toArray()); 258 } else if (!initialElementSelections.isEmpty()) { 259 IWorkingSet set = (IWorkingSet) initialElementSelections.get(0); 260 if (set instanceof AggregateWorkingSet) { 261 AggregateWorkingSet aggregate = (AggregateWorkingSet) set; 262 listViewer.setCheckedElements(aggregate.getComponents()); 263 } 264 else { 265 listViewer.setCheckedElements(initialElementSelections.toArray()); 266 } 267 } 268 269 availableWorkingSetsChanged(); 270 Dialog.applyDialogFont(composite); 271 272 return composite; 273 } 274 275 private int getInitialRadioSelection() { 276 IWorkingSet windowSet = workbenchWindow.getActivePage().getAggregateWorkingSet(); 277 278 int selectionIndex; 279 if (getSelection() != null && getSelection().length > 0) { 280 if (windowSet.equals(getSelection()[0])) { 281 selectionIndex = 0; 282 } 283 else { 284 selectionIndex = 2; 285 } 286 } 287 else { 288 selectionIndex = 1; 289 } 290 291 return selectionIndex; 292 } 293 294 300 protected Control createContents(Composite parent) { 301 Control control = super.createContents(parent); 302 List selections = getInitialElementSelections(); 303 if (!selections.isEmpty()) { 304 listViewer.setSelection(new StructuredSelection(selections), true); 305 } 306 updateButtonAvailability(); 307 return control; 308 } 309 310 315 protected List getSelectedWorkingSets() { 316 ISelection selection = listViewer.getSelection(); 317 if (selection instanceof IStructuredSelection) { 318 return ((IStructuredSelection) selection).toList(); 319 } 320 return null; 321 } 322 323 326 void handleSelectionChanged() { 327 updateButtonAvailability(); 328 } 329 330 336 protected void okPressed() { 337 if (buttonWindowSet.getSelection()) { 338 IWorkingSet [] windowSet = new IWorkingSet[] {workbenchWindow.getActivePage().getAggregateWorkingSet()}; 339 setSelection(windowSet); 340 setResult(Arrays.asList(getSelection())); 341 } 342 else if (buttonNoSet.getSelection()) { 343 setSelection(new IWorkingSet[0]); 344 setResult(Arrays.asList(getSelection())); 345 } 346 else if (buttonSelectedSets.getSelection()) { 347 Object [] untypedResult = listViewer.getCheckedElements(); 348 IWorkingSet[] typedResult = new IWorkingSet[untypedResult.length]; 349 System.arraycopy(untypedResult, 0, typedResult, 0, 350 untypedResult.length); 351 if (multiSelect || typedResult.length <= 1) { 354 setSelection(typedResult); 355 setResult(Arrays.asList(typedResult)); 356 } 357 else { 358 String setId = getAggregateIdForSets(typedResult); 359 IWorkingSetManager workingSetManager = workbenchWindow 360 .getWorkbench().getWorkingSetManager(); 361 IWorkingSet aggregate = workingSetManager 362 .getWorkingSet(setId); 363 if (aggregate == null) { 364 aggregate = workingSetManager 365 .createAggregateWorkingSet( 366 setId, 367 WorkbenchMessages.WorkbenchPage_workingSet_multi_label, 368 typedResult); 369 workingSetManager.addWorkingSet(aggregate); 370 } 371 setSelection(new IWorkingSet[] {aggregate}); 372 setResult(Collections.singletonList(aggregate)); 373 } 374 } 375 376 super.okPressed(); 377 } 378 379 386 private String getAggregateIdForSets(IWorkingSet[] typedResult) { 387 StringBuffer buffer = new StringBuffer (); 388 buffer.append("Aggregate:"); for (int i = 0; i < typedResult.length; i++) { 390 buffer.append(typedResult[i].getName()).append(':'); 391 } 392 return buffer.toString(); 393 } 394 395 398 private void restoreAddedWorkingSets() { 399 IWorkingSetManager manager = WorkbenchPlugin.getDefault() 400 .getWorkingSetManager(); 401 Iterator iterator = getAddedWorkingSets().iterator(); 402 403 while (iterator.hasNext()) { 404 manager.removeWorkingSet(((IWorkingSet) iterator.next())); 405 } 406 } 407 408 411 private void restoreChangedWorkingSets() { 412 Iterator iterator = getEditedWorkingSets().keySet().iterator(); 413 414 while (iterator.hasNext()) { 415 IWorkingSet editedWorkingSet = (IWorkingSet) iterator.next(); 416 IWorkingSet originalWorkingSet = (IWorkingSet) getEditedWorkingSets() 417 .get(editedWorkingSet); 418 419 if (editedWorkingSet.getName().equals(originalWorkingSet.getName()) == false) { 420 editedWorkingSet.setName(originalWorkingSet.getName()); 421 } 422 if (editedWorkingSet.getElements().equals( 423 originalWorkingSet.getElements()) == false) { 424 editedWorkingSet.setElements(originalWorkingSet.getElements()); 425 } 426 } 427 } 428 429 432 private void restoreRemovedWorkingSets() { 433 IWorkingSetManager manager = WorkbenchPlugin.getDefault() 434 .getWorkingSetManager(); 435 Iterator iterator = getRemovedWorkingSets().iterator(); 436 437 while (iterator.hasNext()) { 438 manager.addWorkingSet(((IWorkingSet) iterator.next())); 439 } 440 iterator = getRemovedMRUWorkingSets().iterator(); 441 while (iterator.hasNext()) { 442 manager.addRecentWorkingSet(((IWorkingSet) iterator.next())); 443 } 444 } 445 446 451 public void setSelection(IWorkingSet[] workingSets) { 452 super.setSelection(workingSets); 453 setInitialSelections(workingSets == null ? new Object [0] : workingSets); 454 } 455 456 protected void availableWorkingSetsChanged() { 457 listViewer.setInput(PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets()); 458 super.availableWorkingSetsChanged(); 459 } 460 461 protected void selectAllSets() { 462 listViewer.setCheckedElements(PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets()); 463 buttonWindowSet.setSelection(false); 465 buttonNoSet.setSelection(false); 466 buttonSelectedSets.setSelection(true); 467 updateButtonAvailability(); 468 } 469 470 protected void deselectAllSets() { 471 listViewer.setCheckedElements(new Object [0]); 472 buttonWindowSet.setSelection(false); 474 buttonNoSet.setSelection(false); 475 buttonSelectedSets.setSelection(true); 476 updateButtonAvailability(); 477 } 478 } 479 | Popular Tags |