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.Vector;
023
024import org.apache.tools.ant.BuildException;
025
026/**
027 * Configures components creating their preference files under under the directory
028 * <tt>.settings</tt>. The attributes <tt>name</tt> of all elements for components
029 * describing preferences must be distinct.
030 * 
031 * @since Ant-Eclipse 1.0
032 * @author Ferdinand Prantl &lt;prantl@users.sourceforge.net&gt;
033 */
034public class SettingsElement {
035
036    private Vector preferences = new Vector();
037
038    /**
039     * Creates a new instance of the settings element.
040     * 
041     * @since Ant-Eclipse 1.0
042     */
043    public SettingsElement() {
044    }
045
046    /**
047     * Returns a list with instances of the descendants of the class PreferencesElement
048     * describing files <em>&lt;full qualified class
049     * name&gt;</em> under the directory
050     * <tt>.settings</tt>. If it is empty nothing happens.
051     * 
052     * @return A list with instances of the descendants of the class PreferencesElement.
053     */
054    public Vector getPreferences() {
055        return preferences;
056    }
057
058    /**
059     * Adds a definition of the general preferences element.
060     * 
061     * @return A definition of the general preferences element.
062     * @since Ant-Eclipse 1.0
063     */
064    public GeneralPreferencesElement createGeneral() {
065        preferences.addElement(new GeneralPreferencesElement(this));
066        return (GeneralPreferencesElement) preferences.lastElement();
067    }
068
069    /**
070     * Adds a definition of the convenience preferences element specific for the package
071     * org.eclipse.core.resources.
072     * 
073     * @return A definition of the convenience preferences element specific for the
074     *         package org.eclipse.core.resources.
075     * @since Ant-Eclipse 1.0
076     */
077    public OrgEclipseCoreResourcesPreferencesElement createResources() {
078        preferences.addElement(new OrgEclipseCoreResourcesPreferencesElement(this));
079        return (OrgEclipseCoreResourcesPreferencesElement) preferences.lastElement();
080    }
081
082    /**
083     * Adds a definition of the convenience preferences element specific for the package
084     * org.eclipse.core.runtime.
085     * 
086     * @return A definition of the convenience preferences element specific for the
087     *         package org.eclipse.core.runtime.
088     * @since Ant-Eclipse 1.0
089     */
090    public OrgEclipseCoreRuntimePreferencesElement createRuntime() {
091        preferences.addElement(new OrgEclipseCoreRuntimePreferencesElement(this));
092        return (OrgEclipseCoreRuntimePreferencesElement) preferences.lastElement();
093    }
094
095    /**
096     * Adds a definition of the convenience preferences element specific for the package
097     * org.eclipse.jdt.core.
098     * 
099     * @return A definition of the convenience preferences element specific for the
100     *         package org.eclipse.jdt.core.
101     * @since Ant-Eclipse 1.0
102     */
103    public OrgEclipseJdtCorePreferencesElement createJdtCore() {
104        preferences.addElement(new OrgEclipseJdtCorePreferencesElement(this));
105        return (OrgEclipseJdtCorePreferencesElement) preferences.lastElement();
106    }
107
108    /**
109     * Adds a definition of the convenience preferences element specific for the package
110     * org.eclipse.jdt.ui.
111     * 
112     * @return A definition of the convenience preferences element specific for the
113     *         package org.eclipse.jdt.ui.
114     * @since Ant-Eclipse 1.0
115     */
116    public OrgEclipseJdtUiPreferencesElement createJdtUi() {
117        preferences.addElement(new OrgEclipseJdtUiPreferencesElement(this));
118        return (OrgEclipseJdtUiPreferencesElement) preferences.lastElement();
119    }
120
121    /**
122     * Checks if the preferences with the specified name is allowed to be defined
123     * according to the already parsed content.
124     * 
125     * @param name
126     *        The name of the configuration preferences to validate.
127     * @since Ant-Eclipse 1.0
128     */
129    void validatePreferencesName(String name) {
130        if (hasPreferences(name))
131            throw new BuildException("The preferences for \"" + name
132                    + "\" has alredy been defined.");
133    }
134
135    /**
136     * Checks if the preferences with the specified name has already been defined for this
137     * preferences.
138     * 
139     * @param name
140     *        The name of the configuration preferences to look for.
141     * @return <tt>True</tt> if the preferences with the specified name are present.
142     * @since Ant-Eclipse 1.0
143     */
144    boolean hasPreferences(String name) {
145        return getPreferences(name) != null;
146    }
147
148    /**
149     * Returns the element defining the preferences with the specified name or
150     * <tt>null</tt> if not having been defined.
151     * 
152     * @param name
153     *        The element defining the configuration preferences or <tt>null</tt> if not
154     *        present.
155     * @return The element defining the preferences with the specified name or
156     *         <tt>null</tt> if not present.
157     * @since Ant-Eclipse 1.0
158     */
159    PreferencesElement getPreferences(String name) {
160        for (int i = 0, size = preferences.size(); i != size; ++i) {
161            PreferencesElement preference = (PreferencesElement) preferences.get(i);
162            if (name.equals(preference.getName()))
163                return preference;
164        }
165        return null;
166    }
167
168}