Package: AcceleoApplicationLaunchShortcut

AcceleoApplicationLaunchShortcut

nameinstructionbranchcomplexitylinemethod
AcceleoApplicationLaunchShortcut()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createConfiguration(IType)
M: 81 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 25 C: 0
0%
M: 1 C: 0
0%
findJavaFileForMTLFile(IFile)
M: 96 C: 0
0%
M: 20 C: 0
0%
M: 11 C: 0
0%
M: 21 C: 0
0%
M: 1 C: 0
0%
findTypes(Object[], IRunnableContext)
M: 48 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
getConfigurationType()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getEditorEmptyMessage()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getJavaElements(Object[])
M: 65 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
getLaunchManager()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getSelectionEmptyMessage()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getTypeSelectionTitle()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isMainMethod(Object)
M: 34 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
launch(IEditorPart, String)
M: 36 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2012 Obeo.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * Obeo - initial API and implementation
10: *******************************************************************************/
11: package org.eclipse.acceleo.internal.ide.ui.launching;
12:
13: import java.lang.reflect.InvocationTargetException;
14: import java.util.ArrayList;
15: import java.util.List;
16:
17: import org.eclipse.acceleo.common.IAcceleoConstants;
18: import org.eclipse.acceleo.ide.ui.AcceleoUIActivator;
19: import org.eclipse.acceleo.internal.ide.ui.AcceleoUIMessages;
20: import org.eclipse.core.resources.IFile;
21: import org.eclipse.core.resources.IResource;
22: import org.eclipse.core.runtime.CoreException;
23: import org.eclipse.core.runtime.IAdaptable;
24: import org.eclipse.core.runtime.Path;
25: import org.eclipse.debug.core.DebugPlugin;
26: import org.eclipse.debug.core.ILaunchConfiguration;
27: import org.eclipse.debug.core.ILaunchConfigurationType;
28: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
29: import org.eclipse.debug.core.ILaunchManager;
30: import org.eclipse.debug.ui.DebugUITools;
31: import org.eclipse.debug.ui.IDebugUIConstants;
32: import org.eclipse.jdt.core.IJavaElement;
33: import org.eclipse.jdt.core.IMember;
34: import org.eclipse.jdt.core.IMethod;
35: import org.eclipse.jdt.core.IType;
36: import org.eclipse.jdt.core.JavaModelException;
37: import org.eclipse.jdt.core.search.IJavaSearchScope;
38: import org.eclipse.jdt.core.search.SearchEngine;
39: import org.eclipse.jdt.debug.ui.launchConfigurations.JavaLaunchShortcut;
40: import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
41: import org.eclipse.jface.dialogs.MessageDialog;
42: import org.eclipse.jface.operation.IRunnableContext;
43: import org.eclipse.jface.viewers.IStructuredSelection;
44: import org.eclipse.jface.viewers.StructuredSelection;
45: import org.eclipse.ui.IEditorInput;
46: import org.eclipse.ui.IEditorPart;
47: import org.eclipse.ui.IWorkbenchWindow;
48: import org.eclipse.ui.PlatformUI;
49:
50: /**
51: * Launch shortcut for local Java applications.
52: * <p>
53: * This class may be instantiated or subclassed.
54: * </p>
55: *
56: * @author <a href="mailto:jonathan.musset@obeo.fr">Jonathan Musset</a>
57: */
58: public class AcceleoApplicationLaunchShortcut extends JavaLaunchShortcut {
59:         /**
60:          * {@inheritDoc}
61:          * <p>
62:          * This specific override allows us to find the Java element corresponding to a given mtl file when
63:          * launched from right click -> run as -> launch acceleo application from within the template editor.
64:          * </p>
65:          *
66:          * @see org.eclipse.jdt.debug.ui.launchConfigurations.JavaLaunchShortcut#launch(org.eclipse.ui.IEditorPart,
67:          * java.lang.String)
68:          */
69:         @Override
70:         public void launch(IEditorPart editor, String mode) {
71:                 IEditorInput input = editor.getEditorInput();
72:                 IJavaElement je = (IJavaElement)input.getAdapter(IJavaElement.class);
73:•                if (je != null) {
74:                         super.launch(editor, mode);
75:                 } else {
76:                         // Acceleo specific
77:                         IFile mtlFile = (IFile)input.getAdapter(IFile.class);
78:                         IFile javaFile = findJavaFileForMTLFile(mtlFile);
79:•                        if (javaFile != null) {
80:                                 IStructuredSelection selection = new StructuredSelection(javaFile);
81:                                 launch(selection, mode);
82:                         }
83:                 }
84:         }
85:
86:         /**
87:          * Returns the Java elements corresponding to the given objects.
88:          *
89:          * @param objects
90:          * selected objects
91:          * @return corresponding Java elements
92:          */
93:         private IJavaElement[] getJavaElements(Object[] objects) {
94:                 List<IJavaElement> list = new ArrayList<IJavaElement>(objects.length);
95:•                for (int i = 0; i < objects.length; i++) {
96:                         Object object = objects[i];
97:•                        if (object instanceof IFile
98:•                                        && IAcceleoConstants.MTL_FILE_EXTENSION.equals(((IFile)object).getFileExtension())) {
99:                                 object = findJavaFileForMTLFile((IFile)object);
100:                         }
101:•                        if (object instanceof IAdaptable) {
102:                                 IJavaElement element = (IJavaElement)((IAdaptable)object).getAdapter(IJavaElement.class);
103:•                                if (element != null) {
104:•                                        if (element instanceof IMember && ((IMember)element).getDeclaringType() != null) {
105:                                                 element = ((IMember)element).getDeclaringType();
106:                                         }
107:                                         list.add(element);
108:                                 }
109:                         }
110:                 }
111:                 return list.toArray(new IJavaElement[list.size()]);
112:         }
113:
114:         /**
115:          * This will try and find the associated java file for an mtl file with a main annotation.
116:          *
117:          * @param mtlFile
118:          * The module file we need the generated java class for.
119:          * @return The associated java launcher.
120:          */
121:         private IFile findJavaFileForMTLFile(IFile mtlFile) {
122:•                if (!IAcceleoConstants.MTL_FILE_EXTENSION.equals(mtlFile.getFileExtension())) {
123:                         throw new IllegalArgumentException();
124:                 }
125:                 try {
126:                         String name1 = new Path(mtlFile.getName()).removeFileExtension().lastSegment().toLowerCase();
127:                         IResource[] members = mtlFile.getParent().members();
128:                         IFile javaFile = null;
129:•                        for (int j = 0; j < members.length && javaFile == null; j++) {
130:                                 IResource resource = members[j];
131:•                                if (resource != mtlFile && resource instanceof IFile
132:•                                                && "java".equals(((IFile)resource).getFileExtension())) { //$NON-NLS-1$
133:                                         String name2 = new Path(((IFile)resource).getName()).removeFileExtension().lastSegment()
134:                                                         .toLowerCase();
135:•                                        if (name1.equals(name2)) {
136:                                                 javaFile = (IFile)resource;
137:                                         }
138:                                 }
139:                         }
140:•                        if (javaFile == null) {
141:                                 javaFile = mtlFile.getParent().getFile(
142:                                                 new Path(mtlFile.getName()).removeFileExtension().addFileExtension("java")); //$NON-NLS-1$
143:                         }
144:•                        if (javaFile != null && javaFile.exists()) {
145:                                 return javaFile;
146:                         }
147:                 } catch (CoreException e) {
148:                         AcceleoUIActivator.getDefault().getLog().log(e.getStatus());
149:                 }
150:                 return null;
151:         }
152:
153:         /**
154:          * {@inheritDoc}
155:          *
156:          * @see org.eclipse.jdt.debug.ui.launchConfigurations.JavaLaunchShortcut#createConfiguration(org.eclipse.jdt.core.IType)
157:          */
158:         @Override
159:         protected ILaunchConfiguration createConfiguration(IType type) {
160:                 ILaunchConfiguration config = null;
161:                 ILaunchConfigurationWorkingCopy wc = null;
162:                 try {
163:                         ILaunchConfigurationType configType = getConfigurationType();
164:                         wc = configType.newInstance(null, getLaunchManager().generateUniqueLaunchConfigurationNameFrom(
165:                                         type.getElementName()));
166:                         wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, type
167:                                         .getFullyQualifiedName());
168:                         wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, type.getJavaProject()
169:                                         .getElementName());
170:                         wc.setMappedResources(new IResource[] {type.getUnderlyingResource() });
171:                         config = wc.doSave();
172:                         IStructuredSelection selection;
173:•                        if (config == null) {
174:                                 selection = new StructuredSelection();
175:                         } else {
176:                                 selection = new StructuredSelection(config);
177:                         }
178:                         DebugUITools.openLaunchConfigurationDialogOnGroup(PlatformUI.getWorkbench()
179:                                         .getActiveWorkbenchWindow().getShell(), selection, IDebugUIConstants.ID_RUN_LAUNCH_GROUP);
180:                 } catch (CoreException e) {
181:                         IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
182:•                        if (window != null) {
183:                                 MessageDialog.openError(window.getShell(), AcceleoUIMessages
184:                                                 .getString("AcceleoApplicationLaunchShortcut.Error"), e.getStatus().getMessage()); //$NON-NLS-1$
185:                         } else {
186:                                 AcceleoUIActivator.getDefault().getLog().log(e.getStatus());
187:                         }
188:                 }
189:                 return config;
190:         }
191:
192:         /**
193:          * {@inheritDoc}
194:          *
195:          * @see org.eclipse.jdt.debug.ui.launchConfigurations.JavaLaunchShortcut#getConfigurationType()
196:          */
197:         @Override
198:         protected ILaunchConfigurationType getConfigurationType() {
199:                 return getLaunchManager()
200:                                 .getLaunchConfigurationType(
201:                                                 org.eclipse.acceleo.internal.ide.ui.launching.IAcceleoLaunchConfigurationConstants.ID_ACCELEO_APPLICATION);
202:         }
203:
204:         /**
205:          * Returns the singleton launch manager.
206:          *
207:          * @return launch manager
208:          */
209:         private ILaunchManager getLaunchManager() {
210:                 return DebugPlugin.getDefault().getLaunchManager();
211:         }
212:
213:         /**
214:          * {@inheritDoc}
215:          *
216:          * @see org.eclipse.jdt.debug.ui.launchConfigurations.JavaLaunchShortcut#findTypes(java.lang.Object[],
217:          * org.eclipse.jface.operation.IRunnableContext)
218:          */
219:         @Override
220:         protected IType[] findTypes(Object[] elements, IRunnableContext context) throws InterruptedException,
221:                         CoreException {
222:                 try {
223:•                        if (elements.length == 1) {
224:                                 IType type = isMainMethod(elements[0]);
225:•                                if (type != null) {
226:                                         return new IType[] {type };
227:                                 }
228:                         }
229:                         IJavaElement[] javaElements = getJavaElements(elements);
230:                         AcceleoMainMethodSearchEngine engine = new AcceleoMainMethodSearchEngine();
231:                         int constraints = IJavaSearchScope.SOURCES;
232:                         constraints |= IJavaSearchScope.APPLICATION_LIBRARIES;
233:                         IJavaSearchScope scope = SearchEngine.createJavaSearchScope(javaElements, constraints);
234:                         return engine.searchMainMethods(context, scope, true);
235:                 } catch (InvocationTargetException e) {
236:                         throw (CoreException)e.getTargetException();
237:                 }
238:         }
239:
240:         /**
241:          * Returns the smallest enclosing <code>IType</code> if the specified object is a main method, or
242:          * <code>null</code>.
243:          *
244:          * @param o
245:          * the object to inspect
246:          * @return the smallest enclosing <code>IType</code> of the specified object if it is a main method or
247:          * <code>null</code> if it is not
248:          */
249:         private IType isMainMethod(Object o) {
250:•                if (o instanceof IAdaptable) {
251:                         IAdaptable adapt = (IAdaptable)o;
252:                         IJavaElement element = (IJavaElement)adapt.getAdapter(IJavaElement.class);
253:•                        if (element != null && element.getElementType() == IJavaElement.METHOD) {
254:                                 try {
255:                                         IMethod method = (IMethod)element;
256:•                                        if (method.isMainMethod()) {
257:                                                 return method.getDeclaringType();
258:                                         }
259:                                 } catch (JavaModelException e) {
260:                                         AcceleoUIActivator.getDefault().getLog().log(e.getStatus());
261:                                 }
262:                         }
263:                 }
264:                 return null;
265:         }
266:
267:         /**
268:          * {@inheritDoc}
269:          *
270:          * @see org.eclipse.jdt.debug.ui.launchConfigurations.JavaLaunchShortcut#getTypeSelectionTitle()
271:          */
272:         @Override
273:         protected String getTypeSelectionTitle() {
274:                 return AcceleoUIMessages.getString("AcceleoApplicationLaunchShortcut.getTypeSelectionTitle"); //$NON-NLS-1$
275:         }
276:
277:         /**
278:          * {@inheritDoc}
279:          *
280:          * @see org.eclipse.jdt.debug.ui.launchConfigurations.JavaLaunchShortcut#getEditorEmptyMessage()
281:          */
282:         @Override
283:         protected String getEditorEmptyMessage() {
284:                 return AcceleoUIMessages.getString("AcceleoApplicationLaunchShortcut.getEditorEmptyMessage"); //$NON-NLS-1$
285:         }
286:
287:         /**
288:          * {@inheritDoc}
289:          *
290:          * @see org.eclipse.jdt.debug.ui.launchConfigurations.JavaLaunchShortcut#getSelectionEmptyMessage()
291:          */
292:         @Override
293:         protected String getSelectionEmptyMessage() {
294:                 return AcceleoUIMessages.getString("AcceleoApplicationLaunchShortcut.getSelectionEmptyMessage"); //$NON-NLS-1$
295:         }
296: }