KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > commands > CommandImagePersistence


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.commands;
13
14 import java.net.URL JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IExtensionDelta;
20 import org.eclipse.core.runtime.IExtensionRegistry;
21 import org.eclipse.core.runtime.IRegistryChangeEvent;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.commands.ICommandService;
25 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
26 import org.eclipse.ui.internal.services.RegistryPersistence;
27 import org.eclipse.ui.internal.util.BundleUtility;
28
29 /**
30  * <p>
31  * Handles persistence for the command images.
32  * </p>
33  * <p>
34  * This class is only intended for internal use within the
35  * <code>org.eclipse.ui.workbench</code> plug-in.
36  * </p>
37  * <p>
38  * <strong>PROVISIONAL</strong>. This class or interface has been added as part
39  * of a work in progress. There is a guarantee neither that this API will work
40  * nor that it will remain the same. Please do not use this API without
41  * consulting with the Platform/UI team.
42  * </p>
43  *
44  * @since 3.2
45  */

46 final class CommandImagePersistence extends RegistryPersistence {
47
48     /**
49      * The index of the image elements in the indexed array.
50      *
51      * @see CommandImagePersistence#read()
52      */

53     private static final int INDEX_IMAGES = 0;
54
55     /**
56      * Reads all of the images from the command images extension point.
57      *
58      * @param configurationElements
59      * The configuration elements in the command images extension
60      * point; must not be <code>null</code>, but may be empty.
61      * @param configurationElementCount
62      * The number of configuration elements that are really in the
63      * array.
64      * @param commandImageManager
65      * The command image manager to which the images should be added;
66      * must not be <code>null</code>.
67      * @param commandService
68      * The command service for the workbench; must not be
69      * <code>null</code>.
70      */

71     private static final void readImagesFromRegistry(
72             final IConfigurationElement[] configurationElements,
73             final int configurationElementCount,
74             final CommandImageManager commandImageManager,
75             final ICommandService commandService) {
76         // Undefine all the previous images.
77
commandImageManager.clear();
78
79         final List JavaDoc warningsToLog = new ArrayList JavaDoc(1);
80
81         for (int i = 0; i < configurationElementCount; i++) {
82             final IConfigurationElement configurationElement = configurationElements[i];
83
84             // Read out the command identifier.
85
final String JavaDoc commandId = readRequired(configurationElement,
86                     ATT_COMMAND_ID, warningsToLog, "Image needs an id"); //$NON-NLS-1$
87
if (commandId == null) {
88                 continue;
89             }
90
91             if (!commandService.getCommand(commandId).isDefined()) {
92                 // Reference to an undefined command. This is invalid.
93
addWarning(warningsToLog,
94                         "Cannot bind to an undefined command", //$NON-NLS-1$
95
configurationElement, commandId);
96                 continue;
97             }
98
99             // Read out the style.
100
final String JavaDoc style = readOptional(configurationElement, ATT_STYLE);
101
102             // Read out the default icon.
103
final String JavaDoc icon = readRequired(configurationElement, ATT_ICON,
104                     warningsToLog, commandId);
105             if (icon == null) {
106                 continue;
107             }
108
109             final String JavaDoc disabledIcon = readOptional(configurationElement,
110                     ATT_DISABLEDICON);
111             final String JavaDoc hoverIcon = readOptional(configurationElement,
112                     ATT_HOVERICON);
113
114             final URL JavaDoc iconURL = BundleUtility.find(configurationElement
115                     .getNamespace(), icon);
116             commandImageManager.bind(commandId,
117                     CommandImageManager.TYPE_DEFAULT, style, iconURL);
118             if (disabledIcon != null) {
119                 final URL JavaDoc disabledIconURL = BundleUtility.find(
120                         configurationElement.getNamespace(), disabledIcon);
121                 commandImageManager.bind(commandId,
122                         CommandImageManager.TYPE_DISABLED, style,
123                         disabledIconURL);
124             }
125             if (hoverIcon != null) {
126                 final URL JavaDoc hoverIconURL = BundleUtility.find(
127                         configurationElement.getNamespace(), hoverIcon);
128                 commandImageManager.bind(commandId,
129                         CommandImageManager.TYPE_HOVER, style, hoverIconURL);
130             }
131         }
132
133         logWarnings(
134                 warningsToLog,
135                 "Warnings while parsing the images from the 'org.eclipse.ui.commandImages' extension point."); //$NON-NLS-1$
136
}
137
138     /**
139      * The command image manager which should be populated with the values from
140      * the registry; must not be <code>null</code>.
141      */

142     private final CommandImageManager commandImageManager;
143
144     /**
145      * The command service for the workbench; must not be <code>null</code>.
146      */

147     private final ICommandService commandService;
148
149     /**
150      * Constructs a new instance of <code>CommandImagePersistence</code>.
151      *
152      * @param commandImageManager
153      * The command image manager which should be populated with the
154      * values from the registry; must not be <code>null</code>.
155      * @param commandService
156      * The command service for the workbench; must not be
157      * <code>null</code>.
158      */

159     CommandImagePersistence(final CommandImageManager commandImageManager,
160             final ICommandService commandService) {
161         this.commandImageManager = commandImageManager;
162         this.commandService = commandService;
163     }
164
165     protected final boolean isChangeImportant(final IRegistryChangeEvent event) {
166         final IExtensionDelta[] imageDeltas = event.getExtensionDeltas(
167                 PlatformUI.PLUGIN_ID,
168                 IWorkbenchRegistryConstants.PL_COMMAND_IMAGES);
169         return (imageDeltas.length != 0);
170     }
171
172     /**
173      * Reads all of the command images from the registry.
174      */

175     protected final void read() {
176         super.read();
177
178         // Create the extension registry mementos.
179
final IExtensionRegistry registry = Platform.getExtensionRegistry();
180         int imageCount = 0;
181         final IConfigurationElement[][] indexedConfigurationElements = new IConfigurationElement[1][];
182
183         // Sort the commands extension point based on element name.
184
final IConfigurationElement[] commandImagesExtensionPoint = registry
185                 .getConfigurationElementsFor(EXTENSION_COMMAND_IMAGES);
186         for (int i = 0; i < commandImagesExtensionPoint.length; i++) {
187             final IConfigurationElement configurationElement = commandImagesExtensionPoint[i];
188             final String JavaDoc name = configurationElement.getName();
189
190             // Check if it is a binding definition.
191
if (TAG_IMAGE.equals(name)) {
192                 addElementToIndexedArray(configurationElement,
193                         indexedConfigurationElements, INDEX_IMAGES,
194                         imageCount++);
195             }
196         }
197
198         readImagesFromRegistry(indexedConfigurationElements[INDEX_IMAGES],
199                 imageCount, commandImageManager, commandService);
200     }
201 }
202
Popular Tags