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 should be
011: * activated and used by other components.
012: *******************************************************************************/package org.eclipse.ui.internal.dialogs;
013:
014: import org.eclipse.jface.dialogs.Dialog;
015: import org.eclipse.jface.dialogs.IDialogConstants;
016: import org.eclipse.jface.viewers.DoubleClickEvent;
017: import org.eclipse.jface.viewers.IDoubleClickListener;
018: import org.eclipse.jface.viewers.ISelectionChangedListener;
019: import org.eclipse.jface.viewers.IStructuredSelection;
020: import org.eclipse.jface.viewers.SelectionChangedEvent;
021: import org.eclipse.jface.viewers.TableViewer;
022: import org.eclipse.jface.viewers.ViewerComparator;
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.events.SelectionAdapter;
025: import org.eclipse.swt.events.SelectionEvent;
026: import org.eclipse.swt.layout.GridData;
027: import org.eclipse.swt.widgets.Button;
028: import org.eclipse.swt.widgets.Composite;
029: import org.eclipse.swt.widgets.Control;
030: import org.eclipse.swt.widgets.Shell;
031: import org.eclipse.ui.IPerspectiveDescriptor;
032: import org.eclipse.ui.IPerspectiveRegistry;
033: import org.eclipse.ui.PlatformUI;
034: import org.eclipse.ui.activities.ITriggerPoint;
035: import org.eclipse.ui.activities.WorkbenchActivityHelper;
036: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
037: import org.eclipse.ui.internal.WorkbenchMessages;
038: import org.eclipse.ui.internal.activities.ws.ActivityMessages;
039: import org.eclipse.ui.internal.activities.ws.ActivityViewerFilter;
040: import org.eclipse.ui.internal.activities.ws.WorkbenchTriggerPoints;
041: import org.eclipse.ui.model.PerspectiveLabelProvider;
042:
043: /**
044: * A dialog for perspective creation
045: */
046: public class SelectPerspectiveDialog extends Dialog implements
047: ISelectionChangedListener {
048:
049: final private static int LIST_HEIGHT = 300;
050:
051: final private static int LIST_WIDTH = 300;
052:
053: private TableViewer list;
054:
055: private Button okButton;
056:
057: private IPerspectiveDescriptor perspDesc;
058:
059: private IPerspectiveRegistry perspReg;
060:
061: private ActivityViewerFilter activityViewerFilter = new ActivityViewerFilter();
062:
063: private Button showAllButton;
064:
065: /**
066: * PerspectiveDialog constructor comment.
067: *
068: * @param parentShell the parent shell
069: * @param perspReg the perspective registry
070: */
071: public SelectPerspectiveDialog(Shell parentShell,
072: IPerspectiveRegistry perspReg) {
073: super (parentShell);
074: this .perspReg = perspReg;
075: }
076:
077: /*
078: * (non-Javadoc)
079: *
080: * @see org.eclipse.jface.dialogs.Dialog#cancelPressed()
081: */
082: protected void cancelPressed() {
083: perspDesc = null;
084: super .cancelPressed();
085: }
086:
087: /*
088: * (non-Javadoc)
089: *
090: * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
091: */
092: protected void configureShell(Shell shell) {
093: super .configureShell(shell);
094: shell.setText(WorkbenchMessages.SelectPerspective_shellTitle);
095: PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
096: IWorkbenchHelpContextIds.SELECT_PERSPECTIVE_DIALOG);
097: }
098:
099: /**
100: * Adds buttons to this dialog's button bar.
101: * <p>
102: * The default implementation of this framework method adds standard ok and
103: * cancel buttons using the <code>createButton</code> framework method.
104: * Subclasses may override.
105: * </p>
106: *
107: * @param parent the button bar composite
108: */
109: protected void createButtonsForButtonBar(Composite parent) {
110: okButton = createButton(parent, IDialogConstants.OK_ID,
111: IDialogConstants.OK_LABEL, true);
112: createButton(parent, IDialogConstants.CANCEL_ID,
113: IDialogConstants.CANCEL_LABEL, false);
114: updateButtons();
115: }
116:
117: /**
118: * Creates and returns the contents of the upper part of this dialog (above
119: * the button bar).
120: *
121: * @param parent the parent composite to contain the dialog area
122: * @return the dialog area control
123: */
124: protected Control createDialogArea(Composite parent) {
125: // Run super.
126: Composite composite = (Composite) super
127: .createDialogArea(parent);
128: composite.setFont(parent.getFont());
129:
130: createViewer(composite);
131: layoutTopControl(list.getControl());
132: if (needsShowAllButton()) {
133: createShowAllButton(composite);
134: }
135: // Return results.
136: return composite;
137: }
138:
139: /**
140: * @return whether a show-all button is needed. A show all button is needed only if the list contains filtered items.
141: */
142: private boolean needsShowAllButton() {
143: return activityViewerFilter.getHasEncounteredFilteredItem();
144: }
145:
146: /**
147: * Create a show all button in the parent.
148: *
149: * @param parent the parent <code>Composite</code>.
150: */
151: private void createShowAllButton(Composite parent) {
152: showAllButton = new Button(parent, SWT.CHECK);
153: showAllButton.setText(ActivityMessages.Perspective_showAll);
154: showAllButton.addSelectionListener(new SelectionAdapter() {
155:
156: /* (non-Javadoc)
157: * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
158: */
159: public void widgetSelected(SelectionEvent e) {
160: if (showAllButton.getSelection()) {
161: list.resetFilters();
162: } else {
163: list.addFilter(activityViewerFilter);
164: }
165: }
166: });
167:
168: }
169:
170: /**
171: * Create a new viewer in the parent.
172: *
173: * @param parent the parent <code>Composite</code>.
174: */
175: private void createViewer(Composite parent) {
176: // Add perspective list.
177: list = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL
178: | SWT.V_SCROLL | SWT.BORDER);
179: list.getTable().setFont(parent.getFont());
180: list.setLabelProvider(new PerspectiveLabelProvider());
181: list.setContentProvider(new PerspContentProvider());
182: list.addFilter(activityViewerFilter);
183: list.setComparator(new ViewerComparator());
184: list.setInput(perspReg);
185: list.addSelectionChangedListener(this );
186: list.addDoubleClickListener(new IDoubleClickListener() {
187: public void doubleClick(DoubleClickEvent event) {
188: handleDoubleClickEvent();
189: }
190: });
191: }
192:
193: /**
194: * Returns the current selection.
195: *
196: * @return the current selection
197: */
198: public IPerspectiveDescriptor getSelection() {
199: return perspDesc;
200: }
201:
202: /**
203: * Handle a double click event on the list
204: */
205: protected void handleDoubleClickEvent() {
206: okPressed();
207: }
208:
209: /**
210: * Layout the top control.
211: *
212: * @param control the control.
213: */
214: private void layoutTopControl(Control control) {
215: GridData spec = new GridData(GridData.FILL_BOTH);
216: spec.widthHint = LIST_WIDTH;
217: spec.heightHint = LIST_HEIGHT;
218: control.setLayoutData(spec);
219: }
220:
221: /**
222: * Notifies that the selection has changed.
223: *
224: * @param event event object describing the change
225: */
226: public void selectionChanged(SelectionChangedEvent event) {
227: updateSelection(event);
228: updateButtons();
229: }
230:
231: /**
232: * Update the button enablement state.
233: */
234: protected void updateButtons() {
235: okButton.setEnabled(getSelection() != null);
236: }
237:
238: /**
239: * Update the selection object.
240: */
241: protected void updateSelection(SelectionChangedEvent event) {
242: perspDesc = null;
243: IStructuredSelection sel = (IStructuredSelection) event
244: .getSelection();
245: if (!sel.isEmpty()) {
246: Object obj = sel.getFirstElement();
247: if (obj instanceof IPerspectiveDescriptor) {
248: perspDesc = (IPerspectiveDescriptor) obj;
249: }
250: }
251: }
252:
253: /* (non-Javadoc)
254: * @see org.eclipse.jface.dialogs.Dialog#okPressed()
255: */
256: protected void okPressed() {
257: ITriggerPoint triggerPoint = PlatformUI.getWorkbench()
258: .getActivitySupport().getTriggerPointManager()
259: .getTriggerPoint(
260: WorkbenchTriggerPoints.OPEN_PERSPECITVE_DIALOG);
261: if (WorkbenchActivityHelper.allowUseOf(triggerPoint,
262: getSelection())) {
263: super .okPressed();
264: }
265: }
266:
267: /*
268: * (non-Javadoc)
269: * @see org.eclipse.jface.dialogs.Dialog#isResizable()
270: */
271: protected boolean isResizable() {
272: return true;
273: }
274: }
|