protected IScanner getScanner(IJavaScriptUnit unit) {
    if (unit.equals(fCuCache)) return fScannerCache;

    fCuCache = unit;
    IJavaScriptProject project = unit.getJavaScriptProject();
    String sourceLevel = project.getOption(JavaScriptCore.COMPILER_SOURCE, true);
    String complianceLevel = project.getOption(JavaScriptCore.COMPILER_COMPLIANCE, true);
    fScannerCache = ToolFactory.createScanner(false, false, false, sourceLevel, complianceLevel);
    return fScannerCache;
  }
 /** Observe the changes of the JSDT "Include Path" to synchronize tern script paths. */
 @Override
 public void elementChanged(ElementChangedEvent event) {
   IJavaScriptElementDelta delta = event.getDelta();
   if (delta.getKind() == IJavaScriptElementDelta.CHANGED) {
     // retrieve the JSDT Project if delta is about "Includes Path"
     // changes.
     IJavaScriptProject jsProject = getJavaScriptProjectIfClassPathChanged(delta);
     if (jsProject != null) {
       Job configJob = new ConfigureJob(jsProject);
       configJob.setRule(jsProject.getProject());
       configJob.schedule();
     }
   }
 }
 @Override
 public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
   if (monitor.isCanceled()) {
     return Status.CANCEL_STATUS;
   }
   monitor.beginTask("Confuguring the project", 1);
   // JSDT "Includes Path", has changed.
   if (fJsProject != null) {
     IProject project = fJsProject.getProject();
     if (TernCorePlugin.hasTernNature(project)) {
       // It's a tern project
       try {
         IIDETernProject ternProject = TernCorePlugin.getTernProject(project);
         // Synchronize tern script paths with JSDT
         // "Include Path"
         synchTernScriptPaths(fJsProject, ternProject);
       } catch (Exception e) {
         Trace.trace(Trace.SEVERE, "Error while JSDT ClassPath changed.", e);
       }
     }
   }
   monitor.worked(1);
   monitor.done();
   return Status.OK_STATUS;
 }
  protected Object[] getPackageFragmentRoots(IJavaScriptProject project)
      throws JavaScriptModelException {
    if (!project.getProject().isOpen()) return NO_CHILDREN;

    IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
    List list = new ArrayList(roots.length);
    // filter out package fragments that correspond to projects and
    // replace them with the package fragments directly
    for (int i = 0; i < roots.length; i++) {
      IPackageFragmentRoot root = roots[i];
      if (!root.isExternal()) {
        Object[] children = root.getChildren();
        for (int k = 0; k < children.length; k++) list.add(children[k]);
      } else if (hasChildren(root)) {
        list.add(root);
      }
    }
    return concatenate(list.toArray(), project.getNonJavaScriptResources());
  }
 /**
  * Synchronize tern script paths with JSDT "Include Path"
  *
  * @param jsProject
  * @param ternProject
  */
 private void synchTernScriptPaths(IJavaScriptProject jsProject, IIDETernProject ternProject) {
   try {
     ternProject.removeExternalScriptPaths(JSDT_EXTERNAL_LABEL);
     IIncludePathEntry[] entries = jsProject.getRawIncludepath();
     for (int i = 0; i < entries.length; i++) {
       IIncludePathEntry entry = entries[i];
       switch (entry.getEntryKind()) {
         case IIncludePathEntry.CPE_LIBRARY:
           // TODO : manage JSDT library
           // JSDT Source file => Tern script path file.
           /*IFolder libFolder = ResourcesPlugin.getWorkspace().getRoot()
           		.getFolder(entry.getPath());
           try {
           	ternProject.addExternalScriptPath(libFolder,
           			ScriptPathsType.FOLDER, JSDT_EXTERNAL_LABEL);
           } catch (IOException e) {
           	Trace.trace(Trace.SEVERE,
           			"Error while adding external tern script path for project "
           					+ ternProject.getProject().getName(), e);
           }*/
           break;
         case IIncludePathEntry.CPE_SOURCE:
           if (entry.getPath().segmentCount() == 1) {
             // It's a project
             synchTernProjectScriptPaths(ternProject, entry);
           } else {
             // It's a folder
             // JSDT Source folder => Tern script path folder.
             IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(entry.getPath());
             try {
               ternProject.addExternalScriptPath(
                   folder, ScriptPathsType.FOLDER, JSDT_EXTERNAL_LABEL);
             } catch (IOException e) {
               Trace.trace(
                   Trace.SEVERE,
                   "Error while adding external tern script path for project "
                       + ternProject.getProject().getName(),
                   e);
             }
           }
           break;
         case IIncludePathEntry.CPE_PROJECT:
           // JS file?
           synchTernProjectScriptPaths(ternProject, entry);
           break;
       }
     }
   } catch (JavaScriptModelException e) {
     Trace.trace(
         Trace.SEVERE,
         "Error while getting JSDT ClassPath for project " + ternProject.getProject().getName(),
         e);
   }
 }
  private CPListElement[] openProjectDialog(CPListElement elem) {

    try {
      ArrayList selectable = new ArrayList();
      selectable.addAll(Arrays.asList(fCurrJProject.getJavaScriptModel().getJavaScriptProjects()));
      selectable.remove(fCurrJProject);

      List elements = fProjectsList.getElements();
      for (int i = 0; i < elements.size(); i++) {
        CPListElement curr = (CPListElement) elements.get(0);
        IJavaScriptProject proj = (IJavaScriptProject) JavaScriptCore.create(curr.getResource());
        selectable.remove(proj);
      }
      Object[] selectArr = selectable.toArray();
      new JavaScriptElementComparator().sort(null, selectArr);

      ListSelectionDialog dialog =
          new ListSelectionDialog(
              getShell(),
              Arrays.asList(selectArr),
              new ArrayContentProvider(),
              new JavaUILabelProvider(),
              NewWizardMessages.ProjectsWorkbookPage_chooseProjects_message);
      dialog.setTitle(NewWizardMessages.ProjectsWorkbookPage_chooseProjects_title);
      dialog.setHelpAvailable(false);
      if (dialog.open() == Window.OK) {
        Object[] result = dialog.getResult();
        CPListElement[] cpElements = new CPListElement[result.length];
        for (int i = 0; i < result.length; i++) {
          IJavaScriptProject curr = (IJavaScriptProject) result[i];
          cpElements[i] =
              new CPListElement(
                  fCurrJProject, IIncludePathEntry.CPE_PROJECT, curr.getPath(), curr.getResource());
        }
        return cpElements;
      }
    } catch (JavaScriptModelException e) {
      return null;
    }
    return null;
  }
 StandardType createBoxed(PrimitiveType type, IJavaScriptProject focus) {
   String fullyQualifiedName = BOXED_PRIMITIVE_NAMES[type.getId()];
   try {
     IType javaElementType = focus.findType(fullyQualifiedName);
     StandardType result = (StandardType) fStandardTypes.get(javaElementType);
     if (result != null) return result;
     ASTParser parser = ASTParser.newParser(AST.JLS3);
     parser.setProject(focus);
     IBinding[] bindings = parser.createBindings(new IJavaScriptElement[] {javaElementType}, null);
     return createStandardType((ITypeBinding) bindings[0]);
   } catch (JavaScriptModelException e) {
     // fall through
   }
   return null;
 }
 private boolean isOnClassPath(IJavaScriptUnit element) throws JavaScriptModelException {
   IJavaScriptProject project = element.getJavaScriptProject();
   if (project == null || !project.exists()) return false;
   return project.isOnIncludepath(element);
 }
 public int hashCode() {
   return fProject.hashCode() + fBindingKey.hashCode();
 }
 public boolean equals(Object other) {
   if (this == other) return true;
   if (!(other instanceof ProjectKeyPair)) return false;
   ProjectKeyPair otherPair = (ProjectKeyPair) other;
   return fProject.equals(otherPair.fProject) && fBindingKey.equals(otherPair.fBindingKey);
 }
 public Change createChange(IProgressMonitor monitor) throws CoreException {
   final Map arguments = new HashMap();
   String project = null;
   IJavaScriptProject javaProject = fCu.getJavaScriptProject();
   if (javaProject != null) project = javaProject.getElementName();
   int flags =
       JavaScriptRefactoringDescriptor.JAR_REFACTORING
           | JavaScriptRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
   if (JdtFlags.getVisibilityCode(fVisibility) != Modifier.PRIVATE)
     flags |= RefactoringDescriptor.STRUCTURAL_CHANGE;
   String pattern = ""; // $NON-NLS-1$
   try {
     pattern =
         BindingLabelProvider.getBindingLabel(
                 getContainingTypeBinding(), JavaScriptElementLabels.ALL_FULLY_QUALIFIED)
             + "."; //$NON-NLS-1$
   } catch (JavaScriptModelException exception) {
     JavaScriptPlugin.log(exception);
   }
   final String expression = ASTNodes.asString(fSelectedExpression.getAssociatedExpression());
   final String description =
       Messages.format(
           RefactoringCoreMessages.ExtractConstantRefactoring_descriptor_description_short,
           fConstantName);
   final String header =
       Messages.format(
           RefactoringCoreMessages.ExtractConstantRefactoring_descriptor_description,
           new String[] {pattern + fConstantName, expression});
   final JDTRefactoringDescriptorComment comment =
       new JDTRefactoringDescriptorComment(project, this, header);
   comment.addSetting(
       Messages.format(
           RefactoringCoreMessages.ExtractConstantRefactoring_constant_name_pattern,
           fConstantName));
   comment.addSetting(
       Messages.format(
           RefactoringCoreMessages.ExtractConstantRefactoring_constant_expression_pattern,
           expression));
   String visibility = fVisibility;
   if ("".equals(visibility)) // $NON-NLS-1$
   visibility = RefactoringCoreMessages.ExtractConstantRefactoring_default_visibility;
   comment.addSetting(
       Messages.format(
           RefactoringCoreMessages.ExtractConstantRefactoring_visibility_pattern, visibility));
   if (fReplaceAllOccurrences)
     comment.addSetting(RefactoringCoreMessages.ExtractConstantRefactoring_replace_occurrences);
   if (fQualifyReferencesWithDeclaringClassName)
     comment.addSetting(RefactoringCoreMessages.ExtractConstantRefactoring_qualify_references);
   final JDTRefactoringDescriptor descriptor =
       new JDTRefactoringDescriptor(
           IJavaScriptRefactorings.EXTRACT_CONSTANT,
           project,
           description,
           comment.asString(),
           arguments,
           flags);
   arguments.put(JDTRefactoringDescriptor.ATTRIBUTE_INPUT, descriptor.elementToHandle(fCu));
   arguments.put(JDTRefactoringDescriptor.ATTRIBUTE_NAME, fConstantName);
   arguments.put(
       JDTRefactoringDescriptor.ATTRIBUTE_SELECTION,
       new Integer(fSelectionStart).toString()
           + " "
           + new Integer(fSelectionLength).toString()); // $NON-NLS-1$
   arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplaceAllOccurrences).toString());
   arguments.put(
       ATTRIBUTE_QUALIFY, Boolean.valueOf(fQualifyReferencesWithDeclaringClassName).toString());
   arguments.put(
       ATTRIBUTE_VISIBILITY, new Integer(JdtFlags.getVisibilityCode(fVisibility)).toString());
   return new RefactoringDescriptorChange(
       descriptor,
       RefactoringCoreMessages.ExtractConstantRefactoring_name,
       new Change[] {fChange});
 }
 /* (non-Javadoc)
  * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object)
  */
 public boolean test(Object receiver, String method, Object[] args, Object expectedValue) {
   if (!(receiver instanceof IJavaScriptElement)) {
     return false;
   }
   IJavaScriptElement res = (IJavaScriptElement) receiver;
   if (method.equals(NAME)) {
     return Pattern.matches(toString(expectedValue), res.getElementName());
   } else if (method.equals(IS_IN_JAVA_PROJECT)) {
     IJavaScriptProject javaProject = res.getJavaScriptProject();
     return javaProject != null && javaProject.exists() && javaProject.getProject().isOpen();
   } else if (method.equals(IS_IN_JAVA_PROJECT_WITH_NATURE)) {
     IJavaScriptProject javaProject = res.getJavaScriptProject();
     if (javaProject != null && javaProject.exists() && javaProject.getProject().isOpen()) {
       if (expectedValue != null) {
         try {
           return javaProject.getProject().hasNature(toString(expectedValue));
         } catch (CoreException e) {
           return false;
         }
       }
     }
     return false;
   } else if (method.equals(IS_ON_CLASSPATH)) {
     IJavaScriptProject javaProject = res.getJavaScriptProject();
     if (javaProject != null && javaProject.exists()) {
       return javaProject.isOnIncludepath(res);
     }
     return false;
   } else if (method.equals(IN_SOURCE_FOLDER)) {
     IJavaScriptElement root = res.getAncestor(IJavaScriptElement.PACKAGE_FRAGMENT_ROOT);
     if (root != null) {
       try {
         return ((IPackageFragmentRoot) root).getKind() == IPackageFragmentRoot.K_SOURCE;
       } catch (JavaScriptModelException e) {
         // ignore
       }
     }
     return false;
   } else if (method.equals(IN_ARCHIVE)) {
     IJavaScriptElement root = res.getAncestor(IJavaScriptElement.PACKAGE_FRAGMENT_ROOT);
     if (root != null) {
       return ((IPackageFragmentRoot) root).isArchive();
     }
     return false;
   } else if (method.equals(IN_EXTERNAL_ARCHIVE)) {
     IJavaScriptElement root = res.getAncestor(IJavaScriptElement.PACKAGE_FRAGMENT_ROOT);
     if (root != null) {
       return ((IPackageFragmentRoot) root).isExternal();
     }
     return false;
   } else if (method.equals(PROJECT_OPTION)) {
     IJavaScriptProject project = res.getJavaScriptProject();
     if (project != null) {
       if (args.length == 2) {
         String current = project.getOption(toString(args[0]), true);
         return current != null && current.equals(args[1]);
       } else if (args.length == 1) {
         return project.getOption(toString(args[0]), false) != null;
       }
     }
     return false;
   } else if (method.equals(HAS_TYPE_ON_CLASSPATH)) {
     IJavaScriptProject javaProject = res.getJavaScriptProject();
     if (javaProject != null && javaProject.exists()) {
       try {
         return javaProject.findType(toString(expectedValue)) != null;
       } catch (JavaScriptModelException e) {
         return false;
       }
     }
   }
   return false;
 }