Esempio n. 1
0
  @Override
  public void doUpdate(AnActionEvent event) {
    removeAll();

    SModel modelDescriptor = event.getData(MPSDataKeys.CONTEXT_MODEL);
    if (modelDescriptor == null) {
      setEnabledState(event.getPresentation(), false);
      return;
    }

    if (!(modelDescriptor instanceof EditableSModel)
        || (((EditableSModel) modelDescriptor).isReadOnly())) {
      event.getPresentation().setEnabled(false);
      event.getPresentation().setVisible(false);
      return;
    }

    IScope scope = event.getData(MPSDataKeys.SCOPE);
    IOperationContext context = event.getData(MPSDataKeys.OPERATION_CONTEXT);

    boolean isStubModel =
        SModelStereotype.isStubModelStereotype(SModelStereotype.getStereotype(modelDescriptor));
    if (scope == null || context == null || isStubModel) {
      setEnabledState(event.getPresentation(), false);
      return;
    }

    boolean inEditor = event.getData(MPSDataKeys.LOGICAL_VIEW_SELECTION_SIZE) == null;

    if (!inEditor) {
      Integer selectedItemsCount = event.getData(MPSDataKeys.LOGICAL_VIEW_SELECTION_SIZE);
      boolean singleItemSelected = selectedItemsCount != null && selectedItemsCount == 1;

      if (!singleItemSelected) {
        setEnabledState(event.getPresentation(), false);
        return;
      }

      TreeNode treeNode = event.getData(MPSDataKeys.LOGICAL_VIEW_NODE);

      if (!(treeNode instanceof PackageNode)) {
        myPackage = null;
      } else {
        final PackageNode node = (PackageNode) treeNode;
        myPackage = node.getPackage();
      }
    } else {
      SNode node = event.getData(MPSDataKeys.NODE);
      myPackage = null;
      if (node != null) {
        SNode root = node.getContainingRoot();
        myPackage =
            SNodeAccessUtil.getProperty(root, SNodeUtil.property_BaseConcept_virtualPackage);
      }
    }

    setEnabledState(event.getPresentation(), true);

    List<Language> modelLanguages = SModelOperations.getLanguages(modelDescriptor, scope);

    LanguageAspect aspect = Language.getModelAspect(modelDescriptor);
    if (aspect != null) {
      SModuleReference ref = aspect.getMainLanguage();
      Language lang = scope.getLanguage(ref);
      if (lang != null) {
        modelLanguages.remove(lang);

        for (SNode conceptDeclaration : lang.getConceptDeclarations()) {
          if (ModelConstraintsManager.canBeRoot(
              context, NameUtil.nodeFQName(conceptDeclaration), modelDescriptor)) {
            add(
                new NewRootNodeAction(
                    new jetbrains.mps.smodel.SNodePointer(conceptDeclaration), modelDescriptor));
          }
        }

        addSeparator();
      }
    }

    Collections.sort(modelLanguages, new ToStringComparator());

    List<Language> languagesWithRoots = new ArrayList<Language>();
    for (final Language language : modelLanguages) {
      for (SNode conceptDeclaration : language.getConceptDeclarations()) {
        if (ModelConstraintsManager.canBeRoot(
            context, NameUtil.nodeFQName(conceptDeclaration), modelDescriptor)) {
          languagesWithRoots.add(language);
          break;
        }
      }
    }

    boolean plain = myPlain || (languagesWithRoots.size() == 1 && aspect == null);

    for (final Language language : languagesWithRoots) {
      String name = language.getModuleName();
      Icon icon = IconManager.getIconForNamespace(language.getModuleName());
      BaseGroup langRootsGroup;

      if (!plain) {
        langRootsGroup = new BaseGroup(NameUtil.compactNamespace(name), name, icon);
        langRootsGroup.setPopup(true);
      } else {
        langRootsGroup = this;
      }

      for (SNode conceptDeclaration : language.getConceptDeclarations()) {
        if (ModelConstraintsManager.getInstance()
            .canBeRoot(context, NameUtil.nodeFQName(conceptDeclaration), modelDescriptor)) {
          langRootsGroup.add(
              new NewRootNodeAction(
                  new jetbrains.mps.smodel.SNodePointer(conceptDeclaration), modelDescriptor));
        }
      }
      if (!plain) {
        this.add(langRootsGroup);
      } else {
        this.addSeparator();
      }
    }

    if (getChildrenCount() == 0) {
      add(
          ActionManager.getInstance()
              .getAction(
                  "jetbrains.mps.ide.editor.actions.AddLanguageImport_Action" /* FIXME AddLanguageImport_Action.class.getName()*/));
    }
  }
Esempio n. 2
0
  public int getModelKind(SModel model, @Nullable SReference reference) {
    DataSource source = (model != null ? model.getSource() : null);
    IFile modelFile =
        (source instanceof FileDataSource ? ((FileDataSource) source).getFile() : null);
    if (modelFile != null) {
      String filePath = modelFile.getAbsolutePath().replace('\\', '/');
      if (filePath.startsWith(languagesUtilPath)) {
        return OTHER;
      }
    }

    SModule module = model.getModule();
    if (module instanceof Language) {
      LanguageAspect aspect = Language.getModelAspect(model);
      if (aspect != null) {
        switch (aspect) {
          case ACTIONS:
            return EDITOR;
          case BEHAVIOR:
            return CORE;
          case CONSTRAINTS:
            return CORE;
          case DATA_FLOW:
            return CORE;
          case EDITOR:
            return EDITOR;
          case FIND_USAGES:
            return CORE;
          case INTENTIONS:
            return EDITOR;
          case PLUGIN:
            return WORKBENCH;
          case REFACTORINGS:
            return CORE;
          case SCRIPTS:
            return CORE;
          case STRUCTURE:
            return CORE;
          case STUBS:
            return CORE;
          case TEST:
            return EDITOR;
          case TEXT_GEN:
            return CORE;
          case TYPESYSTEM:
            return CORE;
          default:
        }
      }
      return CORE;

    } else if (module instanceof Solution) {

      String moduleFqName = module.getModuleName();
      if (moduleFqName.equals("JDK")) {
        return CORE;
      }
      if (moduleFqName.equals("MPS.Core")) {
        return CORE;
      }
      if (moduleFqName.equals("MPS.Editor")) {
        return EDITOR;
      }
      if (moduleFqName.equals("MPS.Workbench")) {
        return WORKBENCH;
      }
      if (moduleFqName.equals("MPS.Classpath")) {
        SNode refTargetRoot = reference.getTargetNode().getContainingRoot();
        if (SNodeOperations.isInstanceOf(
            refTargetRoot, "jetbrains.mps.baseLanguage.structure.Classifier")) {
          String cName =
              SPropertyOperations.getString(
                  SNodeOperations.cast(
                      refTargetRoot, "jetbrains.mps.baseLanguage.structure.Classifier"),
                  "name");
          String modelName = model.getModelName();
          if (findInModule(coreModule, modelName, cName)) {
            return CORE;
          }
          if (findInModule(editorModule, modelName, cName)) {
            return EDITOR;
          }
          return WORKBENCH;
        }
        return OTHER;
      }

      Solution sol = (Solution) module;
      switch (sol.getKind()) {
        case NONE:
          return OTHER;
        case PLUGIN_CORE:
          return CORE;
        case PLUGIN_EDITOR:
          return EDITOR;
        case PLUGIN_OTHER:
          return WORKBENCH;
        default:
      }
    }
    return OTHER;
  }