KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > saveparticipant > SaveParticipantRegistry


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.jdt.internal.ui.javaeditor.saveparticipant;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.preferences.IScopeContext;
19
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.resources.ProjectScope;
22
23 import org.eclipse.jdt.internal.corext.fix.CleanUpPostSaveListener;
24
25 import org.eclipse.jdt.internal.ui.JavaPlugin;
26 import org.eclipse.jdt.internal.ui.fix.CleanUpSaveParticipantPreferenceConfiguration;
27 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitDocumentProvider;
28
29 /**
30  * A registry for save participants. This registry manages
31  * {@link SaveParticipantDescriptor}s and keeps track of enabled save
32  * participants.
33  * <p>
34  * Save participants can be enabled and disabled on the Java &gt; Editor &gt;
35  * Save Participants preference page. Enabled save participants are notified
36  * through a call to
37  * {@link IPostSaveListener#saved(org.eclipse.jdt.core.ICompilationUnit, org.eclipse.core.runtime.IProgressMonitor)}
38  * whenever the {@link CompilationUnitDocumentProvider} saves a compilation unit
39  * that is in the workspace.</p>
40  * <p>
41  * An instance of this registry can be received through a call to {@link JavaPlugin#getSaveParticipantRegistry()}.</p>
42  *
43  * @since 3.3
44  */

45 public final class SaveParticipantRegistry {
46
47     private static final IPostSaveListener[] EMPTY_ARRAY= new IPostSaveListener[0];
48     
49     /** The map of descriptors, indexed by their identifiers. */
50     private Map JavaDoc fDescriptors;
51
52     /**
53      * Creates a new instance.
54      */

55     public SaveParticipantRegistry() {
56     }
57
58     /**
59      * Returns an array of <code>SaveParticipantDescriptor</code> describing
60      * all registered save participants.
61      *
62      * @return the array of registered save participant descriptors
63      */

64     public synchronized SaveParticipantDescriptor[] getSaveParticipantDescriptors() {
65         ensureRegistered();
66         return (SaveParticipantDescriptor[]) fDescriptors.values().toArray(new SaveParticipantDescriptor[fDescriptors.size()]);
67     }
68
69     /**
70      * Returns the save participant descriptor for the given <code>id</code> or
71      * <code>null</code> if no such listener is registered.
72      *
73      * @param id the identifier of the requested save participant
74      * @return the corresponding descriptor, or <code>null</code> if none can be found
75      */

76     public synchronized SaveParticipantDescriptor getSaveParticipantDescriptor(String JavaDoc id) {
77         ensureRegistered();
78         return (SaveParticipantDescriptor) fDescriptors.get(id);
79     }
80
81     /**
82      * Ensures that all descriptors are created and stored in
83      * <code>fDescriptors</code>.
84      */

85     private void ensureRegistered() {
86         if (fDescriptors == null)
87             reloadDescriptors();
88     }
89
90     /**
91      * Loads the save participants.
92      * <p>
93      * This method can be called more than once in
94      * order to reload from a changed extension registry.
95      * </p>
96      */

97     private void reloadDescriptors() {
98         Map JavaDoc map= new HashMap JavaDoc();
99         SaveParticipantDescriptor desc= new SaveParticipantDescriptor(new CleanUpPostSaveListener()) {
100             /**
101              * {@inheritDoc}
102              */

103             public ISaveParticipantPreferenceConfiguration createPreferenceConfiguration() {
104                 return new CleanUpSaveParticipantPreferenceConfiguration();
105             }
106         };
107         map.put(desc.getId(), desc);
108                 
109         fDescriptors= map;
110     }
111
112     public void dispose() {
113     }
114
115     /**
116      * Checks weather there are enabled or disabled post save listener in the given context.
117      *
118      * @param context to context to check, not null
119      * @return true if there are settings in context
120      */

121     public synchronized boolean hasSettingsInScope(IScopeContext context) {
122         ensureRegistered();
123     
124         for (Iterator JavaDoc iterator= fDescriptors.values().iterator(); iterator.hasNext();) {
125             SaveParticipantDescriptor descriptor= (SaveParticipantDescriptor)iterator.next();
126             if (descriptor.getPreferenceConfiguration().hasSettingsInScope(context))
127                 return true;
128         }
129         
130         return false;
131     }
132     
133     public IPostSaveListener[] getEnabledPostSaveListeners(IProject project) {
134         return getEnabledPostSaveListeners(new ProjectScope(project));
135     }
136
137     /**
138      * Returns an array of <code>IPostSaveListener</code> which are
139      * enabled in the given context.
140      *
141      * @param context the context from which to retrive the settings from, not null
142      * @return the current enabled post save listeners according to the preferences
143      */

144     public synchronized IPostSaveListener[] getEnabledPostSaveListeners(IScopeContext context) {
145         ensureRegistered();
146         
147         ArrayList JavaDoc result= null;
148         for (Iterator JavaDoc iterator= fDescriptors.values().iterator(); iterator.hasNext();) {
149             SaveParticipantDescriptor descriptor= (SaveParticipantDescriptor)iterator.next();
150             if (descriptor.getPreferenceConfiguration().isEnabled(context)) {
151                 if (result == null) {
152                     result= new ArrayList JavaDoc();
153                 }
154                 result.add(descriptor.getPostSaveListener());
155             }
156         }
157         
158         if (result == null) {
159             return EMPTY_ARRAY;
160         } else {
161             return (IPostSaveListener[])result.toArray(new IPostSaveListener[result.size()]);
162         }
163     }
164     
165 }
166
Popular Tags