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.commands;
011:
012: import java.net.URL;
013: import java.util.ArrayList;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.core.runtime.IExtensionDelta;
018: import org.eclipse.core.runtime.IExtensionRegistry;
019: import org.eclipse.core.runtime.IRegistryChangeEvent;
020: import org.eclipse.core.runtime.Platform;
021: import org.eclipse.ui.PlatformUI;
022: import org.eclipse.ui.commands.ICommandService;
023: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
024: import org.eclipse.ui.internal.services.RegistryPersistence;
025: import org.eclipse.ui.internal.util.BundleUtility;
026:
027: /**
028: * <p>
029: * Handles persistence for the command images.
030: * </p>
031: * <p>
032: * This class is only intended for internal use within the
033: * <code>org.eclipse.ui.workbench</code> plug-in.
034: * </p>
035: * <p>
036: * <strong>PROVISIONAL</strong>. This class or interface has been added as part
037: * of a work in progress. There is a guarantee neither that this API will work
038: * nor that it will remain the same. Please do not use this API without
039: * consulting with the Platform/UI team.
040: * </p>
041: *
042: * @since 3.2
043: */
044: final class CommandImagePersistence extends RegistryPersistence {
045:
046: /**
047: * The index of the image elements in the indexed array.
048: *
049: * @see CommandImagePersistence#read()
050: */
051: private static final int INDEX_IMAGES = 0;
052:
053: /**
054: * Reads all of the images from the command images extension point.
055: *
056: * @param configurationElements
057: * The configuration elements in the command images extension
058: * point; must not be <code>null</code>, but may be empty.
059: * @param configurationElementCount
060: * The number of configuration elements that are really in the
061: * array.
062: * @param commandImageManager
063: * The command image manager to which the images should be added;
064: * must not be <code>null</code>.
065: * @param commandService
066: * The command service for the workbench; must not be
067: * <code>null</code>.
068: */
069: private static final void readImagesFromRegistry(
070: final IConfigurationElement[] configurationElements,
071: final int configurationElementCount,
072: final CommandImageManager commandImageManager,
073: final ICommandService commandService) {
074: // Undefine all the previous images.
075: commandImageManager.clear();
076:
077: final List warningsToLog = new ArrayList(1);
078:
079: for (int i = 0; i < configurationElementCount; i++) {
080: final IConfigurationElement configurationElement = configurationElements[i];
081:
082: // Read out the command identifier.
083: final String commandId = readRequired(configurationElement,
084: ATT_COMMAND_ID, warningsToLog, "Image needs an id"); //$NON-NLS-1$
085: if (commandId == null) {
086: continue;
087: }
088:
089: if (!commandService.getCommand(commandId).isDefined()) {
090: // Reference to an undefined command. This is invalid.
091: addWarning(warningsToLog,
092: "Cannot bind to an undefined command", //$NON-NLS-1$
093: configurationElement, commandId);
094: continue;
095: }
096:
097: // Read out the style.
098: final String style = readOptional(configurationElement,
099: ATT_STYLE);
100:
101: // Read out the default icon.
102: final String icon = readRequired(configurationElement,
103: ATT_ICON, warningsToLog, commandId);
104: if (icon == null) {
105: continue;
106: }
107:
108: final String disabledIcon = readOptional(
109: configurationElement, ATT_DISABLEDICON);
110: final String hoverIcon = readOptional(configurationElement,
111: ATT_HOVERICON);
112:
113: final URL iconURL = BundleUtility.find(configurationElement
114: .getNamespace(), icon);
115: commandImageManager.bind(commandId,
116: CommandImageManager.TYPE_DEFAULT, style, iconURL);
117: if (disabledIcon != null) {
118: final URL disabledIconURL = BundleUtility.find(
119: configurationElement.getNamespace(),
120: disabledIcon);
121: commandImageManager.bind(commandId,
122: CommandImageManager.TYPE_DISABLED, style,
123: disabledIconURL);
124: }
125: if (hoverIcon != null) {
126: final URL hoverIconURL = BundleUtility.find(
127: configurationElement.getNamespace(), hoverIcon);
128: commandImageManager.bind(commandId,
129: CommandImageManager.TYPE_HOVER, style,
130: hoverIconURL);
131: }
132: }
133:
134: logWarnings(
135: warningsToLog,
136: "Warnings while parsing the images from the 'org.eclipse.ui.commandImages' extension point."); //$NON-NLS-1$
137: }
138:
139: /**
140: * The command image manager which should be populated with the values from
141: * the registry; must not be <code>null</code>.
142: */
143: private final CommandImageManager commandImageManager;
144:
145: /**
146: * The command service for the workbench; must not be <code>null</code>.
147: */
148: private final ICommandService commandService;
149:
150: /**
151: * Constructs a new instance of <code>CommandImagePersistence</code>.
152: *
153: * @param commandImageManager
154: * The command image manager which should be populated with the
155: * values from the registry; must not be <code>null</code>.
156: * @param commandService
157: * The command service for the workbench; must not be
158: * <code>null</code>.
159: */
160: CommandImagePersistence(
161: final CommandImageManager commandImageManager,
162: final ICommandService commandService) {
163: this .commandImageManager = commandImageManager;
164: this .commandService = commandService;
165: }
166:
167: protected final boolean isChangeImportant(
168: final IRegistryChangeEvent event) {
169: final IExtensionDelta[] imageDeltas = event.getExtensionDeltas(
170: PlatformUI.PLUGIN_ID,
171: IWorkbenchRegistryConstants.PL_COMMAND_IMAGES);
172: return (imageDeltas.length != 0);
173: }
174:
175: /**
176: * Reads all of the command images from the registry.
177: */
178: protected final void read() {
179: super .read();
180:
181: // Create the extension registry mementos.
182: final IExtensionRegistry registry = Platform
183: .getExtensionRegistry();
184: int imageCount = 0;
185: final IConfigurationElement[][] indexedConfigurationElements = new IConfigurationElement[1][];
186:
187: // Sort the commands extension point based on element name.
188: final IConfigurationElement[] commandImagesExtensionPoint = registry
189: .getConfigurationElementsFor(EXTENSION_COMMAND_IMAGES);
190: for (int i = 0; i < commandImagesExtensionPoint.length; i++) {
191: final IConfigurationElement configurationElement = commandImagesExtensionPoint[i];
192: final String name = configurationElement.getName();
193:
194: // Check if it is a binding definition.
195: if (TAG_IMAGE.equals(name)) {
196: addElementToIndexedArray(configurationElement,
197: indexedConfigurationElements, INDEX_IMAGES,
198: imageCount++);
199: }
200: }
201:
202: readImagesFromRegistry(
203: indexedConfigurationElements[INDEX_IMAGES], imageCount,
204: commandImageManager, commandService);
205: }
206: }
|