/* (non-Javadoc)
   * @see org.eclipse.core.runtime.IAdapterFactory#getAdapter(java.lang.Object, java.lang.Class)
   */
  public Object getAdapter(Object adaptableObject, Class adapterType) {

    Object returnValue = null;

    if (adaptableObject instanceof IFile) {
      IFile file = (IFile) adaptableObject;
      if (ContentTypeUtils.isGroovyLikeFileName(file.getName())) {
        if (ClassNode.class.equals(adapterType) || ClassNode[].class.equals(adapterType)) {
          try {

            // we know this will be a GCU because of the file extension
            GroovyCompilationUnit unit =
                (GroovyCompilationUnit) JavaCore.createCompilationUnitFrom(file);
            ModuleNode module = unit.getModuleNode();
            if (module != null) {
              List<ClassNode> classNodeList = module.getClasses();

              if (classNodeList != null && !classNodeList.isEmpty()) {
                if (ClassNode.class.equals(adapterType)) {
                  returnValue = classNodeList.get(0);
                } else if (ClassNode[].class.equals(adapterType)) {
                  returnValue = classNodeList.toArray(new ClassNode[0]);
                }
              }
            }
          } catch (Exception ex) {
            GroovyCore.logException("error adapting file to ClassNode", ex);
          }
        }
      }
    }
    return returnValue;
  }
 /* (non-Javadoc)
  * Method declared on SelectionDispatchAction.
  */
 @Override
 public void run(ITextSelection selection) {
   IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
   if (doc != null && unit != null) {
     DefaultGroovyFormatter formatter =
         new DefaultGroovyFormatter(
             selection, doc, new FormatterPreferences(unit), kind == FormatKind.INDENT_ONLY);
     TextEdit edit = formatter.format();
     try {
       unit.applyTextEdit(edit, new NullProgressMonitor());
     } catch (MalformedTreeException e) {
       GroovyCore.logException("Exception when formatting", e);
     } catch (JavaModelException e) {
       GroovyCore.logException("Exception when formatting", e);
     }
   }
 }
  public List<IJavaCompletionProposal> getQuickFixProposals() {
    IJavaProject project = getQuickFixProblem().getCompilationUnit().getJavaProject();
    List<IJavaCompletionProposal> proposals = new ArrayList<IJavaCompletionProposal>(2);
    try {
      if (!GroovyRuntime.hasGroovyClasspathContainer(project)) {
        proposals.add(new AddGroovyRuntimeProposal(project, getQuickFixProblem(), 100));
        return proposals;
      }
    } catch (CoreException e) {
      GroovyCore.logWarning("Problem calculating quickfixes", e);
    }

    return null;
  }
 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;
 }
 @Override
 protected IStatus run(IProgressMonitor monitor) {
   try {
     IProgressMonitor sub = SubMonitor.convert(monitor, projects.length);
     for (IProject project : projects) {
       project.build(IncrementalProjectBuilder.FULL_BUILD, sub);
       sub.worked(1);
     }
     return Status.OK_STATUS;
   } catch (CoreException e) {
     GroovyCore.logException("Error building groovy project", e);
     return e.getStatus();
   } finally {
     monitor.done();
   }
 }
  /**
   * Swtiches to or from groovy version 1.6.x depending on the boolean passed in A restart is
   * required immediately after or else many exceptions will be thrown.
   *
   * @param toVersion16
   * @return {@link Status.OK_STATUS} if successful or error status that contains the exception
   *     thrown otherwise
   */
  public static IStatus switchVersions(SpecifiedVersion fromVersion, SpecifiedVersion toVersion) {
    try {
      State state = ((StateManager) Platform.getPlatformAdmin()).getSystemState();
      BundleDescription toBundle = getBundleDescription(toVersion);
      BundleDescription[] allBundles = getAllGroovyBundleDescriptions();
      if (toBundle == null) {
        throw new Exception("Could not find any " + toVersion + " groovy version to enable");
      }

      // go through all bundles and ensure disabled
      for (BundleDescription bundle : allBundles) {
        DisabledInfo info = createDisabledInfo(state, bundle.getBundleId());
        if (bundle.equals(toBundle)) {
          // ensure enabled
          Platform.getPlatformAdmin().removeDisabledInfo(info);
        } else {
          // don't actually stop
          // switch (bundle.getState()) {
          // case Bundle.ACTIVE:
          // case Bundle.INSTALLED:
          // case Bundle.STARTING:
          // case Bundle.RESOLVED:
          // bundle.stop();
          // }
          // ensure disabled
          Platform.getPlatformAdmin().addDisabledInfo(info);
        }
      }
      CompilerLevelUtils.writeConfigurationVersion(
          toVersion,
          // need to get the system bundle
          GroovyCoreActivator.getDefault()
              .getBundle()
              .getBundleContext()
              .getBundle(0)
              .getBundleContext());
      return Status.OK_STATUS;
    } catch (Exception e) {
      GroovyCore.logException(e.getMessage(), e);
      return new Status(
          IStatus.ERROR,
          GroovyCoreActivator.PLUGIN_ID,
          e.getMessage() + "\n\nSee the error log for more information.",
          e);
    }
  }
  public static void openUrl(String location) {
    try {
      URL url = null;

      if (location != null) {
        url = new URL(location);
      }

      if (WebBrowserPreference.getBrowserChoice() == WebBrowserPreference.EXTERNAL) {
        try {
          IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport();
          support.getExternalBrowser().openURL(url);
        } catch (Exception e) {
          GroovyCore.logException("Could not open browser", e);
        }
      } else {
        IWebBrowser browser = null;
        int flags = 0;
        if (WorkbenchBrowserSupport.getInstance().isInternalWebBrowserAvailable()) {
          flags |=
              IWorkbenchBrowserSupport.AS_EDITOR
                  | IWorkbenchBrowserSupport.LOCATION_BAR
                  | IWorkbenchBrowserSupport.NAVIGATION_BAR;
        } else {
          flags |=
              IWorkbenchBrowserSupport.AS_EXTERNAL
                  | IWorkbenchBrowserSupport.LOCATION_BAR
                  | IWorkbenchBrowserSupport.NAVIGATION_BAR;
        }

        String id = "org.eclipse.contribution.weaving.jdt";
        browser = WorkbenchBrowserSupport.getInstance().createBrowser(flags, id, null, null);
        browser.openURL(url);
      }
    } catch (PartInitException e) {
      MessageDialog.openError(
          Display.getDefault().getActiveShell(),
          "Browser initialization error",
          "Browser could not be initiated");
    } catch (MalformedURLException e) {
      MessageDialog.openInformation(
          Display.getDefault().getActiveShell(), "Malformed URL", location);
    }
  }