KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > operations > OperationsManager


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.operations;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 import org.eclipse.update.core.*;
18 import org.eclipse.update.internal.configurator.*;
19 import org.eclipse.update.internal.operations.*;
20
21 /**
22  * Entry point for update manager operations. Use this class to obtain the factory that creates
23  * update manager operations, or to get the operation validator.
24  * <p>
25  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
26  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
27  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
28  * (repeatedly) as the API evolves.
29  * </p>
30  * @since 3.0
31  */

32 public class OperationsManager {
33     private static IOperationValidator validator;
34     private static IOperationFactory operationFactory;
35     private static Vector JavaDoc listeners = new Vector JavaDoc();
36     private static Vector JavaDoc pendingOperations = new Vector JavaDoc();
37     
38     private static boolean inProgress;
39
40     private OperationsManager() {
41     }
42     
43     /**
44      * Each update operations must be created by the operation factory.
45      * Use this method to obtain the factory.
46      * @return returns the operation factory
47      */

48     public static IOperationFactory getOperationFactory() {
49         if (operationFactory == null)
50             operationFactory = new OperationFactory();
51         return operationFactory;
52     }
53
54     /**
55      * Check if the feature is the subject of an update operation such as install,
56      * configure, etc. and return it. Currently there can only be one pending
57      * operation on a feature.
58      * @param feature feature to check for pending operations
59      * @return pending operation if any, otherwise null.
60      */

61     public static IFeatureOperation findPendingOperation(IFeature feature) {
62         for (int i = 0; i < pendingOperations.size(); i++) {
63             IFeatureOperation operation =
64                 (IFeatureOperation) pendingOperations.elementAt(i);
65             if (operation.getFeature().equals(feature))
66                 return operation;
67         }
68         return null;
69     }
70     
71     /**
72      * Register a pending operation.
73      * @param operation pending operation
74      */

75     public static void addPendingOperation(IOperation operation) {
76         pendingOperations.add(operation);
77         //fireObjectsAdded(this, new Object[] { change });
78
}
79
80     /**
81      * Unregister a pending operation.
82      * @param operation pending operation
83      */

84     public static void removePendingOperation(IOperation operation) {
85         pendingOperations.remove(operation);
86         //fireObjectsRemoved(this, new Object[] { change });
87
}
88
89     /**
90      * Adds a model changed listener.
91      * @param listener update model change listener
92      */

93     public static void addUpdateModelChangedListener(IUpdateModelChangedListener listener) {
94         if (!listeners.contains(listener))
95             listeners.add(listener);
96     }
97
98     /**
99      * Removes an model changed listener.
100      * @param listener update model change listener
101      */

102     public static void removeUpdateModelChangedListener(IUpdateModelChangedListener listener) {
103         if (listeners.contains(listener))
104             listeners.remove(listener);
105     }
106
107     /**
108      * Notifies model changed listeners when features/sites/etc. are added.
109      * @param parent parent object
110      * @param children children added
111      */

112     public static void fireObjectsAdded(Object JavaDoc parent, Object JavaDoc[] children) {
113         for (Iterator JavaDoc iter = listeners.iterator(); iter.hasNext();) {
114             IUpdateModelChangedListener listener =
115                 (IUpdateModelChangedListener) iter.next();
116             listener.objectsAdded(parent, children);
117         }
118     }
119
120     /**
121      * Notifies model changed listeners when features/sites/etc are removed.
122      * @param parent parent object
123      * @param children children removed
124      */

125     public static void fireObjectsRemoved(Object JavaDoc parent, Object JavaDoc[] children) {
126         for (Iterator JavaDoc iter = listeners.iterator(); iter.hasNext();) {
127             IUpdateModelChangedListener listener =
128                 (IUpdateModelChangedListener) iter.next();
129             listener.objectsRemoved(parent, children);
130         }
131     }
132
133     /**
134      * Notifies model changed listeners when features/sites/etc. have changed.
135      * @param object changed object
136      * @param property changed object property
137      */

138     public static void fireObjectChanged(Object JavaDoc object, String JavaDoc property) {
139         for (Iterator JavaDoc iter = listeners.iterator(); iter.hasNext();) {
140             IUpdateModelChangedListener listener =
141                 (IUpdateModelChangedListener) iter.next();
142             listener.objectChanged(object, property);
143         }
144     }
145
146     /**
147      * Returns true when any of the install operations requires a license agreement.
148      * @param jobs features to install
149      * @return true when any of the features to install have a license
150      */

151     public static boolean hasSelectedJobsWithLicenses(IInstallFeatureOperation[] jobs) {
152         for (int i = 0; i < jobs.length; i++) {
153             if (UpdateUtils.hasLicense(jobs[i].getFeature()))
154                 return true;
155         }
156         return false;
157     }
158
159     /**
160      * Returns true when any of the features to install has optional features.
161      * @param jobs features to install
162      * @return true when any of the features has optional features
163      */

164     public static boolean hasSelectedJobsWithOptionalFeatures(IInstallFeatureOperation[] jobs) {
165         for (int i = 0; i < jobs.length; i++) {
166             if (UpdateUtils.hasOptionalFeatures(jobs[i].getFeature()))
167                 return true;
168         }
169         return false;
170     }
171
172     /**
173      * Returns the list of operations that need a license agreement.
174      * @param jobs features to install
175      * @return the list of operation that need a license agreement
176      */

177     public static IInstallFeatureOperation[] getSelectedJobsWithLicenses(IInstallFeatureOperation[] jobs) {
178         ArrayList JavaDoc list = new ArrayList JavaDoc();
179         for (int i = 0; i < jobs.length; i++) {
180             if (UpdateUtils.hasLicense(jobs[i].getFeature()))
181                 list.add(jobs[i]);
182         }
183         return (IInstallFeatureOperation[]) list.toArray(
184             new IInstallFeatureOperation[list.size()]);
185     }
186
187     /**
188      * Returns the list of operations that have optional features to install.
189      * @param jobs features to install
190      * @return list of operations that have optional features to install
191      */

192     public static IInstallFeatureOperation[] getSelectedJobsWithOptionalFeatures(IInstallFeatureOperation[] jobs) {
193         ArrayList JavaDoc list = new ArrayList JavaDoc();
194         for (int i = 0; i < jobs.length; i++) {
195             if (UpdateUtils.hasOptionalFeatures(jobs[i].getFeature()))
196                 list.add(jobs[i]);
197         }
198         return (IInstallFeatureOperation[]) list.toArray(
199             new IInstallFeatureOperation[list.size()]);
200     }
201     
202     /**
203      * Sets whether any operations is in progress.
204      * @param inProgress true when operation is in progress
205      */

206     public static synchronized void setInProgress(boolean inProgress) {
207         OperationsManager.inProgress = inProgress;
208     }
209     
210     /**
211      * Returns true when some operation is being executed, false otherwise.
212      * @return true when some operation execution is in progress, false otherwise
213      */

214     public static synchronized boolean isInProgress() {
215         return inProgress;
216     }
217
218     /**
219      * Returns the operations validator.
220      * @return the operation validator
221      */

222     public static IOperationValidator getValidator() {
223         if (validator == null)
224             validator = new OperationValidator();
225         return validator;
226     }
227     
228     /**
229      * Sets a custom operation validator
230      * @param validator the custom validator
231      */

232     public static void setValidator(IOperationValidator validator) {
233         OperationsManager.validator = validator;
234     }
235     
236     /**
237      * Applies the changes made to the current configuration.
238      * Care must be taken when using this method. Normally, if you install a new
239      * plugin it is safe to do it.
240      */

241     public static void applyChangesNow() {
242         ConfigurationActivator configurator = ConfigurationActivator.getConfigurator();
243         configurator.installBundles();
244         pendingOperations.clear();
245     }
246 }
247
Popular Tags