KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.PDE;
33 import org.eclipse.pde.internal.builders.CompilerFlags;
34 import org.eclipse.pde.internal.ui.PDEUIMessages;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.events.ModifyEvent;
37 import org.eclipse.swt.events.ModifyListener;
38 import org.eclipse.swt.events.SelectionAdapter;
39 import org.eclipse.swt.events.SelectionEvent;
40 import org.eclipse.swt.events.SelectionListener;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.layout.GridLayout;
43 import org.eclipse.swt.widgets.Button;
44 import org.eclipse.swt.widgets.Combo;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Control;
47 import org.eclipse.swt.widgets.Group;
48 import org.eclipse.swt.widgets.Label;
49 import org.eclipse.swt.widgets.Shell;
50 import org.eclipse.swt.widgets.TabFolder;
51 import org.eclipse.swt.widgets.TabItem;
52 import org.eclipse.swt.widgets.Text;
53
54 /**
55  */

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

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

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

146         if (project != null) { // property page
147
try {
148                 if (project.hasNature(PDE.PLUGIN_NATURE)) {
149                     fPluginPage = createPage(
150                             container,
151                             PDEUIMessages.CompilersConfigurationBlock_plugins, CompilerFlags.PLUGIN_FLAGS, choices); //$NON-NLS-1$
152

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

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

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

373     public void performDefaults() {
374         fChangedControls.clear();
375         for (int i = 0; i < fFlagControls.size(); i++) {
376             boolean hasChange = false;
377             Control control = (Control) fFlagControls.get(i);
378             String JavaDoc flagId = (String JavaDoc) control.getData();
379             if (control instanceof Combo) {
380                 if (project != null)
381                     hasChange = CompilerFlags.getFlag(project, flagId) != CompilerFlags
382                             .getDefaultFlag(flagId);
383                 else
384                     hasChange = ((Combo) control).getSelectionIndex() != CompilerFlags
385                             .getDefaultFlag(flagId);
386                 ((Combo) control).select(CompilerFlags.getDefaultFlag(flagId));
387             } else if (control instanceof Button) {
388                 if (project != null)
389                     hasChange = CompilerFlags.getBoolean(project, flagId) != CompilerFlags
390                             .getDefaultBoolean(flagId);
391                 else
392                     hasChange = ((Button) control).getSelection() != CompilerFlags
393                             .getDefaultBoolean(flagId);
394                 ((Button) control).setSelection(CompilerFlags
395                         .getDefaultBoolean(flagId));
396             } else if (control instanceof Text) {
397                 if (project != null)
398                     hasChange = !CompilerFlags.getString(project, flagId)
399                             .equals(CompilerFlags.getDefaultString(flagId));
400                 else
401                     hasChange = ((Text) control).getText() != CompilerFlags
402                             .getDefaultString(flagId);
403                 ((Text) control)
404                         .setText(CompilerFlags.getDefaultString(flagId));
405             }
406             if (hasChange)
407                 fChangedControls.add(control);
408         }
409     }
410
411     public boolean performOk(boolean enabled) {
412         Set JavaDoc changedControls = fChangedControls;
413         if (!enabled) {
414             // fChangedControls is not a valid change.
415
// The change is the difference between values in
416
// PROJECT,INSTANCE,DEFAULD
417
// and INSTANCE,DEFAULT scopes.
418
changedControls = new HashSet JavaDoc();
419             for (Iterator JavaDoc iter = fFlagControls.iterator(); iter.hasNext();) {
420                 Control control = (Control) iter.next();
421                 String JavaDoc flagId = (String JavaDoc) control.getData();
422                 if (!CompilerFlags.getString(project, flagId).equals(
423                         CompilerFlags.getString(null, flagId))) {
424                     changedControls.add(control);
425                     break;
426                 }
427             }
428         }
429         boolean build = false;
430         if (changedControls.size() > 0) {
431             String JavaDoc title;
432             String JavaDoc message;
433             if (project != null) {
434                 title = PDEUIMessages.CompilersConfigurationBlock_rebuild_title; //$NON-NLS-1$
435
message = PDEUIMessages.CompilersConfigurationBlock_rebuild_message; //$NON-NLS-1$
436
} else {
437                 title = PDEUIMessages.CompilersConfigurationBlock_rebuild_many_title; //$NON-NLS-1$
438
message = PDEUIMessages.CompilersConfigurationBlock_rebuild_many_message; //$NON-NLS-1$
439

440             }
441
442             MessageDialog dialog = new MessageDialog(getShell(), title, null,
443                     message, MessageDialog.QUESTION, new String JavaDoc[] {
444                             IDialogConstants.YES_LABEL,
445                             IDialogConstants.NO_LABEL,
446                             IDialogConstants.CANCEL_LABEL }, 2);
447             int res = dialog.open();
448
449             if (res == 2) {
450                 return false;
451             } else if (res == 0) {
452                 build = true;
453             }
454         }
455         if (project != null
456                 && enabled != CompilerFlags.getBoolean(project,
457                         CompilerFlags.USE_PROJECT_PREF)) {
458             if (enabled) {
459                 CompilerFlags.setBoolean(project,
460                         CompilerFlags.USE_PROJECT_PREF, true);
461             } else {
462                 CompilerFlags.clear(project, CompilerFlags.USE_PROJECT_PREF);
463             }
464         }
465         if (changedControls.size() > 0) {
466             fBuilders = new HashSet JavaDoc();
467             for (Iterator JavaDoc iter = changedControls.iterator(); iter.hasNext();) {
468                 Control control = (Control) iter.next();
469                 String JavaDoc flagId = (String JavaDoc) control.getData();
470                 if (control instanceof Combo) {
471                     int index = ((Combo) control).getSelectionIndex();
472                     if (project == null) {
473                         CompilerFlags.setFlag(flagId, index);
474                     }
475                 } else if (control instanceof Button) {
476                     boolean value = ((Button) control).getSelection();
477                     if (project == null) {
478                         CompilerFlags.setBoolean(flagId, value);
479                     }
480                 } else if (control instanceof Text) {
481                     String JavaDoc value = ((Text) control).getText();
482                     if (project == null) {
483                         CompilerFlags.setString(flagId, value);
484                     }
485                 }
486                 if (control.getParent().equals(fPluginPage))
487                     fBuilders.add(PDE.MANIFEST_BUILDER_ID);
488                 else if (control.getParent().equals(fSchemaPage))
489                     fBuilders.add(PDE.SCHEMA_BUILDER_ID);
490                 else if (control.getParent().equals(fFeaturePage)) {
491                     fBuilders.add(PDE.FEATURE_BUILDER_ID);
492                     fBuilders.add(PDE.SITE_BUILDER_ID);
493                 }
494             }
495             if (project == null) {
496                 CompilerFlags.save();
497             }
498         }
499         if (project != null) {
500             for (Iterator JavaDoc iter = fFlagControls.iterator(); iter.hasNext();) {
501                 Control control = (Control) iter.next();
502                 String JavaDoc flagId = (String JavaDoc) control.getData();
503                 if (control instanceof Combo) {
504                     int index = ((Combo) control).getSelectionIndex();
505                     if (enabled) {
506                         CompilerFlags.setFlag(project, flagId, index);
507                     } else {
508                         CompilerFlags.clear(project, flagId);
509                     }
510                 } else if (control instanceof Button) {
511                     boolean value = ((Button) control).getSelection();
512                     if (enabled) {
513                         CompilerFlags.setBoolean(project, flagId, value);
514                     } else {
515                         CompilerFlags.clear(project, flagId);
516                     }
517                 } else if (control instanceof Text) {
518                     String JavaDoc value = ((Text) control).getText();
519                     if (enabled) {
520                         CompilerFlags.setString(project, flagId, value);
521                     } else {
522                         CompilerFlags.clear(project, flagId);
523                     }
524                 }
525             }
526         }
527
528         if (build && fBuilders.size() > 0) {
529             doFullBuild();
530         }
531
532         fChangedControls.clear();
533         return true;
534     }
535
536     protected void setShell(Shell shell) {
537         fShell = shell;
538     }
539 }
540
Popular Tags