PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.jdt.debug.ui.source_3.6.1.v20110803_r371/org/eclipse/jdt/internal/debug/ui/jres/StandardVMPage.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 397 lines | 283 code | 27 blank | 87 comment | 52 complexity | b144ab765330e574a8983480ff1263fa MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2007, 2011 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. import java.io.File;
  13. import java.io.IOException;
  14. import java.net.URL;
  15. import org.eclipse.core.runtime.IPath;
  16. import org.eclipse.core.runtime.IStatus;
  17. import org.eclipse.core.runtime.Path;
  18. import org.eclipse.core.runtime.Status;
  19. import org.eclipse.debug.internal.ui.SWTFactory;
  20. import org.eclipse.debug.ui.StringVariableSelectionDialog;
  21. import org.eclipse.jdt.debug.ui.launchConfigurations.AbstractVMInstallPage;
  22. import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
  23. import org.eclipse.jdt.internal.debug.ui.JavaDebugImages;
  24. import org.eclipse.jdt.internal.debug.ui.StatusInfo;
  25. import org.eclipse.jdt.launching.AbstractVMInstallType;
  26. import org.eclipse.jdt.launching.IVMInstallType;
  27. import org.eclipse.jdt.launching.VMStandin;
  28. import org.eclipse.jface.dialogs.Dialog;
  29. import org.eclipse.jface.window.Window;
  30. import org.eclipse.swt.SWT;
  31. import org.eclipse.swt.custom.BusyIndicator;
  32. import org.eclipse.swt.events.ModifyEvent;
  33. import org.eclipse.swt.events.ModifyListener;
  34. import org.eclipse.swt.events.SelectionAdapter;
  35. import org.eclipse.swt.events.SelectionEvent;
  36. import org.eclipse.swt.events.SelectionListener;
  37. import org.eclipse.swt.graphics.Image;
  38. import org.eclipse.swt.layout.GridData;
  39. import org.eclipse.swt.layout.GridLayout;
  40. import org.eclipse.swt.widgets.Button;
  41. import org.eclipse.swt.widgets.Composite;
  42. import org.eclipse.swt.widgets.Control;
  43. import org.eclipse.swt.widgets.DirectoryDialog;
  44. import org.eclipse.swt.widgets.Text;
  45. import org.eclipse.ui.PlatformUI;
  46. /**
  47. * Page used to edit a standard VM.
  48. *
  49. * @since 3.3
  50. */
  51. public class StandardVMPage extends AbstractVMInstallPage {
  52. // VM being edited or created
  53. private VMStandin fVM;
  54. private Text fVMName;
  55. private Text fVMArgs;
  56. private Text fJRERoot;
  57. private VMLibraryBlock fLibraryBlock;
  58. // the VM install's javadoc location
  59. private URL fJavadocLocation = null;
  60. private boolean fAutoDetectAttributes = false;
  61. private IStatus[] fFieldStatus = new IStatus[1];
  62. /**
  63. *
  64. */
  65. public StandardVMPage() {
  66. super(JREMessages.StandardVMPage_0);
  67. for (int i = 0; i < fFieldStatus.length; i++) {
  68. fFieldStatus[i] = Status.OK_STATUS;
  69. }
  70. }
  71. /* (non-Javadoc)
  72. * @see org.eclipse.jface.dialogs.IDialogPage#getImage()
  73. */
  74. public Image getImage() {
  75. return JavaDebugImages.get(JavaDebugImages.IMG_WIZBAN_LIBRARY);
  76. }
  77. /* (non-Javadoc)
  78. * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
  79. */
  80. public void createControl(Composite p) {
  81. // create a composite with standard margins and spacing
  82. Composite composite = new Composite(p, SWT.NONE);
  83. GridLayout layout = new GridLayout();
  84. layout.numColumns = 3;
  85. composite.setLayout(layout);
  86. composite.setLayoutData(new GridData(GridData.FILL_BOTH));
  87. // VM location
  88. SWTFactory.createLabel(composite, JREMessages.addVMDialog_jreHome, 1);
  89. fJRERoot = SWTFactory.createSingleText(composite, 1);
  90. Button folders = SWTFactory.createPushButton(composite, JREMessages.AddVMDialog_22, null);
  91. GridData data = (GridData) folders.getLayoutData();
  92. data.horizontalAlignment = GridData.END;
  93. //VM name
  94. SWTFactory.createLabel(composite, JREMessages.addVMDialog_jreName, 1);
  95. fVMName = SWTFactory.createSingleText(composite, 2);
  96. //VM arguments
  97. SWTFactory.createLabel(composite, JREMessages.AddVMDialog_23, 1);
  98. fVMArgs = SWTFactory.createSingleText(composite, 1);
  99. Button variables = SWTFactory.createPushButton(composite, JREMessages.StandardVMPage_3, null);
  100. data = (GridData) variables.getLayoutData();
  101. data.horizontalAlignment = GridData.END;
  102. //VM libraries block
  103. SWTFactory.createLabel(composite, JREMessages.AddVMDialog_JRE_system_libraries__1, 3);
  104. fLibraryBlock = new VMLibraryBlock();
  105. fLibraryBlock.setWizard(getWizard());
  106. fLibraryBlock.createControl(composite);
  107. Control libControl = fLibraryBlock.getControl();
  108. GridData gd = new GridData(GridData.FILL_BOTH);
  109. gd.horizontalSpan = 3;
  110. libControl.setLayoutData(gd);
  111. //add the listeners now to prevent them from monkeying with initialized settings
  112. fVMName.addModifyListener(new ModifyListener() {
  113. public void modifyText(ModifyEvent e) {
  114. validateVMName();
  115. }
  116. });
  117. fJRERoot.addModifyListener(new ModifyListener() {
  118. public void modifyText(ModifyEvent e) {
  119. validateJRELocation();
  120. }
  121. });
  122. folders.addSelectionListener(new SelectionListener() {
  123. public void widgetDefaultSelected(SelectionEvent e) {}
  124. public void widgetSelected(SelectionEvent e) {
  125. DirectoryDialog dialog = new DirectoryDialog(getShell());
  126. File file = new File(fJRERoot.getText());
  127. String text = fJRERoot.getText();
  128. if (file.isFile()) {
  129. text = file.getParentFile().getAbsolutePath();
  130. }
  131. dialog.setFilterPath(text);
  132. dialog.setMessage(JREMessages.addVMDialog_pickJRERootDialog_message);
  133. String newPath = dialog.open();
  134. if (newPath != null) {
  135. fJRERoot.setText(newPath);
  136. }
  137. }
  138. });
  139. variables.addSelectionListener(new SelectionAdapter() {
  140. public void widgetSelected(SelectionEvent e) {
  141. StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(getShell());
  142. if (dialog.open() == Window.OK) {
  143. String expression = dialog.getVariableExpression();
  144. if (expression != null) {
  145. fVMArgs.insert(expression);
  146. }
  147. }
  148. }
  149. });
  150. Dialog.applyDialogFont(composite);
  151. setControl(composite);
  152. initializeFields();
  153. PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.EDIT_JRE_STD_VM_WIZARD_PAGE);
  154. }
  155. /**
  156. * Validates the JRE location
  157. * @return the status after validating the JRE location
  158. */
  159. private void validateJRELocation() {
  160. String locationName = fJRERoot.getText();
  161. IStatus s = null;
  162. File file = null;
  163. if (locationName.length() == 0) {
  164. s = new StatusInfo(IStatus.WARNING, JREMessages.addVMDialog_enterLocation);
  165. }
  166. else {
  167. file = new File(locationName);
  168. if (!file.exists()) {
  169. s = new StatusInfo(IStatus.ERROR, JREMessages.addVMDialog_locationNotExists);
  170. }
  171. else {
  172. final IStatus[] temp = new IStatus[1];
  173. final File tempFile = file;
  174. Runnable r = new Runnable() {
  175. public void run() {
  176. temp[0] = fVM.getVMInstallType().validateInstallLocation(tempFile);
  177. }
  178. };
  179. BusyIndicator.showWhile(getShell().getDisplay(), r);
  180. s = temp[0];
  181. }
  182. }
  183. if (file != null) {
  184. fVM.setInstallLocation(file);
  185. }
  186. if (s.isOK() && file != null) {
  187. String name = fVMName.getText();
  188. if (name == null || name.trim().length() == 0) {
  189. // auto-generate VM name
  190. if (file.isFile()) {
  191. String fileName = file.getName();
  192. int index = fileName.lastIndexOf(".ee"); //$NON-NLS-1$
  193. if (index > 0) {
  194. fileName = fileName.substring(0, index);
  195. }
  196. fVMName.setText(fileName);
  197. } else {
  198. try {
  199. String genName = null;
  200. IPath path = new Path(file.getCanonicalPath());
  201. int segs = path.segmentCount();
  202. if (segs == 1) {
  203. genName = path.segment(0);
  204. }
  205. else if (segs >= 2) {
  206. String last = path.lastSegment();
  207. if ("jre".equalsIgnoreCase(last)) { //$NON-NLS-1$
  208. genName = path.segment(segs - 2);
  209. }
  210. else {
  211. genName = last;
  212. }
  213. }
  214. if (genName != null) {
  215. fVMName.setText(genName);
  216. }
  217. } catch (IOException e) {}
  218. }
  219. }
  220. }
  221. detectJavadocLocation();
  222. setJRELocationStatus(s);
  223. fLibraryBlock.setSelection(fVM);
  224. updatePageStatus();
  225. }
  226. /**
  227. * Auto-detects the default javadoc location
  228. */
  229. private void detectJavadocLocation() {
  230. if (fAutoDetectAttributes) {
  231. IVMInstallType type = fVM.getVMInstallType();
  232. if (type instanceof AbstractVMInstallType) {
  233. AbstractVMInstallType atype = (AbstractVMInstallType)type;
  234. fJavadocLocation = atype.getDefaultJavadocLocation(getInstallLocation());
  235. String args = atype.getDefaultVMArguments(getInstallLocation());
  236. if (args != null) {
  237. fVMArgs.setText(args);
  238. }
  239. }
  240. } else {
  241. fJavadocLocation = fVM.getJavadocLocation();
  242. }
  243. }
  244. /**
  245. * Returns the installation location as a file from the JRE root text control
  246. * @return the installation location as a file
  247. */
  248. protected File getInstallLocation() {
  249. return new File(fJRERoot.getText());
  250. }
  251. /**
  252. * Validates the entered name of the VM
  253. * @return the status of the name validation
  254. */
  255. private void validateVMName() {
  256. nameChanged(fVMName.getText());
  257. }
  258. /* (non-Javadoc)
  259. * @see org.eclipse.jdt.debug.ui.launchConfigurations.AbstractVMInstallPage#finish()
  260. */
  261. public boolean finish() {
  262. setFieldValuesToVM(fVM);
  263. fLibraryBlock.finish();
  264. return true;
  265. }
  266. /* (non-Javadoc)
  267. * @see org.eclipse.jdt.debug.ui.launchConfigurations.AbstractVMInstallPage#getSelection()
  268. */
  269. public VMStandin getSelection() {
  270. return fVM;
  271. }
  272. /* (non-Javadoc)
  273. * @see org.eclipse.jdt.debug.ui.launchConfigurations.AbstractVMInstallPage#setSelection(org.eclipse.jdt.launching.VMStandin)
  274. */
  275. public void setSelection(VMStandin vm) {
  276. super.setSelection(vm);
  277. fVM = vm;
  278. fAutoDetectAttributes = vm.getJavadocLocation() == null;
  279. setTitle(JREMessages.StandardVMPage_1);
  280. setDescription(JREMessages.StandardVMPage_2);
  281. }
  282. /**
  283. * initialize fields to the specified VM
  284. * @param vm the VM to initialize from
  285. */
  286. protected void setFieldValuesToVM(VMStandin vm) {
  287. File dir = new File(fJRERoot.getText());
  288. File file = dir.getAbsoluteFile();
  289. vm.setInstallLocation(file);
  290. vm.setName(fVMName.getText());
  291. vm.setJavadocLocation(getURL());
  292. String argString = fVMArgs.getText().trim();
  293. if (argString != null && argString.length() > 0) {
  294. vm.setVMArgs(argString);
  295. }
  296. else {
  297. vm.setVMArgs(null);
  298. }
  299. }
  300. /**
  301. * Returns the URL for the javadoc location
  302. * @return the URL for the javadoc location
  303. */
  304. protected URL getURL() {
  305. return fJavadocLocation;
  306. }
  307. /**
  308. * Creates a unique name for the VMInstallType
  309. * @param vmType the vm install type
  310. * @return a unique name
  311. */
  312. protected static String createUniqueId(IVMInstallType vmType) {
  313. String id = null;
  314. do {
  315. id = String.valueOf(System.currentTimeMillis());
  316. } while (vmType.findVMInstall(id) != null);
  317. return id;
  318. }
  319. /**
  320. * Initialize the dialogs fields
  321. */
  322. private void initializeFields() {
  323. fLibraryBlock.setSelection(fVM);
  324. fVMName.setText(fVM.getName());
  325. File installLocation = fVM.getInstallLocation();
  326. if (installLocation != null) {
  327. fJRERoot.setText(installLocation.getAbsolutePath());
  328. }
  329. String vmArgs = fVM.getVMArgs();
  330. if (vmArgs != null) {
  331. fVMArgs.setText(vmArgs);
  332. }
  333. validateVMName();
  334. validateJRELocation();
  335. }
  336. /**
  337. * Sets the status of the JRE location field.
  338. *
  339. * @param status JRE location status
  340. */
  341. private void setJRELocationStatus(IStatus status) {
  342. fFieldStatus[0] = status;
  343. }
  344. /* (non-Javadoc)
  345. * @see org.eclipse.jface.dialogs.DialogPage#getErrorMessage()
  346. */
  347. public String getErrorMessage() {
  348. String message = super.getErrorMessage();
  349. if (message == null) {
  350. return fLibraryBlock.getErrorMessage();
  351. }
  352. return message;
  353. }
  354. /* (non-Javadoc)
  355. * @see org.eclipse.jface.wizard.WizardPage#isPageComplete()
  356. */
  357. public boolean isPageComplete() {
  358. boolean complete = super.isPageComplete();
  359. if (complete) {
  360. return fLibraryBlock.isPageComplete();
  361. }
  362. return complete;
  363. }
  364. /* (non-Javadoc)
  365. * @see org.eclipse.jdt.debug.ui.launchConfigurations.AbstractVMInstallPage#getVMStatus()
  366. */
  367. protected IStatus[] getVMStatus() {
  368. return fFieldStatus;
  369. }
  370. }