KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > debug > ui > launchConfigurations > JavaLaunchTab


1 /*******************************************************************************
2  * Copyright (c) 2006 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.debug.ui.launchConfigurations;
12
13  
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.debug.core.ILaunchConfiguration;
17 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
18 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
19 import org.eclipse.jdt.core.IJavaElement;
20 import org.eclipse.jdt.core.IJavaProject;
21 import org.eclipse.jdt.core.JavaCore;
22 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
23 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.ui.IEditorInput;
27 import org.eclipse.ui.IEditorPart;
28 import org.eclipse.ui.IWorkbenchPage;
29
30 /**
31  * Common function for Java launch configuration tabs.
32  * <p>
33  * Clients may subclass this class.
34  * </p>
35  * @since 3.2
36  */

37 public abstract class JavaLaunchTab extends AbstractLaunchConfigurationTab {
38         
39     /**
40      * Config being modified
41      */

42     private ILaunchConfiguration fLaunchConfig;
43     
44     /**
45      * Returns the current Java element context in the active workbench page
46      * or <code>null</code> if none.
47      *
48      * @return current Java element in the active page or <code>null</code>
49      */

50     protected IJavaElement getContext() {
51         IWorkbenchPage page = JDIDebugUIPlugin.getActivePage();
52         if (page != null) {
53             ISelection selection = page.getSelection();
54             if (selection instanceof IStructuredSelection) {
55                 IStructuredSelection ss = (IStructuredSelection)selection;
56                 if (!ss.isEmpty()) {
57                     Object JavaDoc obj = ss.getFirstElement();
58                     if (obj instanceof IJavaElement) {
59                         return (IJavaElement)obj;
60                     }
61                     if (obj instanceof IResource) {
62                         IJavaElement je = JavaCore.create((IResource)obj);
63                         if (je == null) {
64                             IProject pro = ((IResource)obj).getProject();
65                             je = JavaCore.create(pro);
66                         }
67                         if (je != null) {
68                             return je;
69                         }
70                     }
71                 }
72             }
73             IEditorPart part = page.getActiveEditor();
74             if (part != null) {
75                 IEditorInput input = part.getEditorInput();
76                 return (IJavaElement) input.getAdapter(IJavaElement.class);
77             }
78         }
79         return null;
80     }
81     
82     /**
83      * Returns the launch configuration this tab was initialized from.
84      *
85      * @return launch configuration this tab was initialized from
86      */

87     protected ILaunchConfiguration getCurrentLaunchConfiguration() {
88         return fLaunchConfig;
89     }
90     
91     /**
92      * Sets the launch configuration this tab was initialized from
93      *
94      * @param config launch configuration this tab was initialized from
95      */

96     private void setCurrentLaunchConfiguration(ILaunchConfiguration config) {
97         fLaunchConfig = config;
98     }
99     
100     /**
101      * Sets the Java project attribute on the given working copy to the Java project
102      * associated with the given Java element.
103      *
104      * @param javaElement Java model element this tab is associated with
105      * @param config configuration on which to set the Java project attribute
106      */

107     protected void initializeJavaProject(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) {
108         IJavaProject javaProject = javaElement.getJavaProject();
109         String JavaDoc name = null;
110         if (javaProject != null && javaProject.exists()) {
111             name = javaProject.getElementName();
112         }
113         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, name);
114     }
115     
116     /* (non-Javadoc)
117      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
118      *
119      * Subclasses may override this method and should call super.initializeFrom(...).
120      */

121     public void initializeFrom(ILaunchConfiguration config) {
122         setCurrentLaunchConfiguration(config);
123     }
124     
125 }
126
127
Popular Tags