1 11 12 package org.eclipse.ui.internal.ide.dialogs; 13 14 import java.util.TreeSet ; 15 16 import org.eclipse.core.resources.IProject; 17 import org.eclipse.core.resources.IWorkspace; 18 import org.eclipse.core.resources.IWorkspaceDescription; 19 import org.eclipse.core.resources.IncrementalProjectBuilder; 20 import org.eclipse.core.resources.ResourcesPlugin; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.jface.preference.FieldEditor; 23 import org.eclipse.jface.preference.IntegerFieldEditor; 24 import org.eclipse.jface.preference.PreferencePage; 25 import org.eclipse.jface.util.IPropertyChangeListener; 26 import org.eclipse.jface.util.PropertyChangeEvent; 27 import org.eclipse.jface.viewers.ILabelProvider; 28 import org.eclipse.jface.viewers.LabelProvider; 29 import org.eclipse.jface.window.Window; 30 import org.eclipse.swt.SWT; 31 import org.eclipse.swt.events.SelectionAdapter; 32 import org.eclipse.swt.events.SelectionEvent; 33 import org.eclipse.swt.events.SelectionListener; 34 import org.eclipse.swt.graphics.Font; 35 import org.eclipse.swt.layout.GridData; 36 import org.eclipse.swt.layout.GridLayout; 37 import org.eclipse.swt.widgets.Button; 38 import org.eclipse.swt.widgets.Composite; 39 import org.eclipse.swt.widgets.Control; 40 import org.eclipse.swt.widgets.Label; 41 import org.eclipse.swt.widgets.List; 42 import org.eclipse.swt.widgets.Text; 43 import org.eclipse.ui.IWorkbench; 44 import org.eclipse.ui.IWorkbenchPreferencePage; 45 import org.eclipse.ui.PlatformUI; 46 import org.eclipse.ui.actions.GlobalBuildAction; 47 import org.eclipse.ui.dialogs.ListSelectionDialog; 48 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 49 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 50 import org.eclipse.ui.internal.util.PrefUtil; 51 52 56 public class BuildOrderPreferencePage extends PreferencePage implements 57 IWorkbenchPreferencePage { 58 59 private IWorkbench workbench; 60 61 private Button defaultOrderButton; 62 63 private Label buildLabel; 64 65 private List buildList; 66 67 private Composite buttonComposite; 68 69 private IntegerFieldEditor maxItersField; 70 71 private String [] defaultBuildOrder; 72 73 private String [] customBuildOrder; 74 75 private boolean notCheckedBuildOrder = true; 77 78 private final String UP_LABEL = IDEWorkbenchMessages.BuildOrderPreference_up; 79 80 private final String DOWN_LABEL = IDEWorkbenchMessages.BuildOrderPreference_down; 81 82 private final String ADD_LABEL = IDEWorkbenchMessages.BuildOrderPreference_add; 83 84 private final String REMOVE_LABEL = IDEWorkbenchMessages.BuildOrderPreference_remove; 85 86 private final String PROJECT_SELECTION_MESSAGE = IDEWorkbenchMessages.BuildOrderPreference_selectOtherProjects; 87 88 private final String DEFAULTS_LABEL = IDEWorkbenchMessages.BuildOrderPreference_useDefaults; 89 90 private final String LIST_LABEL = IDEWorkbenchMessages.BuildOrderPreference_projectBuildOrder; 91 92 private final String NOTE_LABEL = IDEWorkbenchMessages.Preference_note; 93 94 private final String REMOVE_MESSAGE = IDEWorkbenchMessages.BuildOrderPreference_removeNote; 95 96 private boolean defaultOrderInitiallySelected; 99 100 private IPropertyChangeListener validityChangeListener = new IPropertyChangeListener() { 101 public void propertyChange(PropertyChangeEvent event) { 102 if (event.getProperty().equals(FieldEditor.IS_VALID)) { 103 updateValidState(); 104 } 105 } 106 }; 107 108 111 private void addProject() { 112 113 String [] currentItems = this.buildList.getItems(); 114 115 IProject[] allProjects = getWorkspace().getRoot().getProjects(); 116 117 ILabelProvider labelProvider = new LabelProvider() { 118 public String getText(Object element) { 119 return (String ) element; 120 } 121 }; 122 123 SimpleListContentProvider contentsProvider = new SimpleListContentProvider(); 124 contentsProvider 125 .setElements(sortedDifference(allProjects, currentItems)); 126 127 ListSelectionDialog dialog = new ListSelectionDialog(this.getShell(), 128 this, contentsProvider, labelProvider, 129 PROJECT_SELECTION_MESSAGE); 130 131 if (dialog.open() != Window.OK) { 132 return; 133 } 134 135 Object [] result = dialog.getResult(); 136 137 int currentItemsLength = currentItems.length; 138 int resultLength = result.length; 139 String [] newItems = new String [currentItemsLength + resultLength]; 140 141 System.arraycopy(currentItems, 0, newItems, 0, currentItemsLength); 142 System 143 .arraycopy(result, 0, newItems, currentItemsLength, 144 result.length); 145 this.buildList.setItems(newItems); 146 } 147 148 151 private void updateValidState() { 152 setValid(maxItersField.isValid()); 153 } 154 155 161 private void createBuildOrderList(Composite composite, boolean enabled) { 162 163 Font font = composite.getFont(); 164 165 this.buildLabel = new Label(composite, SWT.NONE); 166 this.buildLabel.setText(LIST_LABEL); 167 this.buildLabel.setEnabled(enabled); 168 GridData gridData = new GridData(); 169 gridData.horizontalAlignment = GridData.FILL; 170 gridData.horizontalSpan = 2; 171 this.buildLabel.setLayoutData(gridData); 172 this.buildLabel.setFont(font); 173 174 this.buildList = new List(composite, SWT.BORDER | SWT.MULTI 175 | SWT.H_SCROLL | SWT.V_SCROLL); 176 this.buildList.setEnabled(enabled); 177 GridData data = new GridData(); 178 data.heightHint = buildList.getItemHeight(); 182 data.verticalAlignment = GridData.FILL; 183 data.horizontalAlignment = GridData.FILL; 184 data.grabExcessHorizontalSpace = true; 185 data.grabExcessVerticalSpace = true; 186 this.buildList.setLayoutData(data); 187 this.buildList.setFont(font); 188 } 189 190 196 protected Control createContents(Composite parent) { 197 198 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, 199 IIDEHelpContextIds.BUILD_ORDER_PREFERENCE_PAGE); 200 201 Font font = parent.getFont(); 202 203 Composite composite = new Composite(parent, SWT.NULL); 205 GridLayout layout = new GridLayout(); 206 layout.numColumns = 2; 207 layout.marginWidth = 0; 208 layout.marginHeight = 0; 209 composite.setLayout(layout); 210 GridData data = new GridData(); 211 data.verticalAlignment = GridData.FILL; 212 data.horizontalAlignment = GridData.FILL; 213 composite.setLayoutData(data); 214 composite.setFont(font); 215 216 String [] buildOrder = getCurrentBuildOrder(); 217 boolean useDefault = (buildOrder == null); 218 219 createDefaultPathButton(composite, useDefault); 220 createBuildOrderList(composite, true); 223 createListButtons(composite, !useDefault); 224 225 Composite noteComposite = createNoteComposite(font, composite, 226 NOTE_LABEL, REMOVE_MESSAGE); 227 GridData noteData = new GridData(); 228 noteData.horizontalSpan = 2; 229 noteComposite.setLayoutData(noteData); 230 231 createSpacer(composite); 232 233 createMaxIterationsField(composite); 234 235 createSpacer(composite); 236 237 if (useDefault) { 238 this.buildList.setItems(getDefaultProjectOrder()); 239 } else { 240 this.buildList.setItems(buildOrder); 241 } 242 243 return composite; 244 245 } 246 247 252 private void createSpacer(Composite composite) { 253 Label spacer = new Label(composite, SWT.NONE); 254 GridData spacerData = new GridData(); 255 spacerData.horizontalSpan = 2; 256 spacer.setLayoutData(spacerData); 257 } 258 259 265 private void createDefaultPathButton(Composite composite, boolean selected) { 266 267 defaultOrderInitiallySelected = selected; 268 269 this.defaultOrderButton = new Button(composite, SWT.LEFT | SWT.CHECK); 270 this.defaultOrderButton.setSelection(selected); 271 this.defaultOrderButton.setText(DEFAULTS_LABEL); 272 SelectionListener listener = new SelectionAdapter() { 273 public void widgetSelected(SelectionEvent e) { 274 defaultsButtonSelected(defaultOrderButton.getSelection()); 275 } 276 }; 277 this.defaultOrderButton.addSelectionListener(listener); 278 279 GridData gridData = new GridData(); 280 gridData.horizontalAlignment = GridData.FILL; 281 gridData.horizontalSpan = 2; 282 this.defaultOrderButton.setLayoutData(gridData); 283 this.defaultOrderButton.setFont(composite.getFont()); 284 } 285 286 292 private void createListButtons(Composite composite, boolean enableComposite) { 293 294 Font font = composite.getFont(); 295 296 this.buttonComposite = new Composite(composite, SWT.RIGHT); 298 GridLayout layout = new GridLayout(); 299 layout.marginWidth = 0; 300 layout.marginHeight = 0; 301 this.buttonComposite.setLayout(layout); 302 GridData gridData = new GridData(); 303 gridData.verticalAlignment = GridData.FILL; 304 gridData.horizontalAlignment = GridData.FILL; 305 this.buttonComposite.setLayoutData(gridData); 306 this.buttonComposite.setFont(font); 307 308 Button upButton = new Button(this.buttonComposite, SWT.CENTER 309 | SWT.PUSH); 310 upButton.setText(UP_LABEL); 311 upButton.setEnabled(enableComposite); 312 upButton.setFont(font); 313 setButtonLayoutData(upButton); 314 315 SelectionListener listener = new SelectionAdapter() { 316 public void widgetSelected(SelectionEvent e) { 317 moveSelectionUp(); 318 } 319 }; 320 upButton.addSelectionListener(listener); 321 322 Button downButton = new Button(this.buttonComposite, SWT.CENTER 323 | SWT.PUSH); 324 downButton.setText(DOWN_LABEL); 325 downButton.setEnabled(enableComposite); 326 listener = new SelectionAdapter() { 327 public void widgetSelected(SelectionEvent e) { 328 moveSelectionDown(); 329 } 330 }; 331 downButton.addSelectionListener(listener); 332 downButton.setFont(font); 333 setButtonLayoutData(downButton); 334 335 Button addButton = new Button(this.buttonComposite, SWT.CENTER 336 | SWT.PUSH); 337 addButton.setText(ADD_LABEL); 338 listener = new SelectionAdapter() { 339 public void widgetSelected(SelectionEvent e) { 340 addProject(); 341 } 342 }; 343 addButton.addSelectionListener(listener); 344 addButton.setEnabled(enableComposite); 345 addButton.setFont(font); 346 setButtonLayoutData(addButton); 347 348 Button removeButton = new Button(this.buttonComposite, SWT.CENTER 349 | SWT.PUSH); 350 removeButton.setText(REMOVE_LABEL); 351 listener = new SelectionAdapter() { 352 public void widgetSelected(SelectionEvent e) { 353 removeSelection(); 354 } 355 }; 356 removeButton.addSelectionListener(listener); 357 removeButton.setEnabled(enableComposite); 358 removeButton.setFont(font); 359 setButtonLayoutData(removeButton); 360 361 } 362 363 367 private void createMaxIterationsField(Composite composite) { 368 Composite maxItersComposite = new Composite(composite, SWT.NONE); 369 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 370 maxItersComposite.setLayoutData(gd); 371 maxItersComposite.setFont(composite.getFont()); 372 373 maxItersField = new IntegerFieldEditor( 374 "", IDEWorkbenchMessages.BuildOrderPreference_maxIterationsLabel, maxItersComposite) { protected void doLoad() { 376 Text text = getTextControl(); 377 if (text != null) { 378 int value = getWorkspace().getDescription() 379 .getMaxBuildIterations(); 380 text.setText(Integer.toString(value)); 381 } 382 } 383 384 protected void doLoadDefault() { 385 Text text = getTextControl(); 386 if (text != null) { 387 int value = ResourcesPlugin.getPlugin() 388 .getPluginPreferences().getDefaultInt( 389 ResourcesPlugin.PREF_MAX_BUILD_ITERATIONS); 390 text.setText(Integer.toString(value)); 391 } 392 valueChanged(); 393 } 394 395 protected void doStore() { 396 throw new UnsupportedOperationException (); 398 } 399 }; 400 maxItersField.setValidRange(1, Integer.MAX_VALUE); 401 maxItersField.setPage(this); 402 maxItersField.setPreferenceStore(getPreferenceStore()); 403 maxItersField.setPropertyChangeListener(validityChangeListener); 404 maxItersField.load(); 405 } 406 407 411 private void defaultsButtonSelected(boolean selected) { 412 if (selected) { 413 setBuildOrderWidgetsEnablement(false); 414 customBuildOrder = buildList.getItems(); 416 buildList.setItems(getDefaultProjectOrder()); 417 418 } else { 419 setBuildOrderWidgetsEnablement(true); 420 String [] buildOrder = getCurrentBuildOrder(); 421 if (buildOrder == null) { 422 buildList.setItems(getDefaultProjectOrder()); 423 } else { 424 buildList.setItems(buildOrder); 425 } 426 } 427 } 428 429 435 private String [] getCurrentBuildOrder() { 436 if (notCheckedBuildOrder) { 437 customBuildOrder = getWorkspace().getDescription().getBuildOrder(); 438 notCheckedBuildOrder = false; 439 } 440 441 return customBuildOrder; 442 } 443 444 450 private String [] getDefaultProjectOrder() { 451 if (defaultBuildOrder == null) { 452 IWorkspace workspace = getWorkspace(); 453 IWorkspace.ProjectOrder projectOrder = getWorkspace() 454 .computeProjectOrder(workspace.getRoot().getProjects()); 455 IProject[] foundProjects = projectOrder.projects; 456 defaultBuildOrder = new String [foundProjects.length]; 457 int foundSize = foundProjects.length; 458 for (int i = 0; i < foundSize; i++) { 459 defaultBuildOrder[i] = foundProjects[i].getName(); 460 } 461 } 462 463 return defaultBuildOrder; 464 } 465 466 470 private IWorkspace getWorkspace() { 471 return ResourcesPlugin.getWorkspace(); 472 } 473 474 477 private boolean includes(String [] testArray, String searchElement) { 478 479 for (int i = 0; i < testArray.length; i++) { 480 if (searchElement.equals(testArray[i])) { 481 return true; 482 } 483 } 484 return false; 485 486 } 487 488 491 public void init(IWorkbench workbench) { 492 this.workbench = workbench; 493 setPreferenceStore(PrefUtil.getInternalPreferenceStore()); 494 } 495 496 499 private void moveSelectionDown() { 500 501 if (this.buildList.getSelectionCount() == 1) { 503 int currentIndex = this.buildList.getSelectionIndex(); 504 if (currentIndex < this.buildList.getItemCount() - 1) { 505 String elementToMove = this.buildList.getItem(currentIndex); 506 this.buildList.remove(currentIndex); 507 this.buildList.add(elementToMove, currentIndex + 1); 508 this.buildList.select(currentIndex + 1); 509 } 510 } 511 } 512 513 516 private void moveSelectionUp() { 517 518 int currentIndex = this.buildList.getSelectionIndex(); 519 520 if (currentIndex > 0 && this.buildList.getSelectionCount() == 1) { 522 String elementToMove = this.buildList.getItem(currentIndex); 523 this.buildList.remove(currentIndex); 524 this.buildList.add(elementToMove, currentIndex - 1); 525 this.buildList.select(currentIndex - 1); 526 } 527 } 528 529 533 protected void performDefaults() { 534 this.defaultOrderButton.setSelection(true); 535 defaultsButtonSelected(true); 536 maxItersField.loadDefault(); 537 super.performDefaults(); 538 } 539 540 544 public boolean performOk() { 545 546 String [] buildOrder = null; 547 boolean useDefault = defaultOrderButton.getSelection(); 548 549 if (!useDefault) { 551 buildOrder = buildList.getItems(); 552 } 553 554 IWorkspaceDescription description = getWorkspace().getDescription(); 557 description.setBuildOrder(buildOrder); 558 description.setMaxBuildIterations(maxItersField.getIntValue()); 559 try { 560 getWorkspace().setDescription(description); 561 } catch (CoreException exception) { 562 return false; 564 } 565 566 if (!useDefault || (useDefault != defaultOrderInitiallySelected)) { 570 defaultOrderInitiallySelected = useDefault; 571 if (ResourcesPlugin.getWorkspace().isAutoBuilding()) { 574 GlobalBuildAction action = new GlobalBuildAction(workbench 575 .getActiveWorkbenchWindow(), 576 IncrementalProjectBuilder.INCREMENTAL_BUILD); 577 action.doBuild(); 578 } 579 } 580 581 customBuildOrder = null; 583 584 return true; 585 } 586 587 590 private void removeSelection() { 591 592 this.buildList.remove(this.buildList.getSelectionIndices()); 593 } 594 595 599 private void setBuildOrderWidgetsEnablement(boolean value) { 600 601 Control[] children = this.buttonComposite.getChildren(); 604 for (int i = 0; i < children.length; i++) { 605 children[i].setEnabled(value); 606 } 607 } 608 609 616 private String [] sortedDifference(IProject[] allProjects, 617 String [] currentlyDisplayed) { 618 619 TreeSet difference = new TreeSet (); 620 621 for (int i = 0; i < allProjects.length; i++) { 622 if (!includes(currentlyDisplayed, allProjects[i].getName())) { 623 difference.add(allProjects[i].getName()); 624 } 625 } 626 627 String [] returnValue = new String [difference.size()]; 628 difference.toArray(returnValue); 629 return returnValue; 630 } 631 } 632 | Popular Tags |