Skip to main content
summaryrefslogtreecommitdiffstats
blob: f2b57dae3943488ea5a88f2a99ac928d15f09337 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*******************************************************************************
 *  Copyright (c) 2006, 2010 Oracle. All rights reserved. This
 *  program and the accompanying materials are made available under the terms of
 *  the Eclipse Public License v1.0 which accompanies this distribution, and is
 *  available at http://www.eclipse.org/legal/epl-v10.html
 *  
 *  Contributors: Oracle. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jpt.jaxb.ui.internal.platform;

import static org.eclipse.jpt.core.internal.XPointUtil.*;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jpt.core.internal.XPointUtil.XPointException;
import org.eclipse.jpt.jaxb.core.JptJaxbCorePlugin;
import org.eclipse.jpt.jaxb.core.platform.JaxbPlatformDescription;
import org.eclipse.jpt.jaxb.ui.JptJaxbUiPlugin;
import org.eclipse.jpt.jaxb.ui.platform.JaxbPlatformUi;
import org.eclipse.jpt.jaxb.ui.platform.JaxbPlatformUiManager;
import org.eclipse.jpt.utility.internal.KeyedSet;

public class JaxbPlatformUiManagerImpl
		implements JaxbPlatformUiManager {
	
	static final String EXTENSION_POINT_ID = "jaxbPlatformUis"; //$NON-NLS-1$
	static final String QUALIFIED_EXTENSION_POINT_ID = JptJaxbCorePlugin.PLUGIN_ID_ + EXTENSION_POINT_ID; //$NON-NLS-1$
	static final String PLATFORM_UI_ELEMENT = "jaxbPlatformUi"; //$NON-NLS-1$
	static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$	
	static final String PLATFORM_ATTRIBUTE = "jaxbPlatform"; //$NON-NLS-1$	
	static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$	
	
	
	private static final JaxbPlatformUiManagerImpl INSTANCE = new JaxbPlatformUiManagerImpl();
	
	
	public static JaxbPlatformUiManagerImpl instance() {
		return INSTANCE;
	}
	
	
	private KeyedSet<String, JaxbPlatformUiConfig> platformUiConfigs;
	private KeyedSet<JaxbPlatformDescription, JaxbPlatformUiConfig> platformToUiConfigs;
	
	
	// ********** constructor/initialization **********
	
	private JaxbPlatformUiManagerImpl() {
		super();
		this.platformUiConfigs = new KeyedSet<String, JaxbPlatformUiConfig>();
		this.platformToUiConfigs = new KeyedSet<JaxbPlatformDescription, JaxbPlatformUiConfig>();
		readExtensions();
	}
	
	
	private void readExtensions() {
		final IExtensionRegistry registry = Platform.getExtensionRegistry();
		
		final IExtensionPoint xpoint 
				= registry.getExtensionPoint(JptJaxbUiPlugin.PLUGIN_ID, EXTENSION_POINT_ID);
		
		if (xpoint == null) {
			throw new IllegalStateException();
		}
		
		List<IConfigurationElement> platformUiConfigs = new ArrayList<IConfigurationElement>();
		
		for (IExtension extension : xpoint.getExtensions()) {
			for (IConfigurationElement configElement : extension.getConfigurationElements()) {
				if (configElement.getName().equals(PLATFORM_UI_ELEMENT)) {
					platformUiConfigs.add(configElement);
				}
			}
		}
		
		for (IConfigurationElement configElement: platformUiConfigs) {
			readPlatformUiExtension(configElement);
		}
	}
	
	private void readPlatformUiExtension(IConfigurationElement element) {
		try {
			JaxbPlatformUiConfig platformUiConfig = new JaxbPlatformUiConfig();
			
			// plug-in id
			platformUiConfig.setPluginId(element.getContributor().getName());
			
			// id
			platformUiConfig.setId(findRequiredAttribute(element, ID_ATTRIBUTE));
			
			if (this.platformUiConfigs.containsKey(platformUiConfig.getId())) {
				logDuplicateExtension(QUALIFIED_EXTENSION_POINT_ID, ID_ATTRIBUTE, platformUiConfig.getId());
				throw new XPointException();
			}
			
			// jaxb platform id
			String jaxbPlatformId = findRequiredAttribute(element, PLATFORM_ATTRIBUTE);
			JaxbPlatformDescription jaxbPlatform = 
					JptJaxbCorePlugin.getJaxbPlatformManager().getJaxbPlatform(jaxbPlatformId);
			
			if (jaxbPlatform == null) {
				logInvalidValue(element, PLATFORM_ATTRIBUTE, jaxbPlatformId);
			}
			
			if (this.platformToUiConfigs.containsKey(jaxbPlatform)) {
				logDuplicateExtension(QUALIFIED_EXTENSION_POINT_ID, PLATFORM_ATTRIBUTE, jaxbPlatformId);
				throw new XPointException();
			}
			
			platformUiConfig.setJaxbPlatform(jaxbPlatform);
			
			// class
			platformUiConfig.setClassName(findRequiredAttribute(element, CLASS_ATTRIBUTE));
			
			this.platformUiConfigs.addItem(platformUiConfig.getId(), platformUiConfig);
			this.platformToUiConfigs.addItem(jaxbPlatform, platformUiConfig);
		}
		catch (XPointException e) {
			// Ignore and continue. The problem has already been reported to the user
			// in the log.
		}
	}
	
	public JaxbPlatformUi getJaxbPlatformUi(JaxbPlatformDescription platformDesc) {
		JaxbPlatformUiConfig config = this.platformToUiConfigs.getItem(platformDesc);
		return (config == null) ? null : config.getPlatformUi();
	}
}

Back to the top