001: /*******************************************************************************
002: * Copyright (c) 2000, 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 java.util.ArrayList;
013: import java.util.List;
014: import java.util.StringTokenizer;
015:
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.core.runtime.IExtensionRegistry;
018: import org.eclipse.core.runtime.Platform;
019: import org.eclipse.ui.PlatformUI;
020:
021: /**
022: * This class is used to read resource editor registry descriptors from
023: * the platform registry.
024: */
025: public class EditorRegistryReader extends RegistryReader {
026:
027: private EditorRegistry editorRegistry;
028:
029: /**
030: * Get the editors that are defined in the registry
031: * and add them to the ResourceEditorRegistry
032: *
033: * Warning:
034: * The registry must be passed in because this method is called during the
035: * process of setting up the registry and at this time it has not been
036: * safely setup with the plugin.
037: */
038: protected void addEditors(EditorRegistry registry) {
039: IExtensionRegistry extensionRegistry = Platform
040: .getExtensionRegistry();
041: this .editorRegistry = registry;
042: readRegistry(extensionRegistry, PlatformUI.PLUGIN_ID,
043: IWorkbenchRegistryConstants.PL_EDITOR);
044: }
045:
046: /**
047: * Implementation of the abstract method that
048: * processes one configuration element.
049: */
050: protected boolean readElement(IConfigurationElement element) {
051: if (!element.getName().equals(
052: IWorkbenchRegistryConstants.TAG_EDITOR)) {
053: return false;
054: }
055:
056: String id = element
057: .getAttribute(IWorkbenchRegistryConstants.ATT_ID);
058: if (id == null) {
059: logMissingAttribute(element,
060: IWorkbenchRegistryConstants.ATT_ID);
061: return true;
062: }
063:
064: EditorDescriptor editor = new EditorDescriptor(id, element);
065:
066: List extensionsVector = new ArrayList();
067: List filenamesVector = new ArrayList();
068: List contentTypeVector = new ArrayList();
069: boolean defaultEditor = false;
070:
071: // Get editor name (required field).
072: if (element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME) == null) {
073: logMissingAttribute(element,
074: IWorkbenchRegistryConstants.ATT_NAME);
075: return true;
076: }
077:
078: // Get editor icon (required field for internal editors)
079: if (element.getAttribute(IWorkbenchRegistryConstants.ATT_ICON) == null) {
080: if (getClassValue(element,
081: IWorkbenchRegistryConstants.ATT_CLASS) != null) {
082: logMissingAttribute(element,
083: IWorkbenchRegistryConstants.ATT_ICON);
084: return true;
085: }
086: }
087:
088: // Get target extensions (optional field)
089: String extensionsString = element
090: .getAttribute(IWorkbenchRegistryConstants.ATT_EXTENSIONS);
091: if (extensionsString != null) {
092: StringTokenizer tokenizer = new StringTokenizer(
093: extensionsString, ",");//$NON-NLS-1$
094: while (tokenizer.hasMoreTokens()) {
095: extensionsVector.add(tokenizer.nextToken().trim());
096: }
097: }
098: String filenamesString = element
099: .getAttribute(IWorkbenchRegistryConstants.ATT_FILENAMES);
100: if (filenamesString != null) {
101: StringTokenizer tokenizer = new StringTokenizer(
102: filenamesString, ",");//$NON-NLS-1$
103: while (tokenizer.hasMoreTokens()) {
104: filenamesVector.add(tokenizer.nextToken().trim());
105: }
106: }
107:
108: IConfigurationElement[] bindings = element
109: .getChildren(IWorkbenchRegistryConstants.TAG_CONTENT_TYPE_BINDING);
110: for (int i = 0; i < bindings.length; i++) {
111: String contentTypeId = bindings[i]
112: .getAttribute(IWorkbenchRegistryConstants.ATT_CONTENT_TYPE_ID);
113: if (contentTypeId == null) {
114: continue;
115: }
116: contentTypeVector.add(contentTypeId);
117: }
118:
119: // Is this the default editor?
120: String def = element
121: .getAttribute(IWorkbenchRegistryConstants.ATT_DEFAULT);
122: if (def != null) {
123: defaultEditor = Boolean.valueOf(def).booleanValue();
124: }
125:
126: // Add the editor to the manager.
127: editorRegistry.addEditorFromPlugin(editor, extensionsVector,
128: filenamesVector, contentTypeVector, defaultEditor);
129: return true;
130: }
131:
132: /**
133: * @param editorRegistry
134: * @param element
135: */
136: public void readElement(EditorRegistry editorRegistry,
137: IConfigurationElement element) {
138: this.editorRegistry = editorRegistry;
139: readElement(element);
140: }
141: }
|