KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > launchConfigurations > AntJRETab


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.ant.internal.ui.launchConfigurations;
13
14 import org.eclipse.ant.internal.ui.AntUIPlugin;
15 import org.eclipse.ant.internal.ui.IAntUIConstants;
16 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.debug.core.DebugPlugin;
19 import org.eclipse.debug.core.ILaunchConfiguration;
20 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
21 import org.eclipse.debug.ui.ILaunchConfigurationDialog;
22 import org.eclipse.jdt.debug.ui.launchConfigurations.JavaJRETab;
23 import org.eclipse.jdt.internal.debug.ui.jres.JREDescriptor;
24 import org.eclipse.jdt.internal.debug.ui.launcher.VMArgumentsBlock;
25 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
26 import org.eclipse.jdt.launching.IVMInstall;
27 import org.eclipse.jdt.launching.JavaRuntime;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.ui.PlatformUI;
32 import org.eclipse.ui.externaltools.internal.launchConfigurations.ExternalToolsUtil;
33
34 public class AntJRETab extends JavaJRETab {
35
36     private static final String JavaDoc MAIN_TYPE_NAME= "org.eclipse.ant.internal.ui.antsupport.InternalAntRunner"; //$NON-NLS-1$
37

38     private VMArgumentsBlock fVMArgumentsBlock= new VMArgumentsBlock();
39     private AntWorkingDirectoryBlock fWorkingDirectoryBlock= new AntWorkingDirectoryBlock();
40
41     /* (non-Javadoc)
42      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
43      */

44     public void createControl(Composite parent) {
45         super.createControl(parent);
46         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IAntUIHelpContextIds.ANT_JRE_TAB);
47         Composite comp= (Composite)fJREBlock.getControl();
48         ((GridData)comp.getLayoutData()).grabExcessVerticalSpace= true;
49         ((GridData)comp.getLayoutData()).verticalAlignment= SWT.FILL;
50         
51         fVMArgumentsBlock.createControl(comp);
52         ((GridData)fVMArgumentsBlock.getControl().getLayoutData()).horizontalSpan= 2;
53                         
54         fWorkingDirectoryBlock.createControl(comp);
55         ((GridData)fWorkingDirectoryBlock.getControl().getLayoutData()).horizontalSpan= 2;
56     }
57
58     /* (non-Javadoc)
59      * @see org.eclipse.jdt.debug.ui.launchConfigurations.JavaJRETab#getDefaultJREDescriptor()
60      */

61     protected JREDescriptor getDefaultJREDescriptor() {
62         return new JREDescriptor() {
63             /* (non-Javadoc)
64              * @see org.eclipse.jdt.internal.debug.ui.jres.JREDescriptor#getDescription()
65              */

66             public String JavaDoc getDescription() {
67                 return AntLaunchConfigurationMessages.AntJRETab_2;
68             }
69         };
70     }
71     
72     /* (non-Javadoc)
73      * @see org.eclipse.jdt.debug.ui.launchConfigurations.JavaJRETab#getSpecificJREDescriptor()
74      */

75     protected JREDescriptor getSpecificJREDescriptor() {
76         return new JREDescriptor() {
77             /* (non-Javadoc)
78              * @see org.eclipse.jdt.internal.debug.ui.jres.JREDescriptor#getDescription()
79              */

80             public String JavaDoc getDescription() {
81                 return AntLaunchConfigurationMessages.AntJRETab_3;
82             }
83         };
84     }
85         
86     /* (non-Javadoc)
87      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
88      */

89     public void performApply(ILaunchConfigurationWorkingCopy configuration) {
90         boolean isDefaultJRE = fJREBlock.isDefaultJRE();
91         fWorkingDirectoryBlock.setEnabled(!isDefaultJRE);
92         fVMArgumentsBlock.setEnabled(!isDefaultJRE);
93         if (isDefaultJRE) {
94             configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME, (String JavaDoc)null);
95             configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, (String JavaDoc)null);
96             configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, (String JavaDoc)null);
97             configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String JavaDoc)null);
98             configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, (String JavaDoc)null);
99             configuration.setAttribute(IAntUIConstants.ATTR_DEFAULT_VM_INSTALL, false);
100         } else {
101             super.performApply(configuration);
102             
103             if (useDefaultSeparateJRE(configuration)) {
104                 configuration.setAttribute(IAntUIConstants.ATTR_DEFAULT_VM_INSTALL, true);
105             } else {
106                 configuration.setAttribute(IAntUIConstants.ATTR_DEFAULT_VM_INSTALL, false);
107             }
108             
109             applySeparateVMAttributes(configuration);
110             fVMArgumentsBlock.performApply(configuration);
111             fWorkingDirectoryBlock.performApply(configuration);
112         }
113         setLaunchConfigurationWorkingCopy(configuration);
114     }
115     
116     private boolean useDefaultSeparateJRE(ILaunchConfigurationWorkingCopy configuration) {
117         boolean deflt= false;
118         String JavaDoc vmInstallType= null;
119         String JavaDoc jreContainerPath= null;
120         try {
121             vmInstallType= configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, (String JavaDoc)null);
122             jreContainerPath= configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, (String JavaDoc)null);
123         } catch (CoreException e) {
124         }
125         if (vmInstallType != null) {
126             configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, (String JavaDoc)null);
127         }
128         if (jreContainerPath != null) {
129             configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, (String JavaDoc)null);
130         }
131         IVMInstall defaultVMInstall= getDefaultVMInstall(configuration);
132         if (defaultVMInstall != null) {
133             IVMInstall vm= fJREBlock.getJRE();
134             deflt= defaultVMInstall.equals(vm);
135         }
136         
137         if (vmInstallType != null) {
138             configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, vmInstallType);
139         }
140         if (jreContainerPath != null) {
141             configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, jreContainerPath);
142         }
143         return deflt;
144     }
145     
146     private void applySeparateVMAttributes(ILaunchConfigurationWorkingCopy configuration) {
147         configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, MAIN_TYPE_NAME);
148         //only set to use the remote ant process factory if the user
149
//has not set to use a logger...bug 84608
150
boolean userLogger= false;
151         try {
152             String JavaDoc[] arguments = ExternalToolsUtil.getArguments(configuration);
153             if (arguments != null) {
154                 for (int i = 0; i < arguments.length; i++) {
155                     String JavaDoc arg = arguments[i];
156                     if (arg.equals("-logger")) { //$NON-NLS-1$
157
userLogger= true;
158                         break;
159                     }
160                 }
161             }
162         } catch (CoreException e) {
163         }
164         if (userLogger) {
165             configuration.setAttribute(DebugPlugin.ATTR_PROCESS_FACTORY_ID, (String JavaDoc) null);
166         } else {
167             configuration.setAttribute(DebugPlugin.ATTR_PROCESS_FACTORY_ID, IAntUIConstants.REMOTE_ANT_PROCESS_FACTORY_ID);
168         }
169     }
170
171     /* (non-Javadoc)
172      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
173      */

174     public void initializeFrom(ILaunchConfiguration configuration) {
175         try {
176              boolean isDefaultVMInstall= configuration.getAttribute(IAntUIConstants.ATTR_DEFAULT_VM_INSTALL, false);
177              if (isDefaultVMInstall) {
178                 ILaunchConfigurationWorkingCopy copy = null;
179                 if (configuration instanceof ILaunchConfigurationWorkingCopy) {
180                     copy= (ILaunchConfigurationWorkingCopy) configuration;
181                 } else {
182                     copy= configuration.getWorkingCopy();
183                 }
184                 
185                 //null out the vm type and jre container path to get the default vm install from JavaRuntime
186
copy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, (String JavaDoc)null);
187                 copy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, (String JavaDoc)null);
188                 IVMInstall defaultVMInstall= getDefaultVMInstall(copy);
189                 if (defaultVMInstall != null) {
190                     //update if required
191
setDefaultVMInstallAttributes(defaultVMInstall, copy);
192                 }
193                 if (copy.isDirty() && !copy.isReadOnly()) {
194                     configuration= copy.doSave();
195                 }
196              }
197         } catch (CoreException ce) {
198             AntUIPlugin.log(ce);
199         }
200         super.initializeFrom(configuration);
201         fVMArgumentsBlock.initializeFrom(configuration);
202         fWorkingDirectoryBlock.initializeFrom(configuration);
203         boolean separateVM= !fJREBlock.isDefaultJRE();
204         fWorkingDirectoryBlock.setEnabled(separateVM);
205         fVMArgumentsBlock.setEnabled(separateVM);
206     }
207     
208     /* (non-Javadoc)
209      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
210      */

211     public boolean isValid(ILaunchConfiguration config) {
212         return super.isValid(config) && fWorkingDirectoryBlock.isValid(config);
213     }
214     
215     /* (non-Javadoc)
216      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setLaunchConfigurationDialog(org.eclipse.debug.ui.ILaunchConfigurationDialog)
217      */

218     public void setLaunchConfigurationDialog(ILaunchConfigurationDialog dialog) {
219         super.setLaunchConfigurationDialog(dialog);
220         fWorkingDirectoryBlock.setLaunchConfigurationDialog(dialog);
221         fVMArgumentsBlock.setLaunchConfigurationDialog(dialog);
222     }
223     
224     /* (non-Javadoc)
225      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getErrorMessage()
226      */

227     public String JavaDoc getErrorMessage() {
228         String JavaDoc m = super.getErrorMessage();
229         if (m == null) {
230             return fWorkingDirectoryBlock.getErrorMessage();
231         }
232         return m;
233     }
234
235     /* (non-Javadoc)
236      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getMessage()
237      */

238     public String JavaDoc getMessage() {
239         String JavaDoc m = super.getMessage();
240         if (m == null) {
241             return fWorkingDirectoryBlock.getMessage();
242         }
243         return m;
244     }
245
246     /* (non-Javadoc)
247      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#activated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
248      */

249     public void activated(ILaunchConfigurationWorkingCopy workingCopy) {
250         setLaunchConfigurationWorkingCopy(workingCopy);
251     }
252
253     /* (non-Javadoc)
254      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
255      */

256     public void setDefaults(ILaunchConfigurationWorkingCopy config) {
257         super.setDefaults(config);
258         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER, "org.eclipse.ant.ui.AntClasspathProvider"); //$NON-NLS-1$
259
IVMInstall defaultVMInstall= getDefaultVMInstall(config);
260         if (defaultVMInstall != null) {
261             config.setAttribute(IAntUIConstants.ATTR_DEFAULT_VM_INSTALL, true);
262             setDefaultVMInstallAttributes(defaultVMInstall, config);
263             applySeparateVMAttributes(config);
264         }
265         
266     }
267
268     private IVMInstall getDefaultVMInstall(ILaunchConfiguration config) {
269         IVMInstall defaultVMInstall;
270         try {
271             defaultVMInstall = JavaRuntime.computeVMInstall(config);
272         } catch (CoreException e) {
273             //core exception thrown for non-Java project
274
defaultVMInstall= JavaRuntime.getDefaultVMInstall();
275         }
276         return defaultVMInstall;
277     }
278
279     private void setDefaultVMInstallAttributes(IVMInstall defaultVMInstall, ILaunchConfigurationWorkingCopy config) {
280         String JavaDoc vmName = defaultVMInstall.getName();
281         String JavaDoc vmTypeID = defaultVMInstall.getVMInstallType().getId();
282         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME, vmName);
283         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, vmTypeID);
284     }
285     
286     /* (non-Javadoc)
287      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#deactivated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
288      */

289     public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) {
290     }
291 }
292
Popular Tags