View Javadoc

1   /** ------------------------------------
2    * SweetDEV RIA library
3    * Copyright [2006 - 2010] [Ideo Technologies]
4    * ------------------------------------
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   * 		http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   *
19   * For more information, please contact us at:
20   *         Ideo Technologies S.A
21   *        124 rue de Verdun
22   *        92800 Puteaux - France
23   *
24   *      France & Europe Phone : +33 1.46.25.09.60
25   *
26   *
27   *        web : http://www.ideotechnologies.com
28   *        email : SweetDEV-RIA@ideotechnologies.com
29   *
30   *
31   * @version ${pom.version}
32   * @author Ideo Technologies
33   */
34  package com.ideo.sweetdevria.config;
35  
36  
37  import java.io.Serializable;
38  import java.util.ArrayList;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.Map;
42  
43  import com.ideo.sweetdevria.config.elements.BuilderConfig;
44  import com.ideo.sweetdevria.config.elements.DrawerConfig;
45  import com.ideo.sweetdevria.config.elements.MessageResourcesConfig;
46  import com.ideo.sweetdevria.config.elements.PlugInConfig;
47  import com.ideo.sweetdevria.config.elements.PropertyConfig;
48  
49  /**
50   * RIA configuration implementation.
51   */
52  public class RIAConfigImpl implements Serializable, IRIAConfig {
53  
54      /**
55  	 * Generated serial version UID.
56  	 */
57  	private static final long serialVersionUID = -6967421386711409234L;
58  
59  	protected HashMap messageResources;
60  	protected List messageResourcesOrder;
61  
62      protected boolean configured;
63  
64      protected HashMap builderConfigs;
65  
66      protected HashMap drawerConfigs;
67  
68      protected ArrayList plugIns;
69      
70      protected Map properties;
71  
72      public RIAConfigImpl() {
73          builderConfigs = new HashMap();
74          drawerConfigs = new HashMap();
75          messageResources = new HashMap();
76          messageResourcesOrder = new ArrayList();
77          plugIns = new ArrayList();
78          properties = new HashMap();
79      }
80  
81      /**
82       * Get the configured.
83       * @return  Returns the configured.
84       */
85      public boolean getConfigured() {
86          return configured;
87      }
88  
89      /**
90       * Add a message resources config to the object
91       * The add is processed from the HIGHER to the LOWER important element.
92       * @param config the new message resources config
93       */
94      public void addMessageResourcesConfig(final MessageResourcesConfig config) {
95          if (configured) {
96              throw new IllegalStateException("Configuration is frozen");
97          } if(messageResources.containsKey(config.getKey())){
98          	return;//due to the add order, we do not erase
99          }else {
100             messageResources.put(config.getKey(), config);
101             messageResourcesOrder.add(config);
102             return;
103         }
104     }
105 
106     public void addPlugInConfig(PlugInConfig plugInConfig) {
107         if (configured) {
108             throw new IllegalStateException("Configuration is frozen");
109         } else {
110             plugIns.add(plugInConfig);
111             return;
112         }
113     }
114     
115     public void addPropertyConfig(PropertyConfig propertyConfig) {
116         if (configured) {
117             throw new IllegalStateException("Configuration is frozen");
118         } else {
119         	if(!properties.containsKey(propertyConfig.getKey()))
120         		properties.put(propertyConfig.getKey(), propertyConfig.getValue());
121             return;
122         }
123     }
124 
125     public Map getProperties() {
126         return properties;
127     }
128     
129     public String getProperty(String key) {
130         return (String)properties.get(key);
131     }
132 
133     public MessageResourcesConfig findMessageResourcesConfig(final String key) {
134         return (MessageResourcesConfig) messageResources.get(key);
135     }
136 
137     public MessageResourcesConfig[] findMessageResourcesConfigs() {
138         MessageResourcesConfig results[] = new MessageResourcesConfig[messageResourcesOrder.size()];
139         return (MessageResourcesConfig[]) messageResourcesOrder.toArray(results);
140     }
141 
142     public void freeze() {
143         configured = true;
144     }
145 
146     /**
147      * Add a builder config to the object
148      * The add is processed from the HIGHER to the LOWER important element.
149      * @param config the new builder config
150      */
151     public void addBuilderConfig(final BuilderConfig config) {
152         if (configured) {
153             throw new IllegalStateException("Configuration is frozen");
154         }if(builderConfigs.containsKey(config.getId())){
155         	return;//due to the add order, we do not erase
156         } else {
157             config.setRiaConfig(this);
158             builderConfigs.put(config.getId(), config);
159             return;
160         }
161     }
162 
163     /**
164      * Set the configured.
165      * @param configured  The configured to set.
166      */
167     public void setConfigured(final boolean configured) {
168         this.configured = configured;
169     }
170 
171     /**
172      * Return the builder configuration for the specified id
173      * otherwise return <code>null/code>
174      * 
175      * @param id Id of the builder configuration to return
176      */
177     public BuilderConfig findBuilderConfig(final String id) {
178         return (BuilderConfig) builderConfigs.get(id);
179     }
180 
181     /**
182      * Add a new <code>DrawerConfig</code> instance to the set 
183      * The add is processed from the HIGHER to the LOWER important element.
184      *
185      * @param config The new configuration instance to be added
186      *
187      * @exception java.lang.IllegalStateException if this module configuration
188      *  has been frozen
189      */
190     public void addDrawerConfig(final DrawerConfig config) {
191         if (configured) {
192             throw new IllegalStateException("Configuration is frozen");
193         }if(drawerConfigs.containsKey(config.getId())){
194         	return;
195         } else {
196             drawerConfigs.put(config.getId(), config);
197             return;
198         }
199     }
200 
201     /**
202      * Return the drawer configuration for the specified id, if any;
203      * otherwise return <code>null</code>.
204      *
205      * @param drawerId ID of the drawer configuration to return.
206      */
207     public DrawerConfig findDrawerConfig(final String drawerId) {
208         return (DrawerConfig) drawerConfigs.get(drawerId);
209     }
210 
211     /**
212      * Return the drawer configurations.  If there
213      * are none, a zero-length array is returned.
214      */
215     public DrawerConfig[] findDrawerConfigs() {
216         DrawerConfig results[] = new DrawerConfig[drawerConfigs.size()];
217         return (DrawerConfig[]) drawerConfigs.values().toArray(results);
218     }
219 
220     public PlugInConfig[] findPlugInConfigs() {
221         PlugInConfig results[] = new PlugInConfig[plugIns.size()];
222         return (PlugInConfig[]) plugIns.toArray(results);
223     }
224 
225 }