KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > SavePerspectiveDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.dialogs;
12
13 import org.eclipse.jface.dialogs.IDialogConstants;
14 import org.eclipse.jface.dialogs.MessageDialog;
15 import org.eclipse.jface.viewers.ISelectionChangedListener;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jface.viewers.SelectionChangedEvent;
18 import org.eclipse.jface.viewers.StructuredSelection;
19 import org.eclipse.jface.viewers.TableViewer;
20 import org.eclipse.jface.viewers.ViewerComparator;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.graphics.Font;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.swt.widgets.Text;
33 import org.eclipse.ui.IPerspectiveDescriptor;
34 import org.eclipse.ui.PlatformUI;
35 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
36 import org.eclipse.ui.internal.WorkbenchMessages;
37 import org.eclipse.ui.internal.activities.ws.ActivityViewerFilter;
38 import org.eclipse.ui.internal.registry.PerspectiveRegistry;
39 import org.eclipse.ui.model.PerspectiveLabelProvider;
40
41 /**
42  * The SavePerspectiveDialog can be used to get the name of a new
43  * perspective or the descriptor of an old perspective. The results
44  * are returned by <code>getNewPerspName</code> and
45  * <code>getOldPersp</code>.
46  */

47 public class SavePerspectiveDialog extends org.eclipse.jface.dialogs.Dialog
48         implements ISelectionChangedListener, ModifyListener {
49     private Text text;
50
51     private TableViewer list;
52
53     private Button okButton;
54
55     private PerspectiveRegistry perspReg;
56
57     private String JavaDoc perspName;
58
59     private IPerspectiveDescriptor persp;
60
61     private IPerspectiveDescriptor initialSelection;
62
63     private boolean ignoreSelection = false;
64
65     final private static int LIST_WIDTH = 40;
66
67     final private static int TEXT_WIDTH = 40;
68
69     final private static int LIST_HEIGHT = 14;
70
71     /**
72      * PerspectiveDialog constructor comment.
73      * @param parentShell the parent shell
74      * @param perspReg the perspective registry
75      */

76     public SavePerspectiveDialog(Shell parentShell, PerspectiveRegistry perspReg) {
77         super(parentShell);
78         this.perspReg = perspReg;
79     }
80
81     /* (non-Javadoc)
82      * Method declared in Window.
83      */

84     protected void configureShell(Shell shell) {
85         super.configureShell(shell);
86         shell
87                 .setText(WorkbenchMessages.SavePerspective_shellTitle);
88         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
89                 IWorkbenchHelpContextIds.SAVE_PERSPECTIVE_DIALOG);
90     }
91
92     /**
93      * Add buttons to the dialog's button bar.
94      *
95      * @param parent the button bar composite
96      */

97     protected void createButtonsForButtonBar(Composite parent) {
98         okButton = createButton(parent, IDialogConstants.OK_ID,
99                 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.createDialogArea(parent);
117
118         // description
119
Label descLabel = new Label(composite, SWT.WRAP);
120         descLabel.setText(WorkbenchMessages.SavePerspectiveDialog_description);
121         descLabel.setFont(parent.getFont());
122
123         // Spacer.
124
Label label = new Label(composite, SWT.NONE);
125         GridData data = new GridData();
126         data.heightHint = 8;
127         label.setLayoutData(data);
128
129         // Create name group.
130
Composite nameGroup = new Composite(composite, SWT.NONE);
131         nameGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
132         GridLayout layout = new GridLayout();
133         layout.numColumns = 2;
134         layout.marginWidth = layout.marginHeight = 0;
135         nameGroup.setLayout(layout);
136
137         // Create name label.
138
label = new Label(nameGroup, SWT.NONE);
139         label.setText(WorkbenchMessages.SavePerspective_name);
140         label.setFont(font);
141
142         // Add text field.
143
text = new Text(nameGroup, SWT.BORDER);
144         text.setFocus();
145         data = new GridData(GridData.FILL_HORIZONTAL);
146         data.widthHint = convertWidthInCharsToPixels(TEXT_WIDTH);
147         text.setLayoutData(data);
148         text.setFont(font);
149         text.addModifyListener(this);
150
151         // Spacer.
152
label = new Label(composite, SWT.NONE);
153         data = new GridData();
154         data.heightHint = 5;
155         label.setLayoutData(data);
156
157         // Another label.
158
label = new Label(composite, SWT.NONE);
159         label.setText(WorkbenchMessages.SavePerspective_existing);
160         label.setFont(font);
161
162         // Add perspective list.
163
list = new TableViewer(composite, SWT.H_SCROLL | SWT.V_SCROLL
164                 | SWT.BORDER);
165         list.setLabelProvider(new PerspectiveLabelProvider());
166         list.setContentProvider(new PerspContentProvider());
167         list.addFilter(new ActivityViewerFilter());
168         list.setComparator(new ViewerComparator());
169         list.setInput(perspReg);
170         list.addSelectionChangedListener(this);
171         list.getTable().setFont(font);
172
173         // Set perspective list size.
174
Control ctrl = list.getControl();
175         GridData spec = new GridData(GridData.FILL_BOTH);
176         spec.widthHint = convertWidthInCharsToPixels(LIST_WIDTH);
177         spec.heightHint = convertHeightInCharsToPixels(LIST_HEIGHT);
178         ctrl.setLayoutData(spec);
179
180         // Set the initial selection
181
if (initialSelection != null) {
182             StructuredSelection sel = new StructuredSelection(initialSelection);
183             list.setSelection(sel, true);
184         }
185         text.selectAll();
186
187         // Return results.
188
return composite;
189     }
190
191     /**
192      * Returns the target name.
193      *
194      * @return the target name
195      */

196     public IPerspectiveDescriptor getPersp() {
197         return persp;
198     }
199
200     /**
201      * Returns the target name.
202      *
203      * @return the target name
204      */

205     public String JavaDoc getPerspName() {
206         return perspName;
207     }
208
209     /**
210      * The user has typed some text.
211      */

212     public void modifyText(org.eclipse.swt.events.ModifyEvent e) {
213         // Get text.
214
perspName = text.getText();
215
216         // Transfer text to persp list.
217
ignoreSelection = true;
218         persp = perspReg.findPerspectiveWithLabel(perspName);
219         if (persp == null) {
220             StructuredSelection sel = new StructuredSelection();
221             list.setSelection(sel);
222         } else {
223             StructuredSelection sel = new StructuredSelection(persp);
224             list.setSelection(sel);
225         }
226         ignoreSelection = false;
227
228         updateButtons();
229     }
230
231     /**
232      * Notifies that the ok button of this dialog has been pressed.
233      * <p>
234      * The default implementation of this framework method sets
235      * this dialog's return code to <code>Window.OK</code>
236      * and closes the dialog. Subclasses may override.
237      * </p>
238      */

239     protected void okPressed() {
240         perspName = text.getText();
241         persp = perspReg.findPerspectiveWithLabel(perspName);
242         if (persp != null) {
243             // Confirm ok to overwrite
244
String JavaDoc message = NLS.bind(WorkbenchMessages.SavePerspective_overwriteQuestion,perspName );
245             String JavaDoc[] buttons = new String JavaDoc[] { IDialogConstants.YES_LABEL,
246                     IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL };
247             MessageDialog d = new MessageDialog(this.getShell(),
248                     WorkbenchMessages.SavePerspective_overwriteTitle,
249                     null, message, MessageDialog.QUESTION, buttons, 0);
250
251             switch (d.open()) {
252             case 0: //yes
253
break;
254             case 1: //no
255
return;
256             case 2: //cancel
257
cancelPressed();
258                 return;
259             default:
260                 return;
261             }
262         }
263
264         super.okPressed();
265     }
266
267     /**
268      * Notifies that the selection has changed.
269      *
270      * @param event event object describing the change
271      */

272     public void selectionChanged(SelectionChangedEvent event) {
273         // If a selection is caused by modifyText ignore it.
274
if (ignoreSelection) {
275             return;
276         }
277
278         // Get selection.
279
IStructuredSelection sel = (IStructuredSelection) list.getSelection();
280         persp = null;
281         if (!sel.isEmpty()) {
282             persp = (IPerspectiveDescriptor) sel.getFirstElement();
283         }
284
285         // Transfer selection to text field.
286
if (persp != null) {
287             perspName = persp.getLabel();
288             text.setText(perspName);
289         }
290
291         updateButtons();
292     }
293
294     /**
295      * Sets the initial selection in this dialog.
296      *
297      * @param selectedElement the perspective descriptor to select
298      */

299     public void setInitialSelection(IPerspectiveDescriptor selectedElement) {
300         initialSelection = selectedElement;
301     }
302
303     /**
304      * Update the OK button.
305      */

306     private void updateButtons() {
307         if (okButton != null) {
308             String JavaDoc label = text.getText();
309             okButton.setEnabled(perspReg.validateLabel(label));
310         }
311     }
312 }
313
Popular Tags