1 11 12 package org.eclipse.ui.internal.ide; 13 14 import java.util.Collection ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 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 59 public class ChooseWorkspaceWithSettingsDialog extends ChooseWorkspaceDialog { 60 61 private static final String WORKBENCH_SETTINGS = "WORKBENCH_SETTINGS"; private static final String ENABLED_TRANSFERS = "ENABLED_TRANSFERS"; 64 67 private static final String ATT_CLASS = "class"; 71 private static final String ATT_NAME = "name"; 75 private static final String ATT_ID = "id"; private static final String ATT_HELP_CONTEXT = "helpContext"; 78 private Collection selectedSettings = new HashSet (); 79 80 88 public ChooseWorkspaceWithSettingsDialog(Shell parentShell, 89 ChooseWorkspaceData launchData, boolean suppressAskAgain, 90 boolean centerOnMonitor) { 91 super(parentShell, launchData, suppressAskAgain, centerOnMonitor); 92 } 93 94 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 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 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 167 public void expansionStateChanging(ExpansionEvent e) { 168 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 191 private boolean createButtons(FormToolkit toolkit, Composite sectionClient) { 192 193 IConfigurationElement[] settings = SettingsTransfer 194 .getSettingsTransfers(); 195 196 String [] 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 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 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 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 249 private String [] getEnabledSettings(IDialogSettings section) { 250 251 if (section == null) 252 return null; 253 254 return section.getArray(ENABLED_TRANSFERS); 255 256 } 257 258 263 protected void okPressed() { 264 Iterator 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 [] selectionIDs = new String [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 300 private void saveSettings(String [] 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 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 330 public void run() throws Exception { 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 347 public void handleException(Throwable 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 372 protected int getDialogBoundsStrategy() { 373 return DIALOG_PERSISTLOCATION; 374 } 375 376 } 377 | Popular Tags |