kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2001, 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 | *******************************************************************************/ |
kchong | 2be71b3 | 2006-04-11 16:32:03 +0000 | [diff] [blame] | 11 | package org.eclipse.wst.xsd.ui.internal.editor; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 12 | |
| 13 | import java.util.ArrayList; |
| 14 | import java.util.Collections; |
| 15 | import java.util.List; |
| 16 | |
| 17 | import org.eclipse.core.runtime.IConfigurationElement; |
| 18 | import org.eclipse.core.runtime.Platform; |
| 19 | import org.eclipse.gef.EditPartFactory; |
kchong | 2be71b3 | 2006-04-11 16:32:03 +0000 | [diff] [blame] | 20 | import org.eclipse.wst.xsd.ui.internal.actions.IXSDToolbarAction; |
| 21 | import org.eclipse.wst.xsd.ui.internal.adapters.XSDAdapterFactory; |
| 22 | import org.eclipse.wst.xsd.ui.internal.design.figures.IExtendedFigureFactory; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 23 | |
| 24 | public class XSDEditorConfiguration |
| 25 | { |
kchong | 618a194 | 2006-04-13 07:14:23 +0000 | [diff] [blame] | 26 | public static final String XSDEDITORCONFIGURATIONEXTENSIONID = "org.eclipse.wst.xsd.ui.XSDEditorExtensionConfiguration"; //$NON-NLS-1$ |
kchong | cf01a7c | 2006-05-26 18:33:23 +0000 | [diff] [blame] | 27 | public static final String INTERNALEDITORCONFIGURATION_EXTENSIONID = "org.eclipse.wst.xsd.ui.internalEditorConfiguration"; //$NON-NLS-1$ |
kchong | 618a194 | 2006-04-13 07:14:23 +0000 | [diff] [blame] | 28 | public static final String CLASSNAME = "class"; //$NON-NLS-1$ |
| 29 | public static final String ADAPTERFACTORY = "adapterFactory"; //$NON-NLS-1$ |
| 30 | public static final String TOOLBARACTION = "toolbarAction"; //$NON-NLS-1$ |
| 31 | public static final String FIGUREFACTORY = "figureFactory"; //$NON-NLS-1$ |
| 32 | public static final String EDITPARTFACTORY = "editPartFactory"; //$NON-NLS-1$ |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 33 | |
| 34 | List definedExtensionsList = null; |
| 35 | |
| 36 | public XSDEditorConfiguration() |
| 37 | { |
| 38 | |
| 39 | } |
| 40 | |
| 41 | public XSDAdapterFactory getAdapterFactory() |
| 42 | { |
| 43 | if (definedExtensionsList == null) |
| 44 | { |
| 45 | readXSDConfigurationRegistry(); |
| 46 | } |
| 47 | if (!definedExtensionsList.isEmpty()) |
| 48 | { |
| 49 | return ((XSDEditorExtensionProperties) definedExtensionsList.get(0)).getAdapterFactory(); |
| 50 | } |
| 51 | return null; |
| 52 | } |
| 53 | |
| 54 | public EditPartFactory getEditPartFactory() |
| 55 | { |
| 56 | if (definedExtensionsList == null) |
| 57 | { |
| 58 | readXSDConfigurationRegistry(); |
| 59 | } |
| 60 | if (!definedExtensionsList.isEmpty()) |
| 61 | { |
| 62 | return ((XSDEditorExtensionProperties) definedExtensionsList.get(0)).getEditPartFactory(); |
| 63 | } |
| 64 | return null; |
| 65 | } |
| 66 | |
| 67 | public IExtendedFigureFactory getFigureFactory() |
| 68 | { |
| 69 | if (definedExtensionsList == null) |
| 70 | { |
| 71 | readXSDConfigurationRegistry(); |
| 72 | } |
| 73 | if (!definedExtensionsList.isEmpty()) |
| 74 | { |
| 75 | return ((XSDEditorExtensionProperties) definedExtensionsList.get(0)).getFigureFactory(); |
| 76 | } |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | public List getToolbarActions() |
| 81 | { |
| 82 | if (definedExtensionsList == null) |
| 83 | { |
| 84 | readXSDConfigurationRegistry(); |
| 85 | } |
| 86 | if (!definedExtensionsList.isEmpty()) |
| 87 | { |
| 88 | return ((XSDEditorExtensionProperties) definedExtensionsList.get(0)).getActionList(); |
| 89 | } |
| 90 | return Collections.EMPTY_LIST; |
| 91 | } |
| 92 | |
| 93 | protected Object loadClass(IConfigurationElement element, String classString) |
| 94 | { |
| 95 | String pluginId = element.getDeclaringExtension().getContributor().getName(); |
| 96 | |
| 97 | try |
| 98 | { |
| 99 | Class theClass = Platform.getBundle(pluginId).loadClass(classString); |
| 100 | Object instance = theClass.newInstance(); |
| 101 | |
| 102 | return instance; |
| 103 | } |
| 104 | catch (Exception e) |
| 105 | { |
| 106 | |
| 107 | } |
| 108 | return null; |
| 109 | } |
| 110 | |
| 111 | public void readXSDConfigurationRegistry() |
| 112 | { |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 113 | definedExtensionsList = new ArrayList(); |
kchong | cf01a7c | 2006-05-26 18:33:23 +0000 | [diff] [blame] | 114 | updateList(INTERNALEDITORCONFIGURATION_EXTENSIONID); |
| 115 | updateList(XSDEDITORCONFIGURATIONEXTENSIONID); |
| 116 | } |
| 117 | |
| 118 | private void updateList(String ID) |
| 119 | { |
| 120 | IConfigurationElement[] xsdEditorExtensionList = Platform.getExtensionRegistry().getConfigurationElementsFor(ID); |
| 121 | boolean definedExtensionsExist = (xsdEditorExtensionList != null && xsdEditorExtensionList.length > 0); |
| 122 | |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 123 | if (definedExtensionsExist) |
| 124 | { |
kchong | cf01a7c | 2006-05-26 18:33:23 +0000 | [diff] [blame] | 125 | |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 126 | for (int i = 0; i < xsdEditorExtensionList.length; i++) |
| 127 | { |
| 128 | XSDEditorExtensionProperties properties = new XSDEditorExtensionProperties(); |
| 129 | definedExtensionsList.add(properties); |
kchong | cf01a7c | 2006-05-26 18:33:23 +0000 | [diff] [blame] | 130 | |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 131 | IConfigurationElement element = xsdEditorExtensionList[i]; |
| 132 | String adapterFactoryClass = element.getAttribute(ADAPTERFACTORY); |
| 133 | if (adapterFactoryClass != null) |
| 134 | { |
| 135 | Object object = loadClass(element, adapterFactoryClass); |
| 136 | XSDAdapterFactory adapterFactory = null; |
| 137 | if (object instanceof XSDAdapterFactory) |
| 138 | { |
| 139 | adapterFactory = (XSDAdapterFactory) object; |
| 140 | properties.setAdapterFactory(adapterFactory); |
| 141 | } |
| 142 | } |
kchong | cf01a7c | 2006-05-26 18:33:23 +0000 | [diff] [blame] | 143 | |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 144 | String figureFactoryClass = element.getAttribute(FIGUREFACTORY); |
| 145 | if (figureFactoryClass != null) |
| 146 | { |
| 147 | Object object = loadClass(element, figureFactoryClass); |
| 148 | IExtendedFigureFactory figureFactory = null; |
| 149 | if (object instanceof IExtendedFigureFactory) |
| 150 | { |
| 151 | figureFactory = (IExtendedFigureFactory) object; |
| 152 | properties.setFigureFactoryList(figureFactory); |
| 153 | } |
| 154 | } |
kchong | cf01a7c | 2006-05-26 18:33:23 +0000 | [diff] [blame] | 155 | |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 156 | IConfigurationElement[] toolbarActions = element.getChildren(TOOLBARACTION); |
| 157 | List actionList = new ArrayList(); |
| 158 | if (toolbarActions != null) |
| 159 | { |
| 160 | for (int j = 0; j < toolbarActions.length; j++) |
| 161 | { |
| 162 | IConfigurationElement actionElement = toolbarActions[j]; |
| 163 | String actionClass = actionElement.getAttribute(CLASSNAME); |
| 164 | IXSDToolbarAction action = null; |
| 165 | if (actionClass != null) |
| 166 | { |
| 167 | Object object = loadClass(actionElement, actionClass); |
| 168 | if (object instanceof IXSDToolbarAction) |
| 169 | { |
| 170 | action = (IXSDToolbarAction) object; |
| 171 | actionList.add(action); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | properties.setActionList(actionList); |
kchong | cf01a7c | 2006-05-26 18:33:23 +0000 | [diff] [blame] | 177 | |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 178 | String editPartFactoryClass = element.getAttribute(EDITPARTFACTORY); |
| 179 | if (editPartFactoryClass != null) |
| 180 | { |
| 181 | Object object = loadClass(element, editPartFactoryClass); |
| 182 | EditPartFactory editPartFactory = null; |
| 183 | if (object instanceof EditPartFactory) |
| 184 | { |
| 185 | editPartFactory = (EditPartFactory) object; |
| 186 | properties.setEditPartFactoryList(editPartFactory); |
| 187 | } |
| 188 | } |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |