KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > preferences > CompilersConfigurationTab


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 package org.eclipse.pde.internal.ui.preferences;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IncrementalProjectBuilder;
21 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.OperationCanceledException;
26 import org.eclipse.core.runtime.Status;
27 import org.eclipse.core.runtime.SubProgressMonitor;
28 import org.eclipse.core.runtime.jobs.Job;
29 import org.eclipse.jface.dialogs.Dialog;
30 import org.eclipse.jface.dialogs.IDialogConstants;
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.pde.internal.core.builders.CompilerFlags;
33 import org.eclipse.pde.internal.core.natures.PDE;
34 import org.eclipse.pde.internal.ui.PDEPlugin;
35 import org.eclipse.pde.internal.ui.PDEUIMessages;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.events.ModifyEvent;
38 import org.eclipse.swt.events.ModifyListener;
39 import org.eclipse.swt.events.SelectionAdapter;
40 import org.eclipse.swt.events.SelectionEvent;
41 import org.eclipse.swt.events.SelectionListener;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.layout.GridLayout;
44 import org.eclipse.swt.widgets.Button;
45 import org.eclipse.swt.widgets.Combo;
46 import org.eclipse.swt.widgets.Composite;
47 import org.eclipse.swt.widgets.Control;
48 import org.eclipse.swt.widgets.Group;
49 import org.eclipse.swt.widgets.Label;
50 import org.eclipse.swt.widgets.Shell;
51 import org.eclipse.swt.widgets.TabFolder;
52 import org.eclipse.swt.widgets.TabItem;
53 import org.eclipse.swt.widgets.Text;
54
55 /**
56  */

57 public class CompilersConfigurationTab {
58
59     private Set JavaDoc fBuilders = new HashSet JavaDoc();
60
61     private Set JavaDoc fChangedControls = new HashSet JavaDoc();
62
63     private Composite fFeaturePage;
64
65     private List JavaDoc fFlagControls;
66
67     private Composite fPluginPage;
68
69     private Composite fSchemaPage;
70
71     private Shell fShell;
72     
73     // The size of label array must match CompilerFlag.fFlags
74
private static final String JavaDoc[][] fLabels = {
75             { PDEUIMessages.compilers_p_unresolved_import,
76                     PDEUIMessages.CompilersConfigurationTab_incompatEnv,
77                     PDEUIMessages.compilers_p_unresolved_ex_points,
78                     PDEUIMessages.compilers_p_no_required_att,
79                     PDEUIMessages.compilers_p_unknown_element,
80                     PDEUIMessages.compilers_p_unknown_attribute,
81                     PDEUIMessages.compilers_p_deprecated,
82                     PDEUIMessages.compilers_p_unknown_class,
83                     PDEUIMessages.compilers_p_unknown_resource,
84                     PDEUIMessages.compilers_p_not_externalized_att,
85                     PDEUIMessages.CompilersConfigurationTab_buildPropertiesErrors,
86                     PDEUIMessages.compilers_p_exported_pkgs},
87             { PDEUIMessages.compilers_s_create_docs,
88                     PDEUIMessages.compilers_s_doc_folder,
89                     PDEUIMessages.compilers_s_open_tags },
90             { PDEUIMessages.compilers_f_unresolved_plugins,
91                     PDEUIMessages.compilers_f_unresolved_features }, {} };
92
93     /**
94      * @param project nNot null in property page
95      */

96     private IProject project;
97
98     public CompilersConfigurationTab(IProject project) {
99         this.project = project;
100     }
101
102     private void addChangedConrol(Control control) {
103         String JavaDoc flagId = (String JavaDoc) control.getData();
104         boolean doAdd = false;
105         if (control instanceof Combo) {
106             int newIndex = ((Combo) control).getSelectionIndex();
107             int oldIndex = CompilerFlags.getFlag(project, flagId);
108             doAdd = (newIndex != oldIndex);
109         } else if (control instanceof Button) {
110             boolean newValue = ((Button) control).getSelection();
111             boolean oldValue = CompilerFlags.getBoolean(project, flagId);
112             doAdd = oldValue != newValue;
113         } else if (control instanceof Text) {
114             String JavaDoc newValue = ((Text) control).getText();
115             String JavaDoc oldValue = CompilerFlags.getString(project, flagId);
116             doAdd = !newValue.equals(oldValue);
117         }
118         if (doAdd)
119             fChangedControls.add(control);
120         else if (fChangedControls.contains(control))
121             fChangedControls.remove(control);
122     }
123
124     /**
125      * @see org.eclipse.jface.preference.PreferencePage#createContents(Composite)
126      */

127     public Control createContents(Composite parent) {
128         setShell(parent.getShell());
129
130         Composite container = new Composite(parent, SWT.NULL);
131         GridLayout layout = new GridLayout();
132         container.setLayout(layout);
133
134         fFlagControls = new ArrayList JavaDoc();
135         SelectionListener listener = new SelectionAdapter() {
136             public void widgetSelected(SelectionEvent e) {
137                 addChangedConrol((Control) e.widget);
138             }
139         };
140
141         ModifyListener mlistener = new ModifyListener() {
142             public void modifyText(ModifyEvent e) {
143                 addChangedConrol((Control) e.widget);
144             }
145         };
146
147         String JavaDoc[] choices = new String JavaDoc[] {
148                 PDEUIMessages.CompilersConfigurationBlock_error, PDEUIMessages.CompilersConfigurationBlock_warning, PDEUIMessages.CompilersConfigurationBlock_ignore }; //
149

150         if (project != null) { // property page
151
try {
152                 if (project.hasNature(PDE.PLUGIN_NATURE)) {
153                     fPluginPage = createPage(
154                             container,
155                             PDEUIMessages.CompilersConfigurationBlock_plugins, CompilerFlags.PLUGIN_FLAGS, choices);
156
157                 }
158             } catch (CoreException ce) {
159                 // does not exist or is closed
160
}
161         } else { // preference page
162
TabFolder folder = new TabFolder(container, SWT.NONE);
163             GridData gd = new GridData(GridData.FILL_BOTH);
164             folder.setLayoutData(gd);
165
166             fPluginPage = createPage(
167                     folder,
168                     PDEUIMessages.CompilersConfigurationBlock_plugins, CompilerFlags.PLUGIN_FLAGS, choices);
169             fSchemaPage = createPage(
170                     folder,
171                     PDEUIMessages.CompilersConfigurationBlock_schemas, CompilerFlags.SCHEMA_FLAGS, choices);
172             fFeaturePage = createPage(
173                     folder,
174                     PDEUIMessages.CompilersConfigurationBlock_features, CompilerFlags.FEATURE_FLAGS, choices);
175             // createPage(folder,
176
// PDEPlugin.getResourceString("CompilersConfigurationBlock.sites"),
177
// CompilerFlags.SITE_FLAGS, choices); //$NON-NLS-1$
178
}
179
180         for (int i = 0; i < fFlagControls.size(); i++) {
181             Control control = (Control) fFlagControls.get(i);
182             if (control instanceof Combo)
183                 ((Combo) control).addSelectionListener(listener);
184             else if (control instanceof Button)
185                 ((Button) control).addSelectionListener(listener);
186             else if (control instanceof Text)
187                 ((Text) control).addModifyListener(mlistener);
188         }
189         Dialog.applyDialogFont(parent);
190         return container;
191     }
192
193     private Control createFlag(Composite page, String JavaDoc flagId, String JavaDoc[] choices) {
194         Control control = null;
195         if (CompilerFlags.getFlagType(flagId) == CompilerFlags.MARKER) {
196             Label label = new Label(page, SWT.NULL);
197             label.setText(getFlagLabel(flagId));
198             Combo combo = new Combo(page, SWT.READ_ONLY);
199             combo.setItems(choices);
200             combo.select(CompilerFlags.getFlag(project, flagId));
201             control = combo;
202         } else if (CompilerFlags.getFlagType(flagId) == CompilerFlags.BOOLEAN) {
203             Button button = new Button(page, SWT.CHECK);
204             button.setText(getFlagLabel(flagId));
205             button.setSelection(CompilerFlags.getBoolean(project, flagId));
206             GridData gd = new GridData();
207             gd.horizontalSpan = 2;
208             button.setLayoutData(gd);
209             control = button;
210         } else if (CompilerFlags.getFlagType(flagId) == CompilerFlags.STRING) {
211             Label label = new Label(page, SWT.NULL);
212             label.setText(getFlagLabel(flagId));
213             Text text = new Text(page, SWT.SINGLE | SWT.BORDER);
214             text.setText(CompilerFlags.getString(project, flagId));
215             GridData gd = new GridData(GridData.FILL_HORIZONTAL);
216             gd.widthHint = 50;
217             text.setLayoutData(gd);
218
219             new Label(page, SWT.NULL).setLayoutData(new GridData());
220             GridData sgd = new GridData();
221             Label slabel = new Label(page, SWT.NULL);
222             slabel.setText(PDEUIMessages.CompilersConfigurationBlock_label);
223             sgd.horizontalSpan = 2;
224             slabel.setLayoutData(sgd);
225
226             control = text;
227         }
228         control.setData(flagId);
229         return control;
230     }
231     
232     private String JavaDoc getFlagLabel(String JavaDoc flagId) {
233         for (int i = 0; i < fLabels.length; i++) {
234             String JavaDoc[] flags = CompilerFlags.getFlags(i);
235             for (int j = 0; j < flags.length; j++) {
236                 if (flags[j].equals(flagId)) {
237                     return fLabels[i][j];
238                 }
239             }
240         }
241         return ""; //$NON-NLS-1$
242
}
243
244     private Composite createPage(Composite parent, String JavaDoc name, int index,
245             String JavaDoc[] choices) {
246         Group group = new Group(parent, SWT.NONE);
247         GridLayout layout = new GridLayout();
248         layout.numColumns = 2;
249         group.setLayout(layout);
250
251         String JavaDoc labelText;
252         if (index == CompilerFlags.SCHEMA_FLAGS)
253             labelText = PDEUIMessages.CompilersConfigurationBlock_altlabel;
254         else
255             labelText = PDEUIMessages.CompilersConfigurationBlock_label;
256         group.setText(labelText);
257         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
258         gd.horizontalSpan = 2;
259         gd.grabExcessHorizontalSpace = true;
260         group.setLayoutData(gd);
261         
262         String JavaDoc[] flagIds = CompilerFlags.getFlags(index);
263         for (int i = 0; i < flagIds.length; i++) {
264             Control control = createFlag(group, flagIds[i], choices);
265             fFlagControls.add(control);
266         }
267         return group;
268     }
269     private Composite createPage(TabFolder folder, String JavaDoc name, int index,
270             String JavaDoc[] choices) {
271         Composite page = new Composite(folder, SWT.NONE);
272         GridLayout layout = new GridLayout();
273         layout.numColumns = 2;
274         page.setLayout(layout);
275
276         TabItem tab = new TabItem(folder, SWT.NONE);
277         tab.setText(name);
278         tab.setControl(page);
279
280         Label label = new Label(page, SWT.NULL);
281         String JavaDoc labelText;
282         if (index == CompilerFlags.SCHEMA_FLAGS)
283             labelText = PDEUIMessages.CompilersConfigurationBlock_altlabel;
284         else
285             labelText = PDEUIMessages.CompilersConfigurationBlock_label;
286         label.setText(labelText);
287         GridData gd = new GridData();
288         gd.horizontalSpan = 2;
289         label.setLayoutData(gd);
290         
291         String JavaDoc[] flagIds = CompilerFlags.getFlags(index);
292         for (int i = 0; i < flagIds.length; i++) {
293             Control control = createFlag(page, flagIds[i], choices);
294             fFlagControls.add(control);
295         }
296         return page;
297     }
298
299     private void doFullBuild() {
300         Job buildJob = new Job(PDEUIMessages.CompilersConfigurationBlock_building) {
301             /*
302              * (non-Javadoc)
303              *
304              * @see org.eclipse.core.runtime.jobs.Job#belongsTo(java.lang.Object)
305              */

306             public boolean belongsTo(Object JavaDoc family) {
307                 return ResourcesPlugin.FAMILY_MANUAL_BUILD == family;
308             }
309
310             //$NON-NLS-1$
311
/*
312              * (non-Javadoc)
313              *
314              * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
315              */

316             protected IStatus run(IProgressMonitor monitor) {
317                 try {
318                     IProject[] projects = null;
319                     if (project == null) {
320                         projects = PDEPlugin.getWorkspace().getRoot().getProjects();
321                     } else {
322                         projects = new IProject[] { project };
323                     }
324                     monitor.beginTask("", projects.length * 2); //$NON-NLS-1$
325
for (int i = 0; i < projects.length; i++) {
326                         IProject projectToBuild = projects[i];
327                         if (!projectToBuild.isOpen())
328                             continue;
329                         if (projectToBuild.hasNature(PDE.PLUGIN_NATURE)) {
330                             if (fBuilders.contains(PDE.MANIFEST_BUILDER_ID))
331                                 projectToBuild.build(
332                                         IncrementalProjectBuilder.FULL_BUILD,
333                                         PDE.MANIFEST_BUILDER_ID, null,
334                                         new SubProgressMonitor(monitor, 1));
335                             else
336                                 monitor.worked(1);
337                             if (fBuilders.contains(PDE.SCHEMA_BUILDER_ID))
338                                 projectToBuild.build(
339                                         IncrementalProjectBuilder.FULL_BUILD,
340                                         PDE.SCHEMA_BUILDER_ID, null,
341                                         new SubProgressMonitor(monitor, 1));
342                             else
343                                 monitor.worked(1);
344                         } else if (projectToBuild.hasNature(PDE.FEATURE_NATURE)) {
345                             if (fBuilders.contains(PDE.FEATURE_BUILDER_ID))
346                                 projectToBuild.build(
347                                         IncrementalProjectBuilder.FULL_BUILD,
348                                         PDE.FEATURE_BUILDER_ID, null,
349                                         new SubProgressMonitor(monitor, 2));
350                         } else {
351                             monitor.worked(2);
352                         }
353                     }
354                 } catch (CoreException e) {
355                     return e.getStatus();
356                 } catch (OperationCanceledException e) {
357                     return Status.CANCEL_STATUS;
358                 } finally {
359                     monitor.done();
360                 }
361                 return Status.OK_STATUS;
362             }
363         };
364         buildJob.setRule(ResourcesPlugin.getWorkspace().getRuleFactory()
365                 .buildRule());
366         buildJob.setUser(true);
367         buildJob.schedule();
368     }
369
370     protected Shell getShell() {
371         return fShell;
372     }
373
374     /**
375      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
376      */

377     public void performDefaults() {
378         fChangedControls.clear();
379         for (int i = 0; i < fFlagControls.size(); i++) {
380             boolean hasChange = false;
381             Control control = (Control) fFlagControls.get(i);
382             String JavaDoc flagId = (String JavaDoc) control.getData();
383             if (control instanceof Combo) {
384                 if (project != null)
385                     hasChange = CompilerFlags.getFlag(project, flagId) != CompilerFlags
386                             .getDefaultFlag(flagId);
387                 else
388                     hasChange = ((Combo) control).getSelectionIndex() != CompilerFlags
389                             .getDefaultFlag(flagId);
390                 ((Combo) control).select(CompilerFlags.getDefaultFlag(flagId));
391             } else if (control instanceof Button) {
392                 if (project != null)
393                     hasChange = CompilerFlags.getBoolean(project, flagId) != CompilerFlags
394                             .getDefaultBoolean(flagId);
395                 else
396                     hasChange = ((Button) control).getSelection() != CompilerFlags
397                             .getDefaultBoolean(flagId);
398                 ((Button) control).setSelection(CompilerFlags
399                         .getDefaultBoolean(flagId));
400             } else if (control instanceof Text) {
401                 if (project != null)
402                     hasChange = !CompilerFlags.getString(project, flagId)
403                             .equals(CompilerFlags.getDefaultString(flagId));
404                 else
405                     hasChange = ((Text) control).getText() != CompilerFlags
406                             .getDefaultString(flagId);
407                 ((Text) control)
408                         .setText(CompilerFlags.getDefaultString(flagId));
409             }
410             if (hasChange)
411                 fChangedControls.add(control);
412         }
413     }
414
415     public boolean performOk(boolean enabled) {
416         Set JavaDoc changedControls = fChangedControls;
417         if (!enabled) {
418             // fChangedControls is not a valid change.
419
// The change is the difference between values in
420
// PROJECT,INSTANCE,DEFAULD
421
// and INSTANCE,DEFAULT scopes.
422
changedControls = new HashSet JavaDoc();
423             for (Iterator JavaDoc iter = fFlagControls.iterator(); iter.hasNext();) {
424                 Control control = (Control) iter.next();
425                 String JavaDoc flagId = (String JavaDoc) control.getData();
426                 if (!CompilerFlags.getString(project, flagId).equals(
427                         CompilerFlags.getString(null, flagId))) {
428                     changedControls.add(control);
429                     break;
430                 }
431             }
432         }
433         boolean build = false;
434         if (changedControls.size() > 0) {
435             String JavaDoc title;
436             String JavaDoc message;
437             if (project != null) {
438                 title = PDEUIMessages.CompilersConfigurationBlock_rebuild_title;
439                 message = PDEUIMessages.CompilersConfigurationBlock_rebuild_message;
440             } else {
441                 title = PDEUIMessages.CompilersConfigurationBlock_rebuild_many_title;
442                 message = PDEUIMessages.CompilersConfigurationBlock_rebuild_many_message;
443
444             }
445
446             MessageDialog dialog = new MessageDialog(getShell(), title, null,
447                     message, MessageDialog.QUESTION, new String JavaDoc[] {
448                             IDialogConstants.YES_LABEL,
449                             IDialogConstants.NO_LABEL,
450                             IDialogConstants.CANCEL_LABEL }, 2);
451             int res = dialog.open();
452
453             if (res == 2) {
454                 return false;
455             } else if (res == 0) {
456                 build = true;
457             }
458         }
459         if (project != null
460                 && enabled != CompilerFlags.getBoolean(project,
461                         CompilerFlags.USE_PROJECT_PREF)) {
462             if (enabled) {
463                 CompilerFlags.setBoolean(project,
464                         CompilerFlags.USE_PROJECT_PREF, true);
465             } else {
466                 CompilerFlags.clear(project, CompilerFlags.USE_PROJECT_PREF);
467             }
468         }
469         if (changedControls.size() > 0) {
470             fBuilders = new HashSet JavaDoc();
471             for (Iterator JavaDoc iter = changedControls.iterator(); iter.hasNext();) {
472                 Control control = (Control) iter.next();
473                 String JavaDoc flagId = (String JavaDoc) control.getData();
474                 if (control instanceof Combo) {
475                     int index = ((Combo) control).getSelectionIndex();
476                     if (project == null) {
477                         CompilerFlags.setFlag(flagId, index);
478                     }
479                 } else if (control instanceof Button) {
480                     boolean value = ((Button) control).getSelection();
481                     if (project == null) {
482                         CompilerFlags.setBoolean(flagId, value);
483                     }
484                 } else if (control instanceof Text) {
485                     String JavaDoc value = ((Text) control).getText();
486                     if (project == null) {
487                         CompilerFlags.setString(flagId, value);
488                     }
489                 }
490                 if (control.getParent().equals(fPluginPage))
491                     fBuilders.add(PDE.MANIFEST_BUILDER_ID);
492                 else if (control.getParent().equals(fSchemaPage))
493                     fBuilders.add(PDE.SCHEMA_BUILDER_ID);
494                 else if (control.getParent().equals(fFeaturePage)) {
495                     fBuilders.add(PDE.FEATURE_BUILDER_ID);
496                     fBuilders.add(PDE.SITE_BUILDER_ID);
497                 }
498             }
499             if (project == null) {
500                 CompilerFlags.save();
501             }
502         }
503         if (project != null) {
504             for (Iterator JavaDoc iter = fFlagControls.iterator(); iter.hasNext();) {
505                 Control control = (Control) iter.next();
506                 String JavaDoc flagId = (String JavaDoc) control.getData();
507                 if (control instanceof Combo) {
508                     int index = ((Combo) control).getSelectionIndex();
509                     if (enabled) {
510                         CompilerFlags.setFlag(project, flagId, index);
511                     } else {
512                         CompilerFlags.clear(project, flagId);
513                     }
514                 } else if (control instanceof Button) {
515                     boolean value = ((Button) control).getSelection();
516                     if (enabled) {
517                         CompilerFlags.setBoolean(project, flagId, value);
518                     } else {
519                         CompilerFlags.clear(project, flagId);
520                     }
521                 } else if (control instanceof Text) {
522                     String JavaDoc value = ((Text) control).getText();
523                     if (enabled) {
524                         CompilerFlags.setString(project, flagId, value);
525                     } else {
526                         CompilerFlags.clear(project, flagId);
527                     }
528                 }
529             }
530         }
531
532         if (build && fBuilders.size() > 0) {
533             doFullBuild();
534         }
535
536         fChangedControls.clear();
537         return true;
538     }
539
540     protected void setShell(Shell shell) {
541         fShell = shell;
542     }
543 }
544
Popular Tags