KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > ComplianceConfigurationBlock


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.jdt.internal.ui.preferences;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IStatus;
20
21 import org.eclipse.core.resources.IProject;
22
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.events.SelectionListener;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Combo;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Group;
33 import org.eclipse.swt.widgets.Link;
34
35 import org.eclipse.jface.dialogs.ControlEnableState;
36
37 import org.eclipse.ui.dialogs.PreferencesUtil;
38 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
39
40 import org.eclipse.jdt.core.JavaCore;
41
42 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
43 import org.eclipse.jdt.internal.corext.util.Messages;
44
45 import org.eclipse.jdt.launching.IVMInstall;
46 import org.eclipse.jdt.launching.IVMInstall2;
47 import org.eclipse.jdt.launching.JavaRuntime;
48
49 import org.eclipse.jdt.internal.ui.JavaPlugin;
50 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
51 import org.eclipse.jdt.internal.ui.util.PixelConverter;
52 import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
53 import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathSupport;
54
55 /**
56   */

57 public class ComplianceConfigurationBlock extends OptionsConfigurationBlock {
58
59     // Preference store keys, see JavaCore.getOptions
60
private static final Key PREF_LOCAL_VARIABLE_ATTR= getJDTCoreKey(JavaCore.COMPILER_LOCAL_VARIABLE_ATTR);
61     private static final Key PREF_LINE_NUMBER_ATTR= getJDTCoreKey(JavaCore.COMPILER_LINE_NUMBER_ATTR);
62     private static final Key PREF_SOURCE_FILE_ATTR= getJDTCoreKey(JavaCore.COMPILER_SOURCE_FILE_ATTR);
63     private static final Key PREF_CODEGEN_UNUSED_LOCAL= getJDTCoreKey(JavaCore.COMPILER_CODEGEN_UNUSED_LOCAL);
64     private static final Key PREF_CODEGEN_TARGET_PLATFORM= getJDTCoreKey(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM);
65     private static final Key PREF_CODEGEN_INLINE_JSR_BYTECODE= getJDTCoreKey(JavaCore.COMPILER_CODEGEN_INLINE_JSR_BYTECODE);
66     
67     private static final Key PREF_SOURCE_COMPATIBILITY= getJDTCoreKey(JavaCore.COMPILER_SOURCE);
68     private static final Key PREF_COMPLIANCE= getJDTCoreKey(JavaCore.COMPILER_COMPLIANCE);
69     private static final Key PREF_PB_ASSERT_AS_IDENTIFIER= getJDTCoreKey(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER);
70     private static final Key PREF_PB_ENUM_AS_IDENTIFIER= getJDTCoreKey(JavaCore.COMPILER_PB_ENUM_IDENTIFIER);
71     
72     private static final Key INTR_DEFAULT_COMPLIANCE= getJDTUIKey("internal.default.compliance"); //$NON-NLS-1$
73

74     // values
75
private static final String JavaDoc GENERATE= JavaCore.GENERATE;
76     private static final String JavaDoc DO_NOT_GENERATE= JavaCore.DO_NOT_GENERATE;
77     
78     private static final String JavaDoc PRESERVE= JavaCore.PRESERVE;
79     private static final String JavaDoc OPTIMIZE_OUT= JavaCore.OPTIMIZE_OUT;
80     
81     private static final String JavaDoc VERSION_1_1= JavaCore.VERSION_1_1;
82     private static final String JavaDoc VERSION_1_2= JavaCore.VERSION_1_2;
83     private static final String JavaDoc VERSION_1_3= JavaCore.VERSION_1_3;
84     private static final String JavaDoc VERSION_1_4= JavaCore.VERSION_1_4;
85     private static final String JavaDoc VERSION_1_5= JavaCore.VERSION_1_5;
86     private static final String JavaDoc VERSION_1_6= JavaCore.VERSION_1_6;
87     
88     private static final String JavaDoc ERROR= JavaCore.ERROR;
89     private static final String JavaDoc WARNING= JavaCore.WARNING;
90     private static final String JavaDoc IGNORE= JavaCore.IGNORE;
91
92     private static final String JavaDoc ENABLED= JavaCore.ENABLED;
93     private static final String JavaDoc DISABLED= JavaCore.DISABLED;
94     
95     
96     private static final String JavaDoc DEFAULT_CONF= "default"; //$NON-NLS-1$
97
private static final String JavaDoc USER_CONF= "user"; //$NON-NLS-1$
98

99     private ArrayList JavaDoc fComplianceControls;
100     private PixelConverter fPixelConverter;
101
102     private String JavaDoc[] fRememberedUserCompliance;
103     
104     private static final int IDX_ASSERT_AS_IDENTIFIER= 0;
105     private static final int IDX_ENUM_AS_IDENTIFIER= 1;
106     private static final int IDX_SOURCE_COMPATIBILITY= 2;
107     private static final int IDX_CODEGEN_TARGET_PLATFORM= 3;
108     private static final int IDX_COMPLIANCE= 4;
109     private static final int IDX_INLINE_JSR_BYTECODE= 5;
110
111     private IStatus fComplianceStatus;
112
113     private Link fJRE50InfoText;
114     private Composite fControlsComposite;
115     private ControlEnableState fBlockEnableState;
116
117     public ComplianceConfigurationBlock(IStatusChangeListener context, IProject project, IWorkbenchPreferenceContainer container) {
118         super(context, project, getKeys(), container);
119
120         fBlockEnableState= null;
121         fComplianceControls= new ArrayList JavaDoc();
122             
123         fComplianceStatus= new StatusInfo();
124         
125         fRememberedUserCompliance= new String JavaDoc[] { // caution: order depends on IDX_* constants
126
getValue(PREF_PB_ASSERT_AS_IDENTIFIER),
127             getValue(PREF_PB_ENUM_AS_IDENTIFIER),
128             getValue(PREF_SOURCE_COMPATIBILITY),
129             getValue(PREF_CODEGEN_TARGET_PLATFORM),
130             getValue(PREF_COMPLIANCE),
131             getValue(PREF_CODEGEN_INLINE_JSR_BYTECODE),
132         };
133     }
134     
135     private static Key[] getKeys() {
136         return new Key[] {
137                 PREF_LOCAL_VARIABLE_ATTR, PREF_LINE_NUMBER_ATTR, PREF_SOURCE_FILE_ATTR, PREF_CODEGEN_UNUSED_LOCAL,
138                 PREF_CODEGEN_INLINE_JSR_BYTECODE,
139                 PREF_COMPLIANCE, PREF_SOURCE_COMPATIBILITY,
140                 PREF_CODEGEN_TARGET_PLATFORM, PREF_PB_ASSERT_AS_IDENTIFIER, PREF_PB_ENUM_AS_IDENTIFIER
141             };
142     }
143         
144     /* (non-Javadoc)
145      * @see org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock#settingsUpdated()
146      */

147     protected void settingsUpdated() {
148         setValue(INTR_DEFAULT_COMPLIANCE, getCurrentCompliance());
149         super.settingsUpdated();
150     }
151     
152     
153     /*
154      * @see org.eclipse.jface.preference.PreferencePage#createContents(Composite)
155      */

156     protected Control createContents(Composite parent) {
157         fPixelConverter= new PixelConverter(parent);
158         setShell(parent.getShell());
159         
160         Composite complianceComposite= createComplianceTabContent(parent);
161         
162         validateSettings(null, null, null);
163     
164         return complianceComposite;
165     }
166     
167     public void enablePreferenceContent(boolean enable) {
168         if (fControlsComposite != null && !fControlsComposite.isDisposed()) {
169             if (enable) {
170                 if (fBlockEnableState != null) {
171                     fBlockEnableState.restore();
172                     fBlockEnableState= null;
173                 }
174             } else {
175                 if (fBlockEnableState == null) {
176                     fBlockEnableState= ControlEnableState.disable(fControlsComposite);
177                 }
178             }
179         }
180     }
181     
182     private Composite createComplianceTabContent(Composite folder) {
183
184
185         String JavaDoc[] values3456= new String JavaDoc[] { VERSION_1_3, VERSION_1_4, VERSION_1_5, VERSION_1_6 };
186         String JavaDoc[] values3456Labels= new String JavaDoc[] {
187             PreferencesMessages.ComplianceConfigurationBlock_version13,
188             PreferencesMessages.ComplianceConfigurationBlock_version14,
189             PreferencesMessages.ComplianceConfigurationBlock_version15,
190             PreferencesMessages.ComplianceConfigurationBlock_version16
191         };
192
193         final ScrolledPageContent sc1 = new ScrolledPageContent(folder);
194         Composite composite= sc1.getBody();
195         GridLayout layout= new GridLayout();
196         layout.marginHeight= 0;
197         layout.marginWidth= 0;
198         composite.setLayout(layout);
199         
200         fControlsComposite= new Composite(composite, SWT.NONE);
201         fControlsComposite.setFont(composite.getFont());
202         fControlsComposite.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));
203         
204         layout= new GridLayout();
205         layout.marginHeight= 0;
206         layout.marginWidth= 0;
207         layout.numColumns= 1;
208         fControlsComposite.setLayout(layout);
209
210         int nColumns= 3;
211
212         layout= new GridLayout();
213         layout.numColumns= nColumns;
214
215         Group group= new Group(fControlsComposite, SWT.NONE);
216         group.setFont(fControlsComposite.getFont());
217         group.setText(PreferencesMessages.ComplianceConfigurationBlock_compliance_group_label);
218         group.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));
219         group.setLayout(layout);
220     
221         String JavaDoc label= PreferencesMessages.ComplianceConfigurationBlock_compiler_compliance_label;
222         addComboBox(group, label, PREF_COMPLIANCE, values3456, values3456Labels, 0);
223
224         label= PreferencesMessages.ComplianceConfigurationBlock_default_settings_label;
225         addCheckBox(group, label, INTR_DEFAULT_COMPLIANCE, new String JavaDoc[] { DEFAULT_CONF, USER_CONF }, 0);
226
227         int indent= fPixelConverter.convertWidthInCharsToPixels(2);
228         Control[] otherChildren= group.getChildren();
229                 
230         String JavaDoc[] versions= new String JavaDoc[] { VERSION_1_1, VERSION_1_2, VERSION_1_3, VERSION_1_4, VERSION_1_5, VERSION_1_6 };
231         String JavaDoc[] versionsLabels= new String JavaDoc[] {
232             PreferencesMessages.ComplianceConfigurationBlock_version11,
233             PreferencesMessages.ComplianceConfigurationBlock_version12,
234             PreferencesMessages.ComplianceConfigurationBlock_version13,
235             PreferencesMessages.ComplianceConfigurationBlock_version14,
236             PreferencesMessages.ComplianceConfigurationBlock_version15,
237             PreferencesMessages.ComplianceConfigurationBlock_version16
238         };
239         
240         label= PreferencesMessages.ComplianceConfigurationBlock_codegen_targetplatform_label;
241         addComboBox(group, label, PREF_CODEGEN_TARGET_PLATFORM, versions, versionsLabels, indent);
242
243         label= PreferencesMessages.ComplianceConfigurationBlock_source_compatibility_label;
244         addComboBox(group, label, PREF_SOURCE_COMPATIBILITY, values3456, values3456Labels, indent);
245
246         String JavaDoc[] errorWarningIgnore= new String JavaDoc[] { ERROR, WARNING, IGNORE };
247         
248         String JavaDoc[] errorWarningIgnoreLabels= new String JavaDoc[] {
249             PreferencesMessages.ComplianceConfigurationBlock_error,
250             PreferencesMessages.ComplianceConfigurationBlock_warning,
251             PreferencesMessages.ComplianceConfigurationBlock_ignore
252         };
253
254         label= PreferencesMessages.ComplianceConfigurationBlock_pb_assert_as_identifier_label;
255         addComboBox(group, label, PREF_PB_ASSERT_AS_IDENTIFIER, errorWarningIgnore, errorWarningIgnoreLabels, indent);
256
257         label= PreferencesMessages.ComplianceConfigurationBlock_pb_enum_as_identifier_label;
258         addComboBox(group, label, PREF_PB_ENUM_AS_IDENTIFIER, errorWarningIgnore, errorWarningIgnoreLabels, indent);
259
260         
261         Control[] allChildren= group.getChildren();
262         fComplianceControls.addAll(Arrays.asList(allChildren));
263         fComplianceControls.removeAll(Arrays.asList(otherChildren));
264
265         layout= new GridLayout();
266         layout.numColumns= nColumns;
267
268         group= new Group(fControlsComposite, SWT.NONE);
269         group.setFont(fControlsComposite.getFont());
270         group.setText(PreferencesMessages.ComplianceConfigurationBlock_classfiles_group_label);
271         group.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));
272         group.setLayout(layout);
273
274         String JavaDoc[] generateValues= new String JavaDoc[] { GENERATE, DO_NOT_GENERATE };
275         String JavaDoc[] enableDisableValues= new String JavaDoc[] { ENABLED, DISABLED };
276
277         label= PreferencesMessages.ComplianceConfigurationBlock_variable_attr_label;
278         addCheckBox(group, label, PREF_LOCAL_VARIABLE_ATTR, generateValues, 0);
279         
280         label= PreferencesMessages.ComplianceConfigurationBlock_line_number_attr_label;
281         addCheckBox(group, label, PREF_LINE_NUMBER_ATTR, generateValues, 0);
282
283         label= PreferencesMessages.ComplianceConfigurationBlock_source_file_attr_label;
284         addCheckBox(group, label, PREF_SOURCE_FILE_ATTR, generateValues, 0);
285
286         label= PreferencesMessages.ComplianceConfigurationBlock_codegen_unused_local_label;
287         addCheckBox(group, label, PREF_CODEGEN_UNUSED_LOCAL, new String JavaDoc[] { PRESERVE, OPTIMIZE_OUT }, 0);
288
289         label= PreferencesMessages.ComplianceConfigurationBlock_codegen_inline_jsr_bytecode_label;
290         addCheckBox(group, label, PREF_CODEGEN_INLINE_JSR_BYTECODE, enableDisableValues, 0);
291         
292         fJRE50InfoText= new Link(composite, SWT.WRAP);
293         fJRE50InfoText.setFont(composite.getFont());
294         // set a text: not the real one, just for layouting
295
fJRE50InfoText.setText(Messages.format(PreferencesMessages.ComplianceConfigurationBlock_jrecompliance_info_project, new String JavaDoc[] { VERSION_1_3, VERSION_1_3 }));
296         fJRE50InfoText.setVisible(false);
297         fJRE50InfoText.addSelectionListener(new SelectionListener() {
298             public void widgetDefaultSelected(SelectionEvent e) {
299                 if ("1".equals(e.text)) { //$NON-NLS-1$
300
openJREInstallPreferencePage();
301                 } else {
302                     openBuildPathPropertyPage();
303                 }
304             }
305             public void widgetSelected(SelectionEvent e) {
306                 widgetDefaultSelected(e);
307             }
308         });
309         GridData gd= new GridData(GridData.FILL, GridData.FILL, true, true);
310         gd.widthHint= fPixelConverter.convertWidthInCharsToPixels(50);
311         fJRE50InfoText.setLayoutData(gd);
312         validateJRE50Status();
313         
314         return sc1;
315     }
316     
317     protected final void openBuildPathPropertyPage() {
318         if (getPreferenceContainer() != null) {
319             Map JavaDoc data= new HashMap JavaDoc();
320             data.put(BuildPathsPropertyPage.DATA_REVEAL_ENTRY, JavaRuntime.getDefaultJREContainerEntry());
321             getPreferenceContainer().openPage(BuildPathsPropertyPage.PROP_ID, data);
322         }
323         validateJRE50Status();
324     }
325     
326     protected final void openJREInstallPreferencePage() {
327         String JavaDoc jreID= BuildPathSupport.JRE_PREF_PAGE_ID;
328         if (fProject == null && getPreferenceContainer() != null) {
329             getPreferenceContainer().openPage(jreID, null);
330         } else {
331             PreferencesUtil.createPreferenceDialogOn(getShell(), jreID, new String JavaDoc[] { jreID }, null).open();
332         }
333         validateJRE50Status();
334     }
335
336     /* (non-javadoc)
337      * Update fields and validate.
338      * @param changedKey Key that changed, or null, if all changed.
339      */

340     protected void validateSettings(Key changedKey, String JavaDoc oldValue, String JavaDoc newValue) {
341         if (!areSettingsEnabled()) {
342             return;
343         }
344         if (changedKey != null) {
345             if (INTR_DEFAULT_COMPLIANCE.equals(changedKey)) {
346                 updateComplianceEnableState();
347                 updateComplianceDefaultSettings(true, null);
348                 fComplianceStatus= validateCompliance();
349             } else if (PREF_COMPLIANCE.equals(changedKey)) {
350                 // set compliance settings to default
351
Object JavaDoc oldDefault= setValue(INTR_DEFAULT_COMPLIANCE, DEFAULT_CONF);
352                 updateComplianceEnableState();
353                 updateComplianceDefaultSettings(USER_CONF.equals(oldDefault), oldValue);
354                 fComplianceStatus= validateCompliance();
355                 validateJRE50Status();
356             } else if (PREF_SOURCE_COMPATIBILITY.equals(changedKey)) {
357                 updateAssertEnumAsIdentifierEnableState();
358                 fComplianceStatus= validateCompliance();
359             } else if (PREF_CODEGEN_TARGET_PLATFORM.equals(changedKey)) {
360                 updateInlineJSREnableState();
361                 fComplianceStatus= validateCompliance();
362             } else if (PREF_PB_ENUM_AS_IDENTIFIER.equals(changedKey) ||
363                     PREF_PB_ASSERT_AS_IDENTIFIER.equals(changedKey)) {
364                 fComplianceStatus= validateCompliance();
365             } else {
366                 return;
367             }
368         } else {
369             updateComplianceEnableState();
370             updateAssertEnumAsIdentifierEnableState();
371             updateInlineJSREnableState();
372             fComplianceStatus= validateCompliance();
373             validateJRE50Status();
374         }
375         fContext.statusChanged(fComplianceStatus);
376     }
377     
378     private void validateJRE50Status() {
379         if (fJRE50InfoText != null && !fJRE50InfoText.isDisposed()) {
380             boolean isVisible= false;
381             String JavaDoc compliance= getStoredValue(PREF_COMPLIANCE); // get actual value
382
IVMInstall install= null;
383             if (fProject != null) { // project specific settings: only test if a 50 JRE is installed
384
try {
385                     install= JavaRuntime.getVMInstall(JavaCore.create(fProject));
386                 } catch (CoreException e) {
387                     JavaPlugin.log(e);
388                 }
389             } else {
390                 install= JavaRuntime.getDefaultVMInstall();
391             }
392             if (install instanceof IVMInstall2) {
393                 String JavaDoc compilerCompliance= JavaModelUtil.getCompilerCompliance((IVMInstall2) install, compliance);
394                 if (JavaModelUtil.isVersionLessThan(compilerCompliance, compliance)) { // Discourage using compiler with version less than compliance
395
String JavaDoc[] args= { getVersionLabel(compliance), getVersionLabel(compilerCompliance) };
396                     if (fProject == null) {
397                         fJRE50InfoText.setText(Messages.format(PreferencesMessages.ComplianceConfigurationBlock_jrecompliance_info, args));
398                     } else {
399                         fJRE50InfoText.setText(Messages.format(PreferencesMessages.ComplianceConfigurationBlock_jrecompliance_info_project, args));
400                     }
401                     isVisible= true;
402                 }
403             }
404             fJRE50InfoText.setVisible(isVisible);
405         }
406     }
407
408     private String JavaDoc getVersionLabel(String JavaDoc version) {
409         if (JavaModelUtil.isVersionLessThan(version, VERSION_1_5)) {
410             return version; // for version <= 1.4, we can use the version as is
411
}
412         if (VERSION_1_5.equals(version))
413             return PreferencesMessages.ComplianceConfigurationBlock_version15;
414         return PreferencesMessages.ComplianceConfigurationBlock_version16;
415     }
416
417     
418     private IStatus validateCompliance() {
419         StatusInfo status= new StatusInfo();
420         String JavaDoc compliance= getValue(PREF_COMPLIANCE);
421         String JavaDoc source= getValue(PREF_SOURCE_COMPATIBILITY);
422         String JavaDoc target= getValue(PREF_CODEGEN_TARGET_PLATFORM);
423         
424         // compliance must not be smaller than source or target
425
if (JavaModelUtil.isVersionLessThan(compliance, source)) {
426             status.setError(PreferencesMessages.ComplianceConfigurationBlock_src_greater_compliance);
427             return status;
428         }
429         
430         if (JavaModelUtil.isVersionLessThan(compliance, target)) {
431             status.setError(PreferencesMessages.ComplianceConfigurationBlock_classfile_greater_compliance);
432             return status;
433         }
434         
435         // target must not be smaller than source
436
if (!VERSION_1_3.equals(source) && JavaModelUtil.isVersionLessThan(target, source)) {
437             status.setError(PreferencesMessages.ComplianceConfigurationBlock_classfile_greater_source);
438             return status;
439         }
440         
441         return status;
442     }
443             
444     
445     /* (non-Javadoc)
446      * @see org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock#useProjectSpecificSettings(boolean)
447      */

448     public void useProjectSpecificSettings(boolean enable) {
449         super.useProjectSpecificSettings(enable);
450         validateJRE50Status();
451     }
452         
453     /*
454      * Update the compliance controls' enable state
455      */

456     private void updateComplianceEnableState() {
457         boolean enabled= checkValue(INTR_DEFAULT_COMPLIANCE, USER_CONF);
458         for (int i= fComplianceControls.size() - 1; i >= 0; i--) {
459             Control curr= (Control) fComplianceControls.get(i);
460             curr.setEnabled(enabled);
461         }
462     }
463     
464     private void updateAssertEnumAsIdentifierEnableState() {
465         if (checkValue(INTR_DEFAULT_COMPLIANCE, USER_CONF)) {
466             String JavaDoc compatibility= getValue(PREF_SOURCE_COMPATIBILITY);
467             
468             boolean isLessThan14= VERSION_1_3.equals(compatibility);
469             updateRememberedComplianceOption(PREF_PB_ASSERT_AS_IDENTIFIER, IDX_ASSERT_AS_IDENTIFIER, isLessThan14);
470         
471             boolean isLessThan15= isLessThan14 || VERSION_1_4.equals(compatibility);
472             updateRememberedComplianceOption(PREF_PB_ENUM_AS_IDENTIFIER, IDX_ENUM_AS_IDENTIFIER, isLessThan15);
473         }
474     }
475     
476     private void updateRememberedComplianceOption(Key prefKey, int idx, boolean enabled) {
477         Combo combo= getComboBox(prefKey);
478         combo.setEnabled(enabled);
479         
480         if (!enabled) {
481             String JavaDoc val= getValue(prefKey);
482             if (!ERROR.equals(val)) {
483                 setValue(prefKey, ERROR);
484                 updateCombo(combo);
485                 fRememberedUserCompliance[idx]= val;
486             }
487         } else {
488             String JavaDoc val= fRememberedUserCompliance[idx];
489             if (!ERROR.equals(val)) {
490                 setValue(prefKey, val);
491                 updateCombo(combo);
492             }
493         }
494     }
495
496     private void updateInlineJSREnableState() {
497         String JavaDoc target= getValue(PREF_CODEGEN_TARGET_PLATFORM);
498         
499         boolean enabled= JavaModelUtil.isVersionLessThan(target, VERSION_1_5);
500         Button checkBox= getCheckBox(PREF_CODEGEN_INLINE_JSR_BYTECODE);
501         checkBox.setEnabled(enabled);
502         
503         if (!enabled) {
504             String JavaDoc val= getValue(PREF_CODEGEN_INLINE_JSR_BYTECODE);
505             fRememberedUserCompliance[IDX_INLINE_JSR_BYTECODE]= val;
506             
507             if (!ENABLED.equals(val)) {
508                 setValue(PREF_CODEGEN_INLINE_JSR_BYTECODE, ENABLED);
509                 updateCheckBox(checkBox);
510             }
511         } else {
512             String JavaDoc val= fRememberedUserCompliance[IDX_INLINE_JSR_BYTECODE];
513             if (!ENABLED.equals(val)) {
514                 setValue(PREF_CODEGEN_INLINE_JSR_BYTECODE, val);
515                 updateCheckBox(checkBox);
516             }
517         }
518     }
519
520     /*
521      * Set the default compliance values derived from the chosen level
522      */

523     private void updateComplianceDefaultSettings(boolean rememberOld, String JavaDoc oldComplianceLevel) {
524         String JavaDoc assertAsId, enumAsId, source, target;
525         boolean isDefault= checkValue(INTR_DEFAULT_COMPLIANCE, DEFAULT_CONF);
526         String JavaDoc complianceLevel= getValue(PREF_COMPLIANCE);
527         
528         if (isDefault) {
529             if (rememberOld) {
530                 if (oldComplianceLevel == null) {
531                     oldComplianceLevel= complianceLevel;
532                 }
533                 
534                 fRememberedUserCompliance[IDX_ASSERT_AS_IDENTIFIER]= getValue(PREF_PB_ASSERT_AS_IDENTIFIER);
535                 fRememberedUserCompliance[IDX_ENUM_AS_IDENTIFIER]= getValue(PREF_PB_ENUM_AS_IDENTIFIER);
536                 fRememberedUserCompliance[IDX_SOURCE_COMPATIBILITY]= getValue(PREF_SOURCE_COMPATIBILITY);
537                 fRememberedUserCompliance[IDX_CODEGEN_TARGET_PLATFORM]= getValue(PREF_CODEGEN_TARGET_PLATFORM);
538                 fRememberedUserCompliance[IDX_COMPLIANCE]= oldComplianceLevel;
539             }
540
541             if (VERSION_1_4.equals(complianceLevel)) {
542                 assertAsId= WARNING;
543                 enumAsId= WARNING;
544                 source= VERSION_1_3;
545                 target= VERSION_1_2;
546             } else if (VERSION_1_5.equals(complianceLevel)) {
547                 assertAsId= ERROR;
548                 enumAsId= ERROR;
549                 source= VERSION_1_5;
550                 target= VERSION_1_5;
551             } else if (VERSION_1_6.equals(complianceLevel)) {
552                 assertAsId= ERROR;
553                 enumAsId= ERROR;
554                 source= VERSION_1_6;
555                 target= VERSION_1_6;
556             } else {
557                 assertAsId= IGNORE;
558                 enumAsId= IGNORE;
559                 source= VERSION_1_3;
560                 target= VERSION_1_1;
561             }
562         } else {
563             if (rememberOld && complianceLevel.equals(fRememberedUserCompliance[IDX_COMPLIANCE])) {
564                 assertAsId= fRememberedUserCompliance[IDX_ASSERT_AS_IDENTIFIER];
565                 enumAsId= fRememberedUserCompliance[IDX_ENUM_AS_IDENTIFIER];
566                 source= fRememberedUserCompliance[IDX_SOURCE_COMPATIBILITY];
567                 target= fRememberedUserCompliance[IDX_CODEGEN_TARGET_PLATFORM];
568             } else {
569                 updateInlineJSREnableState();
570                 updateAssertEnumAsIdentifierEnableState();
571                 return;
572             }
573         }
574         setValue(PREF_PB_ASSERT_AS_IDENTIFIER, assertAsId);
575         setValue(PREF_PB_ENUM_AS_IDENTIFIER, enumAsId);
576         setValue(PREF_SOURCE_COMPATIBILITY, source);
577         setValue(PREF_CODEGEN_TARGET_PLATFORM, target);
578         updateControls();
579         updateInlineJSREnableState();
580         updateAssertEnumAsIdentifierEnableState();
581     }
582     
583     /*
584      * Evaluate if the current compliance setting correspond to a default setting
585      */

586     private String JavaDoc getCurrentCompliance() {
587         Object JavaDoc complianceLevel= getValue(PREF_COMPLIANCE);
588         if ((VERSION_1_3.equals(complianceLevel)
589                 && IGNORE.equals(getValue(PREF_PB_ASSERT_AS_IDENTIFIER))
590                 && IGNORE.equals(getValue(PREF_PB_ENUM_AS_IDENTIFIER))
591                 && VERSION_1_3.equals(getValue(PREF_SOURCE_COMPATIBILITY))
592                 && VERSION_1_1.equals(getValue(PREF_CODEGEN_TARGET_PLATFORM)))
593             || (VERSION_1_4.equals(complianceLevel)
594                 && WARNING.equals(getValue(PREF_PB_ASSERT_AS_IDENTIFIER))
595                 && WARNING.equals(getValue(PREF_PB_ENUM_AS_IDENTIFIER))
596                 && VERSION_1_3.equals(getValue(PREF_SOURCE_COMPATIBILITY))
597                 && VERSION_1_2.equals(getValue(PREF_CODEGEN_TARGET_PLATFORM)))
598             || (VERSION_1_5.equals(complianceLevel)
599                 && ERROR.equals(getValue(PREF_PB_ASSERT_AS_IDENTIFIER))
600                 && ERROR.equals(getValue(PREF_PB_ENUM_AS_IDENTIFIER))
601                 && VERSION_1_5.equals(getValue(PREF_SOURCE_COMPATIBILITY))
602                 && VERSION_1_5.equals(getValue(PREF_CODEGEN_TARGET_PLATFORM)))
603             || (VERSION_1_6.equals(complianceLevel)
604                 && ERROR.equals(getValue(PREF_PB_ASSERT_AS_IDENTIFIER))
605                 && ERROR.equals(getValue(PREF_PB_ENUM_AS_IDENTIFIER))
606                 && VERSION_1_6.equals(getValue(PREF_SOURCE_COMPATIBILITY))
607                 && VERSION_1_6.equals(getValue(PREF_CODEGEN_TARGET_PLATFORM)))) {
608             return DEFAULT_CONF;
609         }
610         return USER_CONF;
611     }
612     
613     
614     protected String JavaDoc[] getFullBuildDialogStrings(boolean workspaceSettings) {
615         String JavaDoc title= PreferencesMessages.ComplianceConfigurationBlock_needsbuild_title;
616         String JavaDoc message;
617         if (workspaceSettings) {
618             message= PreferencesMessages.ComplianceConfigurationBlock_needsfullbuild_message;
619         } else {
620             message= PreferencesMessages.ComplianceConfigurationBlock_needsprojectbuild_message;
621         }
622         return new String JavaDoc[] { title, message };
623     }
624     
625     /* (non-Javadoc)
626      * @see org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock#performOk()
627      */

628     public boolean performOk() {
629         setValue(INTR_DEFAULT_COMPLIANCE, null);
630         return super.performOk();
631     }
632     
633     /* (non-Javadoc)
634      * @see org.eclipse.jdt.internal.ui.preferences.OptionsConfigurationBlock#performApply()
635      */

636     public boolean performApply() {
637         setValue(INTR_DEFAULT_COMPLIANCE, null);
638         boolean result= super.performApply();
639         setValue(INTR_DEFAULT_COMPLIANCE, getCurrentCompliance());
640         return result;
641     }
642         
643 }
644
Popular Tags