KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > launcher > JUnitLaunchConfigurationTab


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.junit.launcher;
12
13  
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.resources.IResource;
16
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.widgets.Button;
19
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22
23 import org.eclipse.ui.IEditorInput;
24 import org.eclipse.ui.IEditorPart;
25 import org.eclipse.ui.IWorkbenchPage;
26 import org.eclipse.ui.IWorkbenchWindow;
27 import org.eclipse.ui.PlatformUI;
28
29 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
30
31 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
32
33 import org.eclipse.jdt.core.IJavaElement;
34 import org.eclipse.jdt.core.IJavaProject;
35 import org.eclipse.jdt.core.JavaCore;
36
37 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
38
39 import org.eclipse.jdt.internal.junit.util.LayoutUtil;
40
41 /**
42  * Common function for Java launch configuration tabs.
43  */

44 public abstract class JUnitLaunchConfigurationTab extends AbstractLaunchConfigurationTab {
45         
46     /**
47      * Returns the current Java element context from which to initialize
48      * default settings, or <code>null</code> if none.
49      *
50      * @return Java element context.
51      */

52     protected IJavaElement getContext() {
53         IWorkbenchWindow activeWorkbenchWindow= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
54         if (activeWorkbenchWindow == null) {
55             return null;
56         }
57         IWorkbenchPage page = activeWorkbenchWindow.getActivePage();
58         if (page != null) {
59             ISelection selection = page.getSelection();
60             if (selection instanceof IStructuredSelection) {
61                 IStructuredSelection ss = (IStructuredSelection)selection;
62                 if (!ss.isEmpty()) {
63                     Object JavaDoc obj = ss.getFirstElement();
64                     if (obj instanceof IJavaElement) {
65                         return (IJavaElement)obj;
66                     }
67                     if (obj instanceof IResource) {
68                         IJavaElement je = JavaCore.create((IResource)obj);
69                         if (je == null) {
70                             IProject pro = ((IResource)obj).getProject();
71                             je = JavaCore.create(pro);
72                         }
73                         if (je != null) {
74                             return je;
75                         }
76                     }
77                 }
78             }
79             IEditorPart part = page.getActiveEditor();
80             if (part != null) {
81                 IEditorInput input = part.getEditorInput();
82                 return (IJavaElement) input.getAdapter(IJavaElement.class);
83             }
84         }
85         return null;
86     }
87     
88     /**
89      * Set the java project attribute based on the IJavaElement.
90      * @param javaElement
91      * @param config
92      */

93     protected void initializeJavaProject(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) {
94         IJavaProject javaProject = javaElement.getJavaProject();
95         String JavaDoc name = null;
96         if (javaProject != null && javaProject.exists()) {
97             name = javaProject.getElementName();
98         }
99         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, name);
100     }
101
102     protected void setButtonGridData(Button button) {
103         GridData gridData= new GridData();
104         button.setLayoutData(gridData);
105         LayoutUtil.setButtonDimensionHint(button);
106     }
107     
108 }
109
Popular Tags