1 11 12 package org.eclipse.ui.internal.commands; 13 14 import java.net.URL ; 15 import java.util.ArrayList ; 16 import java.util.List ; 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 46 final class CommandImagePersistence extends RegistryPersistence { 47 48 53 private static final int INDEX_IMAGES = 0; 54 55 71 private static final void readImagesFromRegistry( 72 final IConfigurationElement[] configurationElements, 73 final int configurationElementCount, 74 final CommandImageManager commandImageManager, 75 final ICommandService commandService) { 76 commandImageManager.clear(); 78 79 final List warningsToLog = new ArrayList (1); 80 81 for (int i = 0; i < configurationElementCount; i++) { 82 final IConfigurationElement configurationElement = configurationElements[i]; 83 84 final String commandId = readRequired(configurationElement, 86 ATT_COMMAND_ID, warningsToLog, "Image needs an id"); if (commandId == null) { 88 continue; 89 } 90 91 if (!commandService.getCommand(commandId).isDefined()) { 92 addWarning(warningsToLog, 94 "Cannot bind to an undefined command", configurationElement, commandId); 96 continue; 97 } 98 99 final String style = readOptional(configurationElement, ATT_STYLE); 101 102 final String icon = readRequired(configurationElement, ATT_ICON, 104 warningsToLog, commandId); 105 if (icon == null) { 106 continue; 107 } 108 109 final String disabledIcon = readOptional(configurationElement, 110 ATT_DISABLEDICON); 111 final String hoverIcon = readOptional(configurationElement, 112 ATT_HOVERICON); 113 114 final URL iconURL = BundleUtility.find(configurationElement 115 .getNamespace(), icon); 116 commandImageManager.bind(commandId, 117 CommandImageManager.TYPE_DEFAULT, style, iconURL); 118 if (disabledIcon != null) { 119 final URL 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 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."); } 137 138 142 private final CommandImageManager commandImageManager; 143 144 147 private final ICommandService commandService; 148 149 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 175 protected final void read() { 176 super.read(); 177 178 final IExtensionRegistry registry = Platform.getExtensionRegistry(); 180 int imageCount = 0; 181 final IConfigurationElement[][] indexedConfigurationElements = new IConfigurationElement[1][]; 182 183 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 name = configurationElement.getName(); 189 190 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 |