001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.registry;
011:
012: import com.ibm.icu.text.Collator;
013: import java.util.ArrayList;
014: import java.util.Collections;
015: import java.util.Comparator;
016: import java.util.HashMap;
017: import java.util.List;
018: import java.util.Map;
019:
020: import org.eclipse.core.runtime.IConfigurationElement;
021: import org.eclipse.core.runtime.IExtensionRegistry;
022: import org.eclipse.core.runtime.Platform;
023: import org.eclipse.core.runtime.preferences.PreferenceFilterEntry;
024: import org.eclipse.ui.internal.WorkbenchPlugin;
025: import org.eclipse.ui.internal.preferences.PreferenceTransferElement;
026:
027: /**
028: * Preference Transfer registry reader to read extenders of the
029: * preferenceTranser schema.
030: *
031: * @since 3.1
032: */
033: public class PreferenceTransferRegistryReader extends RegistryReader {
034: private List preferenceTransfers;
035:
036: private String pluginPoint;
037:
038: /**
039: * Create an instance of this class.
040: *
041: * @param pluginPointId
042: * java.lang.String
043: */
044: public PreferenceTransferRegistryReader(String pluginPointId) {
045: pluginPoint = pluginPointId;
046: }
047:
048: /**
049: * Returns a new PreferenceTransferElement configured according to the
050: * parameters contained in the passed Registry.
051: *
052: * May answer null if there was not enough information in the Extension to
053: * create an adequate wizard
054: */
055: protected PreferenceTransferElement createPreferenceTransferElement(
056: IConfigurationElement element) {
057: // PreferenceTransfers must have a name and class attribute
058: if (element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME) == null) {
059: logMissingAttribute(element,
060: IWorkbenchRegistryConstants.ATT_NAME);
061: return null;
062: }
063:
064: // must specifiy a mapping
065: if (element
066: .getChildren(IWorkbenchRegistryConstants.TAG_MAPPING) == null) {
067: logMissingElement(element,
068: IWorkbenchRegistryConstants.TAG_MAPPING);
069: return null;
070: }
071:
072: return new PreferenceTransferElement(element);
073: }
074:
075: /**
076: * Returns a sorted list of preference transfers.
077: *
078: * @return an array of <code>IPreferenceTransfer</code> objects
079: */
080: public PreferenceTransferElement[] getPreferenceTransfers() {
081: readPreferenceTransfers();
082: PreferenceTransferElement[] transfers = new PreferenceTransferElement[preferenceTransfers
083: .size()];
084: Collections.sort(preferenceTransfers, new Comparator() {
085: public int compare(Object o1, Object o2) {
086: String name1 = ((PreferenceTransferElement) o1)
087: .getName();
088: String name2 = ((PreferenceTransferElement) o2)
089: .getName();
090:
091: return Collator.getInstance().compare(name1, name2);
092: }
093: });
094: preferenceTransfers.toArray(transfers);
095: return transfers;
096: }
097:
098: /*
099: * (non-Javadoc)
100: *
101: * @see org.eclipse.ui.internal.registry.RegistryReader#readElement(org.eclipse.core.runtime.IConfigurationElement)
102: */
103: protected boolean readElement(IConfigurationElement element) {
104: if (element.getName().equals(
105: IWorkbenchRegistryConstants.TAG_TRANSFER)) {
106:
107: PreferenceTransferElement transfer = createPreferenceTransferElement(element);
108: if (transfer != null)
109: preferenceTransfers.add(transfer);
110: return true;
111: }
112:
113: // Allow settings transfers as well.
114:
115: return element.getName().equals(
116: IWorkbenchRegistryConstants.TAG_SETTINGS_TRANSFER);
117: }
118:
119: /**
120: * Reads the wizards in a registry.
121: */
122: protected void readPreferenceTransfers() {
123: preferenceTransfers = new ArrayList();
124: IExtensionRegistry registry = Platform.getExtensionRegistry();
125: readRegistry(registry, WorkbenchPlugin.PI_WORKBENCH,
126: pluginPoint);
127: }
128:
129: /**
130: * Get the preference mappings.
131: *
132: * @param configElement
133: * @return the child configuration elements
134: */
135: public static IConfigurationElement[] getMappings(
136: IConfigurationElement configElement) {
137: IConfigurationElement[] children = configElement
138: .getChildren(IWorkbenchRegistryConstants.TAG_MAPPING);
139: if (children.length < 1) {
140: logMissingElement(configElement,
141: IWorkbenchRegistryConstants.TAG_MAPPING);
142: return null;
143: }
144: return children;
145: }
146:
147: /**
148: * @param element
149: * @return the scope attribute for this element
150: */
151: public static String getScope(IConfigurationElement element) {
152: return element
153: .getAttribute(IWorkbenchRegistryConstants.ATT_SCOPE);
154: }
155:
156: /**
157: * @param element
158: * @return the maps mapping nodes to keys for this element
159: */
160: public static Map getEntry(IConfigurationElement element) {
161: IConfigurationElement[] entries = element
162: .getChildren(IWorkbenchRegistryConstants.TAG_ENTRY);
163: if (entries.length == 0) {
164: return null;
165: }
166: Map map = new HashMap(entries.length);
167: for (int i = 0; i < entries.length; i++) {
168: IConfigurationElement entry = entries[i];
169: IConfigurationElement[] keys = entry
170: .getChildren(IWorkbenchRegistryConstants.ATT_KEY);
171: PreferenceFilterEntry[] prefFilters = null;
172: if (keys.length > 0) {
173: prefFilters = new PreferenceFilterEntry[keys.length];
174: for (int j = 0; j < keys.length; j++) {
175: IConfigurationElement keyElement = keys[j];
176: prefFilters[j] = new PreferenceFilterEntry(
177: keyElement
178: .getAttribute(IWorkbenchRegistryConstants.ATT_NAME));
179: }
180: }
181: map
182: .put(
183: entry
184: .getAttribute(IWorkbenchRegistryConstants.ATT_NODE),
185: prefFilters);
186: }
187: return map;
188: }
189: }
|