001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.preferences;
011:
012: import java.util.ArrayList;
013: import java.util.HashSet;
014: import java.util.Iterator;
015: import java.util.List;
016: import java.util.Set;
017:
018: import org.eclipse.core.resources.IProject;
019: import org.eclipse.core.resources.IncrementalProjectBuilder;
020: import org.eclipse.core.resources.ResourcesPlugin;
021: import org.eclipse.core.runtime.CoreException;
022: import org.eclipse.core.runtime.IProgressMonitor;
023: import org.eclipse.core.runtime.IStatus;
024: import org.eclipse.core.runtime.OperationCanceledException;
025: import org.eclipse.core.runtime.Status;
026: import org.eclipse.core.runtime.SubProgressMonitor;
027: import org.eclipse.core.runtime.jobs.Job;
028: import org.eclipse.jface.dialogs.Dialog;
029: import org.eclipse.jface.dialogs.IDialogConstants;
030: import org.eclipse.jface.dialogs.MessageDialog;
031: import org.eclipse.pde.internal.core.builders.CompilerFlags;
032: import org.eclipse.pde.internal.core.natures.PDE;
033: import org.eclipse.pde.internal.ui.PDEPlugin;
034: import org.eclipse.pde.internal.ui.PDEUIMessages;
035: import org.eclipse.swt.SWT;
036: import org.eclipse.swt.events.ModifyEvent;
037: import org.eclipse.swt.events.ModifyListener;
038: import org.eclipse.swt.events.SelectionAdapter;
039: import org.eclipse.swt.events.SelectionEvent;
040: import org.eclipse.swt.events.SelectionListener;
041: import org.eclipse.swt.layout.GridData;
042: import org.eclipse.swt.layout.GridLayout;
043: import org.eclipse.swt.widgets.Button;
044: import org.eclipse.swt.widgets.Combo;
045: import org.eclipse.swt.widgets.Composite;
046: import org.eclipse.swt.widgets.Control;
047: import org.eclipse.swt.widgets.Group;
048: import org.eclipse.swt.widgets.Label;
049: import org.eclipse.swt.widgets.Shell;
050: import org.eclipse.swt.widgets.TabFolder;
051: import org.eclipse.swt.widgets.TabItem;
052: import org.eclipse.swt.widgets.Text;
053:
054: /**
055: */
056: public class CompilersConfigurationTab {
057:
058: private Set fBuilders = new HashSet();
059:
060: private Set fChangedControls = new HashSet();
061:
062: private Composite fFeaturePage;
063:
064: private List fFlagControls;
065:
066: private Composite fPluginPage;
067:
068: private Composite fSchemaPage;
069:
070: private Shell fShell;
071:
072: // The size of label array must match CompilerFlag.fFlags
073: private static final String[][] fLabels = {
074: {
075: PDEUIMessages.compilers_p_unresolved_import,
076: PDEUIMessages.CompilersConfigurationTab_incompatEnv,
077: PDEUIMessages.compilers_p_unresolved_ex_points,
078: PDEUIMessages.compilers_p_no_required_att,
079: PDEUIMessages.compilers_p_unknown_element,
080: PDEUIMessages.compilers_p_unknown_attribute,
081: PDEUIMessages.compilers_p_deprecated,
082: PDEUIMessages.compilers_p_unknown_class,
083: PDEUIMessages.compilers_p_unknown_resource,
084: PDEUIMessages.compilers_p_not_externalized_att,
085: PDEUIMessages.CompilersConfigurationTab_buildPropertiesErrors,
086: PDEUIMessages.compilers_p_exported_pkgs },
087: { PDEUIMessages.compilers_s_create_docs,
088: PDEUIMessages.compilers_s_doc_folder,
089: PDEUIMessages.compilers_s_open_tags },
090: { PDEUIMessages.compilers_f_unresolved_plugins,
091: PDEUIMessages.compilers_f_unresolved_features }, {} };
092:
093: /**
094: * @param project nNot null in property page
095: */
096: private IProject project;
097:
098: public CompilersConfigurationTab(IProject project) {
099: this .project = project;
100: }
101:
102: private void addChangedConrol(Control control) {
103: String flagId = (String) 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
112: .getBoolean(project, flagId);
113: doAdd = oldValue != newValue;
114: } else if (control instanceof Text) {
115: String newValue = ((Text) control).getText();
116: String oldValue = CompilerFlags.getString(project, flagId);
117: doAdd = !newValue.equals(oldValue);
118: }
119: if (doAdd)
120: fChangedControls.add(control);
121: else if (fChangedControls.contains(control))
122: fChangedControls.remove(control);
123: }
124:
125: /**
126: * @see org.eclipse.jface.preference.PreferencePage#createContents(Composite)
127: */
128: public Control createContents(Composite parent) {
129: setShell(parent.getShell());
130:
131: Composite container = new Composite(parent, SWT.NULL);
132: GridLayout layout = new GridLayout();
133: container.setLayout(layout);
134:
135: fFlagControls = new ArrayList();
136: SelectionListener listener = new SelectionAdapter() {
137: public void widgetSelected(SelectionEvent e) {
138: addChangedConrol((Control) e.widget);
139: }
140: };
141:
142: ModifyListener mlistener = new ModifyListener() {
143: public void modifyText(ModifyEvent e) {
144: addChangedConrol((Control) e.widget);
145: }
146: };
147:
148: String[] choices = new String[] {
149: PDEUIMessages.CompilersConfigurationBlock_error,
150: PDEUIMessages.CompilersConfigurationBlock_warning,
151: PDEUIMessages.CompilersConfigurationBlock_ignore }; //
152:
153: if (project != null) { // property page
154: try {
155: if (project.hasNature(PDE.PLUGIN_NATURE)) {
156: fPluginPage = createPage(
157: container,
158: PDEUIMessages.CompilersConfigurationBlock_plugins,
159: CompilerFlags.PLUGIN_FLAGS, choices);
160:
161: }
162: } catch (CoreException ce) {
163: // does not exist or is closed
164: }
165: } else { // preference page
166: TabFolder folder = new TabFolder(container, SWT.NONE);
167: GridData gd = new GridData(GridData.FILL_BOTH);
168: folder.setLayoutData(gd);
169:
170: fPluginPage = createPage(folder,
171: PDEUIMessages.CompilersConfigurationBlock_plugins,
172: CompilerFlags.PLUGIN_FLAGS, choices);
173: fSchemaPage = createPage(folder,
174: PDEUIMessages.CompilersConfigurationBlock_schemas,
175: CompilerFlags.SCHEMA_FLAGS, choices);
176: fFeaturePage = createPage(folder,
177: PDEUIMessages.CompilersConfigurationBlock_features,
178: CompilerFlags.FEATURE_FLAGS, choices);
179: // createPage(folder,
180: // PDEPlugin.getResourceString("CompilersConfigurationBlock.sites"),
181: // CompilerFlags.SITE_FLAGS, choices); //$NON-NLS-1$
182: }
183:
184: for (int i = 0; i < fFlagControls.size(); i++) {
185: Control control = (Control) fFlagControls.get(i);
186: if (control instanceof Combo)
187: ((Combo) control).addSelectionListener(listener);
188: else if (control instanceof Button)
189: ((Button) control).addSelectionListener(listener);
190: else if (control instanceof Text)
191: ((Text) control).addModifyListener(mlistener);
192: }
193: Dialog.applyDialogFont(parent);
194: return container;
195: }
196:
197: private Control createFlag(Composite page, String flagId,
198: String[] choices) {
199: Control control = null;
200: if (CompilerFlags.getFlagType(flagId) == CompilerFlags.MARKER) {
201: Label label = new Label(page, SWT.NULL);
202: label.setText(getFlagLabel(flagId));
203: Combo combo = new Combo(page, SWT.READ_ONLY);
204: combo.setItems(choices);
205: combo.select(CompilerFlags.getFlag(project, flagId));
206: control = combo;
207: } else if (CompilerFlags.getFlagType(flagId) == CompilerFlags.BOOLEAN) {
208: Button button = new Button(page, SWT.CHECK);
209: button.setText(getFlagLabel(flagId));
210: button.setSelection(CompilerFlags.getBoolean(project,
211: flagId));
212: GridData gd = new GridData();
213: gd.horizontalSpan = 2;
214: button.setLayoutData(gd);
215: control = button;
216: } else if (CompilerFlags.getFlagType(flagId) == CompilerFlags.STRING) {
217: Label label = new Label(page, SWT.NULL);
218: label.setText(getFlagLabel(flagId));
219: Text text = new Text(page, SWT.SINGLE | SWT.BORDER);
220: text.setText(CompilerFlags.getString(project, flagId));
221: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
222: gd.widthHint = 50;
223: text.setLayoutData(gd);
224:
225: new Label(page, SWT.NULL).setLayoutData(new GridData());
226: GridData sgd = new GridData();
227: Label slabel = new Label(page, SWT.NULL);
228: slabel
229: .setText(PDEUIMessages.CompilersConfigurationBlock_label);
230: sgd.horizontalSpan = 2;
231: slabel.setLayoutData(sgd);
232:
233: control = text;
234: }
235: control.setData(flagId);
236: return control;
237: }
238:
239: private String getFlagLabel(String flagId) {
240: for (int i = 0; i < fLabels.length; i++) {
241: String[] flags = CompilerFlags.getFlags(i);
242: for (int j = 0; j < flags.length; j++) {
243: if (flags[j].equals(flagId)) {
244: return fLabels[i][j];
245: }
246: }
247: }
248: return ""; //$NON-NLS-1$
249: }
250:
251: private Composite createPage(Composite parent, String name,
252: int index, String[] choices) {
253: Group group = new Group(parent, SWT.NONE);
254: GridLayout layout = new GridLayout();
255: layout.numColumns = 2;
256: group.setLayout(layout);
257:
258: String labelText;
259: if (index == CompilerFlags.SCHEMA_FLAGS)
260: labelText = PDEUIMessages.CompilersConfigurationBlock_altlabel;
261: else
262: labelText = PDEUIMessages.CompilersConfigurationBlock_label;
263: group.setText(labelText);
264: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
265: gd.horizontalSpan = 2;
266: gd.grabExcessHorizontalSpace = true;
267: group.setLayoutData(gd);
268:
269: String[] flagIds = CompilerFlags.getFlags(index);
270: for (int i = 0; i < flagIds.length; i++) {
271: Control control = createFlag(group, flagIds[i], choices);
272: fFlagControls.add(control);
273: }
274: return group;
275: }
276:
277: private Composite createPage(TabFolder folder, String name,
278: int index, String[] choices) {
279: Composite page = new Composite(folder, SWT.NONE);
280: GridLayout layout = new GridLayout();
281: layout.numColumns = 2;
282: page.setLayout(layout);
283:
284: TabItem tab = new TabItem(folder, SWT.NONE);
285: tab.setText(name);
286: tab.setControl(page);
287:
288: Label label = new Label(page, SWT.NULL);
289: String labelText;
290: if (index == CompilerFlags.SCHEMA_FLAGS)
291: labelText = PDEUIMessages.CompilersConfigurationBlock_altlabel;
292: else
293: labelText = PDEUIMessages.CompilersConfigurationBlock_label;
294: label.setText(labelText);
295: GridData gd = new GridData();
296: gd.horizontalSpan = 2;
297: label.setLayoutData(gd);
298:
299: String[] flagIds = CompilerFlags.getFlags(index);
300: for (int i = 0; i < flagIds.length; i++) {
301: Control control = createFlag(page, flagIds[i], choices);
302: fFlagControls.add(control);
303: }
304: return page;
305: }
306:
307: private void doFullBuild() {
308: Job buildJob = new Job(
309: PDEUIMessages.CompilersConfigurationBlock_building) {
310: /*
311: * (non-Javadoc)
312: *
313: * @see org.eclipse.core.runtime.jobs.Job#belongsTo(java.lang.Object)
314: */
315: public boolean belongsTo(Object family) {
316: return ResourcesPlugin.FAMILY_MANUAL_BUILD == family;
317: }
318:
319: //$NON-NLS-1$
320: /*
321: * (non-Javadoc)
322: *
323: * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
324: */
325: protected IStatus run(IProgressMonitor monitor) {
326: try {
327: IProject[] projects = null;
328: if (project == null) {
329: projects = PDEPlugin.getWorkspace().getRoot()
330: .getProjects();
331: } else {
332: projects = new IProject[] { project };
333: }
334: monitor.beginTask("", projects.length * 2); //$NON-NLS-1$
335: for (int i = 0; i < projects.length; i++) {
336: IProject projectToBuild = projects[i];
337: if (!projectToBuild.isOpen())
338: continue;
339: if (projectToBuild.hasNature(PDE.PLUGIN_NATURE)) {
340: if (fBuilders
341: .contains(PDE.MANIFEST_BUILDER_ID))
342: projectToBuild
343: .build(
344: IncrementalProjectBuilder.FULL_BUILD,
345: PDE.MANIFEST_BUILDER_ID,
346: null,
347: new SubProgressMonitor(
348: monitor, 1));
349: else
350: monitor.worked(1);
351: if (fBuilders
352: .contains(PDE.SCHEMA_BUILDER_ID))
353: projectToBuild
354: .build(
355: IncrementalProjectBuilder.FULL_BUILD,
356: PDE.SCHEMA_BUILDER_ID,
357: null,
358: new SubProgressMonitor(
359: monitor, 1));
360: else
361: monitor.worked(1);
362: } else if (projectToBuild
363: .hasNature(PDE.FEATURE_NATURE)) {
364: if (fBuilders
365: .contains(PDE.FEATURE_BUILDER_ID))
366: projectToBuild
367: .build(
368: IncrementalProjectBuilder.FULL_BUILD,
369: PDE.FEATURE_BUILDER_ID,
370: null,
371: new SubProgressMonitor(
372: monitor, 2));
373: } else {
374: monitor.worked(2);
375: }
376: }
377: } catch (CoreException e) {
378: return e.getStatus();
379: } catch (OperationCanceledException e) {
380: return Status.CANCEL_STATUS;
381: } finally {
382: monitor.done();
383: }
384: return Status.OK_STATUS;
385: }
386: };
387: buildJob.setRule(ResourcesPlugin.getWorkspace()
388: .getRuleFactory().buildRule());
389: buildJob.setUser(true);
390: buildJob.schedule();
391: }
392:
393: protected Shell getShell() {
394: return fShell;
395: }
396:
397: /**
398: * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
399: */
400: public void performDefaults() {
401: fChangedControls.clear();
402: for (int i = 0; i < fFlagControls.size(); i++) {
403: boolean hasChange = false;
404: Control control = (Control) fFlagControls.get(i);
405: String flagId = (String) control.getData();
406: if (control instanceof Combo) {
407: if (project != null)
408: hasChange = CompilerFlags.getFlag(project, flagId) != CompilerFlags
409: .getDefaultFlag(flagId);
410: else
411: hasChange = ((Combo) control).getSelectionIndex() != CompilerFlags
412: .getDefaultFlag(flagId);
413: ((Combo) control).select(CompilerFlags
414: .getDefaultFlag(flagId));
415: } else if (control instanceof Button) {
416: if (project != null)
417: hasChange = CompilerFlags.getBoolean(project,
418: flagId) != CompilerFlags
419: .getDefaultBoolean(flagId);
420: else
421: hasChange = ((Button) control).getSelection() != CompilerFlags
422: .getDefaultBoolean(flagId);
423: ((Button) control).setSelection(CompilerFlags
424: .getDefaultBoolean(flagId));
425: } else if (control instanceof Text) {
426: if (project != null)
427: hasChange = !CompilerFlags.getString(project,
428: flagId).equals(
429: CompilerFlags.getDefaultString(flagId));
430: else
431: hasChange = ((Text) control).getText() != CompilerFlags
432: .getDefaultString(flagId);
433: ((Text) control).setText(CompilerFlags
434: .getDefaultString(flagId));
435: }
436: if (hasChange)
437: fChangedControls.add(control);
438: }
439: }
440:
441: public boolean performOk(boolean enabled) {
442: Set changedControls = fChangedControls;
443: if (!enabled) {
444: // fChangedControls is not a valid change.
445: // The change is the difference between values in
446: // PROJECT,INSTANCE,DEFAULD
447: // and INSTANCE,DEFAULT scopes.
448: changedControls = new HashSet();
449: for (Iterator iter = fFlagControls.iterator(); iter
450: .hasNext();) {
451: Control control = (Control) iter.next();
452: String flagId = (String) control.getData();
453: if (!CompilerFlags.getString(project, flagId).equals(
454: CompilerFlags.getString(null, flagId))) {
455: changedControls.add(control);
456: break;
457: }
458: }
459: }
460: boolean build = false;
461: if (changedControls.size() > 0) {
462: String title;
463: String message;
464: if (project != null) {
465: title = PDEUIMessages.CompilersConfigurationBlock_rebuild_title;
466: message = PDEUIMessages.CompilersConfigurationBlock_rebuild_message;
467: } else {
468: title = PDEUIMessages.CompilersConfigurationBlock_rebuild_many_title;
469: message = PDEUIMessages.CompilersConfigurationBlock_rebuild_many_message;
470:
471: }
472:
473: MessageDialog dialog = new MessageDialog(getShell(), title,
474: null, message, MessageDialog.QUESTION,
475: new String[] { IDialogConstants.YES_LABEL,
476: IDialogConstants.NO_LABEL,
477: IDialogConstants.CANCEL_LABEL }, 2);
478: int res = dialog.open();
479:
480: if (res == 2) {
481: return false;
482: } else if (res == 0) {
483: build = true;
484: }
485: }
486: if (project != null
487: && enabled != CompilerFlags.getBoolean(project,
488: CompilerFlags.USE_PROJECT_PREF)) {
489: if (enabled) {
490: CompilerFlags.setBoolean(project,
491: CompilerFlags.USE_PROJECT_PREF, true);
492: } else {
493: CompilerFlags.clear(project,
494: CompilerFlags.USE_PROJECT_PREF);
495: }
496: }
497: if (changedControls.size() > 0) {
498: fBuilders = new HashSet();
499: for (Iterator iter = changedControls.iterator(); iter
500: .hasNext();) {
501: Control control = (Control) iter.next();
502: String flagId = (String) control.getData();
503: if (control instanceof Combo) {
504: int index = ((Combo) control).getSelectionIndex();
505: if (project == null) {
506: CompilerFlags.setFlag(flagId, index);
507: }
508: } else if (control instanceof Button) {
509: boolean value = ((Button) control).getSelection();
510: if (project == null) {
511: CompilerFlags.setBoolean(flagId, value);
512: }
513: } else if (control instanceof Text) {
514: String value = ((Text) control).getText();
515: if (project == null) {
516: CompilerFlags.setString(flagId, value);
517: }
518: }
519: if (control.getParent().equals(fPluginPage))
520: fBuilders.add(PDE.MANIFEST_BUILDER_ID);
521: else if (control.getParent().equals(fSchemaPage))
522: fBuilders.add(PDE.SCHEMA_BUILDER_ID);
523: else if (control.getParent().equals(fFeaturePage)) {
524: fBuilders.add(PDE.FEATURE_BUILDER_ID);
525: fBuilders.add(PDE.SITE_BUILDER_ID);
526: }
527: }
528: if (project == null) {
529: CompilerFlags.save();
530: }
531: }
532: if (project != null) {
533: for (Iterator iter = fFlagControls.iterator(); iter
534: .hasNext();) {
535: Control control = (Control) iter.next();
536: String flagId = (String) control.getData();
537: if (control instanceof Combo) {
538: int index = ((Combo) control).getSelectionIndex();
539: if (enabled) {
540: CompilerFlags.setFlag(project, flagId, index);
541: } else {
542: CompilerFlags.clear(project, flagId);
543: }
544: } else if (control instanceof Button) {
545: boolean value = ((Button) control).getSelection();
546: if (enabled) {
547: CompilerFlags
548: .setBoolean(project, flagId, value);
549: } else {
550: CompilerFlags.clear(project, flagId);
551: }
552: } else if (control instanceof Text) {
553: String value = ((Text) control).getText();
554: if (enabled) {
555: CompilerFlags.setString(project, flagId, value);
556: } else {
557: CompilerFlags.clear(project, flagId);
558: }
559: }
560: }
561: }
562:
563: if (build && fBuilders.size() > 0) {
564: doFullBuild();
565: }
566:
567: fChangedControls.clear();
568: return true;
569: }
570:
571: protected void setShell(Shell shell) {
572: fShell = shell;
573: }
574: }
|