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: *******************************************************************************/package org.eclipse.ui.internal.dialogs;
011:
012: import org.eclipse.jface.dialogs.IDialogConstants;
013: import org.eclipse.jface.dialogs.MessageDialog;
014: import org.eclipse.jface.viewers.ISelectionChangedListener;
015: import org.eclipse.jface.viewers.IStructuredSelection;
016: import org.eclipse.jface.viewers.SelectionChangedEvent;
017: import org.eclipse.jface.viewers.StructuredSelection;
018: import org.eclipse.jface.viewers.TableViewer;
019: import org.eclipse.jface.viewers.ViewerComparator;
020: import org.eclipse.osgi.util.NLS;
021: import org.eclipse.swt.SWT;
022: import org.eclipse.swt.events.ModifyListener;
023: import org.eclipse.swt.graphics.Font;
024: import org.eclipse.swt.layout.GridData;
025: import org.eclipse.swt.layout.GridLayout;
026: import org.eclipse.swt.widgets.Button;
027: import org.eclipse.swt.widgets.Composite;
028: import org.eclipse.swt.widgets.Control;
029: import org.eclipse.swt.widgets.Label;
030: import org.eclipse.swt.widgets.Shell;
031: import org.eclipse.swt.widgets.Text;
032: import org.eclipse.ui.IPerspectiveDescriptor;
033: import org.eclipse.ui.PlatformUI;
034: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
035: import org.eclipse.ui.internal.WorkbenchMessages;
036: import org.eclipse.ui.internal.activities.ws.ActivityViewerFilter;
037: import org.eclipse.ui.internal.registry.PerspectiveRegistry;
038: import org.eclipse.ui.model.PerspectiveLabelProvider;
039:
040: /**
041: * The SavePerspectiveDialog can be used to get the name of a new
042: * perspective or the descriptor of an old perspective. The results
043: * are returned by <code>getNewPerspName</code> and
044: * <code>getOldPersp</code>.
045: */
046: public class SavePerspectiveDialog extends
047: org.eclipse.jface.dialogs.Dialog implements
048: ISelectionChangedListener, ModifyListener {
049: private Text text;
050:
051: private TableViewer list;
052:
053: private Button okButton;
054:
055: private PerspectiveRegistry perspReg;
056:
057: private String perspName;
058:
059: private IPerspectiveDescriptor persp;
060:
061: private IPerspectiveDescriptor initialSelection;
062:
063: private boolean ignoreSelection = false;
064:
065: final private static int LIST_WIDTH = 40;
066:
067: final private static int TEXT_WIDTH = 40;
068:
069: final private static int LIST_HEIGHT = 14;
070:
071: /**
072: * PerspectiveDialog constructor comment.
073: * @param parentShell the parent shell
074: * @param perspReg the perspective registry
075: */
076: public SavePerspectiveDialog(Shell parentShell,
077: PerspectiveRegistry perspReg) {
078: super (parentShell);
079: this .perspReg = perspReg;
080: }
081:
082: /* (non-Javadoc)
083: * Method declared in Window.
084: */
085: protected void configureShell(Shell shell) {
086: super .configureShell(shell);
087: shell.setText(WorkbenchMessages.SavePerspective_shellTitle);
088: PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
089: IWorkbenchHelpContextIds.SAVE_PERSPECTIVE_DIALOG);
090: }
091:
092: /**
093: * Add buttons to the dialog's button bar.
094: *
095: * @param parent the button bar composite
096: */
097: protected void createButtonsForButtonBar(Composite parent) {
098: okButton = createButton(parent, IDialogConstants.OK_ID,
099: IDialogConstants.OK_LABEL, true);
100: createButton(parent, IDialogConstants.CANCEL_ID,
101: IDialogConstants.CANCEL_LABEL, false);
102: updateButtons();
103: text.setFocus();
104: }
105:
106: /**
107: * Creates and returns the contents of the upper part
108: * of this dialog (above the button bar).
109: *
110: * @param parent the parent composite to contain the dialog area
111: * @return the dialog area control
112: */
113: protected Control createDialogArea(Composite parent) {
114: Font font = parent.getFont();
115: // Run super.
116: Composite composite = (Composite) super
117: .createDialogArea(parent);
118:
119: // description
120: Label descLabel = new Label(composite, SWT.WRAP);
121: descLabel
122: .setText(WorkbenchMessages.SavePerspectiveDialog_description);
123: descLabel.setFont(parent.getFont());
124:
125: // Spacer.
126: Label label = new Label(composite, SWT.NONE);
127: GridData data = new GridData();
128: data.heightHint = 8;
129: label.setLayoutData(data);
130:
131: // Create name group.
132: Composite nameGroup = new Composite(composite, SWT.NONE);
133: nameGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
134: GridLayout layout = new GridLayout();
135: layout.numColumns = 2;
136: layout.marginWidth = layout.marginHeight = 0;
137: nameGroup.setLayout(layout);
138:
139: // Create name label.
140: label = new Label(nameGroup, SWT.NONE);
141: label.setText(WorkbenchMessages.SavePerspective_name);
142: label.setFont(font);
143:
144: // Add text field.
145: text = new Text(nameGroup, SWT.BORDER);
146: text.setFocus();
147: data = new GridData(GridData.FILL_HORIZONTAL);
148: data.widthHint = convertWidthInCharsToPixels(TEXT_WIDTH);
149: text.setLayoutData(data);
150: text.setFont(font);
151: text.addModifyListener(this );
152:
153: // Spacer.
154: label = new Label(composite, SWT.NONE);
155: data = new GridData();
156: data.heightHint = 5;
157: label.setLayoutData(data);
158:
159: // Another label.
160: label = new Label(composite, SWT.NONE);
161: label.setText(WorkbenchMessages.SavePerspective_existing);
162: label.setFont(font);
163:
164: // Add perspective list.
165: list = new TableViewer(composite, SWT.H_SCROLL | SWT.V_SCROLL
166: | SWT.BORDER);
167: list.setLabelProvider(new PerspectiveLabelProvider());
168: list.setContentProvider(new PerspContentProvider());
169: list.addFilter(new ActivityViewerFilter());
170: list.setComparator(new ViewerComparator());
171: list.setInput(perspReg);
172: list.addSelectionChangedListener(this );
173: list.getTable().setFont(font);
174:
175: // Set perspective list size.
176: Control ctrl = list.getControl();
177: GridData spec = new GridData(GridData.FILL_BOTH);
178: spec.widthHint = convertWidthInCharsToPixels(LIST_WIDTH);
179: spec.heightHint = convertHeightInCharsToPixels(LIST_HEIGHT);
180: ctrl.setLayoutData(spec);
181:
182: // Set the initial selection
183: if (initialSelection != null) {
184: StructuredSelection sel = new StructuredSelection(
185: initialSelection);
186: list.setSelection(sel, true);
187: }
188: text.selectAll();
189:
190: // Return results.
191: return composite;
192: }
193:
194: /**
195: * Returns the target name.
196: *
197: * @return the target name
198: */
199: public IPerspectiveDescriptor getPersp() {
200: return persp;
201: }
202:
203: /**
204: * Returns the target name.
205: *
206: * @return the target name
207: */
208: public String getPerspName() {
209: return perspName;
210: }
211:
212: /**
213: * The user has typed some text.
214: */
215: public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
216: // Get text.
217: perspName = text.getText();
218:
219: // Transfer text to persp list.
220: ignoreSelection = true;
221: persp = perspReg.findPerspectiveWithLabel(perspName);
222: if (persp == null) {
223: StructuredSelection sel = new StructuredSelection();
224: list.setSelection(sel);
225: } else {
226: StructuredSelection sel = new StructuredSelection(persp);
227: list.setSelection(sel);
228: }
229: ignoreSelection = false;
230:
231: updateButtons();
232: }
233:
234: /**
235: * Notifies that the ok button of this dialog has been pressed.
236: * <p>
237: * The default implementation of this framework method sets
238: * this dialog's return code to <code>Window.OK</code>
239: * and closes the dialog. Subclasses may override.
240: * </p>
241: */
242: protected void okPressed() {
243: perspName = text.getText();
244: persp = perspReg.findPerspectiveWithLabel(perspName);
245: if (persp != null) {
246: // Confirm ok to overwrite
247: String message = NLS
248: .bind(
249: WorkbenchMessages.SavePerspective_overwriteQuestion,
250: perspName);
251: String[] buttons = new String[] {
252: IDialogConstants.YES_LABEL,
253: IDialogConstants.NO_LABEL,
254: IDialogConstants.CANCEL_LABEL };
255: MessageDialog d = new MessageDialog(this .getShell(),
256: WorkbenchMessages.SavePerspective_overwriteTitle,
257: null, message, MessageDialog.QUESTION, buttons, 0);
258:
259: switch (d.open()) {
260: case 0: //yes
261: break;
262: case 1: //no
263: return;
264: case 2: //cancel
265: cancelPressed();
266: return;
267: default:
268: return;
269: }
270: }
271:
272: super .okPressed();
273: }
274:
275: /**
276: * Notifies that the selection has changed.
277: *
278: * @param event event object describing the change
279: */
280: public void selectionChanged(SelectionChangedEvent event) {
281: // If a selection is caused by modifyText ignore it.
282: if (ignoreSelection) {
283: return;
284: }
285:
286: // Get selection.
287: IStructuredSelection sel = (IStructuredSelection) list
288: .getSelection();
289: persp = null;
290: if (!sel.isEmpty()) {
291: persp = (IPerspectiveDescriptor) sel.getFirstElement();
292: }
293:
294: // Transfer selection to text field.
295: if (persp != null) {
296: perspName = persp.getLabel();
297: text.setText(perspName);
298: }
299:
300: updateButtons();
301: }
302:
303: /**
304: * Sets the initial selection in this dialog.
305: *
306: * @param selectedElement the perspective descriptor to select
307: */
308: public void setInitialSelection(
309: IPerspectiveDescriptor selectedElement) {
310: initialSelection = selectedElement;
311: }
312:
313: /**
314: * Update the OK button.
315: */
316: private void updateButtons() {
317: if (okButton != null) {
318: String label = text.getText();
319: okButton.setEnabled(perspReg.validateLabel(label));
320: }
321: }
322: }
|