KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > jres > JREsPreferencePage


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.internal.debug.ui.jres;
12
13
14 import java.io.File JavaDoc;
15
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
20 import org.eclipse.core.runtime.preferences.InstanceScope;
21 import org.eclipse.jdt.core.IJavaModel;
22 import org.eclipse.jdt.core.JavaCore;
23 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
24 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
25 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
26 import org.eclipse.jdt.internal.debug.ui.SWTFactory;
27 import org.eclipse.jdt.launching.AbstractVMInstall;
28 import org.eclipse.jdt.launching.IVMInstall;
29 import org.eclipse.jdt.launching.JavaRuntime;
30 import org.eclipse.jdt.launching.LibraryLocation;
31 import org.eclipse.jface.dialogs.ErrorDialog;
32 import org.eclipse.jface.dialogs.IDialogSettings;
33 import org.eclipse.jface.dialogs.IMessageProvider;
34 import org.eclipse.jface.preference.PreferencePage;
35 import org.eclipse.jface.viewers.ISelectionChangedListener;
36 import org.eclipse.jface.viewers.SelectionChangedEvent;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.custom.BusyIndicator;
39 import org.eclipse.swt.events.SelectionEvent;
40 import org.eclipse.swt.events.SelectionListener;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.layout.GridLayout;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.swt.widgets.Control;
45 import org.eclipse.swt.widgets.Link;
46 import org.eclipse.ui.IWorkbench;
47 import org.eclipse.ui.IWorkbenchPreferencePage;
48 import org.eclipse.ui.PlatformUI;
49 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
50
51 import com.ibm.icu.text.MessageFormat;
52
53 /**
54  * The Installed JREs preference page.
55  *
56  * @since 3.0
57  */

58 public class JREsPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
59                             
60     // JRE Block
61
private InstalledJREsBlock fJREBlock;
62     private Link fCompliance;
63         
64     /**
65      * Constructor
66      */

67     public JREsPreferencePage() {
68         super(JREMessages.JREsPreferencePage_1);
69     }
70
71     /* (non-Javadoc)
72      * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
73      */

74     public void init(IWorkbench workbench) {
75     }
76
77     /**
78      * Find & verify the default VM.
79      */

80     private void initDefaultVM() {
81         IVMInstall realDefault= JavaRuntime.getDefaultVMInstall();
82         if (realDefault != null) {
83             IVMInstall[] vms= fJREBlock.getJREs();
84             for (int i = 0; i < vms.length; i++) {
85                 IVMInstall fakeVM= vms[i];
86                 if (fakeVM.equals(realDefault)) {
87                     verifyDefaultVM(fakeVM);
88                     break;
89                 }
90             }
91         }
92     }
93     
94     /* (non-Javadoc)
95      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
96      */

97     protected Control createContents(Composite ancestor) {
98         initializeDialogUnits(ancestor);
99         
100         noDefaultAndApplyButton();
101         
102         GridLayout layout= new GridLayout();
103         layout.numColumns= 1;
104         layout.marginHeight = 0;
105         layout.marginWidth = 0;
106         ancestor.setLayout(layout);
107         
108         SWTFactory.createWrapLabel(ancestor, JREMessages.JREsPreferencePage_2, 1, 300);
109         SWTFactory.createVerticalSpacer(ancestor, 1);
110         
111         fJREBlock = new InstalledJREsBlock();
112         fJREBlock.createControl(ancestor);
113         Control control = fJREBlock.getControl();
114         GridData data = new GridData(GridData.FILL_BOTH);
115         data.horizontalSpan = 1;
116         control.setLayoutData(data);
117         
118         fJREBlock.restoreColumnSettings(JDIDebugUIPlugin.getDefault().getDialogSettings(), IJavaDebugHelpContextIds.JRE_PREFERENCE_PAGE);
119                     
120         fCompliance = new Link(ancestor, SWT.NONE);
121         fCompliance.setText(JREMessages.JREsPreferencePage_14);
122         fCompliance.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
123         fCompliance.setVisible(false);
124         fCompliance.addSelectionListener(new SelectionListener() {
125             public void widgetDefaultSelected(SelectionEvent e) {}
126             public void widgetSelected(SelectionEvent e) {openCompliancePreferencePage();}
127         });
128         PlatformUI.getWorkbench().getHelpSystem().setHelp(ancestor, IJavaDebugHelpContextIds.JRE_PREFERENCE_PAGE);
129         initDefaultVM();
130         fJREBlock.addSelectionChangedListener(new ISelectionChangedListener() {
131             public void selectionChanged(SelectionChangedEvent event) {
132                 IVMInstall install = getCurrentDefaultVM();
133                 if (install == null) {
134                     setValid(false);
135                     setErrorMessage(JREMessages.JREsPreferencePage_13);
136                 } else {
137                     //if we change the VM make sure the compliance level supports
138
//generated class files
139
String JavaDoc compliance = getCurrentCompilerCompliance();
140                     if(!supportsCurrentCompliance(install, compliance)) {
141                         setMessage(MessageFormat.format(JREMessages.JREsPreferencePage_0, new String JavaDoc[] {compliance}), IMessageProvider.WARNING);
142                         fCompliance.setVisible(true);
143                     }
144                     else {
145                         setMessage(null);
146                         fCompliance.setVisible(false);
147                     }
148                     setValid(true);
149                     setErrorMessage(null);
150                 }
151             }
152         });
153         applyDialogFont(ancestor);
154         return ancestor;
155     }
156
157     /* (non-Javadoc)
158      * @see org.eclipse.jface.preference.PreferencePage#isValid()
159      */

160     public boolean isValid() {
161         String JavaDoc compliance = getCurrentCompilerCompliance();
162         if(!supportsCurrentCompliance(getCurrentDefaultVM(), compliance)) {
163             setMessage(MessageFormat.format(JREMessages.JREsPreferencePage_0, new String JavaDoc[] {compliance}), IMessageProvider.WARNING);
164             fCompliance.setVisible(true);
165         }
166         else {
167             setMessage(null);
168             fCompliance.setVisible(false);
169         }
170         return super.isValid();
171     }
172
173     /**
174      * Opens the <code>CompliancePreferencePage</code>
175      * @since 3.3
176      */

177     private void openCompliancePreferencePage() {
178         String JavaDoc compliancepage = "org.eclipse.jdt.ui.preferences.CompliancePreferencePage"; //$NON-NLS-1$
179
IWorkbenchPreferenceContainer wpc = (IWorkbenchPreferenceContainer)getContainer();
180         if (wpc != null) {
181             wpc.openPage(compliancepage, null);
182         } else {
183             SWTFactory.showPreferencePage(compliancepage);
184         }
185     }
186     
187     /**
188      * @return the current compiler compliance level
189      * @since 3.3
190      */

191     private String JavaDoc getCurrentCompilerCompliance() {
192         IEclipsePreferences setting = new InstanceScope().getNode(JavaCore.PLUGIN_ID);
193         if(getContainer() instanceof IWorkbenchPreferenceContainer) {
194             IEclipsePreferences wcs = ((IWorkbenchPreferenceContainer)getContainer()).getWorkingCopyManager().getWorkingCopy(setting);
195             return wcs.get(JavaCore.COMPILER_COMPLIANCE, (String JavaDoc) JavaCore.getDefaultOptions().get(JavaCore.COMPILER_COMPLIANCE));
196         } else {
197             return JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE);
198         }
199         
200     }
201     
202     /**
203      * Determines if the vm version will run the currently compiled code based on the compiler compliance lvl
204      * @param vm the vm install
205      * @param compliance the current compiler compliance level
206      * @return true if the selected vm will run the current code, false otherwise
207      * @since 3.3
208      */

209     private boolean supportsCurrentCompliance(IVMInstall vm, String JavaDoc compliance) {
210         if(vm instanceof AbstractVMInstall) {
211             AbstractVMInstall install = (AbstractVMInstall) vm;
212             String JavaDoc vmver = install.getJavaVersion();
213             if(vmver == null) {
214                 //if we cannot get a version from the VM we must return true, and let the runtime
215
//error sort it out
216
return true;
217             }
218             int val = compliance.compareTo(vmver);
219             return val < 0 || val == 0;
220         }
221         return false;
222     }
223     
224     /* (non-Javadoc)
225      * @see org.eclipse.jface.preference.IPreferencePage#performOk()
226      */

227     public boolean performOk() {
228         final boolean[] canceled = new boolean[] {false};
229         BusyIndicator.showWhile(null, new Runnable JavaDoc() {
230             public void run() {
231                 IVMInstall defaultVM = getCurrentDefaultVM();
232                 IVMInstall[] vms = fJREBlock.getJREs();
233                 JREsUpdater updater = new JREsUpdater();
234                 if (!updater.updateJRESettings(vms, defaultVM)) {
235                     canceled[0] = true;
236                 }
237             }
238         });
239         
240         if(canceled[0]) {
241             return false;
242         }
243         
244         // save column widths
245
IDialogSettings settings = JDIDebugUIPlugin.getDefault().getDialogSettings();
246         fJREBlock.saveColumnSettings(settings, IJavaDebugHelpContextIds.JRE_PREFERENCE_PAGE);
247         
248         return super.performOk();
249     }
250     
251     protected IJavaModel getJavaModel() {
252         return JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
253     }
254
255     /**
256      * Verify that the specified VM can be a valid default VM. This amounts to verifying
257      * that all of the VM's library locations exist on the file system. If this fails,
258      * remove the VM from the table and try to set another default.
259      */

260     private void verifyDefaultVM(IVMInstall vm) {
261         if (vm != null) {
262             
263             // Verify that all of the specified VM's library locations actually exist
264
LibraryLocation[] locations= JavaRuntime.getLibraryLocations(vm);
265             boolean exist = true;
266             for (int i = 0; i < locations.length; i++) {
267                 exist = exist && new File JavaDoc(locations[i].getSystemLibraryPath().toOSString()).exists();
268             }
269             
270             // If all library locations exist, check the corresponding entry in the list,
271
// otherwise remove the VM
272
if (exist) {
273                 fJREBlock.setCheckedJRE(vm);
274             } else {
275                 fJREBlock.removeJREs(new IVMInstall[]{vm});
276                 IVMInstall def = JavaRuntime.getDefaultVMInstall();
277                 if (def == null) {
278                     fJREBlock.setCheckedJRE(null);
279                 } else {
280                     fJREBlock.setCheckedJRE(def);
281                 }
282                 ErrorDialog.openError(getControl().getShell(), JREMessages.JREsPreferencePage_1, JREMessages.JREsPreferencePage_10, new Status(IStatus.ERROR, IJavaDebugUIConstants.PLUGIN_ID, IJavaDebugUIConstants.INTERNAL_ERROR, JREMessages.JREsPreferencePage_11, null)); //
283
return;
284             }
285         } else {
286             fJREBlock.setCheckedJRE(null);
287         }
288     }
289     
290     private IVMInstall getCurrentDefaultVM() {
291         return fJREBlock.getCheckedJRE();
292     }
293 }
294
Popular Tags