001: /*******************************************************************************
002: * Copyright (c) 2005, 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.ArrayList;
015: import java.util.HashMap;
016: import java.util.HashSet;
017: import java.util.Iterator;
018: import java.util.List;
019: import java.util.Map;
020: import java.util.Set;
021:
022: import org.eclipse.jface.dialogs.IDialogConstants;
023: import org.eclipse.jface.window.Window;
024: import org.eclipse.jface.wizard.WizardDialog;
025: import org.eclipse.swt.SWT;
026: import org.eclipse.swt.events.SelectionAdapter;
027: import org.eclipse.swt.events.SelectionEvent;
028: import org.eclipse.swt.layout.GridData;
029: import org.eclipse.swt.layout.GridLayout;
030: import org.eclipse.swt.widgets.Button;
031: import org.eclipse.swt.widgets.Composite;
032: import org.eclipse.swt.widgets.Shell;
033: import org.eclipse.ui.IWorkingSet;
034: import org.eclipse.ui.IWorkingSetManager;
035: import org.eclipse.ui.PlatformUI;
036: import org.eclipse.ui.dialogs.IWorkingSetEditWizard;
037: import org.eclipse.ui.dialogs.IWorkingSetNewWizard;
038: import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
039: import org.eclipse.ui.dialogs.SelectionDialog;
040: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
041: import org.eclipse.ui.internal.WorkbenchMessages;
042: import org.eclipse.ui.internal.WorkbenchPlugin;
043: import org.eclipse.ui.internal.WorkingSet;
044: import org.eclipse.ui.internal.registry.WorkingSetRegistry;
045:
046: /**
047: * Abstract baseclass for various working set dialogs.
048: *
049: * @since 3.2
050: */
051: public abstract class AbstractWorkingSetDialog extends SelectionDialog
052: implements IWorkingSetSelectionDialog {
053:
054: private static final int ID_NEW = IDialogConstants.CLIENT_ID + 1;
055: private static final int ID_DETAILS = ID_NEW + 1;
056: private static final int ID_REMOVE = ID_DETAILS + 1;
057: private static final int ID_SELECTALL = ID_REMOVE + 1;
058: private static final int ID_DESELECTALL = ID_SELECTALL + 1;
059:
060: private Button newButton;
061:
062: private Button detailsButton;
063:
064: private Button removeButton;
065:
066: private Button selectAllButton;
067:
068: private Button deselectAllButton;
069:
070: private IWorkingSet[] result;
071:
072: private List addedWorkingSets;
073:
074: private List removedWorkingSets;
075:
076: private Map editedWorkingSets;
077:
078: private List removedMRUWorkingSets;
079:
080: private Set workingSetIds;
081:
082: private boolean canEdit;
083:
084: protected AbstractWorkingSetDialog(Shell parentShell,
085: String[] workingSetIds, boolean canEdit) {
086: super (parentShell);
087: if (workingSetIds != null) {
088: this .workingSetIds = new HashSet();
089: for (int i = 0; i < workingSetIds.length; i++) {
090: this .workingSetIds.add(workingSetIds[i]);
091: }
092: }
093: this .canEdit = canEdit;
094: }
095:
096: /**
097: * Return the set of supported working set types.
098: *
099: * @return the supported working set types
100: */
101: protected Set getSupportedWorkingSetIds() {
102: return workingSetIds;
103: }
104:
105: /**
106: * Adds the modify buttons to the dialog.
107: *
108: * @param composite
109: * Composite to add the buttons to
110: */
111: protected void addModifyButtons(Composite composite) {
112: Composite buttonComposite = new Composite(composite, SWT.RIGHT);
113: GridLayout layout = new GridLayout();
114: layout.marginHeight = layout.marginWidth = 0;
115: layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
116: buttonComposite.setLayout(layout);
117: GridData data = new GridData(GridData.VERTICAL_ALIGN_BEGINNING
118: | GridData.GRAB_VERTICAL);
119: buttonComposite.setLayoutData(data);
120:
121: newButton = createButton(
122: buttonComposite,
123: ID_NEW,
124: WorkbenchMessages.WorkingSetSelectionDialog_newButton_label,
125: false);
126: newButton.addSelectionListener(new SelectionAdapter() {
127: public void widgetSelected(SelectionEvent e) {
128: createWorkingSet();
129: }
130: });
131:
132: if (canEdit) {
133: detailsButton = createButton(
134: buttonComposite,
135: ID_DETAILS,
136: WorkbenchMessages.WorkingSetSelectionDialog_detailsButton_label,
137: false);
138: detailsButton.setEnabled(false);
139: detailsButton.addSelectionListener(new SelectionAdapter() {
140: public void widgetSelected(SelectionEvent e) {
141: editSelectedWorkingSet();
142: }
143: });
144:
145: removeButton = createButton(
146: buttonComposite,
147: ID_REMOVE,
148: WorkbenchMessages.WorkingSetSelectionDialog_removeButton_label,
149: false);
150: removeButton.setEnabled(false);
151: removeButton.addSelectionListener(new SelectionAdapter() {
152: public void widgetSelected(SelectionEvent e) {
153: removeSelectedWorkingSets();
154: }
155: });
156: }
157:
158: layout.numColumns = 1; // must manually reset the number of columns because createButton increments it - we want these buttons to be laid out vertically.
159: }
160:
161: /**
162: * Add the select/deselect buttons.
163: *
164: * @param composite Composite to add the buttons to
165: */
166: protected void addSelectionButtons(Composite composite) {
167: Composite buttonComposite = new Composite(composite, SWT.NONE);
168: GridLayout layout = new GridLayout(2, false);
169: layout.marginHeight = layout.marginWidth = 0;
170: layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
171: buttonComposite.setLayout(layout);
172: GridData data = new GridData(
173: GridData.HORIZONTAL_ALIGN_BEGINNING);
174: buttonComposite.setLayoutData(data);
175:
176: selectAllButton = createButton(buttonComposite, ID_SELECTALL,
177: WorkbenchMessages.SelectionDialog_selectLabel, false);
178: selectAllButton.addSelectionListener(new SelectionAdapter() {
179: public void widgetSelected(SelectionEvent e) {
180: selectAllSets();
181: }
182: });
183:
184: deselectAllButton = createButton(buttonComposite,
185: ID_DESELECTALL,
186: WorkbenchMessages.SelectionDialog_deselectLabel, false);
187: deselectAllButton.addSelectionListener(new SelectionAdapter() {
188: public void widgetSelected(SelectionEvent e) {
189: deselectAllSets();
190: }
191: });
192: }
193:
194: /**
195: * Select all working sets.
196: */
197: protected abstract void selectAllSets();
198:
199: /**
200: * Deselect all working sets.
201: */
202: protected abstract void deselectAllSets();
203:
204: /**
205: * Opens a working set wizard for editing the currently selected working
206: * set.
207: *
208: * @see org.eclipse.ui.dialogs.IWorkingSetPage
209: */
210: void editSelectedWorkingSet() {
211: IWorkingSetManager manager = WorkbenchPlugin.getDefault()
212: .getWorkingSetManager();
213: IWorkingSet editWorkingSet = (IWorkingSet) getSelectedWorkingSets()
214: .get(0);
215: IWorkingSetEditWizard wizard = manager
216: .createWorkingSetEditWizard(editWorkingSet);
217: WizardDialog dialog = new WizardDialog(getShell(), wizard);
218: IWorkingSet originalWorkingSet = (IWorkingSet) editedWorkingSets
219: .get(editWorkingSet);
220: boolean firstEdit = originalWorkingSet == null;
221:
222: // save the original working set values for restoration when selection
223: // dialog is cancelled.
224: if (firstEdit) {
225: originalWorkingSet = new WorkingSet(editWorkingSet
226: .getName(), editWorkingSet.getLabel(),
227: editWorkingSet.getElements());
228: } else {
229: editedWorkingSets.remove(editWorkingSet);
230: }
231: dialog.create();
232: PlatformUI.getWorkbench().getHelpSystem().setHelp(
233: dialog.getShell(),
234: IWorkbenchHelpContextIds.WORKING_SET_EDIT_WIZARD);
235: if (dialog.open() == Window.OK) {
236: editWorkingSet = wizard.getSelection();
237: availableWorkingSetsChanged();
238: // make sure ok button is enabled when the selected working set
239: // is edited. Fixes bug 33386.
240: updateButtonAvailability();
241: }
242: editedWorkingSets.put(editWorkingSet, originalWorkingSet);
243: }
244:
245: /**
246: * Opens a working set wizard for creating a new working set.
247: */
248: void createWorkingSet() {
249: IWorkingSetManager manager = WorkbenchPlugin.getDefault()
250: .getWorkingSetManager();
251: String ids[] = null;
252: if (workingSetIds != null) {
253: ids = (String[]) workingSetIds
254: .toArray(new String[workingSetIds.size()]);
255: }
256: IWorkingSetNewWizard wizard = manager
257: .createWorkingSetNewWizard(ids);
258: // the wizard can never be null since we have at least a resource
259: // working set
260: // creation page
261: WizardDialog dialog = new WizardDialog(getShell(), wizard);
262:
263: dialog.create();
264: PlatformUI.getWorkbench().getHelpSystem().setHelp(
265: dialog.getShell(),
266: IWorkbenchHelpContextIds.WORKING_SET_NEW_WIZARD);
267: if (dialog.open() == Window.OK) {
268: IWorkingSet workingSet = wizard.getSelection();
269: manager.addWorkingSet(workingSet);
270: addedWorkingSets.add(workingSet);
271: availableWorkingSetsChanged();
272: }
273: }
274:
275: protected abstract List getSelectedWorkingSets();
276:
277: /**
278: * Notifies the dialog that there has been a change to the sets available
279: * for use. In other words, the user has either added, deleted or renamed a
280: * set.
281: * <p>
282: * Subclasses should override, but should call <code>super.availableWorkingSetsChanged</code>
283: * to update the selection button enablements.
284: * </p>
285: */
286: protected void availableWorkingSetsChanged() {
287: boolean enable = PlatformUI.getWorkbench()
288: .getWorkingSetManager().getWorkingSets().length > 0;
289: if (!(selectAllButton == null || selectAllButton.isDisposed())) {
290: selectAllButton.setEnabled(enable);
291: }
292: if (!(deselectAllButton == null || deselectAllButton
293: .isDisposed())) {
294: deselectAllButton.setEnabled(enable);
295: }
296: }
297:
298: /* (non-Javadoc)
299: * @see org.eclipse.ui.dialogs.IWorkingSetSelectionDialog#getSelection()
300: */
301: public IWorkingSet[] getSelection() {
302: return result;
303: }
304:
305: /* (non-Javadoc)
306: * @see org.eclipse.ui.dialogs.IWorkingSetSelectionDialog#setSelection(org.eclipse.ui.IWorkingSet[])
307: */
308: public void setSelection(IWorkingSet[] selection) {
309: result = selection;
310: }
311:
312: /**
313: * Overrides method in Dialog
314: *
315: * @see org.eclipse.jface.dialogs.Dialog#open()
316: */
317: public int open() {
318: addedWorkingSets = new ArrayList();
319: removedWorkingSets = new ArrayList();
320: editedWorkingSets = new HashMap();
321: removedMRUWorkingSets = new ArrayList();
322: return super .open();
323: }
324:
325: /**
326: * Return the list of working sets that were added during the life of this
327: * dialog.
328: *
329: * @return the working sets
330: */
331: protected final List getAddedWorkingSets() {
332: return addedWorkingSets;
333: }
334:
335: /**
336: * Return the map of working sets that were edited during the life of this
337: * dialog.
338: *
339: * @return the working sets
340: */
341: protected final Map getEditedWorkingSets() {
342: return editedWorkingSets;
343: }
344:
345: /**
346: * Return the list of working sets that were removed from the MRU list
347: * during the life of this dialog.
348: *
349: * @return the working sets
350: */
351: protected final List getRemovedMRUWorkingSets() {
352: return removedMRUWorkingSets;
353: }
354:
355: /**
356: * Return the list of working sets that were removed during the life of this
357: * dialog.
358: *
359: * @return the working sets
360: */
361: protected final List getRemovedWorkingSets() {
362: return removedWorkingSets;
363: }
364:
365: /**
366: * Updates the modify buttons' enabled state based on the current seleciton.
367: */
368: protected void updateButtonAvailability() {
369: List selection = getSelectedWorkingSets();
370: boolean hasSelection = selection != null
371: && !selection.isEmpty();
372: boolean hasSingleSelection = hasSelection;
373: WorkingSetRegistry registry = WorkbenchPlugin.getDefault()
374: .getWorkingSetRegistry();
375:
376: newButton.setEnabled(registry.hasNewPageWorkingSetDescriptor());
377:
378: if (canEdit)
379: removeButton.setEnabled(hasSelection);
380:
381: IWorkingSet selectedWorkingSet = null;
382: if (hasSelection) {
383: hasSingleSelection = selection.size() == 1;
384: if (hasSingleSelection) {
385: selectedWorkingSet = (IWorkingSet) selection.get(0);
386: }
387: }
388: if (canEdit)
389: detailsButton.setEnabled(hasSingleSelection
390: && selectedWorkingSet.isEditable());
391:
392: getOkButton().setEnabled(true);
393: }
394:
395: /**
396: * Removes the selected working sets from the workbench.
397: */
398: protected void removeSelectedWorkingSets() {
399: List selection = getSelectedWorkingSets();
400: removeSelectedWorkingSets(selection);
401: }
402:
403: /**
404: * Remove the working sets contained in the provided selection from the
405: * working set manager.
406: *
407: * @param selection
408: * the sets
409: */
410: protected void removeSelectedWorkingSets(List selection) {
411: IWorkingSetManager manager = WorkbenchPlugin.getDefault()
412: .getWorkingSetManager();
413: Iterator iter = selection.iterator();
414: while (iter.hasNext()) {
415: IWorkingSet workingSet = (IWorkingSet) iter.next();
416: if (getAddedWorkingSets().contains(workingSet)) {
417: getAddedWorkingSets().remove(workingSet);
418: } else {
419: IWorkingSet[] recentWorkingSets = manager
420: .getRecentWorkingSets();
421: for (int i = 0; i < recentWorkingSets.length; i++) {
422: if (workingSet.equals(recentWorkingSets[i])) {
423: getRemovedMRUWorkingSets().add(workingSet);
424: break;
425: }
426: }
427: getRemovedWorkingSets().add(workingSet);
428: }
429: manager.removeWorkingSet(workingSet);
430: }
431: availableWorkingSetsChanged();
432: }
433: }
|