Lines 13-51
Link Here
|
13 |
|
13 |
|
14 |
import java.util.ArrayList; |
14 |
import java.util.ArrayList; |
15 |
import java.util.Arrays; |
15 |
import java.util.Arrays; |
16 |
import java.util.Collection; |
16 |
|
17 |
import java.util.Comparator; |
|
|
18 |
import java.util.HashSet; |
19 |
import java.util.Iterator; |
20 |
import java.util.List; |
21 |
|
22 |
import org.eclipse.core.runtime.Assert; |
23 |
import org.eclipse.jface.dialogs.Dialog; |
24 |
import org.eclipse.jface.dialogs.IDialogConstants; |
25 |
import org.eclipse.jface.dialogs.IDialogSettings; |
17 |
import org.eclipse.jface.dialogs.IDialogSettings; |
26 |
import org.eclipse.jface.resource.JFaceResources; |
18 |
import org.eclipse.jface.viewers.ISelectionChangedListener; |
27 |
import org.eclipse.jface.viewers.IStructuredSelection; |
19 |
import org.eclipse.jface.viewers.IStructuredSelection; |
28 |
import org.eclipse.jface.window.Window; |
20 |
import org.eclipse.jface.viewers.SelectionChangedEvent; |
29 |
import org.eclipse.jface.wizard.IWizardPage; |
21 |
import org.eclipse.jface.wizard.IWizardPage; |
30 |
import org.eclipse.swt.SWT; |
22 |
import org.eclipse.swt.SWT; |
|
|
23 |
import org.eclipse.swt.events.DisposeEvent; |
24 |
import org.eclipse.swt.events.DisposeListener; |
31 |
import org.eclipse.swt.events.SelectionAdapter; |
25 |
import org.eclipse.swt.events.SelectionAdapter; |
32 |
import org.eclipse.swt.events.SelectionEvent; |
26 |
import org.eclipse.swt.events.SelectionEvent; |
33 |
import org.eclipse.swt.graphics.FontMetrics; |
|
|
34 |
import org.eclipse.swt.graphics.GC; |
35 |
import org.eclipse.swt.graphics.Point; |
36 |
import org.eclipse.swt.layout.GridData; |
27 |
import org.eclipse.swt.layout.GridData; |
37 |
import org.eclipse.swt.layout.GridLayout; |
28 |
import org.eclipse.swt.layout.GridLayout; |
38 |
import org.eclipse.swt.widgets.Button; |
29 |
import org.eclipse.swt.widgets.Button; |
39 |
import org.eclipse.swt.widgets.Combo; |
|
|
40 |
import org.eclipse.swt.widgets.Composite; |
30 |
import org.eclipse.swt.widgets.Composite; |
|
|
31 |
import org.eclipse.swt.widgets.Control; |
41 |
import org.eclipse.swt.widgets.Label; |
32 |
import org.eclipse.swt.widgets.Label; |
42 |
import org.eclipse.ui.IWorkingSet; |
33 |
import org.eclipse.ui.IWorkingSet; |
43 |
import org.eclipse.ui.IWorkingSetManager; |
|
|
44 |
import org.eclipse.ui.PlatformUI; |
45 |
import org.eclipse.ui.internal.WorkbenchMessages; |
34 |
import org.eclipse.ui.internal.WorkbenchMessages; |
46 |
import org.eclipse.ui.internal.dialogs.SimpleWorkingSetSelectionDialog; |
|
|
47 |
|
48 |
import com.ibm.icu.text.Collator; |
49 |
|
35 |
|
50 |
/** |
36 |
/** |
51 |
* Instances of this class provide a reusable composite with controls that allow |
37 |
* Instances of this class provide a reusable composite with controls that allow |
Lines 87-115
Link Here
|
87 |
return (IWorkingSet[]) result.toArray(new IWorkingSet[result.size()]); |
73 |
return (IWorkingSet[]) result.toArray(new IWorkingSet[result.size()]); |
88 |
} |
74 |
} |
89 |
|
75 |
|
90 |
/** |
76 |
private SimpleWorkingSetComboBlock comboBlock; |
91 |
* Empty working set array constant. |
|
|
92 |
*/ |
93 |
private static final IWorkingSet[] EMPTY_WORKING_SET_ARRAY = new IWorkingSet[0]; |
94 |
|
95 |
private static final String WORKINGSET_SELECTION_HISTORY = "workingset_selection_history"; //$NON-NLS-1$ |
96 |
private static final int MAX_HISTORY_SIZE = 5; |
97 |
|
98 |
private Label workingSetLabel; |
99 |
private Combo workingSetCombo; |
100 |
private Button selectButton; |
101 |
private Button enableButton; |
102 |
|
77 |
|
103 |
private IWorkingSet[] selectedWorkingSets; |
78 |
private Label workingSetLabel; |
104 |
private ArrayList selectionHistory; |
|
|
105 |
private final IDialogSettings dialogSettings; |
106 |
private final String[] workingSetTypeIds; |
107 |
|
108 |
private final String selectLabel; |
109 |
|
110 |
private final String comboLabel; |
79 |
private final String comboLabel; |
|
|
80 |
|
81 |
private Button enableButton; |
82 |
private String addButtonLabel; |
111 |
|
83 |
|
112 |
private final String addButtonLabel; |
84 |
protected IWorkingSet[] lastSelected; |
|
|
85 |
|
113 |
|
86 |
|
114 |
/** |
87 |
/** |
115 |
* Create a new instance of this working set block using default labels. |
88 |
* Create a new instance of this working set block using default labels. |
Lines 143-166
Link Here
|
143 |
*/ |
116 |
*/ |
144 |
public WorkingSetConfigurationBlock(String[] workingSetIds, |
117 |
public WorkingSetConfigurationBlock(String[] workingSetIds, |
145 |
IDialogSettings settings, String addButtonLabel, String comboLabel, String selectLabel) { |
118 |
IDialogSettings settings, String addButtonLabel, String comboLabel, String selectLabel) { |
146 |
Assert.isNotNull(workingSetIds); |
119 |
this(workingSetIds, settings, addButtonLabel, comboLabel, selectLabel, false); |
147 |
Assert.isNotNull(settings); |
120 |
} |
148 |
|
121 |
|
149 |
workingSetTypeIds = workingSetIds; |
122 |
/** |
150 |
Arrays.sort(workingSetIds); // we'll be performing some searches with these later - presort them |
123 |
* Create a new instance of this working set block using custom labels. |
151 |
selectedWorkingSets = EMPTY_WORKING_SET_ARRAY; |
124 |
* |
152 |
dialogSettings = settings; |
125 |
* @param workingSetIds |
153 |
selectionHistory = loadSelectionHistory(settings, workingSetIds); |
126 |
* working set ids from which the user can choose |
154 |
|
127 |
* @param settings |
|
|
128 |
* to store/load the selection history |
129 |
* @param addButtonLabel |
130 |
* the label to use for the checkable enablement button. May be |
131 |
* <code>null</code> to use the default value. |
132 |
* @param comboLabel |
133 |
* the label to use for the recent working set combo. May be |
134 |
* <code>null</code> to use the default value. |
135 |
* @param selectLabel |
136 |
* the label to use for the select button. May be |
137 |
* <code>null</code> to use the default value. |
138 |
* @param editMode |
139 |
* decides if the working set selection dialog should |
140 |
* have edit controls |
141 |
*/ |
142 |
public WorkingSetConfigurationBlock(String[] workingSetIds, |
143 |
IDialogSettings settings, String addButtonLabel, String comboLabel, String selectLabel,boolean editMode) { |
144 |
comboBlock=new SimpleWorkingSetComboBlock(workingSetIds,settings,selectLabel,editMode); |
155 |
this.addButtonLabel = addButtonLabel == null ? WorkbenchMessages.WorkingSetGroup_EnableWorkingSet_button |
145 |
this.addButtonLabel = addButtonLabel == null ? WorkbenchMessages.WorkingSetGroup_EnableWorkingSet_button |
156 |
: addButtonLabel; |
146 |
: addButtonLabel; |
157 |
this.comboLabel = comboLabel == null ? WorkbenchMessages.WorkingSetConfigurationBlock_WorkingSetText_name |
147 |
this.comboLabel = comboLabel == null ? WorkbenchMessages.WorkingSetConfigurationBlock_WorkingSetText_name |
158 |
: comboLabel; |
148 |
: comboLabel; |
159 |
this.selectLabel = selectLabel == null ? WorkbenchMessages.WorkingSetConfigurationBlock_SelectWorkingSet_button |
|
|
160 |
: selectLabel; |
161 |
} |
149 |
} |
162 |
|
|
|
163 |
|
164 |
/** |
150 |
/** |
165 |
* Set the current selection in the workbench. |
151 |
* Set the current selection in the workbench. |
166 |
* |
152 |
* |
Lines 171-180
Link Here
|
171 |
* instead. This method will be removed before 3.4 ships. |
157 |
* instead. This method will be removed before 3.4 ships. |
172 |
*/ |
158 |
*/ |
173 |
public void setSelection(IStructuredSelection selection) { |
159 |
public void setSelection(IStructuredSelection selection) { |
174 |
selectedWorkingSets = findApplicableWorkingSets(selection); |
160 |
comboBlock.setSelection(selection); |
175 |
|
|
|
176 |
if (workingSetCombo != null) |
177 |
updateSelectedWorkingSets(); |
178 |
} |
161 |
} |
179 |
|
162 |
|
180 |
/** |
163 |
/** |
Lines 185-193
Link Here
|
185 |
* the working sets |
168 |
* the working sets |
186 |
*/ |
169 |
*/ |
187 |
public void setWorkingSets(IWorkingSet[] workingSets) { |
170 |
public void setWorkingSets(IWorkingSet[] workingSets) { |
188 |
selectedWorkingSets = filterWorkingSets(Arrays.asList(workingSets)); |
171 |
comboBlock.setSelectedWorkingSets(workingSets); |
189 |
if (workingSetCombo != null) |
|
|
190 |
updateSelectedWorkingSets(); |
191 |
} |
172 |
} |
192 |
|
173 |
|
193 |
/** |
174 |
/** |
Lines 201-247
Link Here
|
201 |
*/ |
182 |
*/ |
202 |
public IWorkingSet[] findApplicableWorkingSets( |
183 |
public IWorkingSet[] findApplicableWorkingSets( |
203 |
IStructuredSelection selection) { |
184 |
IStructuredSelection selection) { |
204 |
if (selection == null) |
185 |
return comboBlock.findApplicableWorkingSets(selection); |
205 |
return EMPTY_WORKING_SET_ARRAY; |
|
|
206 |
|
207 |
return filterWorkingSets(selection.toList()); |
208 |
} |
186 |
} |
209 |
|
187 |
|
210 |
/** |
188 |
/** |
211 |
* Prune a list of working sets such that they all match the criteria set |
|
|
212 |
* out by this block. |
213 |
* |
214 |
* @param elements |
215 |
* the elements to filter |
216 |
* @return the filtered elements |
217 |
*/ |
218 |
private IWorkingSet[] filterWorkingSets(Collection elements) { |
219 |
ArrayList result = new ArrayList(); |
220 |
for (Iterator iterator = elements.iterator(); iterator.hasNext();) { |
221 |
Object element = iterator.next(); |
222 |
if (element instanceof IWorkingSet |
223 |
&& verifyWorkingSet((IWorkingSet) element)) { |
224 |
result.add(element); |
225 |
} |
226 |
} |
227 |
return (IWorkingSet[]) result.toArray(new IWorkingSet[result.size()]); |
228 |
} |
229 |
|
230 |
/** |
231 |
* Verifies that the given working set is suitable for selection in this |
232 |
* block. |
233 |
* |
234 |
* @param workingSetCandidate |
235 |
* the candidate to test |
236 |
* @return whether it is suitable |
237 |
*/ |
238 |
private boolean verifyWorkingSet(IWorkingSet workingSetCandidate) { |
239 |
return !workingSetCandidate.isAggregateWorkingSet() |
240 |
&& Arrays.binarySearch(workingSetTypeIds, workingSetCandidate |
241 |
.getId()) >= 0 ; |
242 |
} |
243 |
|
244 |
/** |
245 |
* Return the currently selected working sets. If the controls representing |
189 |
* Return the currently selected working sets. If the controls representing |
246 |
* this block are disabled this array will be empty regardless of the |
190 |
* this block are disabled this array will be empty regardless of the |
247 |
* currently selected values. |
191 |
* currently selected values. |
Lines 250-258
Link Here
|
250 |
*/ |
194 |
*/ |
251 |
public IWorkingSet[] getSelectedWorkingSets() { |
195 |
public IWorkingSet[] getSelectedWorkingSets() { |
252 |
if (enableButton.getSelection()) { |
196 |
if (enableButton.getSelection()) { |
253 |
return selectedWorkingSets; |
197 |
return comboBlock.getSelectedWorkingSets(); |
254 |
} |
198 |
} |
255 |
return EMPTY_WORKING_SET_ARRAY; |
199 |
return SimpleWorkingSetComboBlock.EMPTY_WORKING_SET_ARRAY; |
256 |
} |
200 |
} |
257 |
|
201 |
|
258 |
/** |
202 |
/** |
Lines 273-317
Link Here
|
273 |
GridData enableData = new GridData(SWT.FILL, SWT.CENTER, true, false); |
217 |
GridData enableData = new GridData(SWT.FILL, SWT.CENTER, true, false); |
274 |
enableData.horizontalSpan = numColumn; |
218 |
enableData.horizontalSpan = numColumn; |
275 |
enableButton.setLayoutData(enableData); |
219 |
enableButton.setLayoutData(enableData); |
276 |
enableButton.setSelection(selectedWorkingSets.length > 0); |
220 |
enableButton.setSelection(comboBlock.getSelectedWorkingSets().length > 0); |
277 |
|
221 |
|
278 |
workingSetLabel = new Label(composite, SWT.NONE); |
222 |
workingSetLabel = new Label(composite, SWT.NONE); |
279 |
workingSetLabel |
223 |
workingSetLabel |
280 |
.setText(comboLabel); |
224 |
.setText(comboLabel); |
281 |
|
225 |
|
282 |
workingSetCombo = new Combo(composite, SWT.READ_ONLY | SWT.BORDER); |
226 |
Control control=comboBlock.createContent(composite); |
283 |
GridData textData = new GridData(SWT.FILL, SWT.CENTER, true, false); |
227 |
GridData comboData = new GridData(SWT.FILL, SWT.CENTER, true, false); |
284 |
textData.horizontalSpan = numColumn - 2; |
228 |
comboData.horizontalSpan = numColumn - 1; |
285 |
textData.horizontalIndent = 0; |
229 |
comboData.horizontalIndent = 0; |
286 |
workingSetCombo.setLayoutData(textData); |
230 |
control.setLayoutData(comboData); |
287 |
|
231 |
|
288 |
selectButton = new Button(composite, SWT.PUSH); |
232 |
final ISelectionChangedListener enablement=new ISelectionChangedListener(){ |
289 |
selectButton |
233 |
public void selectionChanged(SelectionChangedEvent event) { |
290 |
.setText(selectLabel); |
234 |
IWorkingSet[] sets=comboBlock.getSelectedWorkingSets(); |
291 |
setButtonLayoutData(selectButton); |
235 |
boolean enabled=sets.length>0; |
292 |
selectButton.addSelectionListener(new SelectionAdapter() { |
236 |
if(enabled) |
293 |
|
237 |
lastSelected=sets; |
294 |
public void widgetSelected(SelectionEvent e) { |
238 |
else{ |
295 |
SimpleWorkingSetSelectionDialog dialog = new SimpleWorkingSetSelectionDialog( |
239 |
comboBlock.setWorkingSets(lastSelected);// |
296 |
parent.getShell(), workingSetTypeIds, |
|
|
297 |
selectedWorkingSets, false); |
298 |
dialog |
299 |
.setMessage(WorkbenchMessages.WorkingSetGroup_WorkingSetSelection_message); |
300 |
|
301 |
if (dialog.open() == Window.OK) { |
302 |
IWorkingSet[] result = dialog.getSelection(); |
303 |
if (result != null && result.length > 0) { |
304 |
selectedWorkingSets = result; |
305 |
PlatformUI.getWorkbench().getWorkingSetManager() |
306 |
.addRecentWorkingSet(result[0]); |
307 |
} else { |
308 |
selectedWorkingSets = EMPTY_WORKING_SET_ARRAY; |
309 |
} |
310 |
updateWorkingSetSelection(); |
311 |
} |
240 |
} |
|
|
241 |
updateEnableState(enabled); |
312 |
} |
242 |
} |
313 |
}); |
243 |
}; |
314 |
|
244 |
|
|
|
245 |
comboBlock.addSelectionChangedListener(enablement); |
246 |
composite.addDisposeListener(new DisposeListener(){ |
247 |
public void widgetDisposed(DisposeEvent e) { |
248 |
comboBlock.removeSelectionChangedListener(enablement); |
249 |
} |
250 |
}); |
251 |
|
252 |
lastSelected=comboBlock.getSelectedWorkingSets(); |
253 |
|
315 |
enableButton.addSelectionListener(new SelectionAdapter() { |
254 |
enableButton.addSelectionListener(new SelectionAdapter() { |
316 |
public void widgetSelected(SelectionEvent e) { |
255 |
public void widgetSelected(SelectionEvent e) { |
317 |
updateEnableState(enableButton.getSelection()); |
256 |
updateEnableState(enableButton.getSelection()); |
Lines 319-478
Link Here
|
319 |
}); |
258 |
}); |
320 |
updateEnableState(enableButton.getSelection()); |
259 |
updateEnableState(enableButton.getSelection()); |
321 |
|
260 |
|
322 |
workingSetCombo.addSelectionListener(new SelectionAdapter() { |
|
|
323 |
public void widgetSelected(SelectionEvent e) { |
324 |
updateSelectedWorkingSets(); |
325 |
} |
326 |
}); |
327 |
|
328 |
workingSetCombo.setItems(getHistoryEntries()); |
329 |
if (selectedWorkingSets.length == 0 && selectionHistory.size() > 0) { |
330 |
workingSetCombo.select(historyIndex((String) selectionHistory |
331 |
.get(0))); |
332 |
updateSelectedWorkingSets(); |
333 |
} else { |
334 |
updateWorkingSetSelection(); |
335 |
} |
336 |
} |
261 |
} |
337 |
|
262 |
|
338 |
private void updateEnableState(boolean enabled) { |
263 |
private void updateEnableState(boolean enabled) { |
|
|
264 |
enableButton.setSelection(enabled); |
339 |
workingSetLabel.setEnabled(enabled); |
265 |
workingSetLabel.setEnabled(enabled); |
340 |
workingSetCombo |
266 |
comboBlock.setEnabled(enabled); |
341 |
.setEnabled(enabled |
|
|
342 |
&& (selectedWorkingSets.length > 0 || getHistoryEntries().length > 0)); |
343 |
selectButton.setEnabled(enabled); |
344 |
} |
345 |
|
346 |
private void updateWorkingSetSelection() { |
347 |
if (selectedWorkingSets.length > 0) { |
348 |
workingSetCombo.setEnabled(true); |
349 |
StringBuffer buf = new StringBuffer(); |
350 |
|
351 |
buf.append(selectedWorkingSets[0].getLabel()); |
352 |
for (int i = 1; i < selectedWorkingSets.length; i++) { |
353 |
IWorkingSet ws = selectedWorkingSets[i]; |
354 |
buf.append(',').append(' '); |
355 |
buf.append(ws.getLabel()); |
356 |
} |
357 |
|
358 |
String currentSelection = buf.toString(); |
359 |
int index = historyIndex(currentSelection); |
360 |
historyInsert(currentSelection); |
361 |
if (index >= 0) { |
362 |
workingSetCombo.select(index); |
363 |
} else { |
364 |
workingSetCombo.setItems(getHistoryEntries()); |
365 |
workingSetCombo.select(historyIndex(currentSelection)); |
366 |
} |
367 |
} else { |
368 |
enableButton.setSelection(false); |
369 |
updateEnableState(false); |
370 |
} |
371 |
} |
372 |
|
373 |
private String[] getHistoryEntries() { |
374 |
String[] history = (String[]) selectionHistory |
375 |
.toArray(new String[selectionHistory.size()]); |
376 |
Arrays.sort(history, new Comparator() { |
377 |
public int compare(Object o1, Object o2) { |
378 |
return Collator.getInstance().compare(o1, o2); |
379 |
} |
380 |
}); |
381 |
return history; |
382 |
} |
383 |
|
384 |
private void historyInsert(String entry) { |
385 |
selectionHistory.remove(entry); |
386 |
selectionHistory.add(0, entry); |
387 |
storeSelectionHistory(dialogSettings); |
388 |
} |
267 |
} |
389 |
|
|
|
390 |
private int historyIndex(String entry) { |
391 |
for (int i = 0; i < workingSetCombo.getItemCount(); i++) { |
392 |
if (workingSetCombo.getItem(i).equals(entry)) |
393 |
return i; |
394 |
} |
395 |
|
396 |
return -1; |
397 |
} |
398 |
|
399 |
private void updateSelectedWorkingSets() { |
400 |
String item = workingSetCombo.getItem(workingSetCombo |
401 |
.getSelectionIndex()); |
402 |
String[] workingSetNames = item.split(", "); //$NON-NLS-1$ |
403 |
|
404 |
IWorkingSetManager workingSetManager = PlatformUI.getWorkbench() |
405 |
.getWorkingSetManager(); |
406 |
selectedWorkingSets = new IWorkingSet[workingSetNames.length]; |
407 |
for (int i = 0; i < workingSetNames.length; i++) { |
408 |
IWorkingSet set = workingSetManager |
409 |
.getWorkingSet(workingSetNames[i]); |
410 |
Assert.isNotNull(set); |
411 |
selectedWorkingSets[i] = set; |
412 |
} |
413 |
} |
414 |
|
415 |
private void storeSelectionHistory(IDialogSettings settings) { |
416 |
String[] history; |
417 |
if (selectionHistory.size() > MAX_HISTORY_SIZE) { |
418 |
List subList = selectionHistory.subList(0, MAX_HISTORY_SIZE); |
419 |
history = (String[]) subList.toArray(new String[subList.size()]); |
420 |
} else { |
421 |
history = (String[]) selectionHistory |
422 |
.toArray(new String[selectionHistory.size()]); |
423 |
} |
424 |
settings.put(WORKINGSET_SELECTION_HISTORY, history); |
425 |
} |
426 |
|
427 |
private ArrayList loadSelectionHistory(IDialogSettings settings, |
428 |
String[] workingSetIds) { |
429 |
String[] strings = settings.getArray(WORKINGSET_SELECTION_HISTORY); |
430 |
if (strings == null || strings.length == 0) |
431 |
return new ArrayList(); |
432 |
|
433 |
ArrayList result = new ArrayList(); |
434 |
|
435 |
HashSet workingSetIdsSet = new HashSet(Arrays.asList(workingSetIds)); |
436 |
|
437 |
IWorkingSetManager workingSetManager = PlatformUI.getWorkbench() |
438 |
.getWorkingSetManager(); |
439 |
for (int i = 0; i < strings.length; i++) { |
440 |
String[] workingSetNames = strings[i].split(", "); //$NON-NLS-1$ |
441 |
boolean valid = true; |
442 |
for (int j = 0; j < workingSetNames.length && valid; j++) { |
443 |
IWorkingSet workingSet = workingSetManager |
444 |
.getWorkingSet(workingSetNames[j]); |
445 |
if (workingSet == null) { |
446 |
valid = false; |
447 |
} else { |
448 |
if (!workingSetIdsSet.contains(workingSet.getId())) |
449 |
valid = false; |
450 |
} |
451 |
} |
452 |
if (valid) { |
453 |
result.add(strings[i]); |
454 |
} |
455 |
} |
456 |
|
457 |
return result; |
458 |
} |
459 |
|
460 |
/* |
461 |
* Copy from DialogPage with changes to accomodate the lack of a Dialog context. |
462 |
*/ |
463 |
private GridData setButtonLayoutData(Button button) { |
464 |
button.setFont(JFaceResources.getDialogFont()); |
465 |
|
466 |
GC gc = new GC(button); |
467 |
gc.setFont(button.getFont()); |
468 |
FontMetrics fontMetrics = gc.getFontMetrics(); |
469 |
gc.dispose(); |
470 |
|
471 |
GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); |
472 |
int widthHint = Dialog.convertHorizontalDLUsToPixels(fontMetrics, IDialogConstants.BUTTON_WIDTH); |
473 |
Point minSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); |
474 |
data.widthHint = Math.max(widthHint, minSize.x); |
475 |
button.setLayoutData(data); |
476 |
return data; |
477 |
} |
478 |
} |
268 |
} |