KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.jdt.debug.ui.launchConfigurations;
12
13  
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.debug.core.ILaunchConfiguration;
24 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
25 import org.eclipse.debug.ui.ILaunchConfigurationTab;
26 import org.eclipse.jdt.core.IJavaProject;
27 import org.eclipse.jdt.core.JavaCore;
28 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
29 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
30 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
31 import org.eclipse.jdt.internal.debug.ui.SWTFactory;
32 import org.eclipse.jdt.internal.debug.ui.jres.JREDescriptor;
33 import org.eclipse.jdt.internal.debug.ui.jres.JREsComboBlock;
34 import org.eclipse.jdt.internal.debug.ui.launcher.LauncherMessages;
35 import org.eclipse.jdt.launching.AbstractVMInstall;
36 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
37 import org.eclipse.jdt.launching.IVMInstall;
38 import org.eclipse.jdt.launching.JavaRuntime;
39 import org.eclipse.jdt.ui.ISharedImages;
40 import org.eclipse.jdt.ui.JavaUI;
41 import org.eclipse.jface.util.IPropertyChangeListener;
42 import org.eclipse.jface.util.PropertyChangeEvent;
43 import org.eclipse.swt.graphics.Font;
44 import org.eclipse.swt.graphics.Image;
45 import org.eclipse.swt.layout.GridData;
46 import org.eclipse.swt.widgets.Composite;
47 import org.eclipse.swt.widgets.Control;
48 import org.eclipse.ui.PlatformUI;
49
50 import com.ibm.icu.text.MessageFormat;
51
52 /**
53  * A launch configuration tab that displays and edits the VM install
54  * launch configuration attributes.
55  * <p>
56  * This class may be instantiated. This class is not intended to be subclassed.
57  * </p>
58  * @since 2.0
59  */

60
61 public class JavaJRETab extends JavaLaunchTab {
62     
63     // JRE Block
64
protected JREsComboBlock fJREBlock;
65     
66     // Dynamic JRE UI widgets
67
protected ILaunchConfigurationTab fDynamicTab;
68     protected Composite fDynamicTabHolder;
69     protected boolean fUseDynamicArea = true;
70     
71     protected ILaunchConfigurationWorkingCopy fWorkingCopy;
72     protected ILaunchConfiguration fLaunchConfiguration;
73     
74     // State
75
protected boolean fIsInitializing = false;
76     
77     // Selection changed listener (checked JRE)
78
private IPropertyChangeListener fCheckListener = new IPropertyChangeListener() {
79         public void propertyChange(PropertyChangeEvent event) {
80             handleSelectedJREChanged();
81         }
82     };
83     
84     // Constants
85
protected static final String JavaDoc EMPTY_STRING = ""; //$NON-NLS-1$
86

87     /* (non-Javadoc)
88      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#dispose()
89      */

90     public void dispose() {
91         super.dispose();
92         if (fJREBlock != null) {
93             fJREBlock.removePropertyChangeListener(fCheckListener);
94         }
95     }
96
97     /**
98      * @see ILaunchConfigurationTab#createControl(Composite)
99      */

100     public void createControl(Composite parent) {
101         Font font = parent.getFont();
102         Composite topComp = SWTFactory.createComposite(parent, font, 1, 1, GridData.FILL_HORIZONTAL, 0, 0);
103         
104         fJREBlock = new JREsComboBlock();
105         fJREBlock.setDefaultJREDescriptor(getDefaultJREDescriptor());
106         fJREBlock.setSpecificJREDescriptor(getSpecificJREDescriptor());
107         fJREBlock.createControl(topComp);
108         Control control = fJREBlock.getControl();
109         fJREBlock.addPropertyChangeListener(fCheckListener);
110         control.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
111         
112         setDynamicTabHolder(SWTFactory.createComposite(topComp, font, 1, 1, GridData.FILL_BOTH, 0, 0));
113         setControl(topComp);
114         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_JRE_TAB);
115     }
116
117     protected void setDynamicTabHolder(Composite tabHolder) {
118         this.fDynamicTabHolder = tabHolder;
119     }
120
121     protected Composite getDynamicTabHolder() {
122         return fDynamicTabHolder;
123     }
124
125     protected void setDynamicTab(ILaunchConfigurationTab tab) {
126         fDynamicTab = tab;
127     }
128
129     protected ILaunchConfigurationTab getDynamicTab() {
130         return fDynamicTab;
131     }
132
133     /**
134      * @see ILaunchConfigurationTab#setDefaults(ILaunchConfigurationWorkingCopy)
135      */

136     public void setDefaults(ILaunchConfigurationWorkingCopy config) {
137         setLaunchConfigurationWorkingCopy(config);
138         ILaunchConfigurationTab dynamicTab = getDynamicTab();
139         if (dynamicTab != null) {
140             dynamicTab.setDefaults(config);
141         }
142     }
143     
144     /**
145      * @see ILaunchConfigurationTab#initializeFrom(ILaunchConfiguration)
146      */

147     public void initializeFrom(ILaunchConfiguration configuration) {
148         fIsInitializing = true;
149         getControl().setRedraw(false);
150         setLaunchConfiguration(configuration);
151         updateJREFromConfig(configuration);
152         fJREBlock.setDefaultJREDescriptor(getDefaultJREDescriptor());
153         ILaunchConfigurationTab dynamicTab = getDynamicTab();
154         if (dynamicTab != null) {
155             dynamicTab.initializeFrom(configuration);
156         }
157         getControl().setRedraw(true);
158         fIsInitializing = false;
159     }
160
161     /**
162      * @see ILaunchConfigurationTab#performApply(ILaunchConfigurationWorkingCopy)
163      */

164     public void performApply(ILaunchConfigurationWorkingCopy configuration) {
165         if (fJREBlock.isDefaultJRE()) {
166             configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, (String JavaDoc)null);
167         } else {
168             IPath containerPath = fJREBlock.getPath();
169             String JavaDoc portablePath = null;
170             if (containerPath != null) {
171                 portablePath = containerPath.toPortableString();
172             }
173             configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, portablePath);
174         }
175         // erase old attributes in case the user changed from 'specific JRE' to 'default' - see bug 152446
176
configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME, (String JavaDoc)null);
177         configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, (String JavaDoc)null);
178         
179         // Handle any attributes in the VM-specific area
180
ILaunchConfigurationTab dynamicTab = getDynamicTab();
181         if (dynamicTab == null) {
182             configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE_SPECIFIC_ATTRS_MAP, (Map JavaDoc)null);
183         } else {
184             dynamicTab.performApply(configuration);
185         }
186     }
187
188     /**
189      * @see ILaunchConfigurationTab#isValid(ILaunchConfiguration)
190      */

191     public boolean isValid(ILaunchConfiguration config) {
192         
193         setErrorMessage(null);
194         setMessage(null);
195         
196         IStatus status = fJREBlock.getStatus();
197         if (!status.isOK()) {
198             setErrorMessage(status.getMessage());
199             return false;
200         }
201         status = checkCompliance();
202         if (!status.isOK()) {
203             setErrorMessage(status.getMessage());
204             return false;
205         }
206
207         ILaunchConfigurationTab dynamicTab = getDynamicTab();
208         if (dynamicTab != null) {
209             return dynamicTab.isValid(config);
210         }
211         return true;
212     }
213     
214     /**
215      * Checks to make sure the class file compliance level and the selected VM are compatible
216      * i.e. such that the selected JRE can run the currently compiled code
217      * @since 3.3
218      */

219     private IStatus checkCompliance() {
220         IJavaProject javaProject = getJavaProject();
221         if (javaProject == null) {
222             return Status.OK_STATUS;
223         }
224         String JavaDoc source = LauncherMessages.JavaJRETab_3;
225         String JavaDoc compliance = javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, false);
226         if(compliance == null) {
227             compliance = javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true);
228             source = LauncherMessages.JavaJRETab_4;
229         }
230         IPath vmPath = fJREBlock.getPath();
231         IVMInstall vm = null;
232         if(JavaRuntime.newDefaultJREContainerPath().equals(vmPath)) {
233             if(javaProject.isOpen()) {
234                 try {
235                     vm = JavaRuntime.getVMInstall(getJavaProject());
236                 } catch (CoreException e) {
237                     JDIDebugUIPlugin.log(e);
238                     return Status.OK_STATUS;
239                 }
240                 if(vm == null) {
241                     vm = JavaRuntime.getVMInstall(vmPath);
242                 }
243             }
244         }
245         else {
246             vm = JavaRuntime.getVMInstall(vmPath);
247         }
248         String JavaDoc environmentId = JavaRuntime.getExecutionEnvironmentId(vmPath);
249         if(vm instanceof AbstractVMInstall) {
250             AbstractVMInstall install = (AbstractVMInstall) vm;
251             String JavaDoc vmver = install.getJavaVersion();
252             if(vmver != null) {
253                 int val = compliance.compareTo(vmver);
254                 if(val > 0) {
255                     String JavaDoc setting = null;
256                     if (environmentId == null) {
257                         setting = LauncherMessages.JavaJRETab_2;
258                     } else {
259                         setting = LauncherMessages.JavaJRETab_1;
260                     }
261                     return new Status(IStatus.ERROR, IJavaDebugUIConstants.PLUGIN_ID,IJavaDebugUIConstants.INTERNAL_ERROR,
262                             MessageFormat.format(LauncherMessages.JavaJRETab_0,
263                                     new String JavaDoc[]{setting, source, compliance}), null);
264                 }
265             }
266         }
267         return Status.OK_STATUS;
268     }
269
270     /**
271      * @see ILaunchConfigurationTab#getName()
272      */

273     public String JavaDoc getName() {
274         return LauncherMessages.JavaJRETab__JRE_1;
275     }
276     
277     /**
278      * @see ILaunchConfigurationTab#getImage()
279      */

280     public Image getImage() {
281         return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_LIBRARY);
282     }
283
284     /**
285      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#getId()
286      *
287      * @since 3.3
288      */

289     public String JavaDoc getId() {
290         return "org.eclipse.jdt.debug.ui.javaJRETab"; //$NON-NLS-1$
291
}
292     
293     /**
294      * This method updates the jre selection from the <code>ILaunchConfiguration</code>
295      * @param config the config to update from
296      */

297     protected void updateJREFromConfig(ILaunchConfiguration config) {
298         try {
299             String JavaDoc path = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_JRE_CONTAINER_PATH, (String JavaDoc)null);
300             if (path != null) {
301                 fJREBlock.setPath(Path.fromPortableString(path));
302                 return;
303             }
304         } catch (CoreException e) {
305             JDIDebugUIPlugin.log(e);
306         }
307         String JavaDoc vmName = null;
308         String JavaDoc vmTypeID = null;
309         try {
310             vmTypeID = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, (String JavaDoc)null);
311             vmName = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME, (String JavaDoc)null);
312         } catch (CoreException ce) {
313             JDIDebugUIPlugin.log(ce);
314         }
315         selectJRE(vmTypeID, vmName);
316     }
317     
318     /**
319      * Notification that the user changed the selection in the JRE combination box.
320      */

321     protected void handleSelectedJREChanged() {
322         loadDynamicJREArea();
323         
324         // always set the newly created area with defaults
325
ILaunchConfigurationWorkingCopy wc = getLaunchConfigurationWorkingCopy();
326         if (getDynamicTab() == null) {
327             // remove any VM specific arguments from the config
328
if (wc == null) {
329                 if (getLaunchConfiguration().isWorkingCopy()) {
330                     wc = (ILaunchConfigurationWorkingCopy)getLaunchConfiguration();
331                 }
332             }
333             if (!fIsInitializing) {
334                 if (wc != null) {
335                     wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE_SPECIFIC_ATTRS_MAP, (Map JavaDoc)null);
336                 }
337             }
338         } else {
339             if (wc == null) {
340                 try {
341                     ILaunchConfiguration config = getLaunchConfiguration();
342                     if (config.isWorkingCopy()) {
343                         // get a fresh copy to work on, only if there is an original to do so
344
wc = (ILaunchConfigurationWorkingCopy) config;
345                         if(wc.getOriginal() != null) {
346                             wc.getOriginal().getWorkingCopy();
347                         }
348                     } else {
349                             wc = getLaunchConfiguration().getWorkingCopy();
350                     }
351                 } catch (CoreException e) {
352                     IStatus status = e.getStatus();
353                     JDIDebugUIPlugin.statusDialog(status);
354                     JDIDebugUIPlugin.log(status);
355                     return;
356                 }
357             }
358             if (!fIsInitializing) {
359                 getDynamicTab().setDefaults(wc);
360                 getDynamicTab().initializeFrom(wc);
361             }
362         }
363                 
364         updateLaunchConfigurationDialog();
365     }
366     
367     protected void selectJRE(String JavaDoc typeID, String JavaDoc vmName) {
368         if (typeID == null) {
369             fJREBlock.setPath(JavaRuntime.newDefaultJREContainerPath());
370         } else {
371             fJREBlock.setPath(JavaRuntime.newJREContainerPath(typeID, vmName));
372         }
373     }
374     
375     /**
376      * Return the class that implements <code>ILaunchConfigurationTab</code>
377      * that is registered against the install type of the currently selected VM.
378      */

379     protected ILaunchConfigurationTab getTabForCurrentJRE() {
380         if (!fJREBlock.isDefaultJRE()) {
381             IPath path = fJREBlock.getPath();
382             if (path != null && JavaRuntime.getExecutionEnvironmentId(path) == null) {
383                 IVMInstall vm = fJREBlock.getJRE();
384                 if (vm != null) {
385                     String JavaDoc vmInstallTypeID = vm.getVMInstallType().getId();
386                     return JDIDebugUIPlugin.getDefault().getVMInstallTypePage(vmInstallTypeID);
387                 }
388             }
389         }
390         return null;
391     }
392     
393     /**
394      * Show the contributed piece of UI that was registered for the install type
395      * of the currently selected VM.
396      */

397     protected void loadDynamicJREArea() {
398         
399         // Dispose of any current child widgets in the tab holder area
400
Control[] children = getDynamicTabHolder().getChildren();
401         for (int i = 0; i < children.length; i++) {
402             children[i].dispose();
403         }
404         
405         if (isUseDynamicJREArea()) {
406             // Retrieve the dynamic UI for the current JRE
407
setDynamicTab(getTabForCurrentJRE());
408             if (getDynamicTab() == null) {
409                 return;
410             }
411             
412             // Ask the dynamic UI to create its Control
413
getDynamicTab().setLaunchConfigurationDialog(getLaunchConfigurationDialog());
414             getDynamicTab().createControl(getDynamicTabHolder());
415             getDynamicTabHolder().layout();
416         }
417             
418     }
419
420     protected ILaunchConfigurationWorkingCopy getLaunchConfigurationWorkingCopy() {
421         return fWorkingCopy;
422     }
423     
424     /**
425      * Overridden here so that any error message in the dynamic UI gets returned.
426      *
427      * @see ILaunchConfigurationTab#getErrorMessage()
428      */

429     public String JavaDoc getErrorMessage() {
430         ILaunchConfigurationTab tab = getDynamicTab();
431         if ((super.getErrorMessage() != null) || (tab == null)) {
432             return super.getErrorMessage();
433         }
434         return tab.getErrorMessage();
435     }
436
437     protected void setLaunchConfigurationWorkingCopy(ILaunchConfigurationWorkingCopy workingCopy) {
438         fWorkingCopy = workingCopy;
439     }
440
441     protected ILaunchConfiguration getLaunchConfiguration() {
442         return fLaunchConfiguration;
443     }
444
445     protected void setLaunchConfiguration(ILaunchConfiguration launchConfiguration) {
446         fLaunchConfiguration = launchConfiguration;
447     }
448     
449     /**
450      * Sets whether this tab will display the VM specific arguments area
451      * if a JRE supports VM specific arguments.
452      *
453      * @param visible whether this tab will display the VM specific arguments area
454      * if a JRE supports VM specific arguments
455      */

456     public void setVMSpecificArgumentsVisible(boolean visible) {
457         fUseDynamicArea = visible;
458     }
459     
460     protected boolean isUseDynamicJREArea() {
461         return fUseDynamicArea;
462     }
463
464     protected JREDescriptor getDefaultJREDescriptor() {
465         return new JREDescriptor() {
466
467             /* (non-Javadoc)
468              * @see org.eclipse.jdt.internal.debug.ui.jres.DefaultJREDescriptor#getDescription()
469              */

470             public String JavaDoc getDescription() {
471                 IJavaProject project = getJavaProject();
472                 String JavaDoc name = LauncherMessages.JavaJRETab_7;
473                 if (project == null) {
474                     IVMInstall vm = JavaRuntime.getDefaultVMInstall();
475                     if (vm != null) {
476                         name = vm.getName();
477                     }
478                     return MessageFormat.format(LauncherMessages.JavaJRETab_8, new String JavaDoc[]{name});
479                 }
480                 try {
481                     IVMInstall vm = JavaRuntime.getVMInstall(project);
482                     if (vm != null) {
483                         name = vm.getName();
484                     }
485                 } catch (CoreException e) {
486                 }
487                 return MessageFormat.format(LauncherMessages.JavaJRETab_9, new String JavaDoc[]{name});
488             }
489         };
490     }
491     
492     protected JREDescriptor getSpecificJREDescriptor() {
493         return null;
494     }
495     
496     /**
497      * Returns the Java project associated with the current config being edited,
498      * or <code>null</code> if none.
499      *
500      * @return java project or <code>null</code>
501      */

502     protected IJavaProject getJavaProject() {
503         if (getLaunchConfiguration() != null) {
504             try {
505                 String JavaDoc name = getLaunchConfiguration().getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String JavaDoc)null);
506                 if (name != null && name.length() > 0) {
507                     IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
508                     if (project.exists()) {
509                         return JavaCore.create(project);
510                     }
511                 }
512             } catch (CoreException e) {
513                 JDIDebugUIPlugin.log(e);
514             }
515         }
516         return null;
517     }
518     
519     /* (non-Javadoc)
520      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#activated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
521      */

522     public void activated(ILaunchConfigurationWorkingCopy workingCopy) {
523         // update the default JRE description, in case it has changed
524
// based on the selected project
525
fJREBlock.refresh();
526     }
527
528     /* (non-Javadoc)
529      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#deactivated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
530      */

531     public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) {
532         // do nothing when deactivated
533
}
534 }
535
Popular Tags