1 11 package org.eclipse.ui.internal.ide; 12 13 import java.io.File ; 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 46 public class ChooseWorkspaceDialog extends TitleAreaDialog { 47 48 private static final String DIALOG_SETTINGS_SECTION = "ChooseWorkspaceDialogSettings"; 50 private ChooseWorkspaceData launchData; 51 52 private Combo text; 53 54 private boolean suppressAskAgain = false; 55 56 private boolean centerOnMonitor = false; 57 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 88 public void prompt(boolean force) { 89 if (force || launchData.getShowDialog()) { 90 open(); 91 92 if (getReturnCode() == CANCEL) { 94 launchData.workspaceSelected(null); 95 } 96 97 return; 98 } 99 100 String [] recent = launchData.getRecentWorkspaces(); 101 102 String 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 125 protected Control createDialogArea(Composite parent) { 126 String 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 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 164 protected void configureShell(Shell shell) { 165 super.configureShell(shell); 166 shell.setText(IDEWorkbenchMessages.ChooseWorkspaceDialog_dialogName); 167 } 168 169 177 protected void okPressed() { 178 launchData.workspaceSelected(TextProcessor.deprocess(getWorkspaceLocation())); 179 super.okPressed(); 180 } 181 182 186 protected String getWorkspaceLocation() { 187 return text.getText(); 188 } 189 190 198 protected void cancelPressed() { 199 launchData.workspaceSelected(null); 200 super.cancelPressed(); 201 } 202 203 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 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 dir = dialog.open(); 257 if (dir != null) { 258 text.setText(TextProcessor.process(dir)); 259 } 260 } 261 }); 262 } 263 264 273 private String getInitialBrowsePath() { 274 File dir = new File (getWorkspaceLocation()); 275 while (dir != null && !dir.exists()) { 276 dir = dir.getParentFile(); 277 } 278 279 return dir != null ? dir.getAbsolutePath() : System 280 .getProperty("user.dir"); } 282 283 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 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 [] 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 345 protected IDialogSettings getDialogBoundsSettings() { 346 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 |