blob: 89e5800fdd09f80c727814c63270ff7ef42d57e1 [file] [log] [blame]
kchong38cbf172006-03-29 03:38:21 +00001/*******************************************************************************
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 *******************************************************************************/
kchong2be71b32006-04-11 16:32:03 +000011package org.eclipse.wst.xsd.ui.internal.editor;
kchong38cbf172006-03-29 03:38:21 +000012
13import java.util.ArrayList;
14import java.util.Collections;
15import java.util.List;
16
17import org.eclipse.core.runtime.IConfigurationElement;
18import org.eclipse.core.runtime.Platform;
19import org.eclipse.gef.EditPartFactory;
kchong2be71b32006-04-11 16:32:03 +000020import org.eclipse.wst.xsd.ui.internal.actions.IXSDToolbarAction;
21import org.eclipse.wst.xsd.ui.internal.adapters.XSDAdapterFactory;
22import org.eclipse.wst.xsd.ui.internal.design.figures.IExtendedFigureFactory;
kchong38cbf172006-03-29 03:38:21 +000023
24public class XSDEditorConfiguration
25{
kchong618a1942006-04-13 07:14:23 +000026 public static final String XSDEDITORCONFIGURATIONEXTENSIONID = "org.eclipse.wst.xsd.ui.XSDEditorExtensionConfiguration"; //$NON-NLS-1$
kchongcf01a7c2006-05-26 18:33:23 +000027 public static final String INTERNALEDITORCONFIGURATION_EXTENSIONID = "org.eclipse.wst.xsd.ui.internalEditorConfiguration"; //$NON-NLS-1$
kchong618a1942006-04-13 07:14:23 +000028 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$
kchong38cbf172006-03-29 03:38:21 +000033
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 {
kchong38cbf172006-03-29 03:38:21 +0000113 definedExtensionsList = new ArrayList();
kchongcf01a7c2006-05-26 18:33:23 +0000114 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
kchong38cbf172006-03-29 03:38:21 +0000123 if (definedExtensionsExist)
124 {
kchongcf01a7c2006-05-26 18:33:23 +0000125
kchong38cbf172006-03-29 03:38:21 +0000126 for (int i = 0; i < xsdEditorExtensionList.length; i++)
127 {
128 XSDEditorExtensionProperties properties = new XSDEditorExtensionProperties();
129 definedExtensionsList.add(properties);
kchongcf01a7c2006-05-26 18:33:23 +0000130
kchong38cbf172006-03-29 03:38:21 +0000131 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 }
kchongcf01a7c2006-05-26 18:33:23 +0000143
kchong38cbf172006-03-29 03:38:21 +0000144 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 }
kchongcf01a7c2006-05-26 18:33:23 +0000155
kchong38cbf172006-03-29 03:38:21 +0000156 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);
kchongcf01a7c2006-05-26 18:33:23 +0000177
kchong38cbf172006-03-29 03:38:21 +0000178 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 }
kchong38cbf172006-03-29 03:38:21 +0000189 }
190 }
191 }
192}