/as/plugins/org.jboss.ide.eclipse.as.ui/jbossui/org/jboss/ide/eclipse/as/ui/wizards/JBossRuntimeWizardFragment.java

https://github.com/adietish/jbosstools-server · Java · 1081 lines · 886 code · 142 blank · 53 comment · 145 complexity · 0858e158b3f575cba964dd3b555a15a7 MD5 · raw file

  1. /*******************************************************************************
  2. * Copyright (c) 2010 Red Hat, Inc.
  3. * Distributed under license by Red Hat, Inc. All rights reserved.
  4. * This program is made available under the terms of the
  5. * Eclipse Public License v1.0 which accompanies this distribution,
  6. * and is available at http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Red Hat, Inc. - initial API and implementation
  10. ******************************************************************************/
  11. package org.jboss.ide.eclipse.as.ui.wizards;
  12. import java.io.File;
  13. import java.util.Arrays;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import org.eclipse.core.runtime.CoreException;
  17. import org.eclipse.core.runtime.IPath;
  18. import org.eclipse.core.runtime.IProgressMonitor;
  19. import org.eclipse.core.runtime.IStatus;
  20. import org.eclipse.core.runtime.NullProgressMonitor;
  21. import org.eclipse.core.runtime.Path;
  22. import org.eclipse.core.runtime.Platform;
  23. import org.eclipse.core.runtime.Status;
  24. import org.eclipse.core.runtime.jobs.Job;
  25. import org.eclipse.core.runtime.preferences.IEclipsePreferences;
  26. import org.eclipse.core.runtime.preferences.InstanceScope;
  27. import org.eclipse.jdt.internal.debug.ui.jres.JREsPreferencePage;
  28. import org.eclipse.jdt.launching.IVMInstall;
  29. import org.eclipse.jface.dialogs.Dialog;
  30. import org.eclipse.jface.dialogs.IDialogConstants;
  31. import org.eclipse.jface.dialogs.IMessageProvider;
  32. import org.eclipse.jface.dialogs.MessageDialog;
  33. import org.eclipse.jface.dialogs.TitleAreaDialog;
  34. import org.eclipse.jface.preference.IPreferenceNode;
  35. import org.eclipse.jface.preference.PreferenceDialog;
  36. import org.eclipse.jface.preference.PreferenceManager;
  37. import org.eclipse.jface.resource.ImageDescriptor;
  38. import org.eclipse.jface.viewers.ISelectionChangedListener;
  39. import org.eclipse.jface.viewers.IStructuredSelection;
  40. import org.eclipse.jface.viewers.SelectionChangedEvent;
  41. import org.eclipse.jface.viewers.StructuredSelection;
  42. import org.eclipse.jface.window.Window;
  43. import org.eclipse.jface.wizard.IWizardPage;
  44. import org.eclipse.osgi.util.NLS;
  45. import org.eclipse.swt.SWT;
  46. import org.eclipse.swt.custom.BusyIndicator;
  47. import org.eclipse.swt.events.ModifyEvent;
  48. import org.eclipse.swt.events.ModifyListener;
  49. import org.eclipse.swt.events.SelectionAdapter;
  50. import org.eclipse.swt.events.SelectionEvent;
  51. import org.eclipse.swt.events.SelectionListener;
  52. import org.eclipse.swt.graphics.GC;
  53. import org.eclipse.swt.graphics.Point;
  54. import org.eclipse.swt.layout.FillLayout;
  55. import org.eclipse.swt.layout.FormAttachment;
  56. import org.eclipse.swt.layout.FormData;
  57. import org.eclipse.swt.layout.FormLayout;
  58. import org.eclipse.swt.layout.GridData;
  59. import org.eclipse.swt.widgets.Button;
  60. import org.eclipse.swt.widgets.Combo;
  61. import org.eclipse.swt.widgets.Composite;
  62. import org.eclipse.swt.widgets.Control;
  63. import org.eclipse.swt.widgets.DirectoryDialog;
  64. import org.eclipse.swt.widgets.Display;
  65. import org.eclipse.swt.widgets.Group;
  66. import org.eclipse.swt.widgets.Label;
  67. import org.eclipse.swt.widgets.Link;
  68. import org.eclipse.swt.widgets.Shell;
  69. import org.eclipse.swt.widgets.Text;
  70. import org.eclipse.ui.PlatformUI;
  71. import org.eclipse.ui.dialogs.PreferencesUtil;
  72. import org.eclipse.ui.internal.dialogs.WorkbenchPreferenceDialog;
  73. import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
  74. import org.eclipse.wst.server.core.IRuntime;
  75. import org.eclipse.wst.server.core.IRuntimeType;
  76. import org.eclipse.wst.server.core.IRuntimeWorkingCopy;
  77. import org.eclipse.wst.server.core.ServerCore;
  78. import org.eclipse.wst.server.core.TaskModel;
  79. import org.eclipse.wst.server.ui.wizard.IWizardHandle;
  80. import org.eclipse.wst.server.ui.wizard.WizardFragment;
  81. import org.jboss.ide.eclipse.as.core.server.IJBossServerRuntime;
  82. import org.jboss.ide.eclipse.as.core.server.bean.JBossServerType;
  83. import org.jboss.ide.eclipse.as.core.server.bean.ServerBeanLoader;
  84. import org.jboss.ide.eclipse.as.core.server.internal.AbstractLocalJBossServerRuntime;
  85. import org.jboss.ide.eclipse.as.core.server.internal.extendedproperties.JBossExtendedProperties;
  86. import org.jboss.ide.eclipse.as.core.util.FileUtil;
  87. import org.jboss.ide.eclipse.as.core.util.IConstants;
  88. import org.jboss.ide.eclipse.as.core.util.RuntimeUtils;
  89. import org.jboss.ide.eclipse.as.ui.IPreferenceKeys;
  90. import org.jboss.ide.eclipse.as.ui.JBossServerUIPlugin;
  91. import org.jboss.ide.eclipse.as.ui.JBossServerUISharedImages;
  92. import org.jboss.ide.eclipse.as.ui.Messages;
  93. import org.jboss.ide.eclipse.as.ui.UIUtil;
  94. import org.jboss.tools.as.runtimes.integration.util.DownloadRuntimeServerUtil;
  95. import org.jboss.tools.runtime.core.RuntimeCoreActivator;
  96. import org.jboss.tools.runtime.core.model.DownloadRuntime;
  97. import org.jboss.tools.runtime.core.model.IDownloadRuntimes;
  98. import org.jboss.tools.runtime.ui.download.DownloadRuntimes;
  99. import org.osgi.service.prefs.BackingStoreException;
  100. /**
  101. * @author Stryker
  102. */
  103. public class JBossRuntimeWizardFragment extends WizardFragment {
  104. protected IWizardHandle handle;
  105. protected boolean beenEntered = false;
  106. protected Label nameLabel, homeDirLabel,
  107. installedJRELabel, explanationLabel;
  108. protected Text nameText, homeDirText;
  109. protected Combo jreCombo;
  110. protected int jreComboIndex;
  111. protected Button homeDirButton, jreButton;
  112. protected Composite nameComposite, homeDirComposite, jreComposite;
  113. protected Link downloadAndInstallButton;
  114. protected Composite downloadAndInstallButtonWrapper;
  115. protected String name, homeDir;
  116. // Configuration stuff
  117. protected Composite configComposite;
  118. protected Group configGroup;
  119. protected Label configDirLabel;
  120. protected Text configDirText;
  121. protected JBossConfigurationTableViewer configurations;
  122. protected Button configCopy, configBrowse, configDelete;
  123. protected String configDirTextVal;
  124. // jre fields
  125. protected List<IVMInstall> installedJREs;
  126. protected String[] jreNames;
  127. protected int defaultVMIndex = -1;
  128. protected IVMInstall selectedVM;
  129. protected String originalName;
  130. public Composite createComposite(Composite parent, IWizardHandle handle) {
  131. /*
  132. * Any state should be CLEARED right now or in enclosed methods and loaded from the model.
  133. * WTP creates only one instance of this wizard fragment for the entire
  134. * life of the workspace.
  135. */
  136. this.handle = handle;
  137. Composite main = new Composite(parent, SWT.NONE);
  138. main.setLayout(new FormLayout());
  139. updateModels();
  140. createWidgets(main);
  141. fillWidgets();
  142. updateWizardHandle(parent);
  143. fireInitialWidgetUpdates();
  144. return main;
  145. }
  146. /*
  147. * This will update the 'download' hyperlink to be enabled
  148. * or disabled depending on if this runtime type has runtimes
  149. * for download or not.
  150. */
  151. private void fireInitialWidgetUpdates() {
  152. new Job("Update Download Runtimes Hyperlink") {
  153. protected IStatus run(IProgressMonitor monitor) {
  154. final DownloadRuntime[] downloads = DownloadRuntimeServerUtil.getDownloadRuntimes(getRuntimeType());
  155. Display.getDefault().asyncExec(new Runnable() {
  156. public void run() {
  157. if( downloads != null && downloads.length > 0 ) {
  158. try {
  159. if( !downloadAndInstallButton.isDisposed())
  160. downloadAndInstallButton.setEnabled(true);
  161. if( !downloadAndInstallButtonWrapper.isDisposed())
  162. downloadAndInstallButtonWrapper.setToolTipText(null);
  163. } catch(Throwable t) {
  164. t.printStackTrace();
  165. }
  166. } else {
  167. if( !downloadAndInstallButtonWrapper.isDisposed())
  168. downloadAndInstallButtonWrapper.setToolTipText(Messages.rwf_downloadTooltipEmpty);
  169. }
  170. }
  171. });
  172. return Status.OK_STATUS;
  173. }
  174. }.schedule();
  175. }
  176. protected void updateModels() {
  177. // clean state from last time this fragment was used
  178. // (fragments apparently do not have new instances constructed. ugh)
  179. homeDir = null;
  180. updateJREs();
  181. }
  182. protected IRuntime getRuntimeFromTaskModel() {
  183. IRuntime r = (IRuntime) getTaskModel().getObject(TaskModel.TASK_RUNTIME);
  184. if( r == null ) {
  185. r = (IRuntime) getTaskModel().getObject(ServerProfileWizardFragment.TASK_CUSTOM_RUNTIME);
  186. }
  187. return r;
  188. }
  189. protected void updateWizardHandle(Composite parent) {
  190. // make modifications to parent
  191. IRuntime r = getRuntimeFromTaskModel();
  192. handle.setTitle( Messages.rwf_JBossRuntime);
  193. String descript = r.getRuntimeType().getDescription();
  194. handle.setDescription(descript);
  195. handle.setImageDescriptor(getImageDescriptor());
  196. PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, "org.jboss.ide.eclipse.as.doc.user.new_server_runtime"); //$NON-NLS-1$
  197. }
  198. protected void createWidgets(Composite main) {
  199. createExplanation(main);
  200. createNameComposite(main);
  201. createHomeComposite(main);
  202. createJREComposite(main);
  203. createConfigurationComposite(main);
  204. }
  205. protected boolean isEAP() {
  206. IRuntime rt = getRuntimeFromTaskModel();
  207. return RuntimeUtils.isEAP(rt);
  208. }
  209. protected ImageDescriptor getImageDescriptor() {
  210. String imageKey = JBossServerUISharedImages.WIZBAN_JBOSS_LOGO;
  211. return JBossServerUISharedImages.getImageDescriptor(imageKey);
  212. }
  213. protected void fillNameWidgets(IRuntime rt) {
  214. originalName = rt.getName();
  215. nameText.setText(originalName);
  216. name = originalName;
  217. }
  218. protected void fillHomeDir(IRuntime rt) {
  219. if( rt.getLocation() == null ) {
  220. // new runtime creation
  221. IEclipsePreferences prefs2 = InstanceScope.INSTANCE.getNode(JBossServerUIPlugin.PLUGIN_ID);
  222. String value = prefs2.get(IPreferenceKeys.RUNTIME_HOME_PREF_KEY_PREFIX + rt.getRuntimeType().getId(), null);
  223. homeDir = (value != null && value.length() != 0) ? value : "";
  224. } else {
  225. // old runtime, load from it
  226. homeDir = rt.getLocation().toOSString();
  227. }
  228. homeDirText.setText(homeDir);
  229. boolean isWC = rt instanceof IRuntimeWorkingCopy;
  230. if( isWC ) {
  231. ((IRuntimeWorkingCopy)rt).setLocation(new Path(homeDir));
  232. }
  233. homeDirText.setEnabled(isWC);
  234. homeDirButton.setEnabled(isWC);
  235. }
  236. protected void fillConfigWidgets(IRuntime rt) {
  237. IJBossServerRuntime jbsrt = getRuntime();
  238. String dirText = jbsrt.getConfigLocation();
  239. configDirText.setText(isEmpty(dirText) ? IConstants.SERVER : dirText);
  240. configurations.setConfiguration(isEmpty(jbsrt.getJBossConfiguration())
  241. ? IConstants.DEFAULT_CONFIGURATION : jbsrt.getJBossConfiguration());
  242. configurations.getTable().setVisible(true);
  243. }
  244. protected boolean isEmpty(String s) {
  245. return s == null || s.length() == 0;
  246. }
  247. protected void fillJREWidgets(IRuntime rt) {
  248. IJBossServerRuntime jbsrt = getRuntime();
  249. if (jbsrt.isUsingDefaultJRE()) {
  250. jreCombo.select(0);
  251. } else {
  252. IVMInstall install = jbsrt.getHardVM();
  253. if( install != null ) {
  254. selectedVM = install;
  255. String vmName = install.getName();
  256. String[] jres = jreCombo.getItems();
  257. for (int i = 0; i < jres.length; i++) {
  258. if (vmName.equals(jres[i]))
  259. jreCombo.select(i);
  260. }
  261. }
  262. }
  263. jreComboIndex = jreCombo.getSelectionIndex();
  264. if( jreCombo.getSelectionIndex() < 0 && jreCombo.getItemCount() > 0)
  265. jreCombo.select(0);
  266. boolean isWC = rt instanceof IRuntimeWorkingCopy;
  267. jreCombo.setEnabled(isWC);
  268. jreButton.setEnabled(isWC);
  269. }
  270. protected void fillWidgets() {
  271. IRuntime rt = getRuntimeFromTaskModel();
  272. if (rt != null) {
  273. fillNameWidgets(rt);
  274. fillHomeDir(rt);
  275. fillConfigWidgets(rt);
  276. fillJREWidgets(rt);
  277. }
  278. }
  279. protected IJBossServerRuntime getRuntime() {
  280. IRuntime r = getRuntimeFromTaskModel();
  281. IJBossServerRuntime ajbsrt = null;
  282. if (r != null) {
  283. ajbsrt = (IJBossServerRuntime) r
  284. .loadAdapter(IJBossServerRuntime.class,
  285. new NullProgressMonitor());
  286. }
  287. return ajbsrt;
  288. }
  289. protected void createExplanation(Composite main) {
  290. explanationLabel = new Label(main, SWT.WRAP);
  291. FormData data = new FormData();
  292. data.top = new FormAttachment(0, 5);
  293. data.left = new FormAttachment(0, 5);
  294. data.right = new FormAttachment(100, -5);
  295. explanationLabel.setLayoutData(data);
  296. explanationLabel.setText(Messages.rwf_Explanation);
  297. }
  298. protected void createNameComposite(Composite main) {
  299. // Create our name composite
  300. nameComposite = new Composite(main, SWT.NONE);
  301. FormData cData = new FormData();
  302. cData.left = new FormAttachment(0, 5);
  303. cData.right = new FormAttachment(100, -5);
  304. cData.top = new FormAttachment(explanationLabel, 10);
  305. nameComposite.setLayoutData(cData);
  306. nameComposite.setLayout(new FormLayout());
  307. // create internal widgets
  308. nameLabel = new Label(nameComposite, SWT.NONE);
  309. nameLabel.setText(Messages.wf_NameLabel);
  310. nameText = new Text(nameComposite, SWT.BORDER);
  311. nameText.addModifyListener(new ModifyListener() {
  312. public void modifyText(ModifyEvent e) {
  313. name = nameText.getText();
  314. updatePage();
  315. }
  316. });
  317. // organize widgets inside composite
  318. FormData nameLabelData = new FormData();
  319. nameLabelData.left = new FormAttachment(0, 0);
  320. nameLabel.setLayoutData(nameLabelData);
  321. FormData nameTextData = new FormData();
  322. nameTextData.left = new FormAttachment(0, 5);
  323. nameTextData.right = new FormAttachment(100, -5);
  324. nameTextData.top = new FormAttachment(nameLabel, 5);
  325. nameText.setLayoutData(nameTextData);
  326. }
  327. protected void createHomeComposite(Composite main) {
  328. // Create our composite
  329. homeDirComposite = new Composite(main, SWT.NONE);
  330. FormData cData = new FormData();
  331. cData.left = new FormAttachment(0, 5);
  332. cData.right = new FormAttachment(100, -5);
  333. cData.top = new FormAttachment(nameComposite, 10);
  334. homeDirComposite.setLayoutData(cData);
  335. homeDirComposite.setLayout(new FormLayout());
  336. // Create Internal Widgets
  337. homeDirLabel = new Label(homeDirComposite, SWT.NONE);
  338. homeDirLabel.setText(Messages.wf_HomeDirLabel);
  339. homeDirText = new Text(homeDirComposite, SWT.BORDER);
  340. homeDirButton = new Button(homeDirComposite, SWT.NONE);
  341. homeDirButton.setText(Messages.browse);
  342. downloadAndInstallButtonWrapper = new Composite(homeDirComposite, SWT.NONE);
  343. downloadAndInstallButtonWrapper.setLayout(new FillLayout());
  344. downloadAndInstallButton = new Link(downloadAndInstallButtonWrapper, SWT.NONE);
  345. downloadAndInstallButton.setText("<a href=\"\">" + Messages.rwf_DownloadRuntime + "</a>");
  346. downloadAndInstallButton.addSelectionListener(new DownloadAndInstallListener());
  347. downloadAndInstallButton.setEnabled(false);
  348. downloadAndInstallButtonWrapper.setToolTipText(Messages.rwf_downloadTooltipLoading);
  349. // Add listeners
  350. homeDirText.addModifyListener(new ModifyListener() {
  351. public void modifyText(ModifyEvent e) {
  352. homeDir = homeDirText.getText();
  353. updatePage();
  354. }
  355. });
  356. homeDirButton.addSelectionListener(new SelectionListener() {
  357. public void widgetDefaultSelected(SelectionEvent e) {
  358. browseHomeDirClicked();
  359. }
  360. public void widgetSelected(SelectionEvent e) {
  361. browseHomeDirClicked();
  362. }
  363. });
  364. // Set Layout Data
  365. homeDirLabel.setLayoutData(UIUtil.createFormData2(null,0,homeDirText,-5,0,5,null,0));
  366. homeDirText.setLayoutData(UIUtil.createFormData2(homeDirLabel,5,null,0,0,5,homeDirButton,-5));
  367. homeDirButton.setLayoutData(UIUtil.createFormData2(homeDirLabel,5,null,0,null,0,100,0));
  368. downloadAndInstallButtonWrapper.setLayoutData(UIUtil.createFormData2(0,0,homeDirButton,-5,null,0,100,-10));
  369. }
  370. protected class DownloadAndInstallListener extends SelectionAdapter {
  371. public void widgetSelected(SelectionEvent se) {
  372. IDownloadRuntimes downloader = RuntimeCoreActivator.getDefault().getDownloader();
  373. if( downloader != null ) {
  374. HashMap<String, Object> data = new HashMap<String, Object>();
  375. data.put(DownloadRuntimes.SHELL, downloadAndInstallButton.getShell() );
  376. IRuntimeType type = getRuntimeType();
  377. data.put(IDownloadRuntimes.RUNTIME_FILTER, new JBossASDownloadRuntimeFilter(type));
  378. downloader.execute(data);
  379. Boolean launched = (Boolean)data.get(IDownloadRuntimes.DOWNLOAD_LAUNCHED);
  380. if( launched != null && launched.booleanValue()) {
  381. ((IWizardPage)handle).getWizard().performCancel();
  382. ((IWizardPage)handle).getWizard().getContainer().getShell().close();
  383. }
  384. }
  385. }
  386. }
  387. protected void createJREComposite(Composite main) {
  388. // Create our composite
  389. jreComposite = new Composite(main, SWT.NONE);
  390. FormData cData = new FormData();
  391. cData.left = new FormAttachment(0, 5);
  392. cData.right = new FormAttachment(100, -5);
  393. cData.top = new FormAttachment(homeDirComposite, 10);
  394. jreComposite.setLayoutData(cData);
  395. jreComposite.setLayout(new FormLayout());
  396. // Create Internal Widgets
  397. installedJRELabel = new Label(jreComposite, SWT.NONE);
  398. installedJRELabel.setText(Messages.wf_JRELabel);
  399. jreCombo = new Combo(jreComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
  400. jreCombo.setItems(jreNames);
  401. if( defaultVMIndex != -1 )
  402. jreCombo.select(defaultVMIndex);
  403. jreButton = new Button(jreComposite, SWT.NONE);
  404. jreButton.setText(Messages.wf_JRELabel);
  405. // Add action listeners
  406. jreButton.addSelectionListener(new SelectionAdapter() {
  407. public void widgetSelected(SelectionEvent e) {
  408. String currentVM = jreCombo.getText();
  409. if (showPreferencePage()) {
  410. updateJREs();
  411. jreCombo.setItems(jreNames);
  412. jreCombo.setText(currentVM);
  413. if (jreCombo.getSelectionIndex() == -1)
  414. jreCombo.select(defaultVMIndex);
  415. jreComboIndex = jreCombo.getSelectionIndex();
  416. updateErrorMessage();
  417. }
  418. }
  419. });
  420. jreCombo.addSelectionListener(new SelectionListener() {
  421. public void widgetDefaultSelected(SelectionEvent e) {
  422. updatePage();
  423. }
  424. public void widgetSelected(SelectionEvent e) {
  425. updatePage();
  426. }
  427. });
  428. // Set Layout Data
  429. FormData labelData = new FormData();
  430. FormData comboData = new FormData();
  431. FormData buttonData = new FormData();
  432. labelData.left = new FormAttachment(0, 0);
  433. installedJRELabel.setLayoutData(labelData);
  434. comboData.left = new FormAttachment(0, 5);
  435. comboData.right = new FormAttachment(jreButton, -5);
  436. comboData.top = new FormAttachment(installedJRELabel, 5);
  437. jreCombo.setLayoutData(comboData);
  438. buttonData.top = new FormAttachment(installedJRELabel, 5);
  439. buttonData.right = new FormAttachment(100, 0);
  440. jreButton.setLayoutData(buttonData);
  441. }
  442. protected void createConfigurationComposite(Composite main) {
  443. UIUtil u = new UIUtil(); // top bottom left right
  444. configComposite = new Composite(main, SWT.NONE);
  445. configComposite.setLayoutData(u.createFormData(
  446. jreComposite, 10, 100, -5, 0, 5, 100, -5));
  447. configComposite.setLayout(new FormLayout());
  448. configGroup = new Group(configComposite, SWT.DEFAULT);
  449. configGroup.setText(Messages.wf_ConfigLabel);
  450. configGroup.setLayoutData(u.createFormData(
  451. 0, 0, 100, 0, 0, 0, 100, 0));
  452. configGroup.setLayout(new FormLayout());
  453. configDirLabel = new Label(configGroup, SWT.NONE);
  454. configDirLabel.setText(Messages.directory);
  455. configDirText = new Text(configGroup, SWT.BORDER);
  456. configurations = new JBossConfigurationTableViewer(configGroup,
  457. SWT.BORDER | SWT.SINGLE);
  458. IRuntime r = getRuntimeFromTaskModel();
  459. IRuntimeWorkingCopy runtimeWC = r.isWorkingCopy() ? ((IRuntimeWorkingCopy) r)
  460. : r.createWorkingCopy();
  461. IJBossServerRuntime srt = (IJBossServerRuntime) runtimeWC.loadAdapter(
  462. IJBossServerRuntime.class, new NullProgressMonitor());
  463. if( srt != null && !isEmpty(srt.getJBossConfiguration()))
  464. configurations.setConfiguration(srt.getJBossConfiguration());
  465. configBrowse = new Button(configGroup, SWT.DEFAULT);
  466. configCopy = new Button(configGroup, SWT.DEFAULT);
  467. configDelete = new Button(configGroup, SWT.DEFAULT);
  468. configBrowse.setText(Messages.browse);
  469. configCopy.setText(Messages.copy);
  470. configDelete.setText(Messages.delete);
  471. // Organize them
  472. configDirLabel.setLayoutData(u.createFormData(
  473. 2, 5, null, 0, 0, 5, null, 0));
  474. configDirText.setLayoutData(u.createFormData(
  475. 0, 5, null, 0, configDirLabel, 5, configBrowse, -5));
  476. configBrowse.setLayoutData(u.createFormData(
  477. 0, 5, null, 0, configurations.getTable(), 5, 100, -5));
  478. configurations.getTable().setLayoutData(u.createFormData(
  479. configDirText, 5, 100,-5, 0,5, 80, 0));
  480. configCopy.setLayoutData(u.createFormData(
  481. configBrowse, 5, null, 0, configurations.getTable(), 5, 100, -5));
  482. configDelete.setLayoutData(u.createFormData(
  483. configCopy, 5, null, 0, configurations.getTable(), 5, 100, -5));
  484. configDirText.addModifyListener(new ModifyListener() {
  485. public void modifyText(ModifyEvent e) {
  486. updatePage();
  487. }
  488. });
  489. configBrowse.addSelectionListener(new SelectionListener(){
  490. public void widgetSelected(SelectionEvent e) {
  491. configBrowsePressed();
  492. }
  493. public void widgetDefaultSelected(SelectionEvent e) {
  494. }
  495. });
  496. configCopy.addSelectionListener(new SelectionListener(){
  497. public void widgetSelected(SelectionEvent e) {
  498. configCopyPressed();
  499. }
  500. public void widgetDefaultSelected(SelectionEvent e) {
  501. }
  502. });
  503. configDelete.addSelectionListener(new SelectionListener(){
  504. public void widgetSelected(SelectionEvent e) {
  505. configDeletePressed();
  506. }
  507. public void widgetDefaultSelected(SelectionEvent e) {
  508. }
  509. });
  510. configurations.addSelectionChangedListener(new ISelectionChangedListener(){
  511. public void selectionChanged(SelectionChangedEvent event) {
  512. updateErrorMessage();
  513. configDelete.setEnabled(!((IStructuredSelection)configurations.getSelection()).isEmpty());
  514. configCopy.setEnabled(!((IStructuredSelection)configurations.getSelection()).isEmpty());
  515. }
  516. });
  517. }
  518. protected void configBrowsePressed() {
  519. String folder = new Path(configDirText.getText()).isAbsolute() ?
  520. configDirText.getText() : new Path(homeDir).append(configDirText.getText()).toString();
  521. File file = new File(folder);
  522. if (!file.exists()) {
  523. file = null;
  524. }
  525. File directory = getDirectory(file, homeDirComposite.getShell());
  526. if (directory != null) {
  527. if(directory.getAbsolutePath().startsWith(new Path(homeDir).toString())) {
  528. String result = directory.getAbsolutePath().substring(homeDir.length());
  529. configDirText.setText(new Path(result).makeRelative().toString());
  530. } else {
  531. configDirText.setText(directory.getAbsolutePath());
  532. }
  533. }
  534. }
  535. protected void configCopyPressed() {
  536. CopyConfigurationDialog d = new CopyConfigurationDialog(configCopy.getShell(), homeDir, configDirText.getText(), configurations.getCurrentlySelectedConfiguration());
  537. if(d.open() == 0 ) {
  538. IPath source = new Path(configDirText.getText());
  539. if( !source.isAbsolute())
  540. source = new Path(homeDir).append(source);
  541. source = source.append(configurations.getCurrentlySelectedConfiguration());
  542. IPath dest = new Path(d.getNewDest());
  543. if( !dest.isAbsolute())
  544. dest = new Path(homeDir).append(dest);
  545. dest = dest.append(d.getNewConfig());
  546. dest.toFile().mkdirs();
  547. FileUtil.copyDir(source.toFile(), dest.toFile());
  548. configDirText.setText(d.getNewDest());
  549. configurations.setSelection(new StructuredSelection(d.getNewConfig()));
  550. }
  551. }
  552. protected void configDeletePressed() {
  553. MessageDialog dialog = new MessageDialog(configBrowse.getShell(),
  554. Messages.JBossRuntimeWizardFragment_DeleteConfigTitle, null,
  555. Messages.JBossRuntimeWizardFragment_DeleteConfigConfirmation,
  556. MessageDialog.WARNING, new String[] { IDialogConstants.YES_LABEL,
  557. IDialogConstants.NO_LABEL }, 0); // yes is the default
  558. if(dialog.open() == 0) {
  559. String config = configurations.getCurrentlySelectedConfiguration();
  560. String configDir = configDirText.getText();
  561. File folder;
  562. if( !new Path(configDir).isAbsolute())
  563. folder = new Path(homeDir).append(configDir).append(config).toFile();
  564. else
  565. folder = new Path(configDir).append(config).toFile();
  566. FileUtil.completeDelete(folder);
  567. configurations.refresh();
  568. updatePage();
  569. }
  570. }
  571. // Launchable only from UI thread
  572. protected void updatePage() {
  573. String folder;
  574. if (!isHomeValid()) {
  575. configurations.getControl().setEnabled(false);
  576. folder = homeDirText.getText();
  577. } else {
  578. IPath p = new Path(configDirText.getText());
  579. if( p.isAbsolute())
  580. folder = p.toString();
  581. else
  582. folder = new Path(homeDirText.getText()).append(p).toString();
  583. }
  584. configurations.setFolder(folder);
  585. File f = new File(folder);
  586. configurations.getControl().setEnabled(f.exists() && f.isDirectory());
  587. jreComboIndex = jreCombo.getSelectionIndex();
  588. int offset = -1;
  589. if( jreComboIndex + offset >= 0 )
  590. selectedVM = installedJREs.get(jreComboIndex + offset);
  591. else // if sel < 0 or sel == 0 and offset == -1
  592. selectedVM = null;
  593. configDirTextVal = configDirText.getText();
  594. updateErrorMessage();
  595. saveDetailsInRuntime();
  596. }
  597. protected void updateErrorMessage() {
  598. if( !beenEntered)
  599. return;
  600. String error = getErrorString();
  601. if (error == null) {
  602. String warn = getWarningString();
  603. if( warn != null )
  604. handle.setMessage(warn, IMessageProvider.WARNING);
  605. else
  606. handle.setMessage(null, IMessageProvider.NONE);
  607. } else
  608. handle.setMessage(error, IMessageProvider.ERROR);
  609. }
  610. protected String getErrorString() {
  611. if (nameText == null) {
  612. // not yet initialized. no errors
  613. return null;
  614. }
  615. if (getRuntime(name) != null) {
  616. return Messages.rwf_NameInUse;
  617. }
  618. if( getValidJREs().size() == 0 ) {
  619. String error = NLS.bind(Messages.rwf_noValidJRE, getRuntime().getExecutionEnvironment().getId());
  620. return error;
  621. }
  622. if (!isHomeValid())
  623. return NLS.bind(Messages.rwf_homeMissingFiles, getSystemJarPath());
  624. if (name == null || name.equals("")) //$NON-NLS-1$
  625. return Messages.rwf_nameTextBlank;
  626. if (jreComboIndex < 0)
  627. return Messages.rwf_NoVMSelected;
  628. if( configurations.getSelection().isEmpty())
  629. return Messages.JBossRuntimeWizardFragment_MustSelectValidConfig;
  630. return null;
  631. }
  632. protected String getWarningString() {
  633. if( getHomeVersionWarning() != null )
  634. return getHomeVersionWarning();
  635. return null;
  636. }
  637. protected boolean isHomeValid() {
  638. if( homeDir == null || homeDir.length() == 1 || !(new File(homeDir).exists())) return false; //$NON-NLS-1$
  639. return new Path(homeDir).append("bin").append("run.jar").toFile().exists(); //$NON-NLS-1$ //$NON-NLS-2$
  640. }
  641. protected String getVersionString(File loc) {
  642. String version = new ServerBeanLoader(loc).getFullServerVersion();
  643. return version == null ? "UNKNOWN" : version;
  644. }
  645. protected String getSystemJarPath() {
  646. return JBossServerType.AS.getSystemJarPath();
  647. }
  648. protected String getHomeVersionWarning() {
  649. File loc = new File(homeDir);
  650. String serverId = new ServerBeanLoader(loc).getServerAdapterId();
  651. String rtId = serverId == null ? null :
  652. ServerCore.findServerType(serverId).getRuntimeType().getId();
  653. IRuntime adapterRt = getRuntimeFromTaskModel();
  654. String adapterRuntimeId = adapterRt.getRuntimeType().getId();
  655. if( !adapterRuntimeId.equals(rtId)) {
  656. return NLS.bind(Messages.rwf_homeIncorrectVersionError,
  657. adapterRt.getRuntimeType().getVersion(),
  658. getVersionString(loc));
  659. }
  660. return null;
  661. }
  662. protected void browseHomeDirClicked() {
  663. File file = homeDir == null ? null : new File(homeDir);
  664. if (file != null && !file.exists()) {
  665. file = null;
  666. }
  667. File directory = getDirectory(file, homeDirComposite.getShell());
  668. if (directory != null) {
  669. homeDir = directory.getAbsolutePath();
  670. homeDirText.setText(homeDir);
  671. }
  672. }
  673. protected static File getDirectory(File startingDirectory, Shell shell) {
  674. DirectoryDialog fileDialog = new DirectoryDialog(shell, SWT.OPEN);
  675. if (startingDirectory != null) {
  676. fileDialog.setFilterPath(startingDirectory.getPath());
  677. }
  678. String dir = fileDialog.open();
  679. if (dir != null) {
  680. dir = dir.trim();
  681. if (dir.length() > 0) {
  682. return new File(dir);
  683. }
  684. }
  685. return null;
  686. }
  687. // Other
  688. protected boolean showPreferencePage() {
  689. PreferenceManager manager = PlatformUI.getWorkbench()
  690. .getPreferenceManager();
  691. IPreferenceNode node = manager
  692. .find("org.eclipse.jdt.ui.preferences.JavaBasePreferencePage") //$NON-NLS-1$
  693. .findSubNode(
  694. "org.eclipse.jdt.debug.ui.preferences.VMPreferencePage"); //$NON-NLS-1$
  695. PreferenceManager manager2 = new PreferenceManager();
  696. manager2.addToRoot(node);
  697. final PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(
  698. jreButton.getShell(),
  699. JREsPreferencePage.ID,
  700. new String[] {},
  701. null);
  702. final boolean[] result = new boolean[] { false };
  703. BusyIndicator.showWhile(jreButton.getDisplay(), new Runnable() {
  704. public void run() {
  705. if (dialog.open() == Window.OK)
  706. result[0] = true;
  707. }
  708. });
  709. return result[0];
  710. }
  711. // JRE methods
  712. protected void updateJREs() {
  713. defaultVMIndex = 0;
  714. // get all valid JVMs
  715. IVMInstall runtimesInstall = getRuntime().getHardVM();
  716. installedJREs = getValidJREs();
  717. // get names
  718. int size = installedJREs.size();
  719. int index = 0;
  720. jreNames = new String[size+1];
  721. jreNames[index++] = NLS.bind(Messages.rwf_DefaultJREForExecEnv, getRuntime().getExecutionEnvironment().getId());
  722. for (int i = 0; i < installedJREs.size(); i++) {
  723. IVMInstall vmInstall = installedJREs.get(i);
  724. if( vmInstall.equals(runtimesInstall)) {
  725. defaultVMIndex = index;
  726. }
  727. jreNames[index++] = vmInstall.getName();
  728. }
  729. }
  730. protected List<IVMInstall> getValidJREs() {
  731. IRuntime r = getRuntimeFromTaskModel();
  732. AbstractLocalJBossServerRuntime jbsrt = (AbstractLocalJBossServerRuntime)r.loadAdapter(AbstractLocalJBossServerRuntime.class, null);
  733. return Arrays.asList(jbsrt.getValidJREs(getRuntimeType()));
  734. }
  735. // WST API methods
  736. public void enter() {
  737. beenEntered = true;
  738. }
  739. public void exit() {
  740. saveDetailsInRuntime();
  741. }
  742. protected void saveDetailsInRuntime() {
  743. IRuntime r = getRuntimeFromTaskModel();
  744. IRuntimeWorkingCopy runtimeWC = r.isWorkingCopy() ? ((IRuntimeWorkingCopy) r)
  745. : r.createWorkingCopy();
  746. saveBasicDetailsInRuntime(runtimeWC);
  747. saveConfigurationDetailsInRuntime(runtimeWC);
  748. //getTaskModel().putObject(TaskModel.TASK_RUNTIME, runtimeWC); // unnecessary
  749. }
  750. protected void saveBasicDetailsInRuntime(IRuntimeWorkingCopy runtimeWC) {
  751. IJBossServerRuntime srt = (IJBossServerRuntime) runtimeWC.loadAdapter(
  752. IJBossServerRuntime.class, new NullProgressMonitor());
  753. if( name != null )
  754. runtimeWC.setName(name);
  755. if( homeDir != null)
  756. runtimeWC.setLocation(new Path(homeDir));
  757. if( selectedVM != null )
  758. srt.setVM(selectedVM);
  759. else
  760. srt.setVM(null);
  761. }
  762. protected void saveConfigurationDetailsInRuntime(IRuntimeWorkingCopy wc) {
  763. IJBossServerRuntime srt = (IJBossServerRuntime) wc.loadAdapter(
  764. IJBossServerRuntime.class, new NullProgressMonitor());
  765. if( configurations != null && configurations.getSelectedConfiguration() != null )
  766. srt.setJBossConfiguration(configurations.getSelectedConfiguration());
  767. if( configDirText != null )
  768. srt.setConfigLocation(configDirTextVal);
  769. }
  770. public void performFinish(IProgressMonitor monitor) throws CoreException {
  771. exit();
  772. IRuntime rt = getRuntimeFromTaskModel();
  773. if( rt instanceof IRuntimeWorkingCopy ) {
  774. IRuntimeWorkingCopy r = (IRuntimeWorkingCopy) rt;
  775. IRuntime saved = r.save(false, new NullProgressMonitor());
  776. getTaskModel().putObject(TaskModel.TASK_RUNTIME, saved);
  777. saveRuntimeLocationInPreferences(saved);
  778. }
  779. }
  780. protected void saveRuntimeLocationInPreferences(IRuntime runtime) {
  781. IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(JBossServerUIPlugin.PLUGIN_ID);
  782. prefs.put(IPreferenceKeys.RUNTIME_HOME_PREF_KEY_PREFIX + runtime.getRuntimeType().getId(), homeDir);
  783. try {
  784. prefs.flush();
  785. } catch(BackingStoreException e) {
  786. // TODO when adding tracing. This is not important enough for an error log entry
  787. }
  788. }
  789. public boolean isComplete() {
  790. return beenEntered && (getErrorString() == null ? true : false);
  791. }
  792. public boolean hasComposite() {
  793. return true;
  794. }
  795. protected IRuntime getRuntime(String runtimeName) {
  796. if (runtimeName.equals(originalName))
  797. return null; // name is same as original. No clash.
  798. IRuntime[] runtimes = ServerCore.getRuntimes();
  799. for (int i = 0; i < runtimes.length; i++) {
  800. if (runtimes[i].getName().equals(runtimeName))
  801. return runtimes[i];
  802. }
  803. return null;
  804. }
  805. public static class CopyConfigurationDialog extends TitleAreaDialog {
  806. private String origHome, origDest, origConfig;
  807. private String newDest, newConfig;
  808. private Text destText;
  809. protected CopyConfigurationDialog(Shell parentShell, String home,
  810. String dir, String config) {
  811. super(new Shell(parentShell));
  812. origHome = home;
  813. origDest = dir;
  814. origConfig = config;
  815. }
  816. protected Control createDialogArea(Composite parent) {
  817. Composite c = (Composite) super.createDialogArea(parent);
  818. Composite main = new Composite(c, SWT.NONE);
  819. main.setLayoutData(new GridData(GridData.FILL_BOTH));
  820. main.setLayout(new FormLayout());
  821. setCleanMessage();
  822. Label nameLabel = new Label(main, SWT.NONE);
  823. nameLabel.setText(Messages.wf_NameLabel);
  824. final Text nameText = new Text(main, SWT.BORDER);
  825. Label destLabel = new Label(main, SWT.NONE);
  826. destLabel.setText(Messages.rwf_DestinationLabel);
  827. destText = new Text(main, SWT.BORDER);
  828. Button browse = new Button(main, SWT.PUSH);
  829. browse.setText(Messages.browse);
  830. Point nameSize = new GC(nameLabel).textExtent(nameLabel.getText());
  831. Point destSize = new GC(destLabel).textExtent(destLabel.getText());
  832. Control wider = nameSize.x > destSize.x ? nameLabel : destLabel;
  833. nameText.setLayoutData(UIUtil.createFormData2(
  834. 0,13,null,0,wider,5,100,-5));
  835. nameLabel.setLayoutData(UIUtil.createFormData2(
  836. 0,15,null,0,0,5,null,0));
  837. destText.setLayoutData(UIUtil.createFormData2(
  838. nameText,5,null,0,wider,5,browse,-5));
  839. destLabel.setLayoutData(UIUtil.createFormData2(
  840. nameText,7,null,0,0,5,null,0));
  841. browse.setLayoutData(UIUtil.createFormData2(
  842. nameText,5,null,0,null,0,100,-5));
  843. nameText.addModifyListener(new ModifyListener(){
  844. public void modifyText(ModifyEvent e) {
  845. newConfig = nameText.getText();
  846. validate();
  847. }
  848. });
  849. destText.addModifyListener(new ModifyListener(){
  850. public void modifyText(ModifyEvent e) {
  851. newDest = destText.getText();
  852. validate();
  853. }
  854. });
  855. browse.addSelectionListener(new SelectionListener(){
  856. public void widgetSelected(SelectionEvent e) {
  857. IPath p = new Path(newDest);
  858. if( !p.isAbsolute())
  859. p = new Path(origHome).append(newDest);
  860. File file = p.toFile();
  861. if (!file.exists()) {
  862. file = null;
  863. }
  864. File directory = getDirectory(file, getShell());
  865. if (directory != null) {
  866. IPath newP = new Path(directory.getAbsolutePath());
  867. IPath result;
  868. if( newP.toOSString().startsWith(new Path(origHome).toOSString()))
  869. result = newP.removeFirstSegments(new Path(origHome).segmentCount());
  870. else
  871. result = newP;
  872. destText.setText(result.toString());
  873. }
  874. }
  875. public void widgetDefaultSelected(SelectionEvent e) {
  876. }
  877. });
  878. destText.setText(origDest);
  879. // we could localize the string _copy, but it would probably cause more trouble than it's worth
  880. nameText.setText(findNewest(origConfig + "_copy")); // TODO increment //$NON-NLS-1$
  881. return c;
  882. }
  883. public void validate() {
  884. boolean valid = false;
  885. IPath p = null;
  886. if( newDest != null && newConfig != null ) {
  887. p = new Path(newDest);
  888. if( !p.isAbsolute())
  889. p = new Path(origHome).append(newDest);
  890. if( !p.append(newConfig).toFile().exists())
  891. valid = true;
  892. }
  893. if( !valid ) {
  894. if( newDest == null || newConfig == null ) {
  895. setMessage(Messages.JBossRuntimeWizardFragment_AllFieldsRequired, IMessageProvider.ERROR);
  896. } else {
  897. setMessage(Messages.JBossRuntimeWizardFragment_OutputFolderExists + p.append(newConfig).toString(), IMessageProvider.ERROR);
  898. }
  899. } else {
  900. setCleanMessage();
  901. }
  902. if( getButton(Dialog.OK) != null )
  903. getButton(Dialog.OK).setEnabled(valid);
  904. }
  905. protected void setCleanMessage() {
  906. setMessage(NLS.bind(Messages.rwf_CopyConfigLabel, origConfig, origDest));
  907. }
  908. // Only to be used in initializing dialog
  909. protected String findNewest(String suggested) {
  910. IPath p = new Path(origDest);
  911. if( !p.isAbsolute())
  912. p = new Path(origHome).append(origDest);
  913. if( p.append(suggested).toFile().exists()) {
  914. int i = 1;
  915. while(p.append(suggested + i).toFile().exists())
  916. i++;
  917. return suggested + i;
  918. }
  919. return suggested;
  920. }
  921. protected Point getInitialSize() {
  922. return new Point(500, super.getInitialSize().y);
  923. }
  924. protected void configureShell(Shell newShell) {
  925. super.configureShell(newShell);
  926. newShell.setText(Messages.JBossRuntimeWizardFragment_CopyAConfigShellText);
  927. }
  928. public String getNewDest() {
  929. return newDest;
  930. }
  931. public String getNewConfig() {
  932. return newConfig;
  933. }
  934. }
  935. public IRuntimeType getRuntimeType() {
  936. IRuntime r = getRuntimeFromTaskModel();
  937. return r.getRuntimeType();
  938. }
  939. }