View | Details | Raw Unified | Return to bug 245258 | Differences between
and this patch

Collapse All | Expand All

(-)search/org/eclipse/search/internal/ui/ScopePart.java (-140 / +162 lines)
Lines 11-48 Link Here
11
package org.eclipse.search.internal.ui;
11
package org.eclipse.search.internal.ui;
12
12
13
import java.util.Arrays;
13
import java.util.Arrays;
14
import java.util.HashSet;
15
import java.util.Set;
16
14
17
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.accessibility.AccessibleAdapter;
19
import org.eclipse.swt.accessibility.AccessibleEvent;
20
import org.eclipse.swt.events.SelectionAdapter;
16
import org.eclipse.swt.events.SelectionAdapter;
21
import org.eclipse.swt.events.SelectionEvent;
17
import org.eclipse.swt.events.SelectionEvent;
18
import org.eclipse.swt.events.TraverseEvent;
19
import org.eclipse.swt.events.TraverseListener;
22
import org.eclipse.swt.layout.GridData;
20
import org.eclipse.swt.layout.GridData;
23
import org.eclipse.swt.layout.GridLayout;
21
import org.eclipse.swt.layout.GridLayout;
24
import org.eclipse.swt.widgets.Button;
22
import org.eclipse.swt.widgets.Button;
25
import org.eclipse.swt.widgets.Composite;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.swt.widgets.Control;
26
import org.eclipse.swt.widgets.Group;
25
import org.eclipse.swt.widgets.Group;
27
import org.eclipse.swt.widgets.Text;
28
26
29
import org.eclipse.core.runtime.Assert;
27
import org.eclipse.core.runtime.Assert;
30
import org.eclipse.core.runtime.IStatus;
28
import org.eclipse.core.runtime.IStatus;
31
import org.eclipse.core.runtime.Status;
29
import org.eclipse.core.runtime.Status;
32
30
33
import org.eclipse.jface.dialogs.IDialogSettings;
31
import org.eclipse.jface.dialogs.IDialogSettings;
32
import org.eclipse.jface.layout.GridLayoutFactory;
34
import org.eclipse.jface.viewers.ISelection;
33
import org.eclipse.jface.viewers.ISelection;
34
import org.eclipse.jface.viewers.ISelectionChangedListener;
35
import org.eclipse.jface.viewers.IStructuredSelection;
35
import org.eclipse.jface.viewers.IStructuredSelection;
36
import org.eclipse.jface.window.Window;
36
import org.eclipse.jface.viewers.SelectionChangedEvent;
37
37
38
import org.eclipse.ui.IWorkbenchPage;
39
import org.eclipse.ui.IWorkingSet;
38
import org.eclipse.ui.IWorkingSet;
40
import org.eclipse.ui.IWorkingSetManager;
39
import org.eclipse.ui.dialogs.WorkingSetComboBlock;
41
import org.eclipse.ui.PlatformUI;
42
import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
43
40
44
import org.eclipse.search.internal.ui.util.PixelConverter;
41
import org.eclipse.search.internal.ui.util.PixelConverter;
45
import org.eclipse.search.internal.ui.util.SWTUtil;
46
import org.eclipse.search.ui.ISearchPageContainer;
42
import org.eclipse.search.ui.ISearchPageContainer;
47
import org.eclipse.search.ui.NewSearchUI;
43
import org.eclipse.search.ui.NewSearchUI;
48
44
Lines 65-72 Link Here
65
61
66
	private int fScope;
62
	private int fScope;
67
	private boolean fCanSearchEnclosingProjects;
63
	private boolean fCanSearchEnclosingProjects;
68
	private Text fWorkingSetText;
64
	private WorkingSetComboBlock fWorkingSetComboBlock;
69
	private IWorkingSet[] fWorkingSets;
65
//	private IWorkingSet[] fWorkingSets;
70
66
71
	// Reference to its search page container (can be null)
67
	// Reference to its search page container (can be null)
72
	private SearchDialog fSearchDialog;
68
	private SearchDialog fSearchDialog;
Lines 84-90 Link Here
84
		fSettingsStore= SearchPlugin.getDefault().getDialogSettingsSection(DIALOG_SETTINGS_KEY);
80
		fSettingsStore= SearchPlugin.getDefault().getDialogSettingsSection(DIALOG_SETTINGS_KEY);
85
		fScope= getStoredScope(fSettingsStore, searchEnclosingProjects);
81
		fScope= getStoredScope(fSettingsStore, searchEnclosingProjects);
86
82
87
		fWorkingSets= getStoredWorkingSets();
83
		fWorkingSetComboBlock= new WorkingSetComboBlock(null, fSettingsStore, SearchMessages.ScopePart_workingSetChooseButton_text);
84
		//TODO: first time init from legacy storage
85
86
//		fWorkingSetConfigurationBlock.setWorkingSets(getStoredWorkingSets());
88
	}
87
	}
89
88
90
	private static int getStoredScope(IDialogSettings settingsStore, boolean canSearchEnclosingProjects) {
89
	private static int getStoredScope(IDialogSettings settingsStore, boolean canSearchEnclosingProjects) {
Lines 106-151 Link Here
106
		return scope;
105
		return scope;
107
	}
106
	}
108
107
109
	private IWorkingSet getWorkingSet(IWorkingSetManager workingSetManager, String storedName) {
108
//	private IWorkingSet getWorkingSet(IWorkingSetManager workingSetManager, String storedName) {
110
		if (storedName.length() == 0) {
109
//		if (storedName.length() == 0) {
111
			IWorkbenchPage page= fSearchDialog.getWorkbenchWindow().getActivePage();
110
//			IWorkbenchPage page= fSearchDialog.getWorkbenchWindow().getActivePage();
112
			if (page != null) {
111
//			if (page != null) {
113
				return page.getAggregateWorkingSet();
112
//				return page.getAggregateWorkingSet();
114
			}
113
//			}
115
			return null;
114
//			return null;
116
		}
115
//		}
117
		return workingSetManager.getWorkingSet(storedName);
116
//		return workingSetManager.getWorkingSet(storedName);
118
	}
117
//	}
119
118
//
120
119
//
121
	private IWorkingSet[] getStoredWorkingSets() {
120
//	private IWorkingSet[] getStoredWorkingSets() {
122
		String[] lruWorkingSetNames= fSettingsStore.getArray(STORE_LRU_WORKING_SET_NAMES);
121
//		String[] lruWorkingSetNames= fSettingsStore.getArray(STORE_LRU_WORKING_SET_NAMES);
123
122
//
124
		IWorkingSetManager workingSetManager= PlatformUI.getWorkbench().getWorkingSetManager();
123
//		IWorkingSetManager workingSetManager= PlatformUI.getWorkbench().getWorkingSetManager();
125
		if (lruWorkingSetNames != null) {
124
//		if (lruWorkingSetNames != null) {
126
			Set existingWorkingSets= new HashSet(lruWorkingSetNames.length);
125
//			Set existingWorkingSets= new HashSet(lruWorkingSetNames.length);
127
			for (int i= 0; i < lruWorkingSetNames.length; i++) {
126
//			for (int i= 0; i < lruWorkingSetNames.length; i++) {
128
				IWorkingSet workingSet= getWorkingSet(workingSetManager, lruWorkingSetNames[i]);
127
//				IWorkingSet workingSet= getWorkingSet(workingSetManager, lruWorkingSetNames[i]);
129
				if (workingSet != null) {
128
//				if (workingSet != null) {
130
					existingWorkingSets.add(workingSet);
129
//					existingWorkingSets.add(workingSet);
131
				}
130
//				}
132
			}
131
//			}
133
			if (!existingWorkingSets.isEmpty()) {
132
//			if (!existingWorkingSets.isEmpty()) {
134
				return (IWorkingSet[]) existingWorkingSets.toArray(new IWorkingSet[existingWorkingSets.size()]);
133
//				return (IWorkingSet[]) existingWorkingSets.toArray(new IWorkingSet[existingWorkingSets.size()]);
135
			}
134
//			}
136
		} else {
135
//		} else {
137
			// Backward compatibility
136
//			// Backward compatibility
138
			String workingSetName= fSettingsStore.get(STORE_LRU_WORKING_SET_NAME);
137
//			String workingSetName= fSettingsStore.get(STORE_LRU_WORKING_SET_NAME);
139
			if (workingSetName != null) {
138
//			if (workingSetName != null) {
140
				IWorkingSet workingSet= getWorkingSet(workingSetManager, workingSetName);
139
//				IWorkingSet workingSet= getWorkingSet(workingSetManager, workingSetName);
141
				if (workingSet != null) {
140
//				if (workingSet != null) {
142
					return new IWorkingSet[] { workingSet };
141
//					return new IWorkingSet[] { workingSet };
143
				}
142
//				}
144
			}
143
//			}
145
		}
144
//		}
146
		return null;
145
//		return null;
147
	}
146
//	}
148
147
//
149
148
150
	/**
149
	/**
151
	 * Returns the scope selected in this part
150
	 * Returns the scope selected in this part
Lines 189-195 Link Here
189
		fUseSelection.setSelection(scope == ISearchPageContainer.SELECTION_SCOPE);
188
		fUseSelection.setSelection(scope == ISearchPageContainer.SELECTION_SCOPE);
190
		fUseProject.setSelection(scope == ISearchPageContainer.SELECTED_PROJECTS_SCOPE);
189
		fUseProject.setSelection(scope == ISearchPageContainer.SELECTED_PROJECTS_SCOPE);
191
		fUseWorkingSet.setSelection(scope == ISearchPageContainer.WORKING_SET_SCOPE);
190
		fUseWorkingSet.setSelection(scope == ISearchPageContainer.WORKING_SET_SCOPE);
192
191
	
192
		
193
		updateSearchPageContainerActionPerformedEnablement();
193
		updateSearchPageContainerActionPerformedEnablement();
194
194
195
	}
195
	}
Lines 207-213 Link Here
207
	 */
207
	 */
208
	public IWorkingSet[] getSelectedWorkingSets() {
208
	public IWorkingSet[] getSelectedWorkingSets() {
209
		if (getSelectedScope() == ISearchPageContainer.WORKING_SET_SCOPE)
209
		if (getSelectedScope() == ISearchPageContainer.WORKING_SET_SCOPE)
210
			return fWorkingSets;
210
			return fWorkingSetComboBlock.getSelectedWorkingSets();
211
		return null;
211
		return null;
212
	}
212
	}
213
213
Lines 218-257 Link Here
218
	 * @param workingSets the working set to be selected
218
	 * @param workingSets the working set to be selected
219
	 */
219
	 */
220
	public void setSelectedWorkingSets(IWorkingSet[] workingSets) {
220
	public void setSelectedWorkingSets(IWorkingSet[] workingSets) {
221
		Assert.isNotNull(workingSets);
221
//		Assert.isNotNull(workingSets);
222
		setSelectedScope(ISearchPageContainer.WORKING_SET_SCOPE);
222
//		setSelectedScope(ISearchPageContainer.WORKING_SET_SCOPE);
223
		fWorkingSets= null;
223
//		fWorkingSets= null;
224
		Set existingWorkingSets= new HashSet(workingSets.length);
224
//		Set existingWorkingSets= new HashSet(workingSets.length);
225
		for (int i= 0; i < workingSets.length; i++) {
225
//		for (int i= 0; i < workingSets.length; i++) {
226
			String name= workingSets[i].getName();
226
//			String name= workingSets[i].getName();
227
			IWorkingSet workingSet= PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(name);
227
//			IWorkingSet workingSet= PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(name);
228
			if (workingSet != null)
228
//			if (workingSet != null)
229
				existingWorkingSets.add(workingSet);
229
//				existingWorkingSets.add(workingSet);
230
		}
230
//		}
231
		if (!existingWorkingSets.isEmpty())
231
//		if (!existingWorkingSets.isEmpty())
232
			fWorkingSets= (IWorkingSet[]) existingWorkingSets.toArray(new IWorkingSet[existingWorkingSets.size()]);
232
//			fWorkingSets= (IWorkingSet[]) existingWorkingSets.toArray(new IWorkingSet[existingWorkingSets.size()]);
233
233
//
234
		saveState();
234
//		saveState();
235
235
236
		if (fWorkingSetText != null)
236
//		setSelectedScope(ISearchPageContainer.WORKING_SET_SCOPE);??
237
			fWorkingSetText.setText(toString(fWorkingSets));
237
		fWorkingSetComboBlock.setSelectedWorkingSets(workingSets);
238
	}
238
	}
239
239
//
240
	/**
240
//	/**
241
	 * Saves the last recently used working sets,
241
//	 * Saves the last recently used working sets,
242
	 * if any.
242
//	 * if any.
243
	 */
243
//	 */
244
	private void saveState() {
244
//	private void saveState() {
245
		if (fWorkingSets != null && fWorkingSets.length > 0) {
245
//		if (fWorkingSets != null && fWorkingSets.length > 0) {
246
			String[] existingWorkingSetNames= new String[fWorkingSets.length];
246
//			String[] existingWorkingSetNames= new String[fWorkingSets.length];
247
			for (int i= 0; i < fWorkingSets.length; i++) {
247
//			for (int i= 0; i < fWorkingSets.length; i++) {
248
				IWorkingSet curr= fWorkingSets[i];
248
//				IWorkingSet curr= fWorkingSets[i];
249
				// use empty name for aggregateWS
249
//				// use empty name for aggregateWS
250
				existingWorkingSetNames[i]= curr.isAggregateWorkingSet() ? "" : curr.getName(); //$NON-NLS-1$
250
//				existingWorkingSetNames[i]= curr.isAggregateWorkingSet() ? "" : curr.getName(); //$NON-NLS-1$
251
			}
251
//			}
252
			fSettingsStore.put(STORE_LRU_WORKING_SET_NAMES, existingWorkingSetNames);
252
//			fSettingsStore.put(STORE_LRU_WORKING_SET_NAMES, existingWorkingSetNames);
253
		}
253
//		}
254
	}
254
//	}
255
255
256
	/**
256
	/**
257
	 * Creates this scope part.
257
	 * Creates this scope part.
Lines 298-326 Link Here
298
		fUseWorkingSet= new Button(fPart, SWT.RADIO);
298
		fUseWorkingSet= new Button(fPart, SWT.RADIO);
299
		fUseWorkingSet.setData(new Integer(ISearchPageContainer.WORKING_SET_SCOPE));
299
		fUseWorkingSet.setData(new Integer(ISearchPageContainer.WORKING_SET_SCOPE));
300
		fUseWorkingSet.setText(SearchMessages.ScopePart_workingSetScope_text);
300
		fUseWorkingSet.setText(SearchMessages.ScopePart_workingSetScope_text);
301
		fWorkingSetText= new Text(fPart, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY);
301
302
		fWorkingSetText.getAccessible().addAccessibleListener(new AccessibleAdapter() {
302
		Composite workingSetComposite = new Composite(fPart, SWT.NONE);
303
		GridLayoutFactory.fillDefaults().applyTo(workingSetComposite);
304
		final Control workingSetControl= fWorkingSetComboBlock.createContent(workingSetComposite);
305
		workingSetControl.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
306
307
		/*fWorkingSetComboBlock.getWorkingSetControl().getAccessible().addAccessibleListener(new AccessibleAdapter() {
303
			public void getName(AccessibleEvent e) {
308
			public void getName(AccessibleEvent e) {
304
				e.result= SearchMessages.ScopePart_workingSetText_accessible_label;
309
				e.result= SearchMessages.ScopePart_workingSetText_accessible_label;
305
			}
310
			}
311
		});*/
312
		fUseWorkingSet.addTraverseListener(new TraverseListener() {
313
			public void keyTraversed(TraverseEvent e) {
314
				if (e.detail == SWT.TRAVERSE_MNEMONIC) {
315
					workingSetControl.setFocus();
316
				}
317
			}
306
		});
318
		});
307
319
308
		Button chooseWorkingSet= new Button(fPart, SWT.PUSH);
320
		fWorkingSetComboBlock.addSelectionChangedListener(new ISelectionChangedListener() {
309
		chooseWorkingSet.setLayoutData(new GridData());
321
			public void selectionChanged(SelectionChangedEvent event) {
310
		chooseWorkingSet.setText(SearchMessages.ScopePart_workingSetChooseButton_text);
322
				if (fWorkingSetComboBlock.getSelectedWorkingSets().length > 0) {
311
		SWTUtil.setButtonDimensionHint(chooseWorkingSet);
312
		chooseWorkingSet.addSelectionListener(new SelectionAdapter() {
313
			public void widgetSelected(SelectionEvent e) {
314
				if (handleChooseWorkingSet()) {
315
					setSelectedScope(ISearchPageContainer.WORKING_SET_SCOPE);
323
					setSelectedScope(ISearchPageContainer.WORKING_SET_SCOPE);
324
				} else {
325
					setSelectedScope(ISearchPageContainer.WORKSPACE_SCOPE);
316
				}
326
				}
327
				updateSearchPageContainerActionPerformedEnablement();
317
			}
328
			}
318
		});
329
		});
330
331
//		Button chooseWorkingSet= new Button(fPart, SWT.PUSH);
332
//		chooseWorkingSet.setLayoutData(new GridData());
333
//		chooseWorkingSet.setText(SearchMessages.ScopePart_workingSetChooseButton_text);
334
//		SWTUtil.setButtonDimensionHint(chooseWorkingSet);
335
//		chooseWorkingSet.addSelectionListener(new SelectionAdapter() {
336
//			public void widgetSelected(SelectionEvent e) {
337
//				if (handleChooseWorkingSet()) {
338
//					setSelectedScope(ISearchPageContainer.WORKING_SET_SCOPE);
339
//				}
340
//			}
341
//		});
319
		gd= new GridData(GridData.FILL_HORIZONTAL);
342
		gd= new GridData(GridData.FILL_HORIZONTAL);
320
		gd.horizontalIndent= 8;
343
		gd.horizontalIndent= 8;
321
		gd.horizontalSpan= 2;
344
		gd.horizontalSpan= 3;
322
		gd.widthHint= new PixelConverter(fWorkingSetText).convertWidthInCharsToPixels(30);
345
		gd.widthHint= new PixelConverter(fPart).convertWidthInCharsToPixels(30);
323
		fWorkingSetText.setLayoutData(gd);
346
		workingSetComposite.setLayoutData(gd);
324
347
325
		// Add scope change listeners
348
		// Add scope change listeners
326
		SelectionAdapter scopeChangedLister= new SelectionAdapter() {
349
		SelectionAdapter scopeChangedLister= new SelectionAdapter() {
Lines 336-344 Link Here
336
		// Set initial scope
359
		// Set initial scope
337
		setSelectedScope(fScope);
360
		setSelectedScope(fScope);
338
361
339
		// Set initial working set
362
//		// Set initial working set
340
		if (fWorkingSets != null)
363
//		fWorkingSetConfigurationBlock.setWorkingSets(fWorkingSets);
341
			fWorkingSetText.setText(toString(fWorkingSets));
342
364
343
		return fPart;
365
		return fPart;
344
	}
366
	}
Lines 357-397 Link Here
357
		}
379
		}
358
	}
380
	}
359
381
360
	private boolean handleChooseWorkingSet() {
382
//	private boolean handleChooseWorkingSet() {
361
		IWorkingSetManager workingSetManager= PlatformUI.getWorkbench().getWorkingSetManager();
383
//		IWorkingSetManager workingSetManager= PlatformUI.getWorkbench().getWorkingSetManager();
362
		IWorkingSetSelectionDialog dialog= workingSetManager.createWorkingSetSelectionDialog(fUseSelection.getShell(), true);
384
//		IWorkingSetSelectionDialog dialog= workingSetManager.createWorkingSetSelectionDialog(fUseSelection.getShell(), true);
363
385
//
364
		if (fWorkingSets != null)
386
//		if (fWorkingSets != null)
365
			dialog.setSelection(fWorkingSets);
387
//			dialog.setSelection(fWorkingSets);
366
		if (dialog.open() == Window.OK) {
388
//		if (dialog.open() == Window.OK) {
367
			Object[] result= dialog.getSelection();
389
//			Object[] result= dialog.getSelection();
368
			if (result.length > 0) {
390
//			if (result.length > 0) {
369
				setSelectedWorkingSets((IWorkingSet[]) result);
391
//				setSelectedWorkingSets((IWorkingSet[]) result);
370
				return true;
392
//				return true;
371
			}
393
//			}
372
			fWorkingSetText.setText(""); //$NON-NLS-1$
394
//			fWorkingSetText.setText(""); //$NON-NLS-1$
373
			fWorkingSets= null;
395
//			fWorkingSets= null;
374
			if (fScope == ISearchPageContainer.WORKING_SET_SCOPE)
396
//			if (fScope == ISearchPageContainer.WORKING_SET_SCOPE)
375
				setSelectedScope(ISearchPageContainer.WORKSPACE_SCOPE);
397
//				setSelectedScope(ISearchPageContainer.WORKSPACE_SCOPE);
376
			return false;
398
//			return false;
377
		}
399
//		}
378
		if (fWorkingSets != null) {
400
//		if (fWorkingSets != null) {
379
			// test if selected working set has been removed
401
//			// test if selected working set has been removed
380
			int i= 0;
402
//			int i= 0;
381
			while (i < fWorkingSets.length) {
403
//			while (i < fWorkingSets.length) {
382
				IWorkingSet workingSet= fWorkingSets[i];
404
//				IWorkingSet workingSet= fWorkingSets[i];
383
				if (!workingSet.isAggregateWorkingSet() && workingSetManager.getWorkingSet(workingSet.getName()) == null)
405
//				if (!workingSet.isAggregateWorkingSet() && workingSetManager.getWorkingSet(workingSet.getName()) == null)
384
					break;
406
//					break;
385
				i++;
407
//				i++;
386
			}
408
//			}
387
			if (i < fWorkingSets.length) {
409
//			if (i < fWorkingSets.length) {
388
				fWorkingSetText.setText(""); //$NON-NLS-1$
410
//				fWorkingSetText.setText(""); //$NON-NLS-1$
389
				fWorkingSets= null;
411
//				fWorkingSets= null;
390
				updateSearchPageContainerActionPerformedEnablement();
412
//				updateSearchPageContainerActionPerformedEnablement();
391
			}
413
//			}
392
		}
414
//		}
393
		return false;
415
//		return false;
394
	}
416
//	}
395
417
396
	void setVisible(boolean state) {
418
	void setVisible(boolean state) {
397
		fPart.setVisible(state);
419
		fPart.setVisible(state);
(-)Eclipse UI/org/eclipse/ui/dialogs/WorkingSetConfigurationBlock.java (-283 / +73 lines)
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
}
(-)Eclipse (+510 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 2008 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
 *     Remy Chi Jian Suen <remy.suen@gmail.com> - bug 201661
11
 *******************************************************************************/
12
package org.eclipse.ui.dialogs;
13
14
import java.util.ArrayList;
15
import java.util.Arrays;
16
import java.util.Collection;
17
import java.util.Comparator;
18
import java.util.Iterator;
19
import java.util.List;
20
21
import org.eclipse.core.runtime.Assert;
22
import org.eclipse.core.runtime.ListenerList;
23
import org.eclipse.jface.dialogs.Dialog;
24
import org.eclipse.jface.dialogs.IDialogConstants;
25
import org.eclipse.jface.dialogs.IDialogSettings;
26
import org.eclipse.jface.resource.JFaceResources;
27
import org.eclipse.jface.util.IPropertyChangeListener;
28
import org.eclipse.jface.util.PropertyChangeEvent;
29
import org.eclipse.jface.util.SafeRunnable;
30
import org.eclipse.jface.viewers.ISelection;
31
import org.eclipse.jface.viewers.ISelectionChangedListener;
32
import org.eclipse.jface.viewers.ISelectionProvider;
33
import org.eclipse.jface.viewers.IStructuredSelection;
34
import org.eclipse.jface.viewers.SelectionChangedEvent;
35
import org.eclipse.jface.viewers.StructuredSelection;
36
import org.eclipse.swt.SWT;
37
import org.eclipse.swt.events.DisposeEvent;
38
import org.eclipse.swt.events.DisposeListener;
39
import org.eclipse.swt.events.SelectionAdapter;
40
import org.eclipse.swt.events.SelectionEvent;
41
import org.eclipse.swt.graphics.FontMetrics;
42
import org.eclipse.swt.graphics.GC;
43
import org.eclipse.swt.graphics.Point;
44
import org.eclipse.swt.layout.GridData;
45
import org.eclipse.swt.layout.GridLayout;
46
import org.eclipse.swt.widgets.Button;
47
import org.eclipse.swt.widgets.Combo;
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.internal.WorkbenchMessages;
55
56
import com.ibm.icu.text.Collator;
57
58
/**
59
 * Instances of this class provide a reusable composite with controls that allow
60
 * the selection of working sets. 
61
 * @see org.eclipse.ui.dialogs.SimpleWorkingSetComboBlock
62
 * @see org.eclipse.ui.dialogs.WorkingSetComboBlock
63
 *
64
 * @since 3.5
65
 */
66
abstract class AbstractWorkingSetComboBlock implements ISelectionProvider {
67
68
	/**
69
	 * Empty working set array constant.
70
	 */
71
	static final IWorkingSet[] EMPTY_WORKING_SET_ARRAY = new IWorkingSet[0];
72
73
	private static final String WORKINGSET_SELECTION_HISTORY = "workingset_selection_history"; //$NON-NLS-1$
74
	private static final int MAX_HISTORY_SIZE = 5;
75
76
	private Combo workingSetCombo;
77
	private Button selectButton;
78
	private ArrayList selectionHistory;
79
80
	private IWorkingSet[] selectedWorkingSets;
81
	private final IDialogSettings dialogSettings;
82
	private final String[] workingSetTypeIds;
83
84
	private final String selectLabel;
85
86
87
	/**
88
	 * List of selection change listeners (element type: <code>ISelectionChangedListener</code>).
89
	 *
90
	 * @see #fireSelectionChanged
91
	 */
92
	private ListenerList selectionChangedListeners = new ListenerList();
93
94
	/**
95
	 * Create a new instance of this working set block to select a <em>container</em> using default labels.
96
	 * 
97
	 * @param workingSetIds
98
	 *            working set ids from which the user can choose, or <code>null</code> if all currently
99
	 *            available working set types are valid
100
	 * @param settings
101
	 *            to store/load the selection history
102
	 */
103
	public AbstractWorkingSetComboBlock(String[] workingSetIds,
104
			IDialogSettings settings) {
105
		this(workingSetIds, settings, null);
106
	}
107
108
	/**
109
	 * Create a new instance of this working set block to select a <em>container</em>.
110
	 * 
111
	 * @param workingSetIds
112
	 *            working set ids from which the user can choose, or <code>null</code> if all currently
113
	 *            available working set types are valid
114
	 * @param settings
115
	 *            to store/load the selection history
116
	 * @param selectLabel 
117
	 */
118
	public AbstractWorkingSetComboBlock(String[] workingSetIds,
119
			IDialogSettings settings,String selectLabel) {
120
		Assert.isNotNull(settings);
121
		if (workingSetIds != null)
122
			Arrays.sort(workingSetIds); 
123
		workingSetTypeIds = workingSetIds;
124
		selectedWorkingSets = EMPTY_WORKING_SET_ARRAY;
125
		dialogSettings = settings;
126
		this.selectLabel = selectLabel == null ? WorkbenchMessages.WorkingSetConfigurationBlock_SelectWorkingSet_button
127
				: selectLabel;
128
	}
129
130
131
	/**
132
	 * Set the current selection of working sets. This array will be filtered to
133
	 * contain only working sets that are applicable to this instance.
134
	 * 
135
	 * @param workingSets
136
	 *            the working sets
137
	 */
138
	public void setSelectedWorkingSets(IWorkingSet[] workingSets) {
139
		setWorkingSets(workingSets);
140
		fireSelectionChanged();
141
	}
142
	
143
	/**
144
	 * Set the currently selected working sets. This array will be filtered to
145
	 * contain only working sets that are applicable to this instance.
146
	 * A selection event is not fired.
147
	 * 
148
	 * @param workingSets
149
	 *            the working sets
150
	 */
151
	/*package*/ void setWorkingSets(IWorkingSet[] workingSets) {
152
		selectedWorkingSets = filterWorkingSets(Arrays.asList(workingSets));
153
		if (workingSetCombo != null)
154
			updateWorkingSetSelection();
155
	}
156
157
	/**
158
	 * Return the currently selected working sets.
159
	 * 
160
	 * @return the selected working sets
161
	 */
162
	public IWorkingSet[] getSelectedWorkingSets() {
163
		if (selectedWorkingSets==null) {
164
			selectedWorkingSets=EMPTY_WORKING_SET_ARRAY;
165
		}
166
		return selectedWorkingSets;
167
	}
168
169
	/**
170
	 * Return the Selection containing currently selected working sets.
171
	 * 
172
	 * @return StructuredSelection
173
	 */
174
	public ISelection getSelection() {
175
		return new StructuredSelection(getSelectedWorkingSets());
176
	}
177
178
	/**
179
	 * Set the currently selected working sets. This selection will be filtered to
180
	 * contain only working sets that are applicable to this instance.
181
	 * 
182
	 * @param selection
183
	 *            the selection containing the Working Sets
184
	 */
185
	public void setSelection(ISelection selection) {
186
		if (selection instanceof IStructuredSelection) {
187
			selectedWorkingSets = findApplicableWorkingSets((IStructuredSelection) selection);
188
		} else {
189
			selectedWorkingSets = EMPTY_WORKING_SET_ARRAY;
190
		}
191
		if (workingSetCombo != null)
192
			updateWorkingSetSelection();
193
		fireSelectionChanged();
194
	}
195
196
	/**
197
	 * Add a Selection change listener
198
	 * 
199
	 * @param listener
200
	 */
201
	public void addSelectionChangedListener(ISelectionChangedListener listener) {
202
		selectionChangedListeners.add(listener);
203
	}
204
205
	/**
206
	 * Removes a Selection change listener
207
	 * 
208
	 * @param listener
209
	 */
210
	public void removeSelectionChangedListener(ISelectionChangedListener listener) {
211
		selectionChangedListeners.remove(listener);
212
	}
213
214
	/**
215
	 * Add this block to the <code>parent</parent>
216
	 * 
217
	 * @param parent the parent to add the block to
218
	 */
219
	public Control createContent(Composite parent) {
220
		selectionHistory = loadSelectionHistory(dialogSettings, workingSetTypeIds);
221
		final Composite block= new Composite(parent,SWT.NONE);
222
		GridLayout layout=new GridLayout(2, false);
223
		block.setLayout(layout);
224
		block.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
225
226
		workingSetCombo = new Combo(block, SWT.READ_ONLY | SWT.BORDER);
227
		GridData textData = new GridData(SWT.FILL, SWT.CENTER, true, false);
228
		workingSetCombo.setLayoutData(textData);
229
230
		selectButton = new Button(block, SWT.PUSH);
231
		selectButton
232
		.setText(selectLabel);
233
		setButtonLayoutData(selectButton);
234
		selectButton.addSelectionListener(new SelectionAdapter() {
235
			public void widgetSelected(SelectionEvent e) {
236
				IWorkingSet[] result=openSelectionDialog(block.getShell());
237
				if(result!=null){
238
					if (result.length > 0) {
239
						selectedWorkingSets = result;
240
						PlatformUI.getWorkbench().getWorkingSetManager()
241
						.addRecentWorkingSet(result[0]);
242
					} else {
243
						selectedWorkingSets = EMPTY_WORKING_SET_ARRAY;
244
					}
245
					updateWorkingSetSelection();
246
					fireSelectionChanged();
247
				}
248
			}
249
		});
250
		final IPropertyChangeListener workingSetChangeListener= new IPropertyChangeListener() {
251
			public void propertyChange(PropertyChangeEvent event) {
252
				String property= event.getProperty();
253
				if (IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE.equals(property)
254
						|| IWorkingSetManager.CHANGE_WORKING_SET_REMOVE.equals(property)) {
255
					// old name is not available - best we can do is reload the history :-(
256
					selectionHistory = loadSelectionHistory(dialogSettings, workingSetTypeIds);
257
					updateWorkingSetSelection();
258
				}
259
			}
260
		};
261
		final IWorkingSetManager workingSetManager= PlatformUI.getWorkbench().getWorkingSetManager();
262
		workingSetManager.addPropertyChangeListener(workingSetChangeListener);
263
		block.addDisposeListener(new DisposeListener() {
264
			public void widgetDisposed(DisposeEvent e) {
265
				workingSetManager.removePropertyChangeListener(workingSetChangeListener);
266
			}
267
		});
268
269
		workingSetCombo.addSelectionListener(new SelectionAdapter() {
270
			public void widgetSelected(SelectionEvent e) {
271
				updateSelectedWorkingSets();
272
				fireSelectionChanged();
273
			}
274
		});
275
276
		workingSetCombo.setItems(getHistoryEntries());
277
		if (selectedWorkingSets.length == 0 && selectionHistory.size() > 0) {
278
			workingSetCombo.select(historyIndex((String) selectionHistory
279
					.get(0)));
280
			updateSelectedWorkingSets();
281
		} else {
282
			updateWorkingSetSelection();
283
		}
284
		return block;
285
	}
286
287
	/**
288
	 * Enables or disables the Controls in the  block.
289
	 * @param enabled
290
	 */
291
	public void setEnabled(boolean enabled) {
292
		workingSetCombo
293
		.setEnabled(enabled
294
				&& (selectedWorkingSets.length > 0 || getHistoryEntries().length > 0));
295
		selectButton.setEnabled(enabled);
296
	}
297
	/**
298
	 * @return Returns the workingSetTypeIds.
299
	 */
300
	public String[] getWorkingSetTypeIds() {
301
		return workingSetTypeIds;
302
	}
303
304
	/**
305
	 * 	Opens a working set selection Dialog
306
	 * 
307
	 * @return IWorkingSet[] 
308
	 *               the workingSet selected in the dialog.
309
	 */
310
	abstract IWorkingSet[] openSelectionDialog(Shell shell);
311
	
312
313
	/**
314
	 * Notifies any selection changed listeners that the block's selection has changed.
315
	 * Only listeners registered at the time this method is called are notified.
316
	 *
317
	 * @see ISelectionChangedListener#selectionChanged
318
	 * 
319
	 * @since 3.5
320
	 */
321
	private void fireSelectionChanged() {
322
		Object[] listeners = selectionChangedListeners.getListeners();
323
		if (listeners.length == 0)
324
			return;
325
326
		final SelectionChangedEvent event = new SelectionChangedEvent(this, new StructuredSelection(getSelectedWorkingSets()));
327
		for (int i = 0; i < listeners.length; ++i) {
328
			final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i];
329
			SafeRunnable.run(new SafeRunnable() {
330
				public void run() {
331
					l.selectionChanged(event);
332
				}
333
			});
334
		}
335
	}
336
337
338
	/**
339
	 * Retrieves a working set from the given <code>selection</code> or an
340
	 * empty array if no working set could be retrieved. This selection is
341
	 * filtered based on the criteria used to construct this instance.
342
	 * 
343
	 * @param selection
344
	 *            the selection to retrieve the working set from
345
	 * @return the selected working set or an empty array
346
	 */
347
	public  IWorkingSet[] findApplicableWorkingSets(
348
			IStructuredSelection selection) {
349
		if (selection == null)
350
			return EMPTY_WORKING_SET_ARRAY;
351
352
		return filterWorkingSets(selection.toList());
353
	}
354
355
	/**
356
	 * Prune a list of working sets such that they all match the criteria set
357
	 * out by this block.
358
	 * 
359
	 * @param elements
360
	 *            the elements to filter
361
	 * @return the filtered elements
362
	 */
363
	private IWorkingSet[] filterWorkingSets(Collection elements) {
364
		ArrayList result = new ArrayList();
365
		for (Iterator iterator = elements.iterator(); iterator.hasNext();) {
366
			Object element = iterator.next();
367
			if (element instanceof IWorkingSet
368
					&& verifyWorkingSet((IWorkingSet) element)) {
369
				result.add(element);
370
			}
371
		}
372
		return (IWorkingSet[]) result.toArray(new IWorkingSet[result.size()]);
373
	}
374
	/**
375
	 * Verifies that the given working set is suitable for selection in this
376
	 * block.
377
	 * 
378
	 * @param workingSetCandidate
379
	 *            the candidate to test
380
	 * @return whether it is suitable
381
	 */
382
	boolean verifyWorkingSet(IWorkingSet workingSetCandidate) {
383
		return (workingSetTypeIds == null || Arrays.binarySearch(workingSetTypeIds, workingSetCandidate
384
				.getId()) >= 0) ;
385
	}
386
	private void updateWorkingSetSelection() {
387
		if (selectedWorkingSets.length > 0) {
388
			StringBuffer buf = new StringBuffer();
389
390
			buf.append(selectedWorkingSets[0].getName());
391
			for (int i = 1; i < selectedWorkingSets.length; i++) {
392
				IWorkingSet ws = selectedWorkingSets[i];
393
				buf.append(',').append(' ');
394
				buf.append(ws.getName());
395
			}
396
397
			String currentSelection = buf.toString();
398
			historyInsert(currentSelection);
399
			workingSetCombo.setItems(getHistoryEntries());
400
			workingSetCombo.select(historyIndex(currentSelection));
401
		}else{
402
			workingSetCombo.deselectAll();  
403
		}
404
	}
405
406
	private String[] getHistoryEntries() {
407
		String[] history = (String[]) selectionHistory
408
		.toArray(new String[selectionHistory.size()]);
409
		Arrays.sort(history, new Comparator() {
410
			public int compare(Object o1, Object o2) {
411
				return Collator.getInstance().compare(o1, o2);
412
			}
413
		});
414
		return history;
415
	}
416
417
	private void historyInsert(String entry) {
418
		selectionHistory.remove(entry);
419
		selectionHistory.add(0, entry);
420
		storeSelectionHistory(dialogSettings);
421
		
422
	}
423
424
	private int historyIndex(String entry) {
425
		for (int i = 0; i < workingSetCombo.getItemCount(); i++) {
426
			if (workingSetCombo.getItem(i).equals(entry))
427
				return i;
428
		}
429
430
		return -1;
431
	}
432
433
	private void updateSelectedWorkingSets() {
434
		String item = workingSetCombo.getItem(workingSetCombo
435
				.getSelectionIndex());
436
		String[] workingSetNames = item.split(", "); //$NON-NLS-1$
437
438
		IWorkingSetManager workingSetManager = PlatformUI.getWorkbench()
439
		.getWorkingSetManager();
440
		selectedWorkingSets = new IWorkingSet[workingSetNames.length];
441
		for (int i = 0; i < workingSetNames.length; i++) {
442
			IWorkingSet set = workingSetManager
443
			.getWorkingSet(workingSetNames[i]);
444
			Assert.isNotNull(set);
445
			selectedWorkingSets[i] = set;
446
		}
447
	}
448
449
	private void storeSelectionHistory(IDialogSettings settings) {
450
		String[] history;
451
		if (selectionHistory.size() > MAX_HISTORY_SIZE) {
452
			List subList = selectionHistory.subList(0, MAX_HISTORY_SIZE);
453
			history = (String[]) subList.toArray(new String[subList.size()]);
454
		} else {
455
			history = (String[]) selectionHistory
456
			.toArray(new String[selectionHistory.size()]);
457
		}
458
		settings.put(WORKINGSET_SELECTION_HISTORY, history);
459
	}
460
461
	private ArrayList loadSelectionHistory(IDialogSettings settings,
462
			String[] workingSetIds) {
463
		String[] strings = settings.getArray(WORKINGSET_SELECTION_HISTORY);
464
		if (strings == null || strings.length == 0)
465
			return new ArrayList();
466
467
		ArrayList result = new ArrayList();
468
469
		IWorkingSetManager workingSetManager = PlatformUI.getWorkbench()
470
		.getWorkingSetManager();
471
		for (int i = 0; i < strings.length; i++) {
472
			String[] workingSetNames = strings[i].split(", "); //$NON-NLS-1$
473
			boolean valid = true;
474
			for (int j = 0; j < workingSetNames.length && valid; j++) {
475
				IWorkingSet workingSet = workingSetManager
476
				.getWorkingSet(workingSetNames[j]);
477
				if (workingSet == null) {
478
					valid = false;
479
				} else{
480
					if (!verifyWorkingSet(workingSet))
481
						valid = false;
482
				}
483
			}
484
			if (valid) {
485
				result.add(strings[i]);
486
			}
487
		}
488
489
		return result;
490
	}
491
492
	/*
493
	 * Copy from DialogPage with changes to accommodate the lack of a Dialog context.
494
	 */
495
	private GridData setButtonLayoutData(Button button) {
496
		button.setFont(JFaceResources.getDialogFont());
497
498
		GC gc = new GC(button);
499
		gc.setFont(button.getFont());
500
		FontMetrics fontMetrics = gc.getFontMetrics();
501
		gc.dispose();
502
503
		GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
504
		int widthHint = Dialog.convertHorizontalDLUsToPixels(fontMetrics, IDialogConstants.BUTTON_WIDTH);
505
		Point minSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
506
		data.widthHint = Math.max(widthHint, minSize.x);
507
		button.setLayoutData(data);
508
		return data;
509
	}
510
}
(-)Eclipse (+92 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 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
 ******************************************************************************/
11
12
package org.eclipse.ui.dialogs;
13
14
import org.eclipse.jface.dialogs.IDialogSettings;
15
import org.eclipse.jface.window.Window;
16
import org.eclipse.swt.widgets.Shell;
17
import org.eclipse.ui.IWorkingSet;
18
import org.eclipse.ui.internal.WorkbenchMessages;
19
import org.eclipse.ui.internal.dialogs.SimpleWorkingSetSelectionDialog;
20
21
/**
22
 * @since 3.5
23
 * 
24
 */
25
public class SimpleWorkingSetComboBlock extends AbstractWorkingSetComboBlock {
26
	//
27
	private boolean editMode;
28
29
	/**
30
	 * @param workingSetIds
31
	 * @param settings
32
	 */
33
	public SimpleWorkingSetComboBlock(String[] workingSetIds,
34
			IDialogSettings settings) {
35
		this(workingSetIds, settings, null);
36
	}
37
38
	/**
39
	 * @param workingSetIds
40
	 * @param settings
41
	 * @param selectLabel
42
	 */
43
	public SimpleWorkingSetComboBlock(String[] workingSetIds,
44
			IDialogSettings settings, String selectLabel) {
45
		this(workingSetIds, settings, selectLabel, false);
46
	}
47
48
	/**
49
	 * @param workingSetIds
50
	 * @param settings
51
	 * @param selectLabel
52
	 * @param editMode
53
	 */
54
	public SimpleWorkingSetComboBlock(String[] workingSetIds,
55
			IDialogSettings settings, String selectLabel, boolean editMode) {
56
		super(workingSetIds, settings, selectLabel);
57
		this.editMode = editMode;
58
	}
59
60
	/*
61
	 * (non-Javadoc)
62
	 * 
63
	 * @see org.eclipse.ui.dialogs.WorkingSetComboBlock#openSelectionDialog()
64
	 */
65
	IWorkingSet[] openSelectionDialog(Shell shell) {
66
		SimpleWorkingSetSelectionDialog dialog = new SimpleWorkingSetSelectionDialog(
67
				shell, getWorkingSetTypeIds(), getSelectedWorkingSets(),
68
				editMode);
69
		dialog.setMessage(WorkbenchMessages.WorkingSetGroup_WorkingSetSelection_message);
70
		if (dialog.open() == Window.OK) {
71
			IWorkingSet[] result = dialog.getSelection();
72
			if (result != null && result.length > 0) {
73
				return result;
74
			}
75
			return EMPTY_WORKING_SET_ARRAY;
76
		}
77
		return null;
78
	}
79
80
	/**
81
	 * Verifies that the given working set is suitable for selection in this
82
	 * block.
83
	 * 
84
	 * @param workingSetCandidate
85
	 *            the candidate to test
86
	 * @return whether it is suitable
87
	 */
88
	boolean verifyWorkingSet(IWorkingSet workingSetCandidate) {
89
		return super.verifyWorkingSet(workingSetCandidate)
90
				&& !workingSetCandidate.isAggregateWorkingSet();
91
	}
92
}
(-)Eclipse (+63 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 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
 ******************************************************************************/
11
12
package org.eclipse.ui.dialogs;
13
14
import org.eclipse.jface.dialogs.IDialogSettings;
15
import org.eclipse.jface.window.Window;
16
import org.eclipse.swt.widgets.Shell;
17
import org.eclipse.ui.IWorkingSet;
18
import org.eclipse.ui.internal.WorkbenchMessages;
19
import org.eclipse.ui.internal.dialogs.WorkingSetSelectionDialog;
20
21
/**
22
 * @since 3.5
23
 *
24
 */
25
public class WorkingSetComboBlock extends AbstractWorkingSetComboBlock {
26
27
	/**
28
	 * @param workingSetIds
29
	 * @param settings
30
	 */
31
	public WorkingSetComboBlock(String[] workingSetIds,
32
			IDialogSettings settings) {
33
		this(workingSetIds, settings, null);
34
	}
35
36
	/**
37
	 * @param workingSetIds
38
	 * @param settings
39
	 * @param selectLabel
40
	 */
41
	public WorkingSetComboBlock(String[] workingSetIds,
42
			IDialogSettings settings, String selectLabel) {
43
		super(workingSetIds, settings, selectLabel);
44
	}
45
	
46
	/* (non-Javadoc)
47
	 * @see org.eclipse.ui.dialogs.AbstractWorkingSetComboBlock#openSelectionDialog(org.eclipse.swt.widgets.Shell)
48
	 */
49
	IWorkingSet[] openSelectionDialog(Shell shell) {
50
		WorkingSetSelectionDialog dialog = new WorkingSetSelectionDialog(shell, true, getWorkingSetTypeIds());
51
		dialog.setSelection(getSelectedWorkingSets());
52
		dialog.setMessage(WorkbenchMessages.WorkingSetGroup_WorkingSetSelection_message);
53
		if (dialog.open() == Window.OK) {
54
			IWorkingSet[] result = dialog.getSelection();
55
			if (result != null && result.length > 0) {
56
				return result;
57
			}
58
			return EMPTY_WORKING_SET_ARRAY;
59
		}
60
		return null;
61
	}
62
63
}

Return to bug 245258