/**
  * Returns the groovy-all-*.jar that is used in the Eclipse project. We know there should only be
  * one specified in the header for org.codehaus.groovy right now.
  *
  * @return Returns the names of the jars that are exported by the org.codehaus.groovy project.
  * @throws BundleException
  */
 public static URL getExportedGroovyAllJar() {
   try {
     Bundle groovyBundle = CompilerUtils.getActiveGroovyBundle();
     if (groovyBundle == null) {
       throw new RuntimeException("Could not find groovy bundle");
     }
     Enumeration<URL> enu = groovyBundle.findEntries("lib", "groovy-all-*.jar", false);
     if (enu == null) {
       // in some versions of the plugin, the groovy-all jar is in the base directory of the
       // plugins
       enu = groovyBundle.findEntries("", "groovy-all-*.jar", false);
     }
     while (enu.hasMoreElements()) {
       URL jar = enu.nextElement();
       if (jar.getFile().indexOf("-sources") == -1
           && jar.getFile().indexOf("-javadoc") == -1
           && jar.getFile().indexOf("-eclipse") == -1) {
         // remove the "reference:/" protocol
         jar = resolve(jar);
         return jar;
       }
     }
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
   throw new RuntimeException("Could not find groovy all jar");
 }
  public static void checkSanity(int expectMinor) throws BundleException {
    Bundle bundle = CompilerUtils.getActiveGroovyBundle();
    assertNotNull("No active Groovy bundle found", bundle);
    assertEquals(
        "Wrong version for groovy bundle: " + bundle.getVersion(),
        expectMinor,
        bundle.getVersion().getMinor());
    if (bundle.getState() != Bundle.ACTIVE) {
      bundle.start();
    }

    bundle = Platform.getBundle("org.eclipse.jdt.core");
    final String qualifier = bundle.getVersion().getQualifier();
    // TODO: the conditions below should be activated... or test will break on non E_3_6 builds...
    // for now leave like this
    // to be 100% sure sanity checks are really executed!
    //		if (StsTestUtil.isOnBuildSite() && StsTestUtil.ECLIPSE_3_6_OR_LATER) {
    assertTrue(
        "JDT patch not properly installed (org.eclipse.jdt.core version = "
            + bundle.getVersion()
            + ")",
        qualifier.contains("xx") || qualifier.equals("qualifier"));
    //		}
    if (bundle.getState() != Bundle.ACTIVE) {
      bundle.start();
    }
    assertTrue(
        "Groovy language support is not active",
        LanguageSupportFactory.getEventHandler()
            .getClass()
            .getName()
            .endsWith("GroovyEventHandler"));
  }
  /**
   * Main entry point to generate UI for compiler switching
   *
   * @param compilerPage
   */
  public static Composite createCompilerSwitchBlock(Composite parent) {
    Composite compilerPage = new Composite(parent, SWT.NONE);

    GridLayout layout = new GridLayout();
    layout.numColumns = 1;
    layout.marginHeight = 3;
    layout.marginWidth = 3;
    compilerPage.setLayout(layout);

    SpecifiedVersion activeGroovyVersion = CompilerUtils.getActiveGroovyVersion();
    Label compilerVersion = new Label(compilerPage, SWT.LEFT | SWT.WRAP);
    compilerVersion.setText(
        "You are currently using Groovy Compiler version "
            + CompilerUtils.getGroovyVersion()
            + ".");

    for (SpecifiedVersion version : SpecifiedVersion.values()) {
      if (activeGroovyVersion != version) {
        switchVersion(version, compilerPage);
      }
    }

    Link moreInfoLink = new Link(compilerPage, 0);
    moreInfoLink.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    moreInfoLink.setText(
        "<a href=\"http://docs.codehaus.org/display/GROOVY/Compiler+Switching+within+Groovy-Eclipse\">See here</a> for more information "
            + "on compiler switching (opens a browser window).");
    moreInfoLink.addListener(
        SWT.Selection,
        new Listener() {
          public void handleEvent(Event event) {
            openUrl(event.text);
          }
        });

    return compilerPage;
  }
 public static URL findDSLDFolder() {
   Bundle groovyBundle = CompilerUtils.getActiveGroovyBundle();
   Enumeration<URL> enu = groovyBundle.findEntries(".", "plugin_dsld_support", false);
   if (enu != null && enu.hasMoreElements()) {
     URL folder = enu.nextElement();
     // remove the "reference:/" protocol
     try {
       folder = resolve(folder);
       return folder;
     } catch (IOException e) {
       GroovyCore.logException("Exception when looking for DSLD folder", e);
     }
   }
   return null;
 }
  /**
   * Provides UI for switching compiler between versions
   *
   * @param toVersion
   */
  private static void switchVersion(
      final SpecifiedVersion toVersion, final Composite compilerPage) {
    final BundleDescription toBundle = CompilerUtils.getBundleDescription(toVersion);
    if (toBundle == null) {
      // this version is not installed
      return;
    }

    Button switchTo = new Button(compilerPage, SWT.PUSH);
    switchTo.setText("Switch to " + toBundle.getVersion());
    switchTo.addSelectionListener(
        new SelectionListener() {

          public void widgetSelected(SelectionEvent e) {
            Shell shell = compilerPage.getShell();
            boolean result =
                MessageDialog.openQuestion(
                    shell,
                    "Change compiler and restart?",
                    "Do you want to change the compiler?\n\nIf you select \"Yes\","
                        + " the compiler will be changed and Eclipse will be restarted.\n\n"
                        + "Make sure all your work is saved before clicking \"Yes\".");

            if (result) {
              // change compiler
              SpecifiedVersion activeGroovyVersion = CompilerUtils.getActiveGroovyVersion();
              IStatus status = CompilerUtils.switchVersions(activeGroovyVersion, toVersion);
              if (status == Status.OK_STATUS) {
                restart(shell);
              } else {
                ErrorDialog error =
                    new ErrorDialog(
                        shell,
                        "Error occurred",
                        "Error occurred when trying to enable Groovy " + toBundle.getVersion(),
                        status,
                        IStatus.ERROR);
                error.open();
              }
            }
          }

          public void widgetDefaultSelected(SelectionEvent e) {}
        });
  }
 /**
  * finds and returns the extra jars that belong inside the Groovy Classpath Container
  *
  * @return jline, servlet-api, ivy, and commons-cli
  */
 public static URL[] getExtraJarsForClasspath() {
   try {
     Bundle groovyBundle = CompilerUtils.getActiveGroovyBundle();
     Enumeration<URL> enu = groovyBundle.findEntries("lib", "*.jar", false);
     if (enu == null) {
       // in some versions of the plugin, the groovy-all jar is in the
       // base directory of the plugins
       enu = groovyBundle.findEntries("", "*.jar", false);
     }
     List<URL> urls = new ArrayList<URL>(9);
     while (enu.hasMoreElements()) {
       URL jar = enu.nextElement();
       if (!jar.getFile().contains("groovy")) {
         // remove the "reference:/" protocol
         jar = resolve(jar);
         urls.add(jar);
       }
     }
     return urls.toArray(new URL[urls.size()]);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }