KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > model > ConfigurationPolicyModel


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.update.internal.model;
12
13 import java.net.*;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.update.core.*;
19 import org.eclipse.update.core.model.*;
20 import org.eclipse.update.internal.core.*;
21
22 /**
23  *
24  */

25 public class ConfigurationPolicyModel extends ModelObject {
26
27     
28     
29
30     private int policy;
31     private Map JavaDoc /* of FeatureReferenceModel */configuredFeatureReferences;
32     private Map JavaDoc /* of FeatureReferenceModel */unconfiguredFeatureReferences;
33     
34     // since 2.0.2
35
private ConfiguredSiteModel configuredSiteModel;
36     
37     // since 2.1
38
private boolean enable;
39
40     /**
41      * Constructor for ConfigurationPolicyModel.
42      */

43     public ConfigurationPolicyModel() {
44         super();
45         enable = true;
46         configuredFeatureReferences = new HashMap JavaDoc();
47 // unconfiguredFeatureReferences = new HashMap();
48
}
49
50     /**
51      * @since 2.0
52      */

53     public int getPolicy() {
54         return policy;
55     }
56
57     /**
58      * Sets the policy.
59      * @param policy The policy to set
60      */

61     public void setPolicy(int policy) {
62         assertIsWriteable();
63         this.policy = policy;
64     }
65
66     /**
67      * @since 2.0
68      */

69     public FeatureReferenceModel[] getConfiguredFeaturesModel() {
70         if (configuredFeatureReferences==null || configuredFeatureReferences.isEmpty())
71             return new FeatureReferenceModel[0];
72         return (FeatureReferenceModel[]) configuredFeatureReferences.keySet().toArray(arrayTypeFor(configuredFeatureReferences.keySet()));
73     }
74
75     /**
76      * @since 2.0
77      */

78     public FeatureReferenceModel[] getUnconfiguredFeaturesModel() {
79         // obtain unconfigured features by comparing configured ones with those installed
80
if (unconfiguredFeatureReferences == null
81                 && configuredSiteModel != null
82                 && configuredSiteModel.getSiteModel() != null) {
83             ISite site = (ISite) configuredSiteModel.getSiteModel();
84             ISiteFeatureReference[] siteFeatures = site.getFeatureReferences();
85             if (siteFeatures.length > getConfiguredFeaturesModel().length) {
86                 for (int i=0; i<siteFeatures.length; i++) {
87                     if (!(siteFeatures[i] instanceof SiteFeatureReference))
88                         continue;
89                     Iterator JavaDoc iterator = configuredFeatureReferences.keySet().iterator();
90                     boolean found = false;
91                     while(!found && iterator.hasNext()) {
92                         FeatureReferenceModel f = (FeatureReferenceModel)iterator.next();
93                         if (UpdateManagerUtils.sameURL(f.getURL(), siteFeatures[i].getURL()))
94                             found = true;
95                     }
96                     if (!found)
97                         addUnconfiguredFeatureReference((SiteFeatureReference)siteFeatures[i]);
98                 }
99             }
100         }
101         if (unconfiguredFeatureReferences == null
102                 || unconfiguredFeatureReferences.isEmpty())
103             return new FeatureReferenceModel[0];
104         return (FeatureReferenceModel[]) unconfiguredFeatureReferences.keySet()
105                 .toArray(arrayTypeFor(unconfiguredFeatureReferences.keySet()));
106     }
107
108     /**
109      * Gets the configuredSiteModel.
110      * @return Returns a ConfiguredSiteModel
111      * @since 2.0.2
112      */

113     public ConfiguredSiteModel getConfiguredSiteModel() {
114         return configuredSiteModel;
115     }
116
117     /**
118      * Sets the configuredSiteModel.
119      * @param configuredSiteModel The configuredSiteModel to set
120      * @since 2.0.2
121      */

122     public void setConfiguredSiteModel(ConfiguredSiteModel configuredSiteModel) {
123         this.configuredSiteModel = configuredSiteModel;
124     }
125
126     /**
127      *
128      */

129     private boolean remove(FeatureReferenceModel feature, Map JavaDoc list) {
130         URL featureURL = feature.getURL();
131         boolean found = false;
132         Iterator JavaDoc iter = list.keySet().iterator();
133         while (iter.hasNext() && !found) {
134             FeatureReferenceModel element = (FeatureReferenceModel) iter.next();
135             if (UpdateManagerUtils.sameURL(element.getURL(),featureURL)) {
136                 list.remove(element);
137                 found = true;
138             }
139         }
140         return found;
141     }
142
143     /**
144      * returns an array of string corresponding to plugins file
145      */

146     /*package*/
147
148     
149     /**
150      *
151      */

152     private void add(FeatureReferenceModel feature, Map JavaDoc list) {
153         URL featureURL = feature.getURL();
154         boolean found = false;
155         Iterator JavaDoc iter = list.keySet().iterator();
156         while (iter.hasNext() && !found) {
157             FeatureReferenceModel element = (FeatureReferenceModel) iter.next();
158             if (UpdateManagerUtils.sameURL(element.getURL(),featureURL)) {
159                 found = true;
160             }
161         }
162
163         if (!found) {
164             list.put(feature,null);
165         } else {
166             UpdateCore.warn("Feature Reference :"+feature+" already part of the list."); //$NON-NLS-1$ //$NON-NLS-2$
167
}
168     }
169
170     /**
171      * adds a feature in the configuredReference list
172      * also used by the parser to avoid creating another activity
173      */

174     public void addConfiguredFeatureReference(FeatureReferenceModel feature) {
175         assertIsWriteable();
176         
177         if (configuredFeatureReferences == null)
178             this.configuredFeatureReferences = new HashMap JavaDoc();
179         if (!configuredFeatureReferences.containsKey(feature)){
180             //DEBUG:
181
if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION){
182                 UpdateCore.debug("Configuring "+feature.getURLString()); //$NON-NLS-1$
183
}
184             this.add(feature, configuredFeatureReferences);
185         }
186
187         // when user configure a feature,
188
// we have to remove it from unconfigured feature if it exists
189
// because the user doesn't know...
190
if (unconfiguredFeatureReferences != null) {
191             boolean success = remove(feature, unconfiguredFeatureReferences);
192             if (!success)
193                 UpdateCore.warn("Feature not part of Unconfigured list: "+feature.getURLString()); //$NON-NLS-1$
194
}
195
196     }
197
198     /**
199      * adds a feature in the list
200      * also used by the parser to avoid creating another activity
201      */

202     public void addUnconfiguredFeatureReference(FeatureReferenceModel feature) {
203         assertIsWriteable();
204         if (unconfiguredFeatureReferences == null)
205             this.unconfiguredFeatureReferences = new HashMap JavaDoc();
206         if (!unconfiguredFeatureReferences.containsKey(feature)){
207             if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_CONFIGURATION){
208                 UpdateCore.debug("Unconfiguring "+feature.getURLString()); //$NON-NLS-1$
209
}
210             this.add(feature, unconfiguredFeatureReferences);
211         }
212
213         // an unconfigured feature is always from a configured one no ?
214
// unless it was parsed right ?
215
if (configuredFeatureReferences != null) {
216             boolean success = remove(feature, configuredFeatureReferences);
217             if (!success)
218                 UpdateCore.warn("Feature not part of Configured list: "+feature.getURLString()); //$NON-NLS-1$
219
}
220     }
221
222     /**
223      * removes a feature from any list
224      */

225     public void removeFeatureReference(FeatureReferenceModel feature) {
226         assertIsWriteable();
227         if (unconfiguredFeatureReferences!=null){
228             boolean success = remove(feature, unconfiguredFeatureReferences);
229             if (!success)
230                 UpdateCore.warn(feature.getURLString()+" not part of unconfigured list."); //$NON-NLS-1$
231
}
232
233         if (configuredFeatureReferences != null) {
234             boolean success = remove(feature, configuredFeatureReferences);
235             if (!success)
236                 UpdateCore.warn(feature.getURLString()+" not part of configured list."); //$NON-NLS-1$
237
}
238     }
239     
240     /**
241      * Sets the unconfiguredFeatureReferences.
242      * @param featureReferences The unconfiguredFeatureReferences to set
243      */

244     protected void setUnconfiguredFeatureReferences(IFeatureReference[] featureReferences) {
245         unconfiguredFeatureReferences = new HashMap JavaDoc();
246         for (int i = 0; i < featureReferences.length; i++) {
247             unconfiguredFeatureReferences.put(featureReferences[i],null);
248         }
249     }
250
251
252     /**
253      * Sets the configuredFeatureReferences.
254      * @param featureReferences The configuredFeatureReferences to set
255      */

256     protected void setConfiguredFeatureReferences(IFeatureReference[] featureReferences) {
257         configuredFeatureReferences = new HashMap JavaDoc();
258         for (int i = 0; i < featureReferences.length; i++) {
259             configuredFeatureReferences.put(featureReferences[i],null);
260         }
261     
262     }
263
264     /**
265      * @return boolean
266      */

267     public boolean isEnabled() {
268         return enable;
269     }
270
271     /**
272      * @param value
273      */

274     public void setEnabled(boolean value) {
275         enable = value;
276     }
277
278
279     
280 }
281
Popular Tags