Home » geronimo-eclipse-plugin-2.2-source-release » org.apache.geronimo.st.ui.internal » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *    http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.geronimo.st.ui.internal;
   18   
   19   import java.io.File;
   20   import java.net.MalformedURLException;
   21   import java.net.URL;
   22   import java.util.ArrayList;
   23   import java.util.List;
   24   import java.util.regex.Matcher;
   25   import java.util.regex.Pattern;
   26   
   27   import org.apache.geronimo.st.core.GeronimoRuntimeDelegate;
   28   import org.apache.geronimo.st.ui.Activator;
   29   import org.eclipse.core.runtime.IPath;
   30   import org.eclipse.core.runtime.IStatus;
   31   import org.eclipse.core.runtime.NullProgressMonitor;
   32   import org.eclipse.core.runtime.Path;
   33   import org.eclipse.jdt.launching.IVMInstall;
   34   import org.eclipse.jdt.launching.IVMInstall2;
   35   import org.eclipse.jdt.launching.IVMInstallType;
   36   import org.eclipse.jdt.launching.JavaRuntime;
   37   import org.eclipse.jface.dialogs.IMessageProvider;
   38   import org.eclipse.jface.preference.IPreferenceNode;
   39   import org.eclipse.jface.preference.PreferenceDialog;
   40   import org.eclipse.jface.preference.PreferenceManager;
   41   import org.eclipse.jface.window.Window;
   42   import org.eclipse.swt.SWT;
   43   import org.eclipse.swt.custom.BusyIndicator;
   44   import org.eclipse.swt.events.ModifyEvent;
   45   import org.eclipse.swt.events.ModifyListener;
   46   import org.eclipse.swt.events.SelectionAdapter;
   47   import org.eclipse.swt.events.SelectionEvent;
   48   import org.eclipse.swt.events.SelectionListener;
   49   import org.eclipse.swt.layout.GridData;
   50   import org.eclipse.swt.layout.GridLayout;
   51   import org.eclipse.swt.widgets.Button;
   52   import org.eclipse.swt.widgets.Combo;
   53   import org.eclipse.swt.widgets.Composite;
   54   import org.eclipse.swt.widgets.DirectoryDialog;
   55   import org.eclipse.swt.widgets.Label;
   56   import org.eclipse.swt.widgets.Text;
   57   import org.eclipse.ui.PartInitException;
   58   import org.eclipse.ui.PlatformUI;
   59   import org.eclipse.ui.browser.IWebBrowser;
   60   import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
   61   import org.eclipse.ui.forms.events.HyperlinkAdapter;
   62   import org.eclipse.ui.forms.events.HyperlinkEvent;
   63   import org.eclipse.ui.forms.widgets.FormText;
   64   import org.eclipse.ui.internal.browser.WorkbenchBrowserSupport;
   65   import org.eclipse.wst.server.core.IRuntime;
   66   import org.eclipse.wst.server.core.IRuntimeType;
   67   import org.eclipse.wst.server.core.IRuntimeWorkingCopy;
   68   import org.eclipse.wst.server.core.ServerCore;
   69   import org.eclipse.wst.server.core.TaskModel;
   70   import org.eclipse.wst.server.core.model.RuntimeDelegate;
   71   import org.eclipse.wst.server.ui.internal.SWTUtil;
   72   import org.eclipse.wst.server.ui.wizard.IWizardHandle;
   73   import org.eclipse.wst.server.ui.wizard.WizardFragment;
   74   
   75   /**
   76    * @version $Rev: 734561 $ $Date: 2009-01-15 07:48:44 +0800 (Thu, 15 Jan 2009) $
   77    */
   78   public class GeronimoRuntimeWizardFragment extends WizardFragment {
   79   
   80       // serverName-N.N or serverName-N.N.N
   81       public static final Pattern SERVER_NAME_VERSION_PATTERN = Pattern.compile("(.*-)((\\d+\\.\\d+)(\\.(\\d+))?)");
   82   
   83       private GeronimoRuntimeDelegate geronimoRuntime;
   84   
   85       protected Text installDir;
   86   
   87       private IWizardHandle fWizard;
   88   
   89       protected List<IVMInstall> installedJREs;
   90   
   91       protected String[] jreNames;
   92   
   93       protected Combo combo;
   94   
   95       public GeronimoRuntimeWizardFragment() {
   96           super();
   97       }
   98   
   99       /*
  100        * (non-Javadoc)
  101        * 
  102        * @see org.eclipse.wst.server.ui.wizard.WizardFragment#hasComposite()
  103        */
  104       public boolean hasComposite() {
  105           return true;
  106       }
  107   
  108       /*
  109        * (non-Javadoc)
  110        * 
  111        * @see
  112        * org.eclipse.wst.server.ui.wizard.WizardFragment#createComposite(org.eclipse
  113        * .swt.widgets.Composite, org.eclipse.wst.server.ui.wizard.IWizardHandle)
  114        */
  115       public Composite createComposite(Composite parent, IWizardHandle handle) {
  116           this.fWizard = handle;
  117           Composite container = new Composite(parent, SWT.NONE);
  118           GridLayout grid = new GridLayout(1, false);
  119           grid.marginWidth = 0;
  120           container.setLayout(grid);
  121           container.setLayoutData(new GridData(GridData.FILL_BOTH));
  122           handle.setImageDescriptor(Activator.getImageDescriptor((Activator.IMG_WIZ_GERONIMO)));
  123           handle.setTitle(Messages.bind(Messages.runtimeWizardTitle, getRuntimeName()));
  124           String name = getGeronimoRuntime().getRuntime().getRuntimeType().getName();
  125           //handle.setDescription(Messages.bind(Messages.runtimeWizardDescription, name));
  126           createContent(container, handle);
  127           return container;
  128       }
  129   
  130       public void createContent(Composite parent, IWizardHandle handle) {
  131   
  132           Composite composite = new Composite(parent, SWT.NONE);
  133           GridLayout layout = new GridLayout(3, false);
  134           composite.setLayout(layout);
  135           composite.setLayoutData(new GridData(GridData.FILL_BOTH));
  136   
  137           addJRESelection(composite);
  138           addInstallDirSection(composite);
  139           addDownloadServerSection(composite);
  140       }
  141   
  142       protected void addDownloadServerSection(final Composite composite) {
  143   		FormText downloadServerText = new FormText(composite, SWT.WRAP);
  144   		IRuntime runtime = getRuntimeDelegate().getRuntime();
  145   		String runtimeName = runtime.getRuntimeType().getName();
  146   		String text = "<form>"
  147   				+ Messages.bind(Messages.DownloadServerText,
  148   						Messages.DownloadServerURL, runtimeName) + "</form>";
  149   		downloadServerText.setText(text, true, true);
  150   		GridData data = new GridData();
  151   		data.horizontalSpan = 3;
  152   		downloadServerText.setLayoutData(data);
  153   		downloadServerText.addHyperlinkListener(new HyperlinkAdapter() {
  154   			@Override
  155   			public void linkActivated(HyperlinkEvent hyperlinkEvent) {
  156   				String url = hyperlinkEvent.getHref().toString();
  157   				Trace.trace(Trace.INFO, "Hyperlink " + url + ".");
  158   				try {
  159   					int style = IWorkbenchBrowserSupport.AS_EXTERNAL
  160   							| IWorkbenchBrowserSupport.STATUS;
  161   					IWebBrowser browser = WorkbenchBrowserSupport.getInstance()
  162   							.createBrowser(style, "download server",
  163   									"get server", "tool tip");
  164   					browser.openURL(new URL(url));
  165   				} catch (MalformedURLException e) {
  166   					e.printStackTrace();
  167   				} catch (PartInitException e) {
  168   					e.printStackTrace();
  169   				}
  170   			}
  171   		});
  172   	}
  173   
  174       protected GeronimoRuntimeDelegate getGeronimoRuntime() {
  175           if (geronimoRuntime == null)
  176               geronimoRuntime = (GeronimoRuntimeDelegate) getRuntimeDelegate().getRuntime().loadAdapter(
  177                       GeronimoRuntimeDelegate.class, null);
  178           return geronimoRuntime;
  179       }
  180   
  181       protected void addInstallDirSection(Composite composite) {
  182           Label label = new Label(composite, SWT.NONE);
  183           label.setText(Messages.installDir);
  184           GridData data = new GridData();
  185           data.horizontalSpan = 3;
  186           label.setLayoutData(data);
  187           String tooltipLoc = Messages.bind(Messages.tooltipLoc, getRuntimeName());
  188           label.setToolTipText(tooltipLoc);
  189   
  190           installDir = new Text(composite, SWT.BORDER);
  191   
  192           IPath currentLocation = getRuntimeDelegate().getRuntimeWorkingCopy().getLocation();
  193           if (currentLocation != null) {
  194               installDir.setText(currentLocation.toOSString());
  195           }
  196   
  197           data = new GridData(GridData.FILL_HORIZONTAL);
  198           data.horizontalSpan = 2;
  199           installDir.setLayoutData(data);
  200           installDir.setToolTipText(tooltipLoc);
  201           installDir.addModifyListener(new ModifyListener() {
  202               public void modifyText(ModifyEvent e) {
  203                   getRuntimeDelegate().getRuntimeWorkingCopy().setLocation(new Path(installDir.getText()));
  204                   validate();
  205               }
  206           });
  207   
  208           final Composite browseComp = composite;
  209           Button browse = SWTUtil.createButton(composite, Messages.browse);
  210           browse.addSelectionListener(new SelectionAdapter() {
  211               public void widgetSelected(SelectionEvent se) {
  212                   DirectoryDialog dialog = new DirectoryDialog(browseComp.getShell());
  213                   dialog.setMessage(Messages.installDir);
  214                   dialog.setFilterPath(installDir.getText());
  215                   String selectedDirectory = dialog.open();
  216                   if (selectedDirectory != null)
  217                       installDir.setText(selectedDirectory);
  218               }
  219           });
  220       }
  221   
  222       protected void addJRESelection(final Composite composite) {
  223           updateJREs();
  224   
  225           Label label = new Label(composite, SWT.NONE);
  226           label.setText(Messages.installedJRE);
  227           label.setLayoutData(new GridData());
  228   
  229           combo = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
  230           combo.setItems(jreNames);
  231           GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
  232           combo.setLayoutData(data);
  233   
  234           combo.addSelectionListener(new SelectionListener() {
  235               public void widgetSelected(SelectionEvent e) {
  236                   // if the first item in the list is selected, then pass null
  237                   // to setVMInstall to use the default JRE.
  238                   // otherwise the array list of JRE's is one off from what is
  239                   // in the combo; subtract 1 from the selection to get the
  240                   // correct JRE.
  241                   int sel = combo.getSelectionIndex();
  242                   IVMInstall vmInstall = null;
  243                   if (sel > 0) {
  244                       vmInstall = (IVMInstall) installedJREs.get(sel - 1);
  245                   }
  246                   getRuntimeDelegate().setVMInstall(vmInstall);
  247                   validate();
  248               }
  249   
  250               public void widgetDefaultSelected(SelectionEvent e) {
  251                   widgetSelected(e);
  252               }
  253           });
  254   
  255           Button button = SWTUtil.createButton(composite, Messages.installedJREs);
  256           button.addSelectionListener(new SelectionAdapter() {
  257               public void widgetSelected(SelectionEvent e) {
  258                   String currentVM = combo.getText();
  259                   if (showPreferencePage(composite)) {
  260                       updateJREs();
  261                       combo.setItems(jreNames);
  262                       combo.setText(currentVM);
  263                       if (combo.getSelectionIndex() == -1)
  264                           combo.select(0);
  265                   }
  266               }
  267           });
  268   
  269           if (getRuntimeDelegate() != null) {
  270               if (getRuntimeDelegate().isUsingDefaultJRE()) {
  271                   combo.select(0);
  272               } else {
  273                   combo.setText(getRuntimeDelegate().getVMInstall().getName());
  274               }
  275           }
  276       }
  277   
  278       protected boolean showPreferencePage(Composite composite) {
  279           PreferenceManager manager = PlatformUI.getWorkbench().getPreferenceManager();
  280           IPreferenceNode node = manager.find("org.eclipse.jdt.ui.preferences.JavaBasePreferencePage").findSubNode(
  281                   "org.eclipse.jdt.debug.ui.preferences.VMPreferencePage");
  282           PreferenceManager manager2 = new PreferenceManager();
  283           manager2.addToRoot(node);
  284           final PreferenceDialog dialog = new PreferenceDialog(composite.getShell(), manager2);
  285           final boolean[] result = new boolean[] { false };
  286           BusyIndicator.showWhile(composite.getDisplay(), new Runnable() {
  287               public void run() {
  288                   dialog.create();
  289                   if (dialog.open() == Window.OK)
  290                       result[0] = true;
  291               }
  292           });
  293           return result[0];
  294       }
  295   
  296       /*
  297        * (non-Javadoc)
  298        * 
  299        * @see org.eclipse.wst.server.ui.wizard.WizardFragment#isComplete()
  300        */
  301       public boolean isComplete() {
  302           IRuntimeWorkingCopy runtimeWC = getRuntimeDelegate().getRuntimeWorkingCopy();
  303           IStatus status = runtimeWC.validate(null);
  304           return status == null || status.getSeverity() != IStatus.ERROR;
  305       }
  306   
  307       protected void validate() {
  308   
  309           IRuntime runtime = getRuntimeDelegate().getRuntime();
  310   
  311           String runtimeName = runtime.getRuntimeType().getName();
  312   
  313           IWizardHandle wizard = getWizard();
  314   
  315           if (runtime == null) {
  316               wizard.setMessage("", IMessageProvider.ERROR);
  317               return;
  318           }
  319   
  320           IRuntimeWorkingCopy runtimeWC = getRuntimeDelegate().getRuntimeWorkingCopy();
  321           getRuntimeDelegate().setInstanceProperty("serverRootDirectory", installDir.getText());
  322   
  323           if (installDir.getText() == null || installDir.getText().length() == 0) {
  324               // installDir field has not been entered
  325               wizard.setMessage(Messages.bind(Messages.installDirInfo, runtimeName), IMessageProvider.NONE);
  326           } else {
  327               IStatus status = runtimeWC.validate(null);
  328               if (status == null || status.isOK()) {
  329                   // a valid install found
  330                   wizard.setMessage(Messages.bind(Messages.serverDetected, runtimeName), IMessageProvider.NONE);
  331               } else if (status.getCode() == GeronimoRuntimeDelegate.INCORRECT_VERSION) {
  332                   if (status.getSeverity() == IStatus.ERROR) {
  333                       wizard.setMessage(status.getMessage(), IMessageProvider.ERROR);
  334                       return;
  335                   }
  336                   wizard.setMessage(status.getMessage(), IMessageProvider.WARNING);
  337               } else if (status.getCode() == GeronimoRuntimeDelegate.PARTIAL_IMAGE) {
  338                   wizard.setMessage(status.getMessage(), IMessageProvider.ERROR);
  339                   return;
  340               } else {
  341                   File file = new Path(installDir.getText()).toFile();
  342                   if (file.isDirectory()) {
  343                       String message = file.canWrite() ? Messages.noImageFound : Messages.cannotInstallAtLocation;
  344                       message = Messages.bind(message, runtimeName);
  345                       wizard.setMessage(message, IMessageProvider.ERROR);
  346                   } else {
  347                       wizard.setMessage(Messages.noSuchDir, IMessageProvider.ERROR);
  348                   }
  349                   return;
  350               }
  351   
  352               if (!isValidVM()) {
  353                   wizard.setMessage(Messages.bind(Messages.jvmWarning, runtimeName), IMessageProvider.WARNING);
  354               }
  355           }
  356       }
  357   
  358       private boolean isValidVM() {
  359           IVMInstall vmInstall = getRuntimeDelegate().getVMInstall();
  360           if (vmInstall instanceof IVMInstall2) {
  361               String javaVersion = ((IVMInstall2) vmInstall).getJavaVersion();
  362               return javaVersion != null && (javaVersion.startsWith("1.5") || javaVersion.startsWith("1.6"));
  363           }
  364           return false;
  365       }
  366   
  367       /*
  368        * (non-Javadoc)
  369        * 
  370        * @see org.eclipse.wst.server.ui.wizard.WizardFragment#enter()
  371        */
  372       public void enter() {
  373           if (getRuntimeDelegate() != null)
  374               getRuntimeDelegate().getRuntimeWorkingCopy().setName(createName());
  375           validate();
  376       }
  377   
  378       /*
  379        * (non-Javadoc)
  380        * 
  381        * @see org.eclipse.wst.server.ui.wizard.WizardFragment#exit()
  382        */
  383       public void exit() {
  384           validate();
  385       }
  386   
  387       private GeronimoRuntimeDelegate getRuntimeDelegate() {
  388           IRuntimeWorkingCopy wc = (IRuntimeWorkingCopy) getTaskModel().getObject(TaskModel.TASK_RUNTIME);
  389           if (wc == null)
  390               return null;
  391           return (GeronimoRuntimeDelegate) wc.loadAdapter(GeronimoRuntimeDelegate.class, new NullProgressMonitor());
  392       }
  393   
  394       private String createName() {
  395           RuntimeDelegate dl = getRuntimeDelegate();
  396           IRuntimeType runtimeType = dl.getRuntime().getRuntimeType();
  397           String name = runtimeType.getName();
  398           IRuntime[] list = ServerCore.getRuntimes();
  399           int suffix = 1;
  400           String suffixName = name;
  401           for (int i = 0; i < list.length; i++) {
  402               if ((list[i].getName().equals(name) || list[i].getName().equals(suffixName))
  403                       && !list[i].equals(dl.getRuntime()))
  404                   suffix++;
  405               suffixName = name + " " + suffix;
  406           }
  407   
  408           if (suffix > 1)
  409               return suffixName;
  410           return name;
  411       }
  412   
  413       protected String getRuntimeName() {
  414           if (getRuntimeDelegate() != null && getRuntimeDelegate().getRuntime() != null)
  415               return getRuntimeDelegate().getRuntime().getName();
  416           return null;
  417       }
  418   
  419       protected void updateJREs() {
  420           installedJREs = new ArrayList<IVMInstall>();
  421           IVMInstallType[] vmInstallTypes = JavaRuntime.getVMInstallTypes();
  422           int size = vmInstallTypes.length;
  423           for (int i = 0; i < size; i++) {
  424               IVMInstall[] vmInstalls = vmInstallTypes[i].getVMInstalls();
  425               int size2 = vmInstalls.length;
  426               for (int j = 0; j < size2; j++) {
  427                   installedJREs.add(vmInstalls[j]);
  428               }
  429           }
  430   
  431           // The Default JRE will always be the first item in the combo. This is
  432           // an assumption that is made by the combo selection listener and that
  433           // all
  434           // other installed JREs are listed afterwards in the same order that
  435           // they
  436           // are found in the list of installed JREs
  437           size = installedJREs.size();
  438           jreNames = new String[size + 1];
  439           jreNames[0] = Messages.runtimeDefaultJRE;
  440           for (int i = 0; i < size; i++) {
  441               IVMInstall vmInstall = (IVMInstall) installedJREs.get(i);
  442               jreNames[i + 1] = vmInstall.getName();
  443           }
  444       }
  445   
  446       public IWizardHandle getWizard() {
  447           return fWizard;
  448       }
  449   
  450       /*
  451        * (non-Javadoc)
  452        * 
  453        * @see
  454        * org.eclipse.wst.server.ui.wizard.WizardFragment#createChildFragments(
  455        * java.util.List)
  456        */
  457       protected void createChildFragments(List list) {
  458           list.add(new GeronimoRuntimeSourceWizardFragment());
  459       }
  460   
  461       /**
  462        * Code for testing server name determination code in the
  463        * updateInstallDir(IPath) method of addInstallableRuntimeSection.
  464        * 
  465        * @param args
  466        */
  467       public static void main(String[] args) {
  468           Pattern SERVER_NAME_VERSION_PATTERN = Pattern.compile("(.*-)((\\d+\\.\\d+)(\\.(\\d+))?)");
  469           for (String path : args) {
  470               StringBuffer installPath = new StringBuffer();
  471               Matcher matcher = SERVER_NAME_VERSION_PATTERN.matcher(path);
  472               if (matcher.find()) {
  473                   String serverName = matcher.group(1);
  474                   String serverVersion = matcher.group(2);
  475                   installPath = installPath.append(serverName + serverVersion);
  476                   System.out.println("path = " + path + ", serverVersion = " + serverVersion + ", installPath = "
  477                           + installPath);
  478               } else {
  479                   System.out.println("No version found in path = " + path);
  480               }
  481           }
  482       }
  483   }

Home » geronimo-eclipse-plugin-2.2-source-release » org.apache.geronimo.st.ui.internal » [javadoc | source]