Skip to main content
summaryrefslogtreecommitdiffstats
blob: 426a2c7609a482b136a7f9816b0d1a3e28a53f8c (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
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 *    
 * 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.nattable.manager.axis;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.papyrus.infra.nattable.Activator;
import org.eclipse.papyrus.infra.nattable.messages.Messages;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxisconfiguration.AxisManagerRepresentation;

/**
 * The axis manager factory
 * 
 * @author Vincent Lorenzo
 * 
 */
public class AxisManagerFactory {

	private static final String CLASS_MANAGER = "manager"; //$NON-NLS-1$

	private static final String CLASS_ID = "id"; //$NON-NLS-1$

	private final Map<String, Class<IAxisManager>> map;

	private static final String EXTENSION_ID = "org.eclipse.papyrus.infra.nattable.axismanager"; //$NON-NLS-1$

	public static final AxisManagerFactory INSTANCE = new AxisManagerFactory();

	private AxisManagerFactory() {
		this.map = new HashMap<String, Class<IAxisManager>>();

		final IConfigurationElement[] configElements = Platform.getExtensionRegistry().getConfigurationElementsFor(EXTENSION_ID);

		for(final IConfigurationElement iConfigurationElement : configElements) {
			final String id = iConfigurationElement.getAttribute(CLASS_ID);
			try {
				//to avoid problem when the provided class in not this plugin!
				IAxisManager axisManager = (IAxisManager)iConfigurationElement.createExecutableExtension(CLASS_MANAGER);
				@SuppressWarnings("unchecked")
				final Class<IAxisManager> myClass = (Class<IAxisManager>)axisManager.getClass();
				this.map.put(id, myClass);
			} catch (final CoreException e) {
				Activator.log.error(String.format(Messages.AxisManagerFactory_AxisManagerClassCantBeLoaded, id), e);
			}
		}
	}

	/**
	 * 
	 * @param axisManagerRepresentation
	 * @return
	 *         the axis manager for this axisManagerRepresentation. The class calling this method must initialize itself the IAxisManager with its
	 *         method #init
	 */
	public IAxisManager getAxisManager(final AxisManagerRepresentation axisManagerRepresentation) {
		final Class<IAxisManager> managerClass = this.map.get(axisManagerRepresentation.getAxisManagerId());
		IAxisManager axisManager = null;
		if(managerClass != null) {
			try {
				axisManager = managerClass.newInstance();
			} catch (final InstantiationException e) {
				Activator.log.error(Messages.AxisManagerFactory_TheClassCantBeInstanciated, e);
			} catch (final IllegalAccessException e) {
				Activator.log.error(e);
			}
		}
		return axisManager;
	}
}

Back to the top