Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2010 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
* Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog font |
11 |
* should be activated and used by other components. |
12 |
*******************************************************************************/ |
13 |
package org.eclipse.ui.internal.dialogs; |
14 |
|
15 |
import java.util.ArrayList; |
16 |
import java.util.Arrays; |
17 |
import java.util.Comparator; |
18 |
import java.util.HashMap; |
19 |
import java.util.HashSet; |
20 |
import java.util.Iterator; |
21 |
import java.util.List; |
22 |
import java.util.Map; |
23 |
import java.util.Set; |
24 |
import org.eclipse.jface.dialogs.IDialogConstants; |
25 |
import org.eclipse.jface.viewers.CheckStateChangedEvent; |
26 |
import org.eclipse.jface.viewers.CheckboxTableViewer; |
27 |
import org.eclipse.jface.viewers.DoubleClickEvent; |
28 |
import org.eclipse.jface.viewers.ICheckStateListener; |
29 |
import org.eclipse.jface.viewers.IDoubleClickListener; |
30 |
import org.eclipse.jface.viewers.ILabelProvider; |
31 |
import org.eclipse.jface.viewers.ISelection; |
32 |
import org.eclipse.jface.viewers.ISelectionChangedListener; |
33 |
import org.eclipse.jface.viewers.IStructuredContentProvider; |
34 |
import org.eclipse.jface.viewers.IStructuredSelection; |
35 |
import org.eclipse.jface.viewers.SelectionChangedEvent; |
36 |
import org.eclipse.jface.viewers.StructuredSelection; |
37 |
import org.eclipse.jface.viewers.Viewer; |
38 |
import org.eclipse.jface.viewers.ViewerComparator; |
39 |
import org.eclipse.jface.viewers.ViewerFilter; |
40 |
import org.eclipse.jface.window.Window; |
41 |
import org.eclipse.jface.wizard.WizardDialog; |
42 |
import org.eclipse.swt.SWT; |
43 |
import org.eclipse.swt.events.SelectionAdapter; |
44 |
import org.eclipse.swt.events.SelectionEvent; |
45 |
import org.eclipse.swt.layout.GridData; |
46 |
import org.eclipse.swt.layout.GridLayout; |
47 |
import org.eclipse.swt.widgets.Button; |
48 |
import org.eclipse.swt.widgets.Composite; |
49 |
import org.eclipse.swt.widgets.Control; |
50 |
import org.eclipse.swt.widgets.Shell; |
51 |
import org.eclipse.ui.IWorkingSet; |
52 |
import org.eclipse.ui.IWorkingSetManager; |
53 |
import org.eclipse.ui.PlatformUI; |
54 |
import org.eclipse.ui.dialogs.IWorkingSetEditWizard; |
55 |
import org.eclipse.ui.dialogs.IWorkingSetNewWizard; |
56 |
import org.eclipse.ui.dialogs.SelectionDialog; |
57 |
import org.eclipse.ui.internal.WorkbenchMessages; |
58 |
import org.eclipse.ui.internal.WorkingSetComparator; |
59 |
|
60 |
public class WorkingSetConfigurationDialog extends SelectionDialog { |
61 |
|
62 |
private List fAllWorkingSets; |
63 |
private CheckboxTableViewer fTableViewer; |
64 |
|
65 |
private Button fNewButton; |
66 |
private Button fEditButton; |
67 |
private Button fRemoveButton; |
68 |
private Button fUpButton; |
69 |
private Button fDownButton; |
70 |
private Button fSelectAll; |
71 |
private Button fDeselectAll; |
72 |
|
73 |
/** |
74 |
* Sort working sets button. |
75 |
* |
76 |
* @since 3.5 |
77 |
*/ |
78 |
private Button fSortWorkingSet; |
79 |
|
80 |
private IWorkingSet[] fResult; |
81 |
private List fAddedWorkingSets; |
82 |
private List fRemovedWorkingSets; |
83 |
private Map fEditedWorkingSets; |
84 |
private List fRemovedMRUWorkingSets; |
85 |
private String[] fNewWorkingSets; |
86 |
|
87 |
private ILabelProvider fLabelProvider; |
88 |
private ViewerFilter fFilter; |
89 |
private String fHelpContext; |
90 |
|
91 |
private int nextButtonId= IDialogConstants.CLIENT_ID + 1; |
92 |
|
93 |
/** |
94 |
* Value of sorted state of working sets. |
95 |
* |
96 |
* @since 3.5 |
97 |
*/ |
98 |
private boolean fIsSortingEnabled; |
99 |
|
100 |
/** |
101 |
* The working set comparator. |
102 |
* |
103 |
* @since 3.5 |
104 |
*/ |
105 |
private Comparator fComparator; |
106 |
|
107 |
public WorkingSetConfigurationDialog(Shell parentShell, IWorkingSet[] allWorkingSets, |
108 |
String[] newWorkingSets, ILabelProvider labelProvider, ViewerFilter filter, |
109 |
String helpContext, boolean isSortingEnabled) { |
110 |
super(parentShell); |
111 |
setTitle(WorkbenchMessages.WorkingSetConfigurationDialog_title); |
112 |
setMessage(WorkbenchMessages.WorkingSetConfigurationDialog_message); |
113 |
fNewWorkingSets = newWorkingSets; |
114 |
fAllWorkingSets= new ArrayList(allWorkingSets.length); |
115 |
fFilter = filter; |
116 |
for (int i= 0; i < allWorkingSets.length; i++) { |
117 |
if (fFilter.select(null, null, allWorkingSets[i])) |
118 |
fAllWorkingSets.add(allWorkingSets[i]); |
119 |
} |
120 |
fLabelProvider = labelProvider; |
121 |
fHelpContext = helpContext; |
122 |
fIsSortingEnabled= isSortingEnabled; |
123 |
} |
124 |
|
125 |
/** |
126 |
* {@inheritDoc} |
127 |
*/ |
128 |
protected void configureShell(Shell shell) { |
129 |
super.configureShell(shell); |
130 |
PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, fHelpContext); |
131 |
} |
132 |
|
133 |
/** |
134 |
* Returns the selected working sets |
135 |
* |
136 |
* @return the selected working sets |
137 |
*/ |
138 |
public IWorkingSet[] getSelection() { |
139 |
return fResult; |
140 |
} |
141 |
|
142 |
/** |
143 |
* Sets the initial selection |
144 |
* |
145 |
* @param workingSets the initial selection |
146 |
*/ |
147 |
public void setSelection(IWorkingSet[] workingSets) { |
148 |
fResult= workingSets; |
149 |
setInitialSelections(workingSets); |
150 |
} |
151 |
|
152 |
/** |
153 |
* {@inheritDoc} |
154 |
*/ |
155 |
protected Control createContents(Composite parent) { |
156 |
Control control= super.createContents(parent); |
157 |
setInitialSelection(); |
158 |
updateButtonAvailability(); |
159 |
return control; |
160 |
} |
161 |
|
162 |
/** |
163 |
* {@inheritDoc} |
164 |
*/ |
165 |
protected Control createDialogArea(Composite parent) { |
166 |
Composite composite= (Composite)super.createDialogArea(parent); |
167 |
|
168 |
createMessageArea(composite); |
169 |
Composite inner= new Composite(composite, SWT.NONE); |
170 |
inner.setLayoutData(new GridData(GridData.FILL_BOTH)); |
171 |
GridLayout layout= new GridLayout(); |
172 |
layout.numColumns= 2; |
173 |
layout.marginHeight= 0; |
174 |
layout.marginWidth= 0; |
175 |
inner.setLayout(layout); |
176 |
createTableViewer(inner); |
177 |
createOrderButtons(inner); |
178 |
createModifyButtons(composite); |
179 |
if (fIsSortingEnabled) { |
180 |
fTableViewer.setComparator(new ViewerComparator(getComparator()) { |
181 |
/* |
182 |
* @see ViewerComparator#compare(Viewer, Object, Object) |
183 |
* @since 3.5 |
184 |
*/ |
185 |
public int compare(Viewer viewer, Object e1, Object e2) { |
186 |
return getComparator().compare(e1, e2); |
187 |
} |
188 |
}); |
189 |
} |
190 |
fTableViewer.setInput(fAllWorkingSets); |
191 |
applyDialogFont(composite); |
192 |
|
193 |
return composite; |
194 |
} |
195 |
|
196 |
private void createTableViewer(Composite parent) { |
197 |
fTableViewer= CheckboxTableViewer.newCheckList(parent, SWT.BORDER | SWT.MULTI); |
198 |
fTableViewer.addCheckStateListener(new ICheckStateListener() { |
199 |
public void checkStateChanged(CheckStateChangedEvent event) { |
200 |
updateButtonAvailability(); |
201 |
} |
202 |
}); |
203 |
GridData data= new GridData(GridData.FILL_BOTH); |
204 |
data.heightHint= convertHeightInCharsToPixels(20); |
205 |
data.widthHint= convertWidthInCharsToPixels(50); |
206 |
fTableViewer.getTable().setLayoutData(data); |
207 |
|
208 |
fTableViewer.addFilter(fFilter); |
209 |
fTableViewer.setLabelProvider(fLabelProvider); |
210 |
fTableViewer.setContentProvider(new IStructuredContentProvider() { |
211 |
public Object[] getElements(Object element) { |
212 |
return ((List)element).toArray(); |
213 |
} |
214 |
public void dispose() { |
215 |
} |
216 |
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { |
217 |
} |
218 |
}); |
219 |
fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() { |
220 |
public void selectionChanged(SelectionChangedEvent event) { |
221 |
handleSelectionChanged(); |
222 |
} |
223 |
}); |
224 |
fTableViewer.addDoubleClickListener(new IDoubleClickListener() { |
225 |
public void doubleClick(DoubleClickEvent event) { |
226 |
if (fEditButton.isEnabled()) |
227 |
editSelectedWorkingSet(); |
228 |
} |
229 |
}); |
230 |
} |
231 |
|
232 |
private void createModifyButtons(Composite composite) { |
233 |
Composite buttonComposite= new Composite(composite, SWT.RIGHT); |
234 |
GridLayout layout= new GridLayout(); |
235 |
layout.numColumns= 2; |
236 |
buttonComposite.setLayout(layout); |
237 |
GridData data= new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.GRAB_HORIZONTAL); |
238 |
data.grabExcessHorizontalSpace= true; |
239 |
composite.setData(data); |
240 |
|
241 |
fNewButton= createButton(buttonComposite, nextButtonId++, |
242 |
WorkbenchMessages.WorkingSetConfigurationDialog_new_label, false); |
243 |
fNewButton.addSelectionListener(new SelectionAdapter() { |
244 |
public void widgetSelected(SelectionEvent e) { |
245 |
createWorkingSet(); |
246 |
} |
247 |
}); |
248 |
|
249 |
fEditButton= createButton(buttonComposite, nextButtonId++, |
250 |
WorkbenchMessages.WorkingSetConfigurationDialog_edit_label, false); |
251 |
fEditButton.addSelectionListener(new SelectionAdapter() { |
252 |
public void widgetSelected(SelectionEvent e) { |
253 |
editSelectedWorkingSet(); |
254 |
} |
255 |
}); |
256 |
|
257 |
fRemoveButton= createButton(buttonComposite, nextButtonId++, |
258 |
WorkbenchMessages.WorkingSetConfigurationDialog_remove_label, false); |
259 |
fRemoveButton.addSelectionListener(new SelectionAdapter() { |
260 |
public void widgetSelected(SelectionEvent e) { |
261 |
removeSelectedWorkingSets(); |
262 |
} |
263 |
}); |
264 |
} |
265 |
|
266 |
private void createOrderButtons(Composite parent) { |
267 |
Composite buttons= new Composite(parent, SWT.NONE); |
268 |
buttons.setLayoutData(new GridData(GridData.FILL_VERTICAL)); |
269 |
GridLayout layout= new GridLayout(); |
270 |
layout.marginHeight= 0; |
271 |
layout.marginWidth= 0; |
272 |
buttons.setLayout(layout); |
273 |
|
274 |
fUpButton= new Button(buttons, SWT.PUSH); |
275 |
fUpButton.setText(WorkbenchMessages.WorkingSetConfigurationDialog_up_label); |
276 |
setButtonLayoutData(fUpButton); |
277 |
fUpButton.addSelectionListener(new SelectionAdapter() { |
278 |
public void widgetSelected(SelectionEvent e) { |
279 |
moveUp(((IStructuredSelection)fTableViewer.getSelection()).toList()); |
280 |
} |
281 |
}); |
282 |
|
283 |
fDownButton= new Button(buttons, SWT.PUSH); |
284 |
fDownButton.setText(WorkbenchMessages.WorkingSetConfigurationDialog_down_label); |
285 |
setButtonLayoutData(fDownButton); |
286 |
fDownButton.addSelectionListener(new SelectionAdapter() { |
287 |
public void widgetSelected(SelectionEvent e) { |
288 |
moveDown(((IStructuredSelection)fTableViewer.getSelection()).toList()); |
289 |
} |
290 |
}); |
291 |
|
292 |
fSelectAll= new Button(buttons, SWT.PUSH); |
293 |
fSelectAll.setText(WorkbenchMessages.WorkingSetConfigurationDialog_selectAll_label); |
294 |
setButtonLayoutData(fSelectAll); |
295 |
fSelectAll.addSelectionListener(new SelectionAdapter() { |
296 |
public void widgetSelected(SelectionEvent e) { |
297 |
selectAll(); |
298 |
} |
299 |
}); |
300 |
|
301 |
fDeselectAll= new Button(buttons, SWT.PUSH); |
302 |
fDeselectAll.setText(WorkbenchMessages.WorkingSetConfigurationDialog_deselectAll_label); |
303 |
setButtonLayoutData(fDeselectAll); |
304 |
fDeselectAll.addSelectionListener(new SelectionAdapter() { |
305 |
public void widgetSelected(SelectionEvent e) { |
306 |
deselectAll(); |
307 |
} |
308 |
}); |
309 |
/** |
310 |
* A check box that has persistence to sort the working sets alphabetically in the |
311 |
* WorkingSetConfigurationDialog. It restores the unsorted order of the working sets when |
312 |
* unchecked. |
313 |
* |
314 |
* @since 3.5 |
315 |
*/ |
316 |
fSortWorkingSet= new Button(parent, SWT.CHECK); |
317 |
fSortWorkingSet.setText(WorkbenchMessages.WorkingSetConfigurationDialog_sort_working_sets); |
318 |
fSortWorkingSet.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, false)); |
319 |
fSortWorkingSet.setSelection(fIsSortingEnabled); |
320 |
fSortWorkingSet.addSelectionListener(new SelectionAdapter() { |
321 |
public void widgetSelected(SelectionEvent e) { |
322 |
fIsSortingEnabled= fSortWorkingSet.getSelection(); |
323 |
updateButtonAvailability(); |
324 |
if (fIsSortingEnabled) { |
325 |
fTableViewer.setComparator(new ViewerComparator(getComparator()) { |
326 |
/* |
327 |
* @see ViewerComparator#compare(Viewer, Object, Object) |
328 |
* @since 3.5 |
329 |
*/ |
330 |
public int compare(Viewer viewer, Object e1, Object e2) { |
331 |
return getComparator().compare(e1, e2); |
332 |
} |
333 |
}); |
334 |
} else { |
335 |
fTableViewer.setComparator(null); |
336 |
} |
337 |
} |
338 |
}); |
339 |
} |
340 |
|
341 |
/** |
342 |
* {@inheritDoc} |
343 |
*/ |
344 |
protected void okPressed() { |
345 |
List newResult= getResultWorkingSets(); |
346 |
fResult= (IWorkingSet[])newResult.toArray(new IWorkingSet[newResult.size()]); |
347 |
setResult(newResult); |
348 |
super.okPressed(); |
349 |
} |
350 |
|
351 |
private List getResultWorkingSets() { |
352 |
Object[] checked= fTableViewer.getCheckedElements(); |
353 |
return new ArrayList(Arrays.asList(checked)); |
354 |
} |
355 |
|
356 |
/** |
357 |
* {@inheritDoc} |
358 |
*/ |
359 |
protected void cancelPressed() { |
360 |
restoreAddedWorkingSets(); |
361 |
restoreChangedWorkingSets(); |
362 |
restoreRemovedWorkingSets(); |
363 |
super.cancelPressed(); |
364 |
} |
365 |
|
366 |
private void setInitialSelection() { |
367 |
List selections= getInitialElementSelections(); |
368 |
if (!selections.isEmpty()) { |
369 |
fTableViewer.setCheckedElements(selections.toArray()); |
370 |
} |
371 |
} |
372 |
|
373 |
private void createWorkingSet() { |
374 |
IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager(); |
375 |
IWorkingSetNewWizard wizard = manager.createWorkingSetNewWizard(fNewWorkingSets); |
376 |
// the wizard can't be null since we have at least the Java working set. |
377 |
WizardDialog dialog= new WizardDialog(getShell(), wizard); |
378 |
dialog.create(); |
379 |
if (dialog.open() == Window.OK) { |
380 |
IWorkingSet workingSet= wizard.getSelection(); |
381 |
if (fFilter.select(null, null, workingSet)) { |
382 |
fAllWorkingSets.add(workingSet); |
383 |
fTableViewer.add(workingSet); |
384 |
fTableViewer.setSelection(new StructuredSelection(workingSet), true); |
385 |
fTableViewer.setChecked(workingSet, true); |
386 |
manager.addWorkingSet(workingSet); |
387 |
fAddedWorkingSets.add(workingSet); |
388 |
} |
389 |
} |
390 |
} |
391 |
|
392 |
private void editSelectedWorkingSet() { |
393 |
IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager(); |
394 |
IWorkingSet editWorkingSet= (IWorkingSet)((IStructuredSelection)fTableViewer.getSelection()).getFirstElement(); |
395 |
IWorkingSetEditWizard wizard= manager.createWorkingSetEditWizard(editWorkingSet); |
396 |
WizardDialog dialog= new WizardDialog(getShell(), wizard); |
397 |
IWorkingSet originalWorkingSet= (IWorkingSet)fEditedWorkingSets.get(editWorkingSet); |
398 |
boolean firstEdit= originalWorkingSet == null; |
399 |
|
400 |
// save the original working set values for restoration when selection |
401 |
// dialog is cancelled. |
402 |
if (firstEdit) { |
403 |
originalWorkingSet= |
404 |
PlatformUI.getWorkbench().getWorkingSetManager(). |
405 |
createWorkingSet(editWorkingSet.getName(), editWorkingSet.getElements()); |
406 |
} else { |
407 |
fEditedWorkingSets.remove(editWorkingSet); |
408 |
} |
409 |
dialog.create(); |
410 |
if (dialog.open() == Window.OK) { |
411 |
editWorkingSet= wizard.getSelection(); |
412 |
if (fIsSortingEnabled) |
413 |
fTableViewer.refresh(); |
414 |
else |
415 |
fTableViewer.update(editWorkingSet, null); |
416 |
|
417 |
// make sure ok button is enabled when the selected working set |
418 |
// is edited. Fixes bug 33386. |
419 |
updateButtonAvailability(); |
420 |
} |
421 |
fEditedWorkingSets.put(editWorkingSet, originalWorkingSet); |
422 |
} |
423 |
|
424 |
/** |
425 |
* Called when the selection has changed. |
426 |
*/ |
427 |
void handleSelectionChanged() { |
428 |
updateButtonAvailability(); |
429 |
} |
430 |
|
431 |
/** |
432 |
* Overrides method in Dialog |
433 |
* |
434 |
* @see org.eclipse.jface.dialogs.Dialog#open() |
435 |
*/ |
436 |
public int open() { |
437 |
fAddedWorkingSets= new ArrayList(); |
438 |
fRemovedWorkingSets= new ArrayList(); |
439 |
fEditedWorkingSets= new HashMap(); |
440 |
fRemovedMRUWorkingSets= new ArrayList(); |
441 |
return super.open(); |
442 |
} |
443 |
|
444 |
/** |
445 |
* Removes the selected working sets from the workbench. |
446 |
*/ |
447 |
private void removeSelectedWorkingSets() { |
448 |
ISelection selection= fTableViewer.getSelection(); |
449 |
if (selection instanceof IStructuredSelection) { |
450 |
IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager(); |
451 |
Iterator iter= ((IStructuredSelection)selection).iterator(); |
452 |
while (iter.hasNext()) { |
453 |
IWorkingSet workingSet= (IWorkingSet)iter.next(); |
454 |
if (fAddedWorkingSets.contains(workingSet)) { |
455 |
fAddedWorkingSets.remove(workingSet); |
456 |
} else { |
457 |
IWorkingSet[] recentWorkingSets= manager.getRecentWorkingSets(); |
458 |
for (int i= 0; i < recentWorkingSets.length; i++) { |
459 |
if (workingSet.equals(recentWorkingSets[i])) { |
460 |
fRemovedMRUWorkingSets.add(workingSet); |
461 |
break; |
462 |
} |
463 |
} |
464 |
fRemovedWorkingSets.add(workingSet); |
465 |
} |
466 |
fAllWorkingSets.remove(workingSet); |
467 |
manager.removeWorkingSet(workingSet); |
468 |
} |
469 |
fTableViewer.remove(((IStructuredSelection)selection).toArray()); |
470 |
} |
471 |
} |
472 |
|
473 |
/** |
474 |
* Removes newly created working sets from the working set manager. |
475 |
*/ |
476 |
private void restoreAddedWorkingSets() { |
477 |
IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager(); |
478 |
Iterator iterator= fAddedWorkingSets.iterator(); |
479 |
|
480 |
while (iterator.hasNext()) { |
481 |
manager.removeWorkingSet(((IWorkingSet)iterator.next())); |
482 |
} |
483 |
} |
484 |
|
485 |
/** |
486 |
* Rolls back changes to working sets. |
487 |
*/ |
488 |
private void restoreChangedWorkingSets() { |
489 |
Iterator iterator= fEditedWorkingSets.keySet().iterator(); |
490 |
|
491 |
while (iterator.hasNext()) { |
492 |
IWorkingSet editedWorkingSet= (IWorkingSet)iterator.next(); |
493 |
IWorkingSet originalWorkingSet= (IWorkingSet)fEditedWorkingSets.get(editedWorkingSet); |
494 |
|
495 |
if (editedWorkingSet.getName().equals(originalWorkingSet.getName()) == false) { |
496 |
editedWorkingSet.setName(originalWorkingSet.getName()); |
497 |
} |
498 |
if (editedWorkingSet.getElements().equals(originalWorkingSet.getElements()) == false) { |
499 |
editedWorkingSet.setElements(originalWorkingSet.getElements()); |
500 |
} |
501 |
} |
502 |
} |
503 |
|
504 |
/** |
505 |
* Adds back removed working sets to the working set manager. |
506 |
*/ |
507 |
private void restoreRemovedWorkingSets() { |
508 |
IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager(); |
509 |
Iterator iterator= fRemovedWorkingSets.iterator(); |
510 |
|
511 |
while (iterator.hasNext()) { |
512 |
manager.addWorkingSet(((IWorkingSet)iterator.next())); |
513 |
} |
514 |
iterator= fRemovedMRUWorkingSets.iterator(); |
515 |
while (iterator.hasNext()) { |
516 |
manager.addRecentWorkingSet(((IWorkingSet)iterator.next())); |
517 |
} |
518 |
} |
519 |
|
520 |
/** |
521 |
* Updates the modify buttons' enabled state based on the current seleciton. |
522 |
*/ |
523 |
private void updateButtonAvailability() { |
524 |
IStructuredSelection selection= (IStructuredSelection) fTableViewer.getSelection(); |
525 |
boolean hasSelection= !selection.isEmpty(); |
526 |
boolean hasSingleSelection= selection.size() == 1; |
527 |
|
528 |
fRemoveButton.setEnabled(hasSelection && areAllGlobalWorkingSets(selection)); |
529 |
fEditButton.setEnabled(hasSingleSelection && ((IWorkingSet)selection.getFirstElement()).isEditable()); |
530 |
if (fUpButton != null) { |
531 |
fUpButton.setEnabled(canMoveUp()); |
532 |
} |
533 |
if (fDownButton != null) { |
534 |
fDownButton.setEnabled(canMoveDown()); |
535 |
} |
536 |
} |
537 |
|
538 |
private boolean areAllGlobalWorkingSets(IStructuredSelection selection) { |
539 |
Set globals= new HashSet(Arrays.asList(PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSets())); |
540 |
for (Iterator iter= selection.iterator(); iter.hasNext();) { |
541 |
if (!globals.contains(iter.next())) |
542 |
return false; |
543 |
} |
544 |
return true; |
545 |
} |
546 |
|
547 |
private void moveUp(List toMoveUp) { |
548 |
if (toMoveUp.size() > 0) { |
549 |
setElements(moveUp(fAllWorkingSets, toMoveUp)); |
550 |
fTableViewer.reveal(toMoveUp.get(0)); |
551 |
} |
552 |
} |
553 |
|
554 |
private void moveDown(List toMoveDown) { |
555 |
if (toMoveDown.size() > 0) { |
556 |
setElements(reverse(moveUp(reverse(fAllWorkingSets), toMoveDown))); |
557 |
fTableViewer.reveal(toMoveDown.get(toMoveDown.size() - 1)); |
558 |
} |
559 |
} |
560 |
|
561 |
private void setElements(List elements) { |
562 |
fAllWorkingSets= elements; |
563 |
fTableViewer.setInput(fAllWorkingSets); |
564 |
updateButtonAvailability(); |
565 |
} |
566 |
|
567 |
private List moveUp(List elements, List move) { |
568 |
int nElements= elements.size(); |
569 |
List res= new ArrayList(nElements); |
570 |
Object floating= null; |
571 |
for (int i= 0; i < nElements; i++) { |
572 |
Object curr= elements.get(i); |
573 |
if (move.contains(curr)) { |
574 |
res.add(curr); |
575 |
} else { |
576 |
if (floating != null) { |
577 |
res.add(floating); |
578 |
} |
579 |
floating= curr; |
580 |
} |
581 |
} |
582 |
if (floating != null) { |
583 |
res.add(floating); |
584 |
} |
585 |
return res; |
586 |
} |
587 |
|
588 |
private List reverse(List p) { |
589 |
List reverse= new ArrayList(p.size()); |
590 |
for (int i= p.size() - 1; i >= 0; i--) { |
591 |
reverse.add(p.get(i)); |
592 |
} |
593 |
return reverse; |
594 |
} |
595 |
|
596 |
private boolean canMoveUp() { |
597 |
if (!fIsSortingEnabled) { |
598 |
int[] indc= fTableViewer.getTable().getSelectionIndices(); |
599 |
for (int i= 0; i < indc.length; i++) { |
600 |
if (indc[i] != i) { |
601 |
return true; |
602 |
} |
603 |
} |
604 |
} |
605 |
return false; |
606 |
} |
607 |
|
608 |
private boolean canMoveDown() { |
609 |
if (!fIsSortingEnabled) { |
610 |
int[] indc= fTableViewer.getTable().getSelectionIndices(); |
611 |
int k= fAllWorkingSets.size() - 1; |
612 |
for (int i= indc.length - 1; i >= 0; i--, k--) { |
613 |
if (indc[i] != k) { |
614 |
return true; |
615 |
} |
616 |
} |
617 |
} |
618 |
return false; |
619 |
} |
620 |
|
621 |
//---- select / deselect -------------------------------------------------- |
622 |
|
623 |
private void selectAll() { |
624 |
fTableViewer.setAllChecked(true); |
625 |
} |
626 |
|
627 |
private void deselectAll() { |
628 |
fTableViewer.setAllChecked(false); |
629 |
} |
630 |
|
631 |
/** |
632 |
* Returns the list of newly added working sets through this dialog. |
633 |
* |
634 |
* @return the list of newly added working sets |
635 |
* @since 3.5 |
636 |
*/ |
637 |
public List getNewlyAddedWorkingSets() { |
638 |
return fAddedWorkingSets; |
639 |
|
640 |
} |
641 |
|
642 |
/** |
643 |
* Returns whether sorting is enabled for working sets. |
644 |
* |
645 |
* @return <code>true</code> if sorting is enabled, <code>false</code> otherwise |
646 |
* @since 3.5 |
647 |
*/ |
648 |
public boolean isSortingEnabled() { |
649 |
return fIsSortingEnabled; |
650 |
} |
651 |
|
652 |
/** |
653 |
* Returns the working set comparator. |
654 |
* |
655 |
* @return the working set comparator |
656 |
* @since 3.5 |
657 |
*/ |
658 |
private Comparator getComparator() { |
659 |
if (fComparator == null) { |
660 |
fComparator = new WorkingSetComparator(); |
661 |
} |
662 |
return fComparator; |
663 |
} |
664 |
} |