Skip to main content
summaryrefslogtreecommitdiffstats
blob: 822dfa59bddecbfc75ae7cbef027c25dfb6a47bc (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * <copyright>
 *
 * Copyright (c) 2005-2006 Sven Efftinge (http://www.efftinge.de) and others.
 * 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:
 *     Sven Efftinge (http://www.efftinge.de) - Initial API and implementation
 *
 * </copyright>
 */
package org.eclipse.xtend.shared.ui;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
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.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.internal.xtend.xtend.XtendFile;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.xtend.expression.ExecutionContext;
import org.eclipse.xtend.expression.ExecutionContextImpl;
import org.eclipse.xtend.expression.TypeSystemImpl;
import org.eclipse.xtend.shared.ui.core.IModelManager;
import org.eclipse.xtend.shared.ui.core.IXtendXpandProject;
import org.eclipse.xtend.shared.ui.core.builder.XtendXpandNature;
import org.eclipse.xtend.shared.ui.core.internal.XtendXpandModelManager;
import org.eclipse.xtend.shared.ui.core.metamodel.Contributor;
import org.eclipse.xtend.shared.ui.core.metamodel.MetamodelContributorRegistry;
import org.eclipse.xtend.shared.ui.expression.PluginExecutionContextImpl;
import org.eclipse.xtend.shared.ui.internal.XtendLog;
import org.eclipse.xtend.typesystem.MetaModel;
import org.osgi.framework.BundleContext;

/**
 * The main plugin class
 */
public class Activator extends AbstractUIPlugin {

	private static final String RESOURCE_CONTRIBUTOR_ID = "org.eclipse.xtend.shared.ui.resourceContributor";
	
	public static final String PLUGIN_ID = "org.eclipse.xtend.shared.ui"; 	

	public static final String getNatureId() {
		return XtendXpandNature.NATURE_ID;
	}

	// The shared instance.
	private static Activator plugin;

	private static HashMap<String, ResourceContributor> contributors;

	private IModelManager modelManager;

	/**
	 * The constructor.
	 */
	public Activator() {
		plugin = this;
		modelManager = new XtendXpandModelManager();
	}

	private final IPropertyChangeListener listener = new IPropertyChangeListener() {

		public void propertyChange(PropertyChangeEvent event) {
			Job job = new Job("Analyzing workspace...") {

				@Override
				protected IStatus run(IProgressMonitor monitor) {
					try {
						new WorkspaceModifyOperation() {
							@Override
							protected void execute(IProgressMonitor monitor) throws CoreException, java.lang.reflect.InvocationTargetException,
									InterruptedException {
								Activator.getExtXptModelManager().analyze(monitor);
							}
						}.run(monitor);
					} catch (InvocationTargetException e) {
						XtendLog.logError(e);
					} catch (InterruptedException e) {
						XtendLog.logError(e);
					}
					return Status.OK_STATUS;
				}

			};
			job.schedule();
		}
	};

	/**
	 * This method is called upon plug-in activation
	 */
	@Override
	public void start(final BundleContext context) throws Exception {
		super.start(context);
		getPreferenceStore().addPropertyChangeListener(listener);

		// initialize mm contributors
		Map<String, Contributor> registeredMetamodelContributors = MetamodelContributorRegistry.getRegisteredMetamodelContributors();
		for (Contributor c  : registeredMetamodelContributors.values()) {
			c.getMetaModelContributor();
		}
	}

	/**
	 * This method is called when the plug-in is stopped
	 */
	@Override
	public void stop(final BundleContext context) throws Exception {
		super.stop(context);
		getPreferenceStore().removePropertyChangeListener(listener);
		plugin = null;
	}

	/**
	 * Returns the shared instance.
	 */
	public static Activator getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given plug-in
	 * relative path.
	 * 
	 * @param path
	 *            the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(final String path) {
		return AbstractUIPlugin.imageDescriptorFromPlugin(getId(), path);
	}

	/**
	 * Returns the standard display to be used. The method first checks, if the
	 * thread calling this method has an associated display. If so, this display
	 * is returned. Otherwise the method returns the default display.
	 */
	public static Display getStandardDisplay() {
		Display display = Display.getCurrent();
		if (display == null) {
			display = Display.getDefault();
		}
		return display;
	}

	public static String getId() {
		return getDefault().getBundle().getSymbolicName();
	}

	public static ResourceContributor[] getRegisteredResourceContributors() {
		final IExtensionRegistry registry = Platform.getExtensionRegistry();
		final IExtensionPoint point = registry.getExtensionPoint(Activator.RESOURCE_CONTRIBUTOR_ID);
		final Set<ResourceContributor> contrs = new HashSet<ResourceContributor>();
		if (point != null) {
			final IExtension[] extensions = point.getExtensions();
			for (int i = 0; i < extensions.length; i++) {
				final IExtension extension = extensions[i];
				final IConfigurationElement[] configs = extension.getConfigurationElements();
				for (int j = 0; j < configs.length; j++) {
					final IConfigurationElement element = configs[j];
					try {
						contrs.add((ResourceContributor) element.createExecutableExtension("class"));
					} catch (final CoreException e) {
						XtendLog.logError(e);
					}
				}
			}
		}
		return contrs.toArray(new ResourceContributor[contrs.size()]);
	}

	public static IModelManager getExtXptModelManager() {
		return getDefault().modelManager;
	}

	public static ExecutionContext getExecutionContext(final IJavaProject project) {
		final IXtendXpandProject xp = Activator.getExtXptModelManager().findProject(project.getPath());
		final ExecutionContextImpl ctx = new PluginExecutionContextImpl(xp, new TypeSystemImpl());

		final List<? extends MetamodelContributor> contr = MetamodelContributorRegistry.getActiveMetamodelContributors(project);
		for (MetamodelContributor contributor : contr) {
			final MetaModel[] metamodels = contributor.getMetamodels(project, ctx);
			for (int i = 0; i < metamodels.length; i++) {
				ctx.registerMetaModel(metamodels[i]);
			}
		}
		return ctx;
	}

	public final static boolean isXtendFile(final Object object) {
		if (object instanceof IFile)
			return XtendFile.FILE_EXTENSION.equals(((IFile) object).getFileExtension());
		return false;
	}

	public static ResourceContributor getRegisteredResourceContributorFor(String fileExtension) {
		if (contributors == null) {
			contributors = new HashMap<String, ResourceContributor>();
			for (ResourceContributor contr : getRegisteredResourceContributors()) {
				contributors.put(contr.getFileExtension(), contr);
			}
		}

		return contributors.get(fileExtension);
	}

	public static void logError(String string, Exception e) {
		getDefault().getLog().log(new Status(IStatus.ERROR,PLUGIN_ID, string,e));
		
	}
}

Back to the top