001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 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.ide;
011:
012: import java.util.Collection;
013: import java.util.HashSet;
014: import java.util.Iterator;
015:
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IConfigurationElement;
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.core.runtime.ISafeRunnable;
020: import org.eclipse.core.runtime.IStatus;
021: import org.eclipse.core.runtime.MultiStatus;
022: import org.eclipse.core.runtime.Path;
023: import org.eclipse.core.runtime.SafeRunner;
024: import org.eclipse.core.runtime.Status;
025: import org.eclipse.jface.dialogs.ErrorDialog;
026: import org.eclipse.jface.dialogs.IDialogConstants;
027: import org.eclipse.jface.dialogs.IDialogSettings;
028: import org.eclipse.osgi.util.NLS;
029: import org.eclipse.swt.SWT;
030: import org.eclipse.swt.events.DisposeEvent;
031: import org.eclipse.swt.events.DisposeListener;
032: import org.eclipse.swt.events.SelectionAdapter;
033: import org.eclipse.swt.events.SelectionEvent;
034: import org.eclipse.swt.graphics.Rectangle;
035: import org.eclipse.swt.layout.GridData;
036: import org.eclipse.swt.layout.GridLayout;
037: import org.eclipse.swt.widgets.Button;
038: import org.eclipse.swt.widgets.Composite;
039: import org.eclipse.swt.widgets.Control;
040: import org.eclipse.swt.widgets.Shell;
041: import org.eclipse.ui.PlatformUI;
042: import org.eclipse.ui.forms.events.ExpansionEvent;
043: import org.eclipse.ui.forms.events.IExpansionListener;
044: import org.eclipse.ui.forms.widgets.ExpandableComposite;
045: import org.eclipse.ui.forms.widgets.FormToolkit;
046: import org.eclipse.ui.forms.widgets.ScrolledForm;
047: import org.eclipse.ui.internal.WorkbenchPlugin;
048: import org.eclipse.ui.preferences.SettingsTransfer;
049:
050: /**
051: * The ChooseWorkspaceWithSettingsDialog is the dialog used to switch workspaces
052: * with an optional settings export.
053: *
054: * @since 3.3
055: *
056: */
057: public class ChooseWorkspaceWithSettingsDialog extends
058: ChooseWorkspaceDialog {
059:
060: private static final String WORKBENCH_SETTINGS = "WORKBENCH_SETTINGS"; //$NON-NLS-1$
061: private static final String ENABLED_TRANSFERS = "ENABLED_TRANSFERS"; //$NON-NLS-1$
062:
063: /**
064: * The class attribute for a settings transfer.
065: */
066: private static final String ATT_CLASS = "class"; //$NON-NLS-1$
067: /**
068: * The name attribute for the settings transfer.
069: */
070: private static final String ATT_NAME = "name"; //$NON-NLS-1$
071: /**
072: * The id attribute for the settings transfer.
073: */
074: private static final String ATT_ID = "id"; //$NON-NLS-1$
075: private static final String ATT_HELP_CONTEXT = "helpContext"; //$NON-NLS-1$
076:
077: private Collection selectedSettings = new HashSet();
078:
079: /**
080: * Open a new instance of the receiver.
081: *
082: * @param parentShell
083: * @param launchData
084: * @param suppressAskAgain
085: * @param centerOnMonitor
086: */
087: public ChooseWorkspaceWithSettingsDialog(Shell parentShell,
088: ChooseWorkspaceData launchData, boolean suppressAskAgain,
089: boolean centerOnMonitor) {
090: super (parentShell, launchData, suppressAskAgain,
091: centerOnMonitor);
092: }
093:
094: /*
095: * (non-Javadoc)
096: *
097: * @see org.eclipse.ui.internal.ide.ChooseWorkspaceDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
098: */
099: 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
116: .getDisplay());
117: workArea.addDisposeListener(new DisposeListener() {
118:
119: public void widgetDisposed(DisposeEvent e) {
120: toolkit.dispose();
121:
122: }
123: });
124: final ScrolledForm form = toolkit.createScrolledForm(workArea);
125: form.setBackground(workArea.getBackground());
126: form.getBody().setLayout(new GridLayout());
127:
128: GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true,
129: true);
130: form.setLayoutData(layoutData);
131: final ExpandableComposite expandable = toolkit
132: .createExpandableComposite(form.getBody(),
133: ExpandableComposite.TWISTIE);
134: expandable
135: .setText(IDEWorkbenchMessages.ChooseWorkspaceWithSettingsDialog_SettingsGroupName);
136: expandable.setBackground(workArea.getBackground());
137: expandable.setLayout(new GridLayout());
138: expandable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
139: true));
140: expandable.addExpansionListener(new IExpansionListener() {
141:
142: boolean notExpanded = true;
143:
144: /*
145: * (non-Javadoc)
146: *
147: * @see org.eclipse.ui.forms.events.IExpansionListener#expansionStateChanged(org.eclipse.ui.forms.events.ExpansionEvent)
148: */
149: public void expansionStateChanged(ExpansionEvent e) {
150: form.reflow(true);
151: if (e.getState() && notExpanded) {
152: getShell().setRedraw(false);
153: Rectangle shellBounds = getShell().getBounds();
154: int entriesToShow = Math.min(4, SettingsTransfer
155: .getSettingsTransfers().length);
156:
157: shellBounds.height += convertHeightInCharsToPixels(entriesToShow)
158: + IDialogConstants.VERTICAL_SPACING;
159: getShell().setBounds(shellBounds);
160: getShell().setRedraw(true);
161: notExpanded = false;
162: }
163:
164: }
165:
166: /*
167: * (non-Javadoc)
168: *
169: * @see org.eclipse.ui.forms.events.IExpansionListener#expansionStateChanging(org.eclipse.ui.forms.events.ExpansionEvent)
170: */
171: public void expansionStateChanging(ExpansionEvent e) {
172: // Nothing to do here
173:
174: }
175: });
176:
177: Composite sectionClient = toolkit.createComposite(expandable);
178: sectionClient.setLayout(new GridLayout());
179: sectionClient.setBackground(workArea.getBackground());
180:
181: if (createButtons(toolkit, sectionClient))
182: expandable.setExpanded(true);
183:
184: expandable.setClient(sectionClient);
185:
186: }
187:
188: /**
189: * Create the buttons for the settings transfer.
190: *
191: * @param toolkit
192: * @param sectionClient
193: * @return boolean <code>true</code> if any were selected
194: */
195: private boolean createButtons(FormToolkit toolkit,
196: Composite sectionClient) {
197:
198: IConfigurationElement[] settings = SettingsTransfer
199: .getSettingsTransfers();
200:
201: String[] enabledSettings = getEnabledSettings(IDEWorkbenchPlugin
202: .getDefault().getDialogSettings().getSection(
203: WORKBENCH_SETTINGS));
204:
205: for (int i = 0; i < settings.length; i++) {
206: final IConfigurationElement settingsTransfer = settings[i];
207: final Button button = toolkit.createButton(sectionClient,
208: settings[i].getAttribute(ATT_NAME), SWT.CHECK);
209:
210: String helpId = settings[i].getAttribute(ATT_HELP_CONTEXT);
211:
212: if (helpId != null)
213: PlatformUI.getWorkbench().getHelpSystem().setHelp(
214: button, helpId);
215:
216: if (enabledSettings != null && enabledSettings.length > 0) {
217:
218: String id = settings[i].getAttribute(ATT_ID);
219: for (int j = 0; j < enabledSettings.length; j++) {
220: if (enabledSettings[j].equals(id)) {
221: button.setSelection(true);
222: selectedSettings.add(settingsTransfer);
223: break;
224: }
225: }
226: }
227:
228: button.setBackground(sectionClient.getBackground());
229: button.addSelectionListener(new SelectionAdapter() {
230:
231: /*
232: * (non-Javadoc)
233: *
234: * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
235: */
236: public void widgetSelected(SelectionEvent e) {
237: if (button.getSelection())
238: selectedSettings.add(settingsTransfer);
239: else
240: selectedSettings.remove(settingsTransfer);
241: }
242: });
243:
244: }
245: return enabledSettings != null && enabledSettings.length > 0;
246: }
247:
248: /**
249: * Get the settings for the receiver based on the entries in section.
250: *
251: * @param section
252: * @return String[] or <code>null</code>
253: */
254: private String[] getEnabledSettings(IDialogSettings section) {
255:
256: if (section == null)
257: return null;
258:
259: return section.getArray(ENABLED_TRANSFERS);
260:
261: }
262:
263: /*
264: * (non-Javadoc)
265: *
266: * @see org.eclipse.ui.internal.ide.ChooseWorkspaceDialog#okPressed()
267: */
268: protected void okPressed() {
269: Iterator settingsIterator = selectedSettings.iterator();
270: MultiStatus result = new MultiStatus(
271: PlatformUI.PLUGIN_ID,
272: IStatus.OK,
273: IDEWorkbenchMessages.ChooseWorkspaceWithSettingsDialog_ProblemsTransferTitle,
274: null);
275:
276: IPath path = new Path(getWorkspaceLocation());
277: String[] selectionIDs = new String[selectedSettings.size()];
278: int index = 0;
279:
280: while (settingsIterator.hasNext()) {
281: IConfigurationElement elem = (IConfigurationElement) settingsIterator
282: .next();
283: result.add(transferSettings(elem, path));
284: selectionIDs[index] = elem.getAttribute(ATT_ID);
285: }
286: if (result.getSeverity() != IStatus.OK) {
287: ErrorDialog
288: .openError(
289: getShell(),
290: IDEWorkbenchMessages.ChooseWorkspaceWithSettingsDialog_TransferFailedMessage,
291: IDEWorkbenchMessages.ChooseWorkspaceWithSettingsDialog_SaveSettingsFailed,
292: result);
293: return;
294: }
295:
296: saveSettings(selectionIDs);
297: super .okPressed();
298: }
299:
300: /**
301: * Save the ids of the selected elements.
302: *
303: * @param selectionIDs
304: */
305: private void saveSettings(String[] selectionIDs) {
306: IDialogSettings settings = IDEWorkbenchPlugin.getDefault()
307: .getDialogSettings().getSection(WORKBENCH_SETTINGS);
308:
309: if (settings == null)
310: settings = IDEWorkbenchPlugin.getDefault()
311: .getDialogSettings().addNewSection(
312: WORKBENCH_SETTINGS);
313:
314: settings.put(ENABLED_TRANSFERS, selectionIDs);
315:
316: }
317:
318: /**
319: * Take the values from element and execute the class for path.
320: *
321: * @param elem
322: * @param path
323: * @return IStatus the result of the settings transfer.
324: */
325: private IStatus transferSettings(
326: final IConfigurationElement element, final IPath path) {
327:
328: final IStatus[] exceptions = new IStatus[1];
329:
330: SafeRunner.run(new ISafeRunnable() {
331: /*
332: * (non-Javadoc)
333: *
334: * @see org.eclipse.core.runtime.ISafeRunnable#run()
335: */
336: public void run() throws Exception {
337:
338: try {
339: SettingsTransfer transfer = (SettingsTransfer) WorkbenchPlugin
340: .createExtension(element, ATT_CLASS);
341: transfer.transferSettings(path);
342: } catch (CoreException exception) {
343: exceptions[0] = exception.getStatus();
344: }
345:
346: }
347:
348: /*
349: * (non-Javadoc)
350: *
351: * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
352: */
353: public void handleException(Throwable exception) {
354: exceptions[0] = StatusUtil
355: .newStatus(
356: IStatus.ERROR,
357: NLS
358: .bind(
359: IDEWorkbenchMessages.ChooseWorkspaceWithSettingsDialog_ClassCreationFailed,
360: element
361: .getAttribute(ATT_CLASS)),
362: exception);
363:
364: }
365: });
366:
367: if (exceptions[0] != null)
368: return exceptions[0];
369:
370: return Status.OK_STATUS;
371:
372: }
373:
374: /*
375: * (non-Javadoc)
376: *
377: * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsStrategy()
378: */
379: protected int getDialogBoundsStrategy() {
380: return DIALOG_PERSISTLOCATION;
381: }
382:
383: }
|