001: /*******************************************************************************
002: * Copyright (c) 2007 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: ******************************************************************************/package org.eclipse.ui.internal.dialogs;
011:
012: import java.util.Iterator;
013: import java.util.List;
014: import java.util.Set;
015:
016: import org.eclipse.jface.dialogs.Dialog;
017: import org.eclipse.jface.dialogs.IDialogConstants;
018: import org.eclipse.jface.viewers.ArrayContentProvider;
019: import org.eclipse.jface.viewers.CheckboxTableViewer;
020: import org.eclipse.jface.viewers.ISelection;
021: import org.eclipse.jface.viewers.ISelectionChangedListener;
022: import org.eclipse.jface.viewers.IStructuredSelection;
023: import org.eclipse.jface.viewers.SelectionChangedEvent;
024: import org.eclipse.jface.viewers.Viewer;
025: import org.eclipse.jface.viewers.ViewerFilter;
026: import org.eclipse.swt.SWT;
027: import org.eclipse.swt.layout.GridData;
028: import org.eclipse.swt.layout.GridLayout;
029: import org.eclipse.swt.widgets.Composite;
030: import org.eclipse.swt.widgets.Control;
031: import org.eclipse.swt.widgets.Shell;
032: import org.eclipse.ui.IWorkingSet;
033: import org.eclipse.ui.PlatformUI;
034: import org.eclipse.ui.internal.WorkbenchMessages;
035:
036: /**
037: * Base implementation for a simple working set dialog that doesn't contain
038: * references to non-editable/non-visible working sets.
039: *
040: * @since 3.4
041: *
042: */
043: public class SimpleWorkingSetSelectionDialog extends
044: AbstractWorkingSetDialog {
045:
046: private class Filter extends ViewerFilter {
047:
048: public boolean select(Viewer viewer, Object parentElement,
049: Object element) {
050: return isCompatible((IWorkingSet) element);
051: }
052:
053: private boolean isCompatible(IWorkingSet set) {
054: if (set.isAggregateWorkingSet())
055: return false;
056:
057: // original JDT code had the catch for self-updating sets that no
058: // one can explain. There doesn't seem to
059: // be a good reason to exclude these sets so the clause has been
060: // removed.
061:
062: // if (set.isAggregateWorkingSet() || !set.isSelfUpdating())
063: // return false;
064:
065: if (!set.isVisible())
066: return false;
067:
068: if (!set.isEditable())
069: return false;
070:
071: Set workingSetTypeIds = getSupportedWorkingSetIds();
072: if (workingSetTypeIds == null)
073: return true;
074:
075: for (Iterator i = workingSetTypeIds.iterator(); i.hasNext();) {
076: String workingSetTypeId = (String) i.next();
077: if (workingSetTypeId.equals(set.getId()))
078: return true;
079: }
080:
081: return false;
082: }
083: }
084:
085: private final static int SIZING_SELECTION_WIDGET_HEIGHT = 200;
086:
087: private final static int SIZING_SELECTION_WIDGET_WIDTH = 50;
088:
089: private CheckboxTableViewer viewer;
090:
091: private IWorkingSet[] initialSelection;
092:
093: /**
094: * Create a new instance of this class.
095: *
096: * @param shell
097: * the shell to parent this dialog on
098: * @param workingSetTypeIds
099: * the types of working set IDs that will be shown in this dialog
100: * @param selectedWorkingSets
101: * the currently selected working sets (if any)
102: * @param canEdit
103: * whether or not this dialog will display edit controls
104: */
105: public SimpleWorkingSetSelectionDialog(Shell shell,
106: String[] workingSetTypeIds,
107: IWorkingSet[] selectedWorkingSets, boolean canEdit) {
108: super (shell, workingSetTypeIds, canEdit);
109: this .initialSelection = selectedWorkingSets;
110: setTitle(WorkbenchMessages.WorkingSetSelectionDialog_title_multiSelect);
111: setMessage(WorkbenchMessages.WorkingSetSelectionDialog_message_multiSelect);
112: }
113:
114: protected Control createDialogArea(Composite parent) {
115: initializeDialogUnits(parent);
116:
117: Composite composite = (Composite) super
118: .createDialogArea(parent);
119:
120: Composite viewerComposite = new Composite(composite, SWT.NONE);
121: GridLayout layout = new GridLayout(2, false);
122: layout.marginHeight = layout.marginWidth = 0;
123: layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
124: layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
125: viewerComposite.setLayout(layout);
126:
127: GridData data = new GridData(GridData.FILL_BOTH);
128: data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
129: data.widthHint = SIZING_SELECTION_WIDGET_WIDTH + 300; // fudge? I like
130: // fudge.
131: viewerComposite.setLayoutData(data);
132:
133: viewer = CheckboxTableViewer.newCheckList(viewerComposite,
134: SWT.BORDER);
135: viewer.getControl().setLayoutData(
136: new GridData(GridData.FILL_BOTH));
137: viewer.setLabelProvider(new WorkingSetLabelProvider());
138: viewer.setContentProvider(new ArrayContentProvider());
139: viewer.addFilter(new WorkingSetFilter(null));
140: viewer.setInput(PlatformUI.getWorkbench()
141: .getWorkingSetManager().getWorkingSets());
142: viewer.setFilters(new ViewerFilter[] { new Filter() });
143:
144: viewer
145: .addSelectionChangedListener(new ISelectionChangedListener() {
146: public void selectionChanged(
147: SelectionChangedEvent event) {
148: handleSelectionChanged();
149: }
150: });
151: viewer.setCheckedElements(initialSelection);
152:
153: data = new GridData(GridData.FILL_BOTH);
154: data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
155: data.widthHint = SIZING_SELECTION_WIDGET_WIDTH;
156:
157: viewer.getControl().setLayoutData(data);
158: addModifyButtons(viewerComposite);
159:
160: addSelectionButtons(composite);
161:
162: availableWorkingSetsChanged();
163:
164: Dialog.applyDialogFont(composite);
165:
166: return composite;
167: }
168:
169: protected void okPressed() {
170: Object[] checked = viewer.getCheckedElements();
171: IWorkingSet[] workingSets = new IWorkingSet[checked.length];
172: System.arraycopy(checked, 0, workingSets, 0, checked.length);
173: setSelection(workingSets);
174: super .okPressed();
175: }
176:
177: protected List getSelectedWorkingSets() {
178: ISelection selection = viewer.getSelection();
179: if (selection instanceof IStructuredSelection) {
180: return ((IStructuredSelection) selection).toList();
181: }
182: return null;
183: }
184:
185: protected void availableWorkingSetsChanged() {
186: viewer.setInput(PlatformUI.getWorkbench()
187: .getWorkingSetManager().getWorkingSets());
188: super .availableWorkingSetsChanged();
189: }
190:
191: /**
192: * Called when the selection has changed.
193: */
194: void handleSelectionChanged() {
195: updateButtonAvailability();
196: }
197:
198: protected void selectAllSets() {
199: viewer.setCheckedElements(PlatformUI.getWorkbench()
200: .getWorkingSetManager().getWorkingSets());
201: updateButtonAvailability();
202: }
203:
204: protected void deselectAllSets() {
205: viewer.setCheckedElements(new Object[0]);
206: updateButtonAvailability();
207: }
208:
209: }
|