KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > registry > PreferenceTransferRegistryReader


1 /*******************************************************************************
2  * Copyright (c) 2005, 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
12 package org.eclipse.ui.internal.registry;
13
14 import com.ibm.icu.text.Collator;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.core.runtime.IExtensionRegistry;
24 import org.eclipse.core.runtime.Platform;
25 import org.eclipse.core.runtime.preferences.PreferenceFilterEntry;
26 import org.eclipse.ui.internal.WorkbenchPlugin;
27 import org.eclipse.ui.internal.preferences.PreferenceTransferElement;
28
29 /**
30  * Preference Transfer registry reader to read extenders of the
31  * preferenceTranser schema.
32  *
33  * @since 3.1
34  */

35 public class PreferenceTransferRegistryReader extends RegistryReader {
36     private List JavaDoc preferenceTransfers;
37
38     private String JavaDoc pluginPoint;
39
40     /**
41      * Create an instance of this class.
42      *
43      * @param pluginPointId
44      * java.lang.String
45      */

46     public PreferenceTransferRegistryReader(String JavaDoc pluginPointId) {
47         pluginPoint = pluginPointId;
48     }
49
50
51
52     /**
53      * Returns a new PreferenceTransferElement configured according to the
54      * parameters contained in the passed Registry.
55      *
56      * May answer null if there was not enough information in the Extension to
57      * create an adequate wizard
58      */

59     protected PreferenceTransferElement createPreferenceTransferElement(
60             IConfigurationElement element) {
61         // PreferenceTransfers must have a name and class attribute
62
if (element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME) == null) {
63             logMissingAttribute(element, IWorkbenchRegistryConstants.ATT_NAME);
64             return null;
65         }
66
67         // must specifiy a mapping
68
if (element.getChildren(IWorkbenchRegistryConstants.TAG_MAPPING) == null) {
69             logMissingElement(element, IWorkbenchRegistryConstants.TAG_MAPPING);
70             return null;
71         }
72
73         return new PreferenceTransferElement(element);
74     }
75
76     /**
77      * Returns a sorted list of preference transfers.
78      *
79      * @return an array of <code>IPreferenceTransfer</code> objects
80      */

81     public PreferenceTransferElement[] getPreferenceTransfers() {
82         readPreferenceTransfers();
83         PreferenceTransferElement[] transfers = new PreferenceTransferElement[preferenceTransfers
84                 .size()];
85         Collections.sort(preferenceTransfers, new Comparator JavaDoc() {
86             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
87                 String JavaDoc name1 = ((PreferenceTransferElement) o1).getName();
88                 String JavaDoc name2 = ((PreferenceTransferElement) o2).getName();
89
90                 return Collator.getInstance().compare(name1, name2);
91             }
92         });
93         preferenceTransfers.toArray(transfers);
94         return transfers;
95     }
96
97     /*
98      * (non-Javadoc)
99      *
100      * @see org.eclipse.ui.internal.registry.RegistryReader#readElement(org.eclipse.core.runtime.IConfigurationElement)
101      */

102     protected boolean readElement(IConfigurationElement element) {
103         if (element.getName().equals(IWorkbenchRegistryConstants.TAG_TRANSFER)) {
104
105             PreferenceTransferElement transfer = createPreferenceTransferElement(element);
106             if (transfer != null)
107                 preferenceTransfers.add(transfer);
108             return true;
109         }
110
111         // Allow settings transfers as well.
112

113         return element.getName().equals(
114                 IWorkbenchRegistryConstants.TAG_SETTINGS_TRANSFER);
115     }
116
117     /**
118      * Reads the wizards in a registry.
119      */

120     protected void readPreferenceTransfers() {
121         preferenceTransfers = new ArrayList JavaDoc();
122         IExtensionRegistry registry = Platform.getExtensionRegistry();
123         readRegistry(registry, WorkbenchPlugin.PI_WORKBENCH, pluginPoint);
124     }
125
126     /**
127      * Get the preference mappings.
128      *
129      * @param configElement
130      * @return the child configuration elements
131      */

132     public static IConfigurationElement[] getMappings(
133             IConfigurationElement configElement) {
134         IConfigurationElement[] children = configElement
135                 .getChildren(IWorkbenchRegistryConstants.TAG_MAPPING);
136         if (children.length < 1) {
137             logMissingElement(configElement,
138                     IWorkbenchRegistryConstants.TAG_MAPPING);
139             return null;
140         }
141         return children;
142     }
143
144     /**
145      * @param element
146      * @return the scope attribute for this element
147      */

148     public static String JavaDoc getScope(IConfigurationElement element) {
149         return element.getAttribute(IWorkbenchRegistryConstants.ATT_SCOPE);
150     }
151
152     /**
153      * @param element
154      * @return the maps mapping nodes to keys for this element
155      */

156     public static Map JavaDoc getEntry(IConfigurationElement element) {
157         IConfigurationElement[] entries = element
158                 .getChildren(IWorkbenchRegistryConstants.TAG_ENTRY);
159         if (entries.length == 0) {
160             return null;
161         }
162         Map JavaDoc map = new HashMap JavaDoc(entries.length);
163         for (int i = 0; i < entries.length; i++) {
164             IConfigurationElement entry = entries[i];
165             IConfigurationElement[] keys = entry
166                     .getChildren(IWorkbenchRegistryConstants.ATT_KEY);
167             PreferenceFilterEntry[] prefFilters = null;
168             if (keys.length > 0) {
169                 prefFilters = new PreferenceFilterEntry[keys.length];
170                 for (int j = 0; j < keys.length; j++) {
171                     IConfigurationElement keyElement = keys[j];
172                     prefFilters[j] = new PreferenceFilterEntry(keyElement
173                             .getAttribute(IWorkbenchRegistryConstants.ATT_NAME));
174                 }
175             }
176             map.put(entry.getAttribute(IWorkbenchRegistryConstants.ATT_NODE),
177                     prefFilters);
178         }
179         return map;
180     }
181 }
182
Popular Tags