1 11 package org.eclipse.jdt.internal.debug.ui.jres; 12 13 14 import java.io.File ; 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 58 public class JREsPreferencePage extends PreferencePage implements IWorkbenchPreferencePage { 59 60 private InstalledJREsBlock fJREBlock; 62 private Link fCompliance; 63 64 67 public JREsPreferencePage() { 68 super(JREMessages.JREsPreferencePage_1); 69 } 70 71 74 public void init(IWorkbench workbench) { 75 } 76 77 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 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 String compliance = getCurrentCompilerCompliance(); 140 if(!supportsCurrentCompliance(install, compliance)) { 141 setMessage(MessageFormat.format(JREMessages.JREsPreferencePage_0, new String [] {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 160 public boolean isValid() { 161 String compliance = getCurrentCompilerCompliance(); 162 if(!supportsCurrentCompliance(getCurrentDefaultVM(), compliance)) { 163 setMessage(MessageFormat.format(JREMessages.JREsPreferencePage_0, new String [] {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 177 private void openCompliancePreferencePage() { 178 String compliancepage = "org.eclipse.jdt.ui.preferences.CompliancePreferencePage"; IWorkbenchPreferenceContainer wpc = (IWorkbenchPreferenceContainer)getContainer(); 180 if (wpc != null) { 181 wpc.openPage(compliancepage, null); 182 } else { 183 SWTFactory.showPreferencePage(compliancepage); 184 } 185 } 186 187 191 private String 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 ) JavaCore.getDefaultOptions().get(JavaCore.COMPILER_COMPLIANCE)); 196 } else { 197 return JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE); 198 } 199 200 } 201 202 209 private boolean supportsCurrentCompliance(IVMInstall vm, String compliance) { 210 if(vm instanceof AbstractVMInstall) { 211 AbstractVMInstall install = (AbstractVMInstall) vm; 212 String vmver = install.getJavaVersion(); 213 if(vmver == null) { 214 return true; 217 } 218 int val = compliance.compareTo(vmver); 219 return val < 0 || val == 0; 220 } 221 return false; 222 } 223 224 227 public boolean performOk() { 228 final boolean[] canceled = new boolean[] {false}; 229 BusyIndicator.showWhile(null, new Runnable () { 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 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 260 private void verifyDefaultVM(IVMInstall vm) { 261 if (vm != null) { 262 263 LibraryLocation[] locations= JavaRuntime.getLibraryLocations(vm); 265 boolean exist = true; 266 for (int i = 0; i < locations.length; i++) { 267 exist = exist && new File (locations[i].getSystemLibraryPath().toOSString()).exists(); 268 } 269 270 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)); return; 284 } 285 } else { 286 fJREBlock.setCheckedJRE(null); 287 } 288 } 289 290 private IVMInstall getCurrentDefaultVM() { 291 return fJREBlock.getCheckedJRE(); 292 } 293 } 294 | Popular Tags |