KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > launcher > SharedJavaMainTab


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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
12 package org.eclipse.jdt.internal.debug.ui.launcher;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.debug.core.ILaunchConfiguration;
18 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
19 import org.eclipse.jdt.core.IClassFile;
20 import org.eclipse.jdt.core.ICompilationUnit;
21 import org.eclipse.jdt.core.IJavaElement;
22 import org.eclipse.jdt.core.IMember;
23 import org.eclipse.jdt.core.IType;
24 import org.eclipse.jdt.core.search.IJavaSearchScope;
25 import org.eclipse.jdt.core.search.SearchEngine;
26 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
27 import org.eclipse.jdt.internal.debug.ui.SWTFactory;
28 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.events.SelectionEvent;
32 import org.eclipse.swt.events.SelectionListener;
33 import org.eclipse.swt.graphics.Font;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Group;
38 import org.eclipse.swt.widgets.Text;
39
40 /**
41  * Provides general widgets and methods for a Java type launch configuration
42  * 'Main' tab.
43  * This class provides shared functionality for those main tabs which have a 'main type' field on them;
44  * such as a main method for a local Java app, or an applet for Java applets
45  *
46  * @since 3.2
47  */

48 public abstract class SharedJavaMainTab extends AbstractJavaMainTab {
49
50     protected Text fMainText;
51     private Button fSearchButton;
52     
53     /**
54      * Creates the widgets for specifying a main type.
55      *
56      * @param parent the parent composite
57      */

58     protected void createMainTypeEditor(Composite parent, String JavaDoc text) {
59         Font font= parent.getFont();
60         Group mainGroup = SWTFactory.createGroup(parent, text, 2, 1, GridData.FILL_HORIZONTAL);
61         Composite comp = SWTFactory.createComposite(mainGroup, font, 2, 2, GridData.FILL_BOTH, 0, 0);
62         fMainText = SWTFactory.createSingleText(comp, 1);
63         fMainText.addModifyListener(new ModifyListener() {
64             public void modifyText(ModifyEvent e) {
65                 updateLaunchConfigurationDialog();
66             }
67         });
68         fSearchButton = createPushButton(comp, LauncherMessages.AbstractJavaMainTab_2, null);
69         fSearchButton.addSelectionListener(new SelectionListener() {
70             public void widgetDefaultSelected(SelectionEvent e) {
71             }
72             public void widgetSelected(SelectionEvent e) {
73                 handleSearchButtonSelected();
74             }
75         });
76         createMainTypeExtensions(comp);
77     }
78     
79     /**
80      * This method allows the group for main type to be extended with custom controls.
81      * All control added via this method come after the main type text editor and search button in
82      * the order they are added to the parent composite
83      *
84      * @param parent the parent to add to
85      * @since 3.3
86      */

87     protected void createMainTypeExtensions(Composite parent) {
88         //do nothing by default
89
}
90     
91     /**
92      * The select button pressed handler
93      */

94     protected abstract void handleSearchButtonSelected();
95
96     /**
97      * Set the main type & name attributes on the working copy based on the IJavaElement
98      */

99     protected void initializeMainTypeAndName(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) {
100         String JavaDoc name = null;
101         if (javaElement instanceof IMember) {
102             IMember member = (IMember)javaElement;
103             if (member.isBinary()) {
104                 javaElement = member.getClassFile();
105             }
106             else {
107                 javaElement = member.getCompilationUnit();
108             }
109         }
110         if (javaElement instanceof ICompilationUnit || javaElement instanceof IClassFile) {
111             try {
112                 IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[]{javaElement}, false);
113                 MainMethodSearchEngine engine = new MainMethodSearchEngine();
114                 IType[] types = engine.searchMainMethods(getLaunchConfigurationDialog(), scope, false);
115                 if (types != null && (types.length > 0)) {
116                     // Simply grab the first main type found in the searched element
117
name = types[0].getFullyQualifiedName();
118                 }
119             }
120             catch (InterruptedException JavaDoc ie) {JDIDebugUIPlugin.log(ie);}
121             catch (InvocationTargetException JavaDoc ite) {JDIDebugUIPlugin.log(ite);}
122         }
123         if (name == null) {
124             name = EMPTY_STRING;
125         }
126         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, name);
127         if (name.length() > 0) {
128             int index = name.lastIndexOf('.');
129             if (index > 0) {
130                 name = name.substring(index + 1);
131             }
132             name = getLaunchConfigurationDialog().generateName(name);
133             config.rename(name);
134         }
135     }
136     
137     /**
138      * Loads the main type from the launch configuration's preference store
139      * @param config the config to load the main type from
140      */

141     protected void updateMainTypeFromConfig(ILaunchConfiguration config) {
142         String JavaDoc mainTypeName = EMPTY_STRING;
143         try {
144             mainTypeName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, EMPTY_STRING);
145         }
146         catch (CoreException ce) {JDIDebugUIPlugin.log(ce);}
147         fMainText.setText(mainTypeName);
148     }
149 }
150
Popular Tags