Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c7cd1d7a23ca38c3c2581a9e3f81809dbff4c2b7 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*****************************************************************************
 * Copyright (c) 2009 Atos Origin.
 *
 *
 * 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:
 *  Alexia Allanic (Atos Origin) alexia.allanic@atosorigin.com - LayoutToolExtensionPointManager Implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.layout;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.InvalidRegistryObjectException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.papyrus.layout.utils.Constants;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;

/**
 * The Class LayoutToolExtensionPointManager.
 */
public class LayoutToolExtensionPointManager {

	/** The instance. */
	private static LayoutToolExtensionPointManager instance = new LayoutToolExtensionPointManager();

	/** The configuration elements layout tools. */
	private IConfigurationElement[] configurationElementsLayoutTools = Platform.getExtensionRegistry()
			.getConfigurationElementsFor(Constants.EXTENSION_POINT_ID);

	/**
	 * Instantiates a new layout tool extension point manager.
	 */
	private LayoutToolExtensionPointManager() {
	}

	/**
	 * Gets the single instance of LayoutToolExtensionPointManager.
	 *
	 * @return single instance of LayoutToolExtensionPointManager
	 */
	public static LayoutToolExtensionPointManager getInstance() {
		return instance;
	}

	/**
	 * Gets the layout area creator.
	 *
	 * @return layout area creator
	 */
	public LayoutToolAreaInterface getLayoutAreaCreator() {
		try {
			for (IConfigurationElement e : configurationElementsLayoutTools) {

				Class<?> o;
				LayouttoolInterface interfaceL = (LayouttoolInterface) Platform.getBundle(e.getContributor().getName())
						.loadClass(e.getAttribute(Constants.EXTENSION_INTERFACE)).newInstance();
				o = interfaceL.getEditorClass();
				IWorkbenchPart activeEditor = getActiveEditor();
				if (activeEditor != null) {
					if (o.isAssignableFrom(activeEditor.getClass())) {
						if (e.getAttribute(Constants.EXTENSION_INTERFACE_LAYOUT_AREA) != null) {
							return (LayoutToolAreaInterface) e
									.createExecutableExtension(Constants.EXTENSION_INTERFACE_LAYOUT_AREA);
						}
					}
				}
			}
		} catch (InvalidRegistryObjectException e1) {
			Activator.getDefault().log(e1.getMessage(), e1);
		} catch (CoreException e) {
			Activator.getDefault().log(e.getMessage(), e);
		} catch (InstantiationException e) {
			Activator.getDefault().log(e.getMessage(), e);
		} catch (IllegalAccessException e) {
			Activator.getDefault().log(e.getMessage(), e);
		} catch (ClassNotFoundException e) {
			Activator.getDefault().log(e.getMessage(), e);
		}
		return null;
	}

	/**
	 * Gets the sub editor.
	 *
	 * @param part
	 *            the part
	 *
	 * @return subEditor class
	 */
	public LayouttoolInterface getSubEditor(IWorkbenchPart part) {
		try {
			for (IConfigurationElement e : configurationElementsLayoutTools) {
				Class<?> o;
				LayouttoolInterface interfaceL = (LayouttoolInterface) Platform.getBundle(e.getContributor().getName())
						.loadClass(e.getAttribute(Constants.EXTENSION_INTERFACE)).newInstance();
				o = interfaceL.getEditorClass();
				IWorkbenchPart activeEditor = null;
				if (part != null) {
					activeEditor = part;
				} else {
					activeEditor = getActiveEditor();
				}
				if (activeEditor != null) {
					if (o.isAssignableFrom(activeEditor.getClass())) {
						return (LayouttoolInterface) e.createExecutableExtension(Constants.EXTENSION_INTERFACE);
					}
				}
			}
		} catch (InvalidRegistryObjectException e1) {
			e1.printStackTrace();
		} catch (CoreException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Gets the sub editor.
	 *
	 * @return subEditor class
	 */
	public LayouttoolInterface getSubEditor() {
		return getSubEditor(null);
	}

	/**
	 * Gets the active editor.
	 *
	 * @return active editor
	 */
	public IEditorPart getActiveEditor() {
		IEditorPart editor = null;
		if (PlatformUI.getWorkbench() != null) {
			if (PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null) {
				if (PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() != null) {
					if (PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor() != null) {
						editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
					}
				}
			}
		}
		return editor;
	}
}

Back to the top