001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog font
011: * should be activated and used by other components.
012: *******************************************************************************/package org.eclipse.ui.internal.dialogs;
013:
014: import java.util.Arrays;
015: import java.util.Collections;
016: import java.util.Iterator;
017: import java.util.List;
018:
019: import org.eclipse.jface.dialogs.Dialog;
020: import org.eclipse.jface.dialogs.IDialogConstants;
021: import org.eclipse.jface.viewers.ArrayContentProvider;
022: import org.eclipse.jface.viewers.CheckStateChangedEvent;
023: import org.eclipse.jface.viewers.CheckboxTableViewer;
024: import org.eclipse.jface.viewers.DoubleClickEvent;
025: import org.eclipse.jface.viewers.ICheckStateListener;
026: import org.eclipse.jface.viewers.IDoubleClickListener;
027: import org.eclipse.jface.viewers.ILabelProvider;
028: import org.eclipse.jface.viewers.ISelection;
029: import org.eclipse.jface.viewers.ISelectionChangedListener;
030: import org.eclipse.jface.viewers.IStructuredContentProvider;
031: import org.eclipse.jface.viewers.IStructuredSelection;
032: import org.eclipse.jface.viewers.SelectionChangedEvent;
033: import org.eclipse.jface.viewers.StructuredSelection;
034: import org.eclipse.swt.SWT;
035: import org.eclipse.swt.events.SelectionAdapter;
036: import org.eclipse.swt.events.SelectionEvent;
037: import org.eclipse.swt.events.SelectionListener;
038: import org.eclipse.swt.layout.GridData;
039: import org.eclipse.swt.layout.GridLayout;
040: import org.eclipse.swt.widgets.Button;
041: import org.eclipse.swt.widgets.Composite;
042: import org.eclipse.swt.widgets.Control;
043: import org.eclipse.swt.widgets.Shell;
044: import org.eclipse.ui.IWorkbenchWindow;
045: import org.eclipse.ui.IWorkingSet;
046: import org.eclipse.ui.IWorkingSetManager;
047: import org.eclipse.ui.PlatformUI;
048: import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
049: import org.eclipse.ui.internal.AggregateWorkingSet;
050: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
051: import org.eclipse.ui.internal.WorkbenchMessages;
052: import org.eclipse.ui.internal.WorkbenchPlugin;
053: import org.eclipse.ui.internal.util.Util;
054: import org.eclipse.ui.model.WorkbenchViewerComparator;
055:
056: /**
057: * A working set selection dialog displays a list of working
058: * sets available in the workbench.
059: *
060: * @see IWorkingSetSelectionDialog
061: * @since 2.0
062: */
063: public class WorkingSetSelectionDialog extends AbstractWorkingSetDialog {
064: private final static int SIZING_SELECTION_WIDGET_HEIGHT = 200;
065:
066: private final static int SIZING_SELECTION_WIDGET_WIDTH = 50;
067:
068: private ILabelProvider labelProvider;
069:
070: private IStructuredContentProvider contentProvider;
071:
072: private CheckboxTableViewer listViewer;
073:
074: private boolean multiSelect;
075:
076: private IWorkbenchWindow workbenchWindow;
077:
078: private Button buttonWindowSet;
079:
080: private Button buttonNoSet;
081:
082: private Button buttonSelectedSets;
083:
084: /**
085: * Creates a working set selection dialog.
086: *
087: * @param parentShell the parent shell
088: * @param multi true=more than one working set can be chosen
089: * in the dialog. false=only one working set can be chosen. Multiple
090: * working sets can still be selected and removed from the list but
091: * the dialog can only be closed when a single working set is selected.
092: * @param workingSetIds a list of working set ids which are valid workings sets
093: * to be selected, created, removed or edited, or <code>null</code> if all currently
094: * available working set types are valid
095: */
096: public WorkingSetSelectionDialog(Shell parentShell, boolean multi,
097: String[] workingSetIds) {
098: super (parentShell, workingSetIds, true);
099: initWorkbenchWindow();
100:
101: contentProvider = new ArrayContentProvider();
102: labelProvider = new WorkingSetLabelProvider();
103: multiSelect = multi;
104: if (multiSelect) {
105: setTitle(WorkbenchMessages.WorkingSetSelectionDialog_title_multiSelect);
106: setMessage(WorkbenchMessages.WorkingSetSelectionDialog_message_multiSelect);
107: } else {
108: setTitle(WorkbenchMessages.WorkingSetSelectionDialog_title);
109: setMessage(WorkbenchMessages.WorkingSetSelectionDialog_message);
110: }
111:
112: }
113:
114: /**
115: * Determine what window this dialog is being opened on. This impacts the
116: * returned working set in the case where the user chooses the window set.
117: *
118: * @since 3.2
119: */
120: private void initWorkbenchWindow() {
121: Shell shellToCheck = getShell();
122:
123: workbenchWindow = Util.getWorkbenchWindowForShell(shellToCheck);
124: }
125:
126: /**
127: * Overrides method from Dialog.
128: *
129: * @see org.eclipse.jface.dialogs.Dialog#cancelPressed()
130: */
131: protected void cancelPressed() {
132: restoreAddedWorkingSets();
133: restoreChangedWorkingSets();
134: restoreRemovedWorkingSets();
135: setSelection(null);
136: super .cancelPressed();
137: }
138:
139: /**
140: * Overrides method from Window.
141: *
142: * @see org.eclipse.jface.window.Window#configureShell(Shell)
143: */
144: protected void configureShell(Shell shell) {
145: super .configureShell(shell);
146: PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
147: IWorkbenchHelpContextIds.WORKING_SET_SELECTION_DIALOG);
148: }
149:
150: /**
151: * Overrides method from Dialog.
152: * Create the dialog widgets.
153: *
154: * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(Composite)
155: */
156: protected Control createDialogArea(Composite parent) {
157: initializeDialogUnits(parent);
158:
159: Composite composite = (Composite) super
160: .createDialogArea(parent);
161:
162: createMessageArea(composite);
163:
164: SelectionListener listener = new SelectionAdapter() {
165: public void widgetSelected(SelectionEvent e) {
166: updateButtonAvailability();
167: }
168: };
169:
170: buttonWindowSet = new Button(composite, SWT.RADIO);
171: buttonWindowSet.setText(WorkbenchMessages.WindowWorkingSets);
172: buttonWindowSet.setLayoutData(new GridData(
173: GridData.FILL_HORIZONTAL));
174: buttonWindowSet.addSelectionListener(listener);
175:
176: buttonNoSet = new Button(composite, SWT.RADIO);
177: buttonNoSet.setText(WorkbenchMessages.NoWorkingSet);
178: buttonNoSet
179: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
180: buttonNoSet.addSelectionListener(listener);
181:
182: buttonSelectedSets = new Button(composite, SWT.RADIO);
183: buttonSelectedSets
184: .setText(WorkbenchMessages.SelectedWorkingSets);
185: buttonSelectedSets.addSelectionListener(listener);
186:
187: switch (getInitialRadioSelection()) {
188: case 0:
189: buttonWindowSet.setSelection(true);
190: break;
191: case 1:
192: buttonNoSet.setSelection(true);
193: break;
194: case 2:
195: buttonSelectedSets.setSelection(true);
196: break;
197: }
198: buttonSelectedSets.setLayoutData(new GridData(
199: GridData.FILL_HORIZONTAL));
200:
201: Composite viewerComposite = new Composite(composite, SWT.NONE);
202: GridLayout layout = new GridLayout(2, false);
203: layout.marginHeight = layout.marginWidth = 0;
204: layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
205: layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
206: viewerComposite.setLayout(layout);
207:
208: GridData data = new GridData(GridData.FILL_BOTH);
209: data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
210: data.widthHint = SIZING_SELECTION_WIDGET_WIDTH + 300; // fudge? I like fudge.
211: viewerComposite.setLayoutData(data);
212:
213: listViewer = CheckboxTableViewer.newCheckList(viewerComposite,
214: SWT.BORDER | SWT.MULTI);
215: data = new GridData(GridData.FILL_BOTH);
216: data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
217: data.widthHint = SIZING_SELECTION_WIDGET_WIDTH;
218: listViewer.getTable().setLayoutData(data);
219:
220: listViewer.setLabelProvider(labelProvider);
221: listViewer.setContentProvider(contentProvider);
222: listViewer.setComparator(new WorkbenchViewerComparator());
223:
224: listViewer.addFilter(new WorkingSetFilter(
225: getSupportedWorkingSetIds()));
226:
227: listViewer
228: .addSelectionChangedListener(new ISelectionChangedListener() {
229: public void selectionChanged(
230: SelectionChangedEvent event) {
231: handleSelectionChanged();
232: }
233: });
234: listViewer.addDoubleClickListener(new IDoubleClickListener() {
235: public void doubleClick(DoubleClickEvent event) {
236: Object obj = ((IStructuredSelection) listViewer
237: .getSelection()).getFirstElement();
238: listViewer.setCheckedElements(new Object[] { obj });
239: buttonWindowSet.setSelection(false);
240: buttonNoSet.setSelection(false);
241: buttonSelectedSets.setSelection(true);
242: okPressed();
243: }
244: });
245: listViewer.addCheckStateListener(new ICheckStateListener() {
246: public void checkStateChanged(CheckStateChangedEvent event) {
247: // implicitly select the third radio button
248: buttonWindowSet.setSelection(false);
249: buttonNoSet.setSelection(false);
250: buttonSelectedSets.setSelection(true);
251: }
252: });
253:
254: addModifyButtons(viewerComposite);
255:
256: addSelectionButtons(composite);
257:
258: listViewer.setInput(Arrays.asList(WorkbenchPlugin.getDefault()
259: .getWorkingSetManager().getWorkingSets()));
260: List initialElementSelections = getInitialElementSelections();
261: if (multiSelect) {
262: listViewer.setCheckedElements(initialElementSelections
263: .toArray());
264: } else if (!initialElementSelections.isEmpty()) {
265: IWorkingSet set = (IWorkingSet) initialElementSelections
266: .get(0);
267: if (set instanceof AggregateWorkingSet) {
268: AggregateWorkingSet aggregate = (AggregateWorkingSet) set;
269: listViewer
270: .setCheckedElements(aggregate.getComponents());
271: } else {
272: listViewer.setCheckedElements(initialElementSelections
273: .toArray());
274: }
275: }
276:
277: availableWorkingSetsChanged();
278: Dialog.applyDialogFont(composite);
279:
280: return composite;
281: }
282:
283: private int getInitialRadioSelection() {
284: IWorkingSet windowSet = workbenchWindow.getActivePage()
285: .getAggregateWorkingSet();
286:
287: int selectionIndex;
288: if (getSelection() != null && getSelection().length > 0) {
289: if (windowSet.equals(getSelection()[0])) {
290: selectionIndex = 0;
291: } else {
292: selectionIndex = 2;
293: }
294: } else {
295: selectionIndex = 1;
296: }
297:
298: return selectionIndex;
299: }
300:
301: /**
302: * Overrides method from Dialog.
303: * Sets the initial selection, if any.
304: *
305: * @see org.eclipse.jface.dialogs.Dialog#createContents(Composite)
306: */
307: protected Control createContents(Composite parent) {
308: Control control = super .createContents(parent);
309: List selections = getInitialElementSelections();
310: if (!selections.isEmpty()) {
311: listViewer.setSelection(
312: new StructuredSelection(selections), true);
313: }
314: updateButtonAvailability();
315: return control;
316: }
317:
318: /**
319: * Returns the selected working sets.
320: *
321: * @return the selected working sets
322: */
323: protected List getSelectedWorkingSets() {
324: ISelection selection = listViewer.getSelection();
325: if (selection instanceof IStructuredSelection) {
326: return ((IStructuredSelection) selection).toList();
327: }
328: return null;
329: }
330:
331: /**
332: * Called when the selection has changed.
333: */
334: void handleSelectionChanged() {
335: updateButtonAvailability();
336: }
337:
338: /**
339: * Sets the selected working sets as the dialog result.
340: * Overrides method from Dialog
341: *
342: * @see org.eclipse.jface.dialogs.Dialog#okPressed()
343: */
344: protected void okPressed() {
345: if (buttonWindowSet.getSelection()) {
346: IWorkingSet[] windowSet = new IWorkingSet[] { workbenchWindow
347: .getActivePage().getAggregateWorkingSet() };
348: setSelection(windowSet);
349: setResult(Arrays.asList(getSelection()));
350: } else if (buttonNoSet.getSelection()) {
351: setSelection(new IWorkingSet[0]);
352: setResult(Arrays.asList(getSelection()));
353: } else if (buttonSelectedSets.getSelection()) {
354: Object[] untypedResult = listViewer.getCheckedElements();
355: IWorkingSet[] typedResult = new IWorkingSet[untypedResult.length];
356: System.arraycopy(untypedResult, 0, typedResult, 0,
357: untypedResult.length);
358: // if multiselect is allowed or there was only one selected then dont create
359: // an aggregate
360: if (multiSelect || typedResult.length <= 1) {
361: setSelection(typedResult);
362: setResult(Arrays.asList(typedResult));
363: } else {
364: String setId = getAggregateIdForSets(typedResult);
365: IWorkingSetManager workingSetManager = workbenchWindow
366: .getWorkbench().getWorkingSetManager();
367: IWorkingSet aggregate = workingSetManager
368: .getWorkingSet(setId);
369: if (aggregate == null) {
370: aggregate = workingSetManager
371: .createAggregateWorkingSet(
372: setId,
373: WorkbenchMessages.WorkbenchPage_workingSet_multi_label,
374: typedResult);
375: workingSetManager.addWorkingSet(aggregate);
376: }
377: setSelection(new IWorkingSet[] { aggregate });
378: setResult(Collections.singletonList(aggregate));
379: }
380: }
381:
382: super .okPressed();
383: }
384:
385: /**
386: * Create a string that represents the name of the aggregate set composed of
387: * the supplied working sets. It's very long and not printworthy.
388: *
389: * @param typedResult the sets
390: * @return the name
391: */
392: private String getAggregateIdForSets(IWorkingSet[] typedResult) {
393: StringBuffer buffer = new StringBuffer();
394: buffer.append("Aggregate:"); //$NON-NLS-1$
395: for (int i = 0; i < typedResult.length; i++) {
396: buffer.append(typedResult[i].getName()).append(':');
397: }
398: return buffer.toString();
399: }
400:
401: /**
402: * Removes newly created working sets from the working set manager.
403: */
404: private void restoreAddedWorkingSets() {
405: IWorkingSetManager manager = WorkbenchPlugin.getDefault()
406: .getWorkingSetManager();
407: Iterator iterator = getAddedWorkingSets().iterator();
408:
409: while (iterator.hasNext()) {
410: manager.removeWorkingSet(((IWorkingSet) iterator.next()));
411: }
412: }
413:
414: /**
415: * Rolls back changes to working sets.
416: */
417: private void restoreChangedWorkingSets() {
418: Iterator iterator = getEditedWorkingSets().keySet().iterator();
419:
420: while (iterator.hasNext()) {
421: IWorkingSet editedWorkingSet = (IWorkingSet) iterator
422: .next();
423: IWorkingSet originalWorkingSet = (IWorkingSet) getEditedWorkingSets()
424: .get(editedWorkingSet);
425:
426: if (editedWorkingSet.getName().equals(
427: originalWorkingSet.getName()) == false) {
428: editedWorkingSet.setName(originalWorkingSet.getName());
429: }
430: if (editedWorkingSet.getElements().equals(
431: originalWorkingSet.getElements()) == false) {
432: editedWorkingSet.setElements(originalWorkingSet
433: .getElements());
434: }
435: }
436: }
437:
438: /**
439: * Adds back removed working sets to the working set manager.
440: */
441: private void restoreRemovedWorkingSets() {
442: IWorkingSetManager manager = WorkbenchPlugin.getDefault()
443: .getWorkingSetManager();
444: Iterator iterator = getRemovedWorkingSets().iterator();
445:
446: while (iterator.hasNext()) {
447: manager.addWorkingSet(((IWorkingSet) iterator.next()));
448: }
449: iterator = getRemovedMRUWorkingSets().iterator();
450: while (iterator.hasNext()) {
451: manager
452: .addRecentWorkingSet(((IWorkingSet) iterator.next()));
453: }
454: }
455:
456: /**
457: * Implements IWorkingSetSelectionDialog.
458: *
459: * @see org.eclipse.ui.dialogs.IWorkingSetSelectionDialog#setSelection(IWorkingSet[])
460: */
461: public void setSelection(IWorkingSet[] workingSets) {
462: super .setSelection(workingSets);
463: setInitialSelections(workingSets == null ? new Object[0]
464: : workingSets);
465: }
466:
467: protected void availableWorkingSetsChanged() {
468: listViewer.setInput(PlatformUI.getWorkbench()
469: .getWorkingSetManager().getWorkingSets());
470: super .availableWorkingSetsChanged();
471: }
472:
473: protected void selectAllSets() {
474: listViewer.setCheckedElements(PlatformUI.getWorkbench()
475: .getWorkingSetManager().getWorkingSets());
476: // implicitly select the third radio button
477: buttonWindowSet.setSelection(false);
478: buttonNoSet.setSelection(false);
479: buttonSelectedSets.setSelection(true);
480: updateButtonAvailability();
481: }
482:
483: protected void deselectAllSets() {
484: listViewer.setCheckedElements(new Object[0]);
485: // implicitly select the third radio button
486: buttonWindowSet.setSelection(false);
487: buttonNoSet.setSelection(false);
488: buttonSelectedSets.setSelection(true);
489: updateButtonAvailability();
490: }
491: }
|