KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > ChooseWorkspaceDialog


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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.ide;
12
13 import java.io.File JavaDoc;
14
15 import org.eclipse.core.runtime.IProduct;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.jface.dialogs.IDialogConstants;
19 import org.eclipse.jface.dialogs.IDialogSettings;
20 import org.eclipse.jface.dialogs.TitleAreaDialog;
21 import org.eclipse.jface.util.Geometry;
22 import org.eclipse.jface.window.Window;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.osgi.util.TextProcessor;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.ModifyEvent;
27 import org.eclipse.swt.events.ModifyListener;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.graphics.Point;
31 import org.eclipse.swt.graphics.Rectangle;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Combo;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.DirectoryDialog;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.Monitor;
41 import org.eclipse.swt.widgets.Shell;
42
43 /**
44  * A dialog that prompts for a directory to use as a workspace.
45  */

46 public class ChooseWorkspaceDialog extends TitleAreaDialog {
47     
48     private static final String JavaDoc DIALOG_SETTINGS_SECTION = "ChooseWorkspaceDialogSettings"; //$NON-NLS-1$
49

50     private ChooseWorkspaceData launchData;
51
52     private Combo text;
53
54     private boolean suppressAskAgain = false;
55
56     private boolean centerOnMonitor = false;
57     /**
58      * Create a modal dialog on the arugment shell, using and updating the
59      * argument data object.
60      * @param parentShell the parent shell for this dialog
61      * @param launchData the launch data from past launches
62      *
63      * @param suppressAskAgain
64      * true means the dialog will not have a "don't ask again" button
65      * @param centerOnMonitor indicates whether the dialog should be centered on
66      * the monitor or according to it's parent if there is one
67      */

68     public ChooseWorkspaceDialog(Shell parentShell,
69             ChooseWorkspaceData launchData, boolean suppressAskAgain, boolean centerOnMonitor) {
70         super(parentShell);
71         this.launchData = launchData;
72         this.suppressAskAgain = suppressAskAgain;
73         this.centerOnMonitor = centerOnMonitor;
74     }
75
76     /**
77      * Show the dialog to the user (if needed). When this method finishes,
78      * #getSelection will return the workspace that should be used (whether it
79      * was just selected by the user or some previous default has been used.
80      * The parameter can be used to override the users preference. For example,
81      * this is important in cases where the default selection is already in use
82      * and the user is forced to choose a different one.
83      *
84      * @param force
85      * true if the dialog should be opened regardless of the value of
86      * the show dialog checkbox
87      */

88     public void prompt(boolean force) {
89         if (force || launchData.getShowDialog()) {
90             open();
91
92             // 70576: make sure dialog gets dismissed on ESC too
93
if (getReturnCode() == CANCEL) {
94                 launchData.workspaceSelected(null);
95             }
96
97             return;
98         }
99
100         String JavaDoc[] recent = launchData.getRecentWorkspaces();
101
102         // If the selection dialog was not used then the workspace to use is either the
103
// most recent selection or the initialDefault (if there is no history).
104
String JavaDoc workspace = null;
105         if (recent != null && recent.length > 0) {
106             workspace = recent[0];
107         }
108         if (workspace == null || workspace.length() == 0) {
109             workspace = launchData.getInitialDefault();
110         }
111         launchData.workspaceSelected(TextProcessor.deprocess(workspace));
112     }
113
114     /**
115      * Creates and returns the contents of the upper part of this dialog (above
116      * the button bar).
117      * <p>
118      * The <code>Dialog</code> implementation of this framework method creates
119      * and returns a new <code>Composite</code> with no margins and spacing.
120      * </p>
121      *
122      * @param parent the parent composite to contain the dialog area
123      * @return the dialog area control
124      */

125     protected Control createDialogArea(Composite parent) {
126         String JavaDoc productName = null;
127         IProduct product = Platform.getProduct();
128         if (product != null) {
129             productName = product.getName();
130         }
131         if (productName == null) {
132             productName = IDEWorkbenchMessages.ChooseWorkspaceDialog_defaultProductName;
133         }
134
135         Composite composite = (Composite) super.createDialogArea(parent);
136         setTitle(IDEWorkbenchMessages.ChooseWorkspaceDialog_dialogTitle);
137         setMessage(NLS.bind(IDEWorkbenchMessages.ChooseWorkspaceDialog_dialogMessage, productName));
138
139         // bug 59934: load title image for sizing, but set it non-visible so the
140
// white background is displayed
141
if (getTitleImageLabel() != null) {
142             getTitleImageLabel().setVisible(false);
143         }
144
145         createWorkspaceBrowseRow(composite);
146         if (!suppressAskAgain) {
147             createShowDialogButton(composite);
148         }
149         Dialog.applyDialogFont(composite);
150         return composite;
151     }
152
153     /**
154      * Configures the given shell in preparation for opening this window
155      * in it.
156      * <p>
157      * The default implementation of this framework method
158      * sets the shell's image and gives it a grid layout.
159      * Subclasses may extend or reimplement.
160      * </p>
161      *
162      * @param shell the shell
163      */

164     protected void configureShell(Shell shell) {
165         super.configureShell(shell);
166         shell.setText(IDEWorkbenchMessages.ChooseWorkspaceDialog_dialogName);
167     }
168
169     /**
170      * Notifies that the ok button of this dialog has been pressed.
171      * <p>
172      * The <code>Dialog</code> implementation of this framework method sets
173      * this dialog's return code to <code>Window.OK</code>
174      * and closes the dialog. Subclasses may override.
175      * </p>
176      */

177     protected void okPressed() {
178         launchData.workspaceSelected(TextProcessor.deprocess(getWorkspaceLocation()));
179         super.okPressed();
180     }
181
182     /**
183      * Get the workspace location from the widget.
184      * @return String
185      */

186     protected String JavaDoc getWorkspaceLocation() {
187         return text.getText();
188     }
189
190     /**
191      * Notifies that the cancel button of this dialog has been pressed.
192      * <p>
193      * The <code>Dialog</code> implementation of this framework method sets
194      * this dialog's return code to <code>Window.CANCEL</code>
195      * and closes the dialog. Subclasses may override if desired.
196      * </p>
197      */

198     protected void cancelPressed() {
199         launchData.workspaceSelected(null);
200         super.cancelPressed();
201     }
202
203     /**
204      * The main area of the dialog is just a row with the current selection
205      * information and a drop-down of the most recently used workspaces.
206      */

207     private void createWorkspaceBrowseRow(Composite parent) {
208         Composite panel = new Composite(parent, SWT.NONE);
209
210         GridLayout layout = new GridLayout(3, false);
211         layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
212         layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
213         layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
214         layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
215         panel.setLayout(layout);
216         panel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
217         panel.setFont(parent.getFont());
218
219         Label label = new Label(panel, SWT.NONE);
220         label.setText(IDEWorkbenchMessages.ChooseWorkspaceDialog_workspaceEntryLabel);
221
222         text = new Combo(panel, SWT.BORDER | SWT.LEAD | SWT.DROP_DOWN);
223         text.setFocus();
224         text.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
225                 | GridData.FILL_HORIZONTAL));
226         text.addModifyListener(new ModifyListener(){
227             public void modifyText(ModifyEvent e) {
228                 Button okButton = getButton(Window.OK);
229                 if(okButton != null && !okButton.isDisposed()) {
230                     boolean nonWhitespaceFound = false;
231                     String JavaDoc characters = getWorkspaceLocation();
232                     for (int i = 0; !nonWhitespaceFound
233                             && i < characters.length(); i++) {
234                         if (!Character.isWhitespace(characters.charAt(i))) {
235                             nonWhitespaceFound = true;
236                         }
237                     }
238                     okButton.setEnabled(nonWhitespaceFound);
239                 }
240             }
241         });
242         setInitialTextValues(text);
243
244         Button browseButton = new Button(panel, SWT.PUSH);
245         browseButton.setText(IDEWorkbenchMessages.ChooseWorkspaceDialog_browseLabel);
246         setButtonLayoutData(browseButton);
247         GridData data = (GridData) browseButton.getLayoutData();
248         data.horizontalAlignment = GridData.HORIZONTAL_ALIGN_END;
249         browseButton.setLayoutData(data);
250         browseButton.addSelectionListener(new SelectionAdapter() {
251             public void widgetSelected(SelectionEvent e) {
252                 DirectoryDialog dialog = new DirectoryDialog(getShell());
253                 dialog.setText(IDEWorkbenchMessages.ChooseWorkspaceDialog_directoryBrowserTitle);
254                 dialog.setMessage(IDEWorkbenchMessages.ChooseWorkspaceDialog_directoryBrowserMessage);
255                 dialog.setFilterPath(getInitialBrowsePath());
256                 String JavaDoc dir = dialog.open();
257                 if (dir != null) {
258                     text.setText(TextProcessor.process(dir));
259                 }
260             }
261         });
262     }
263
264     /**
265      * Return a string containing the path that is closest to the current
266      * selection in the text widget. This starts with the current value and
267      * works toward the root until there is a directory for which File.exists
268      * returns true. Return the current working dir if the text box does not
269      * contain a valid path.
270      *
271      * @return closest parent that exists or an empty string
272      */

273     private String JavaDoc getInitialBrowsePath() {
274         File JavaDoc dir = new File JavaDoc(getWorkspaceLocation());
275         while (dir != null && !dir.exists()) {
276             dir = dir.getParentFile();
277         }
278
279         return dir != null ? dir.getAbsolutePath() : System
280                 .getProperty("user.dir"); //$NON-NLS-1$
281
}
282
283     /*
284      * see org.eclipse.jface.Window.getInitialLocation()
285      */

286     protected Point getInitialLocation(Point initialSize) {
287         Composite parent = getShell().getParent();
288         
289         if (!centerOnMonitor || parent == null) {
290             return super.getInitialLocation(initialSize);
291         }
292
293         Monitor monitor = parent.getMonitor();
294         Rectangle monitorBounds = monitor.getClientArea();
295         Point centerPoint = Geometry.centerPoint(monitorBounds);
296
297         return new Point(centerPoint.x - (initialSize.x / 2), Math.max(
298                 monitorBounds.y, Math.min(centerPoint.y
299                         - (initialSize.y * 2 / 3), monitorBounds.y
300                         + monitorBounds.height - initialSize.y)));
301     }
302
303     /**
304      * The show dialog button allows the user to choose to neven be nagged again.
305      */

306     private void createShowDialogButton(Composite parent) {
307         Composite panel = new Composite(parent, SWT.NONE);
308         panel.setFont(parent.getFont());
309
310         GridLayout layout = new GridLayout(1, false);
311         layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
312         panel.setLayout(layout);
313
314         GridData data = new GridData(GridData.FILL_BOTH);
315         data.verticalAlignment = GridData.END;
316         panel.setLayoutData(data);
317
318         Button button = new Button(panel, SWT.CHECK);
319         button.setText(IDEWorkbenchMessages.ChooseWorkspaceDialog_useDefaultMessage);
320         button.setSelection(!launchData.getShowDialog());
321         button.addSelectionListener(new SelectionAdapter() {
322             public void widgetSelected(SelectionEvent e) {
323                 launchData.toggleShowDialog();
324             }
325         });
326     }
327
328     private void setInitialTextValues(Combo text) {
329         String JavaDoc[] recentWorkspaces = launchData.getRecentWorkspaces();
330         for (int i = 0; i < recentWorkspaces.length; ++i) {
331             if (recentWorkspaces[i] != null) {
332                 text.add(recentWorkspaces[i]);
333             }
334         }
335
336         text.setText(TextProcessor.process((text.getItemCount() > 0 ? text
337                 .getItem(0) : launchData.getInitialDefault())));
338     }
339     
340     /* (non-Javadoc)
341      * @see org.eclipse.jface.window.Dialog#getDialogBoundsSettings()
342      *
343      * @since 3.2
344      */

345     protected IDialogSettings getDialogBoundsSettings() {
346         // If we were explicitly instructed to center on the monitor, then
347
// do not provide any settings for retrieving a different location or, worse,
348
// saving the centered location.
349
if (centerOnMonitor) {
350             return null;
351         }
352         
353         IDialogSettings settings = IDEWorkbenchPlugin.getDefault().getDialogSettings();
354         IDialogSettings section = settings.getSection(DIALOG_SETTINGS_SECTION);
355         if (section == null) {
356             section = settings.addNewSection(DIALOG_SETTINGS_SECTION);
357         }
358         return section;
359     }
360
361 }
362
Popular Tags