KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > BuildOrderPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.dialogs;
13
14 import java.util.TreeSet JavaDoc;
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 /**
53  * Page used to determine what order projects will be built in
54  * by the workspace.
55  */

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 JavaDoc[] defaultBuildOrder;
72
73     private String JavaDoc[] customBuildOrder;
74
75     //Boolean to indicate if we have looked it up
76
private boolean notCheckedBuildOrder = true;
77
78     private final String JavaDoc UP_LABEL = IDEWorkbenchMessages.BuildOrderPreference_up;
79
80     private final String JavaDoc DOWN_LABEL = IDEWorkbenchMessages.BuildOrderPreference_down;
81
82     private final String JavaDoc ADD_LABEL = IDEWorkbenchMessages.BuildOrderPreference_add;
83
84     private final String JavaDoc REMOVE_LABEL = IDEWorkbenchMessages.BuildOrderPreference_remove;
85
86     private final String JavaDoc PROJECT_SELECTION_MESSAGE = IDEWorkbenchMessages.BuildOrderPreference_selectOtherProjects;
87
88     private final String JavaDoc DEFAULTS_LABEL = IDEWorkbenchMessages.BuildOrderPreference_useDefaults;
89
90     private final String JavaDoc LIST_LABEL = IDEWorkbenchMessages.BuildOrderPreference_projectBuildOrder;
91
92     private final String JavaDoc NOTE_LABEL = IDEWorkbenchMessages.Preference_note;
93
94     private final String JavaDoc REMOVE_MESSAGE = IDEWorkbenchMessages.BuildOrderPreference_removeNote;
95
96     // whether or not the use defaults option was selected when Apply (or OK) was last pressed
97
// (or when the preference page was opened). This represents the most recent applied state.
98
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     /**
109      * Add another project to the list at the end.
110      */

111     private void addProject() {
112
113         String JavaDoc[] currentItems = this.buildList.getItems();
114
115         IProject[] allProjects = getWorkspace().getRoot().getProjects();
116
117         ILabelProvider labelProvider = new LabelProvider() {
118             public String JavaDoc getText(Object JavaDoc element) {
119                 return (String JavaDoc) 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 JavaDoc[] result = dialog.getResult();
136
137         int currentItemsLength = currentItems.length;
138         int resultLength = result.length;
139         String JavaDoc[] newItems = new String JavaDoc[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     /**
149      * Updates the valid state of the page.
150      */

151     private void updateValidState() {
152         setValid(maxItersField.isValid());
153     }
154
155     /**
156      * Create the list of build paths. If the current build order is empty make the list empty
157      * and disable it.
158      * @param composite - the parent to create the list in
159      * @param enabled - the boolean that indcates if the list will be sensitive initially or not
160      */

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         //Set heightHint with a small value so the list size will be defined by
179
//the space available in the dialog instead of resizing the dialog to
180
//fit all the items in the list.
181
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     /**
191      * Create the widgets that are used to determine the build order.
192      *
193      * @param parent the parent composite
194      * @return the new control
195      */

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         //The main composite
204
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 JavaDoc[] buildOrder = getCurrentBuildOrder();
217         boolean useDefault = (buildOrder == null);
218
219         createDefaultPathButton(composite, useDefault);
220         // List always enabled so user can scroll list.
221
// Only the buttons need to be disabled.
222
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     /**
248      * Adds in a spacer.
249      *
250      * @param composite the parent composite
251      */

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     /**
260      * Create the default path button. Set it to selected based on the current workspace
261      * build path.
262      * @param composite org.eclipse.swt.widgets.Composite
263      * @param selected - the boolean that indicates the buttons initial state
264      */

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     /**
287      * Create the buttons used to manipulate the list. These Add, Remove and Move Up or Down
288      * the list items.
289      * @param composite the parent of the buttons
290      * @param enableComposite - boolean that indicates if a composite should be enabled
291      */

292     private void createListButtons(Composite composite, boolean enableComposite) {
293
294         Font font = composite.getFont();
295
296         //Create an intermeditate composite to keep the buttons in the same column
297
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     /**
364      * Create the field for the maximum number of iterations in the presence
365      * of cycles.
366      */

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) { //$NON-NLS-1$
375
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                 // handled specially in performOK()
397
throw new UnsupportedOperationException JavaDoc();
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     /**
408      * The defaults button has been selected - update the other widgets as required.
409      * @param selected - whether or not the defaults button got selected
410      */

411     private void defaultsButtonSelected(boolean selected) {
412         if (selected) {
413             setBuildOrderWidgetsEnablement(false);
414             //Cache the current value as the custom order
415
customBuildOrder = buildList.getItems();
416             buildList.setItems(getDefaultProjectOrder());
417
418         } else {
419             setBuildOrderWidgetsEnablement(true);
420             String JavaDoc[] buildOrder = getCurrentBuildOrder();
421             if (buildOrder == null) {
422                 buildList.setItems(getDefaultProjectOrder());
423             } else {
424                 buildList.setItems(buildOrder);
425             }
426         }
427     }
428
429     /**
430      * Get the project names for the current custom build
431      * order stored in the workspace description.
432      *
433      * @return java.lang.String[] or null if there is no setting
434      */

435     private String JavaDoc[] getCurrentBuildOrder() {
436         if (notCheckedBuildOrder) {
437             customBuildOrder = getWorkspace().getDescription().getBuildOrder();
438             notCheckedBuildOrder = false;
439         }
440
441         return customBuildOrder;
442     }
443
444     /**
445      * Get the project names in the default build order
446      * based on the current Workspace settings.
447      *
448      * @return java.lang.String[]
449      */

450     private String JavaDoc[] 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 JavaDoc[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     /**
467      * Return the Workspace the build order is from.
468      * @return org.eclipse.core.resources.IWorkspace
469      */

470     private IWorkspace getWorkspace() {
471         return ResourcesPlugin.getWorkspace();
472     }
473
474     /**
475      * Return whether or not searchElement is in testArray.
476      */

477     private boolean includes(String JavaDoc[] testArray, String JavaDoc 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     /**
489      * See IWorkbenchPreferencePage. This class does nothing with he Workbench.
490      */

491     public void init(IWorkbench workbench) {
492         this.workbench = workbench;
493         setPreferenceStore(PrefUtil.getInternalPreferenceStore());
494     }
495
496     /**
497      * Move the current selection in the build list down.
498      */

499     private void moveSelectionDown() {
500
501         //Only do this operation on a single selection
502
if (this.buildList.getSelectionCount() == 1) {
503             int currentIndex = this.buildList.getSelectionIndex();
504             if (currentIndex < this.buildList.getItemCount() - 1) {
505                 String JavaDoc 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     /**
514      * Move the current selection in the build list up.
515      */

516     private void moveSelectionUp() {
517
518         int currentIndex = this.buildList.getSelectionIndex();
519
520         //Only do this operation on a single selection
521
if (currentIndex > 0 && this.buildList.getSelectionCount() == 1) {
522             String JavaDoc 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     /**
530      * Performs special processing when this page's Defaults button has been pressed.
531      * In this case change the defaultOrderButton to have it's selection set to true.
532      */

533     protected void performDefaults() {
534         this.defaultOrderButton.setSelection(true);
535         defaultsButtonSelected(true);
536         maxItersField.loadDefault();
537         super.performDefaults();
538     }
539
540     /**
541      * OK has been pressed. If the defualt button is pressed then reset the build order to false;
542      * otherwise set it to the contents of the list.
543      */

544     public boolean performOk() {
545
546         String JavaDoc[] buildOrder = null;
547         boolean useDefault = defaultOrderButton.getSelection();
548
549         // if use defaults is turned off
550
if (!useDefault) {
551             buildOrder = buildList.getItems();
552         }
553
554         //Get a copy of the description from the workspace, set the build order and then
555
//apply it to the workspace.
556
IWorkspaceDescription description = getWorkspace().getDescription();
557         description.setBuildOrder(buildOrder);
558         description.setMaxBuildIterations(maxItersField.getIntValue());
559         try {
560             getWorkspace().setDescription(description);
561         } catch (CoreException exception) {
562             //failed - return false
563
return false;
564         }
565
566         // Perform auto-build if use default is off (because
567
// order could have changed) or if use default setting
568
// was changed.
569
if (!useDefault || (useDefault != defaultOrderInitiallySelected)) {
570             defaultOrderInitiallySelected = useDefault;
571             // If auto build is turned on, then do a global incremental
572
// build on all the projects.
573
if (ResourcesPlugin.getWorkspace().isAutoBuilding()) {
574                 GlobalBuildAction action = new GlobalBuildAction(workbench
575                         .getActiveWorkbenchWindow(),
576                         IncrementalProjectBuilder.INCREMENTAL_BUILD);
577                 action.doBuild();
578             }
579         }
580
581         // Clear the custom build order cache
582
customBuildOrder = null;
583
584         return true;
585     }
586
587     /**
588      * Remove the current selection in the build list.
589      */

590     private void removeSelection() {
591
592         this.buildList.remove(this.buildList.getSelectionIndices());
593     }
594
595     /**
596      * Set the widgets that select build order to be enabled or diabled.
597      * @param value boolean
598      */

599     private void setBuildOrderWidgetsEnablement(boolean value) {
600
601         // Only change enablement of buttons. Leave list alone
602
// because you can't scroll it when disabled.
603
Control[] children = this.buttonComposite.getChildren();
604         for (int i = 0; i < children.length; i++) {
605             children[i].setEnabled(value);
606         }
607     }
608
609     /**
610      * Return a sorted array of the names of the projects that are already in the currently
611      * displayed names.
612      * @return String[]
613      * @param allProjects - all of the projects in the workspace
614      * @param currentlyDisplayed - the names of the projects already being displayed
615      */

616     private String JavaDoc[] sortedDifference(IProject[] allProjects,
617             String JavaDoc[] currentlyDisplayed) {
618
619         TreeSet JavaDoc difference = new TreeSet JavaDoc();
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 JavaDoc[] returnValue = new String JavaDoc[difference.size()];
628         difference.toArray(returnValue);
629         return returnValue;
630     }
631 }
632
Popular Tags