001// Copyright 2005-2006 Ferdinand Prantl <prantl@users.sourceforge.net>
002// Copyright 2001-2004 The Apache Software Foundation
003// All rights reserved.
004//
005// Licensed under the Apache License, Version 2.0 (the "License");
006// you may not use this file except in compliance with the License.
007// You may obtain a copy of the License at
008//
009// http://www.apache.org/licenses/LICENSE-2.0
010//
011// Unless required by applicable law or agreed to in writing, software
012// distributed under the License is distributed on an "AS IS" BASIS,
013// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014// See the License for the specific language governing permissions and
015// limitations under the License.
016//
017// See http://ant-eclipse.sourceforge.net for the most recent version
018// and more information.
019
020package prantl.ant.eclipse;
021
022import java.util.HashSet;
023
024import org.apache.tools.ant.BuildException;
025
026/**
027 * Configures the component preferences file <tt>.settings/org.eclipse.jdt.ui.prefs</tt>
028 * on the high level using attributes for the typical constellations of variable values.
029 * 
030 * @since Ant-Eclipse 1.0
031 * @author Ferdinand Prantl &lt;prantl@users.sourceforge.net&gt;
032 */
033public class OrgEclipseJdtUiPreferencesElement extends PreferencesElement {
034
035    // private static final String ELEMENT = "jdtui";
036    private static final String COMPLIANCE_ATTRIBUTE = "compliance";
037
038    private static final String COMPLIANCE_NAME = "internal.default.compliance";
039
040    private static final String COMPLIANCE_DEFAULT = "user";
041
042    private static final HashSet COMPLIANCE_VALUES = new HashSet();
043
044    /**
045     * Returns the name of the package these preferences belong to.
046     * 
047     * @return The name of the package these preferences belong to.
048     */
049    static final String getPackageName() {
050        return "org.eclipse.jdt.ui";
051    }
052
053    /**
054     * Creates a new instance of the element for the file with preferences for
055     * org.eclipse.jdt.ui.
056     * 
057     * @param parent
058     *        The parent settings element of this preferences one.
059     * @since Ant-Eclipse 1.0
060     */
061    public OrgEclipseJdtUiPreferencesElement(SettingsElement parent) {
062        super(parent);
063        internalSetName(getPackageName());
064        COMPLIANCE_VALUES.add("default");
065        COMPLIANCE_VALUES.add("user");
066    }
067
068    /**
069     * Returns the source file encoding for the project (default is inherited from the
070     * workspace settings and not set here in the file).
071     * 
072     * @return The source file encoding for the project (default is inherited from the
073     *         workspace settings and not set here in the file)
074     */
075    public String getCompliance() {
076        VariableElement variable = getVariable(COMPLIANCE_NAME);
077        return variable == null ? null : variable.getValue();
078    }
079
080    /**
081     * Sets the version of the Eclipse preferences. The default value should be left and
082     * not set explicitely.
083     * 
084     * @param value
085     *        A valid encoding for the project.
086     * @since Ant-Eclipse 1.0
087     */
088    public void setCompliance(String value) {
089        value = value.toLowerCase();
090        if (!COMPLIANCE_VALUES.contains(value))
091            throw new BuildException("The attribute \"" + COMPLIANCE_ATTRIBUTE
092                    + "\" (variable \"" + COMPLIANCE_NAME + "\") has an invalid value \""
093                    + value + "\". Valid values are " + getValidComplianceValues() + ".");
094        internalCreateVariable(COMPLIANCE_NAME, value);
095    }
096
097    /**
098     * Returns allowed values for the variable internal.default.compliance.
099     * 
100     * @return A new string with allowed values for the variable line.separator.
101     * @since Ant-Eclipse 1.0
102     */
103    String getValidComplianceValues() {
104        return getValidValues(COMPLIANCE_VALUES);
105    }
106
107    /**
108     * Performs the validation of the element at the time when the whole build file was
109     * parsed checking the content of the element and possibly adding mandatory variables
110     * with default settings.
111     * 
112     * @since Ant-Eclipse 1.0
113     */
114    public void validate() {
115        if (!hasVariable(COMPLIANCE_NAME))
116            setCompliance(COMPLIANCE_DEFAULT);
117        super.validate();
118    }
119
120}