KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 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
12 package org.eclipse.ui.internal.ide;
13
14 import java.util.Collection JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.ISafeRunnable;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.MultiStatus;
24 import org.eclipse.core.runtime.Path;
25 import org.eclipse.core.runtime.SafeRunner;
26 import org.eclipse.core.runtime.Status;
27 import org.eclipse.jface.dialogs.ErrorDialog;
28 import org.eclipse.jface.dialogs.IDialogConstants;
29 import org.eclipse.jface.dialogs.IDialogSettings;
30 import org.eclipse.osgi.util.NLS;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.events.DisposeEvent;
33 import org.eclipse.swt.events.DisposeListener;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.graphics.Rectangle;
37 import org.eclipse.swt.layout.GridData;
38 import org.eclipse.swt.layout.GridLayout;
39 import org.eclipse.swt.widgets.Button;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Control;
42 import org.eclipse.swt.widgets.Shell;
43 import org.eclipse.ui.PlatformUI;
44 import org.eclipse.ui.forms.events.ExpansionEvent;
45 import org.eclipse.ui.forms.events.IExpansionListener;
46 import org.eclipse.ui.forms.widgets.ExpandableComposite;
47 import org.eclipse.ui.forms.widgets.FormToolkit;
48 import org.eclipse.ui.forms.widgets.ScrolledForm;
49 import org.eclipse.ui.internal.WorkbenchPlugin;
50 import org.eclipse.ui.preferences.SettingsTransfer;
51
52 /**
53  * The ChooseWorkspaceWithSettingsDialog is the dialog used to switch workspaces
54  * with an optional settings export.
55  *
56  * @since 3.3
57  *
58  */

59 public class ChooseWorkspaceWithSettingsDialog extends ChooseWorkspaceDialog {
60
61     private static final String JavaDoc WORKBENCH_SETTINGS = "WORKBENCH_SETTINGS"; //$NON-NLS-1$
62
private static final String JavaDoc ENABLED_TRANSFERS = "ENABLED_TRANSFERS"; //$NON-NLS-1$
63

64     /**
65      * The class attribute for a settings transfer.
66      */

67     private static final String JavaDoc ATT_CLASS = "class"; //$NON-NLS-1$
68
/**
69      * The name attribute for the settings transfer.
70      */

71     private static final String JavaDoc ATT_NAME = "name"; //$NON-NLS-1$
72
/**
73      * The id attribute for the settings transfer.
74      */

75     private static final String JavaDoc ATT_ID = "id"; //$NON-NLS-1$
76
private static final String JavaDoc ATT_HELP_CONTEXT = "helpContext"; //$NON-NLS-1$
77

78     private Collection JavaDoc selectedSettings = new HashSet JavaDoc();
79
80     /**
81      * Open a new instance of the receiver.
82      *
83      * @param parentShell
84      * @param launchData
85      * @param suppressAskAgain
86      * @param centerOnMonitor
87      */

88     public ChooseWorkspaceWithSettingsDialog(Shell parentShell,
89             ChooseWorkspaceData launchData, boolean suppressAskAgain,
90             boolean centerOnMonitor) {
91         super(parentShell, launchData, suppressAskAgain, centerOnMonitor);
92     }
93
94     /*
95      * (non-Javadoc)
96      *
97      * @see org.eclipse.ui.internal.ide.ChooseWorkspaceDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
98      */

99     protected Control createDialogArea(Composite parent) {
100         Control top = super.createDialogArea(parent);
101         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
102                 IIDEHelpContextIds.SWITCH_WORKSPACE_ACTION);
103         createSettingsControls((Composite) top);
104         applyDialogFont(parent);
105         return top;
106
107     }
108
109     /**
110      * Create the controls for selecting the controls we are going to export.
111      *
112      * @param workArea
113      */

114     private void createSettingsControls(Composite workArea) {
115         final FormToolkit toolkit = new FormToolkit(workArea.getDisplay());
116         workArea.addDisposeListener(new DisposeListener() {
117
118             public void widgetDisposed(DisposeEvent e) {
119                 toolkit.dispose();
120                 
121             }});
122         final ScrolledForm form = toolkit.createScrolledForm(workArea);
123         form.setBackground(workArea.getBackground());
124         form.getBody().setLayout(new GridLayout());
125
126         GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
127         form.setLayoutData(layoutData);
128         final ExpandableComposite expandable = toolkit
129                 .createExpandableComposite(form.getBody(),
130                         ExpandableComposite.TWISTIE);
131         expandable
132                 .setText(IDEWorkbenchMessages.ChooseWorkspaceWithSettingsDialog_SettingsGroupName);
133         expandable.setBackground(workArea.getBackground());
134         expandable.setLayout(new GridLayout());
135         expandable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
136         expandable.addExpansionListener(new IExpansionListener() {
137
138             boolean notExpanded = true;
139
140             /*
141              * (non-Javadoc)
142              *
143              * @see org.eclipse.ui.forms.events.IExpansionListener#expansionStateChanged(org.eclipse.ui.forms.events.ExpansionEvent)
144              */

145             public void expansionStateChanged(ExpansionEvent e) {
146                 form.reflow(true);
147                 if (e.getState() && notExpanded) {
148                     getShell().setRedraw(false);
149                     Rectangle shellBounds = getShell().getBounds();
150                     int entriesToShow = Math.min(4, SettingsTransfer
151                             .getSettingsTransfers().length);
152
153                     shellBounds.height += convertHeightInCharsToPixels(entriesToShow)
154                             + IDialogConstants.VERTICAL_SPACING;
155                     getShell().setBounds(shellBounds);
156                     getShell().setRedraw(true);
157                     notExpanded = false;
158                 }
159
160             }
161
162             /*
163              * (non-Javadoc)
164              *
165              * @see org.eclipse.ui.forms.events.IExpansionListener#expansionStateChanging(org.eclipse.ui.forms.events.ExpansionEvent)
166              */

167             public void expansionStateChanging(ExpansionEvent e) {
168                 // Nothing to do here
169

170             }
171         });
172
173         Composite sectionClient = toolkit.createComposite(expandable);
174         sectionClient.setLayout(new GridLayout());
175         sectionClient.setBackground(workArea.getBackground());
176
177         if (createButtons(toolkit, sectionClient))
178             expandable.setExpanded(true);
179
180         expandable.setClient(sectionClient);
181
182     }
183
184     /**
185      * Create the buttons for the settings transfer.
186      *
187      * @param toolkit
188      * @param sectionClient
189      * @return boolean <code>true</code> if any were selected
190      */

191     private boolean createButtons(FormToolkit toolkit, Composite sectionClient) {
192
193         IConfigurationElement[] settings = SettingsTransfer
194                 .getSettingsTransfers();
195
196         String JavaDoc[] enabledSettings = getEnabledSettings(IDEWorkbenchPlugin
197                 .getDefault().getDialogSettings()
198                 .getSection(WORKBENCH_SETTINGS));
199
200         for (int i = 0; i < settings.length; i++) {
201             final IConfigurationElement settingsTransfer = settings[i];
202             final Button button = toolkit.createButton(sectionClient,
203                     settings[i].getAttribute(ATT_NAME), SWT.CHECK);
204
205             String JavaDoc helpId = settings[i].getAttribute(ATT_HELP_CONTEXT);
206
207             if (helpId != null)
208                 PlatformUI.getWorkbench().getHelpSystem().setHelp(button,
209                         helpId);
210
211             if (enabledSettings != null && enabledSettings.length > 0) {
212
213                 String JavaDoc id = settings[i].getAttribute(ATT_ID);
214                 for (int j = 0; j < enabledSettings.length; j++) {
215                     if (enabledSettings[j].equals(id)) {
216                         button.setSelection(true);
217                         selectedSettings.add(settingsTransfer);
218                         break;
219                     }
220                 }
221             }
222
223             button.setBackground(sectionClient.getBackground());
224             button.addSelectionListener(new SelectionAdapter() {
225
226                 /*
227                  * (non-Javadoc)
228                  *
229                  * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
230                  */

231                 public void widgetSelected(SelectionEvent e) {
232                     if (button.getSelection())
233                         selectedSettings.add(settingsTransfer);
234                     else
235                         selectedSettings.remove(settingsTransfer);
236                 }
237             });
238
239         }
240         return enabledSettings != null && enabledSettings.length > 0;
241     }
242
243     /**
244      * Get the settings for the receiver based on the entries in section.
245      *
246      * @param section
247      * @return String[] or <code>null</code>
248      */

249     private String JavaDoc[] getEnabledSettings(IDialogSettings section) {
250
251         if (section == null)
252             return null;
253
254         return section.getArray(ENABLED_TRANSFERS);
255
256     }
257
258     /*
259      * (non-Javadoc)
260      *
261      * @see org.eclipse.ui.internal.ide.ChooseWorkspaceDialog#okPressed()
262      */

263     protected void okPressed() {
264         Iterator JavaDoc settingsIterator = selectedSettings.iterator();
265         MultiStatus result = new MultiStatus(
266                 PlatformUI.PLUGIN_ID,
267                 IStatus.OK,
268                 IDEWorkbenchMessages.ChooseWorkspaceWithSettingsDialog_ProblemsTransferTitle,
269                 null);
270
271         IPath path = new Path(getWorkspaceLocation());
272         String JavaDoc[] selectionIDs = new String JavaDoc[selectedSettings.size()];
273         int index = 0;
274
275         while (settingsIterator.hasNext()) {
276             IConfigurationElement elem = (IConfigurationElement) settingsIterator
277                     .next();
278             result.add(transferSettings(elem, path));
279             selectionIDs[index] = elem.getAttribute(ATT_ID);
280         }
281         if (result.getSeverity() != IStatus.OK) {
282             ErrorDialog
283                     .openError(
284                             getShell(),
285                             IDEWorkbenchMessages.ChooseWorkspaceWithSettingsDialog_TransferFailedMessage,
286                             IDEWorkbenchMessages.ChooseWorkspaceWithSettingsDialog_SaveSettingsFailed,
287                             result);
288             return;
289         }
290
291         saveSettings(selectionIDs);
292         super.okPressed();
293     }
294
295     /**
296      * Save the ids of the selected elements.
297      *
298      * @param selectionIDs
299      */

300     private void saveSettings(String JavaDoc[] selectionIDs) {
301         IDialogSettings settings = IDEWorkbenchPlugin.getDefault()
302                 .getDialogSettings().getSection(WORKBENCH_SETTINGS);
303
304         if (settings == null)
305             settings = IDEWorkbenchPlugin.getDefault().getDialogSettings()
306                     .addNewSection(WORKBENCH_SETTINGS);
307
308         settings.put(ENABLED_TRANSFERS, selectionIDs);
309
310     }
311
312     /**
313      * Take the values from element and execute the class for path.
314      *
315      * @param elem
316      * @param path
317      * @return IStatus the result of the settings transfer.
318      */

319     private IStatus transferSettings(final IConfigurationElement element,
320             final IPath path) {
321
322         final IStatus[] exceptions = new IStatus[1];
323
324         SafeRunner.run(new ISafeRunnable() {
325             /*
326              * (non-Javadoc)
327              *
328              * @see org.eclipse.core.runtime.ISafeRunnable#run()
329              */

330             public void run() throws Exception JavaDoc {
331
332                 try {
333                     SettingsTransfer transfer = (SettingsTransfer) WorkbenchPlugin
334                             .createExtension(element, ATT_CLASS);
335                     transfer.transferSettings(path);
336                 } catch (CoreException exception) {
337                     exceptions[0] = exception.getStatus();
338                 }
339
340             }
341
342             /*
343              * (non-Javadoc)
344              *
345              * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
346              */

347             public void handleException(Throwable JavaDoc exception) {
348                 exceptions[0] = StatusUtil
349                         .newStatus(
350                                 IStatus.ERROR,
351                                 NLS
352                                         .bind(
353                                                 IDEWorkbenchMessages.ChooseWorkspaceWithSettingsDialog_ClassCreationFailed,
354                                                 element.getAttribute(ATT_CLASS)),
355                                 exception);
356
357             }
358         });
359
360         if (exceptions[0] != null)
361             return exceptions[0];
362
363         return Status.OK_STATUS;
364
365     }
366
367     /*
368      * (non-Javadoc)
369      *
370      * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsStrategy()
371      */

372     protected int getDialogBoundsStrategy() {
373         return DIALOG_PERSISTLOCATION;
374     }
375
376 }
377
Popular Tags