/
EmfParsleySwtBotTestsActivator.java
170 lines (143 loc) · 5.88 KB
/
EmfParsleySwtBotTestsActivator.java
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
/*******************************************************************************
* Copyright (c) 2013 RCP Vision (http://www.rcp-vision.com) 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:
* Lorenzo Bettini - Initial contribution and API
*******************************************************************************/
package org.eclipse.emf.parsley.tests.swtbot.activator;
import static com.google.inject.Guice.createInjector;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.parsley.EmfParsleyExtensionFactory;
import org.eclipse.emf.parsley.tests.swtbot.factories.CustomContentProviderLibraryExecutableExtensionFactory;
import org.eclipse.emf.parsley.tests.swtbot.factories.CustomContentProviderLibraryModule;
import org.eclipse.emf.parsley.tests.swtbot.factories.CustomElementsContentProviderLibraryExecutableExtensionFactory;
import org.eclipse.emf.parsley.tests.swtbot.factories.CustomElementsContentProviderLibraryModule;
import org.eclipse.emf.parsley.tests.swtbot.factories.CustomLibraryExecutableExtensionFactory;
import org.eclipse.emf.parsley.tests.swtbot.factories.CustomLibraryModule;
import org.eclipse.emf.parsley.tests.swtbot.factories.resourcelistening.ResourceListeningLibraryExecutableExtensionFactory;
import org.eclipse.emf.parsley.tests.swtbot.factories.resourcelistening.ResourceListeningLibraryModule;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import com.google.inject.Injector;
public class EmfParsleySwtBotTestsActivator extends AbstractUIPlugin {
// The shared instance
private static EmfParsleySwtBotTestsActivator plugin;
public static final String EMF_TREE_EDITOR = "org.eclipse.emf.parsley.editors.treeEditor";
public static final String EMF_TREE_EDITOR_NO_MOUSE_ID = "org.eclipse.emf.parsley.noMouseEvents";
public static final String EMF_TREE_EDITOR_OPEN_FORM_DIALOG_ID = "org.eclipse.emf.parsley.openFormDialogOnDoubleClick";
public static final String EMF_TREE_EDITOR_OPEN_DIALOG_ID = "org.eclipse.emf.parsley.openDialogOnDoubleClick";
public static final String EMF_TREE_EDITOR_OPEN_DIALOG_UNDOABLE_ID = "org.eclipse.emf.parsley.openDialogUndoable";
public static final String EMF_TREE_EDITOR_CUSTOM_LABEL = "org.eclipse.emf.parsley.customLabel";
public static final String EMF_TREE_EDITOR_FOR_XTEXT = "org.eclipse.emf.parsley.xtext";
public static final String EMF_EDITOR_FOR_LIBRARY = "org.eclipse.emf.parsley.customLibrary";
public static final String EMF_EDITOR_FOR_MENU_LIBRARY = "org.eclipse.emf.parsley.customMenuLibrary";
/**
* This will only simulate generated code by the wizard to make sure a
* singleton injector is used.
*/
private Map<String, Injector> injectorsMap = new HashMap<String, Injector>();
public EmfParsleySwtBotTestsActivator() {
}
/*
* (non-Javadoc)
*
* @see
* org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
* )
*/
@Override
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
}
/*
* (non-Javadoc)
*
* @see
* org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
@Override
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
}
/**
* Returns the shared instance
*
* @return the shared instance
*/
public static EmfParsleySwtBotTestsActivator getDefault() {
return plugin;
}
public Injector getInjector(
Class<? extends EmfParsleyExtensionFactory> cName) {
Injector injector = injectorsMap.get(cName.getCanonicalName());
if (injector == null) {
if (CustomLibraryExecutableExtensionFactory.class.equals(cName)) {
injector = createInjector(new CustomLibraryModule(this));
injectorsMap.put(cName.getCanonicalName(), injector);
} else if (CustomContentProviderLibraryExecutableExtensionFactory.class
.equals(cName)) {
injector = createInjector(new CustomContentProviderLibraryModule(
this));
injectorsMap.put(cName.getCanonicalName(), injector);
} else if (CustomElementsContentProviderLibraryExecutableExtensionFactory.class
.equals(cName)) {
injector = createInjector(new CustomElementsContentProviderLibraryModule(
this));
injectorsMap.put(cName.getCanonicalName(), injector);
} else if (ResourceListeningLibraryExecutableExtensionFactory.class
.equals(cName)) {
injector = createInjector(new ResourceListeningLibraryModule(
this));
injectorsMap.put(cName.getCanonicalName(), injector);
}
}
if (injector != null)
return injector;
throw new IllegalArgumentException(cName.getCanonicalName());
}
public static String localFileContents(String filename) throws IOException {
File f = localFile(filename);
if (f.exists()) {
return readFileAsString(f);
}
return f.getAbsolutePath();
}
public static File localFile(String filename) throws IOException {
IPath path = new Path("models/" + filename);
URL url = FileLocator.find(getDefault().getBundle(), path, null);
url = FileLocator.resolve(url);
url.toExternalForm();
File f = new File(url.getFile());
return f;
}
private static String readFileAsString(File file) throws IOException {
byte[] buffer = new byte[(int) file.length()];
BufferedInputStream f = null;
try {
f = new BufferedInputStream(new FileInputStream(file));
f.read(buffer);
} finally {
if (f != null)
try {
f.close();
} catch (IOException ignored) {
}
}
return new String(buffer);
}
}