示例#1
0
  public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
    try {
      final PackageNode treeNode =
          (PackageNode) ((TreeNode) MapSequence.fromMap(_params).get("ppNode"));
      final String name = treeNode.getPackage();
      ModelAccess modelAccess =
          ((MPSProject) MapSequence.fromMap(_params).get("project"))
              .getRepository()
              .getModelAccess();
      final String newName =
          JOptionPane.showInputDialog(
              ((Frame) MapSequence.fromMap(_params).get("frame")), "Enter New Package Name", name);
      if (newName == null) {
        return;
      }

      modelAccess.executeCommandInEDT(
          new Runnable() {
            public void run() {
              Set<SNode> nodesUnderPackage = treeNode.getNodesUnderPackage();
              for (SNode node : nodesUnderPackage) {
                String oldPackage = SPropertyOperations.getString(node, "virtualPackage");
                String newPackage = newName + oldPackage.substring(name.length());
                SPropertyOperations.set(
                    node, "virtualPackage", (newPackage.length() > 0 ? newPackage : null));
              }
            }
          });
    } catch (Throwable t) {
      if (LOG.isEnabledFor(Priority.ERROR)) {
        LOG.error("User's action execute method failed. Action:" + "RenamePackage", t);
      }
    }
  }
  @Override
  protected void doRefactoringAction() {
    final boolean needToRegenerate = myRegenerateLanguage.getModel().isSelected();

    final String fqName = getCurrentValue();
    if (MPSModuleRepository.getInstance().getModuleByFqName(fqName) != null) {
      setErrorText("Duplicate language name");
      return;
    }
    if (!((SourceVersion.isName(fqName)))) {
      setErrorText("Language namespace should be valid Java package");
      return;
    }
    final LanguageRenamer renamer = new LanguageRenamer(myProject, myLanguage, fqName);
    ModelAccess modelAccess = myProject.getRepository().getModelAccess();
    modelAccess.executeCommand(
        new Runnable() {
          public void run() {
            renamer.rename(needToRegenerate);
            renamer.update();
          }
        });
    if (needToRegenerate) {
      final Map<SModule, List<SModel>> langs = new LinkedHashMap<SModule, List<SModel>>();
      modelAccess.runReadAction(
          new Runnable() {
            public void run() {
              RefactoringUtil.fillLanguageAndItsExtendingLanguageModels(myLanguage, langs);
            }
          });
      for (final List<SModel> models : langs.values()) {
        ModuleContext context = new ModuleContext(myLanguage, myProject);
        MakeSession sess = new MakeSession(context);
        if (IMakeService.INSTANCE.get().openNewSession(sess)) {
          IMakeService.INSTANCE
              .get()
              .make(sess, new ModelsToResources(context, models).resources(false));
        }
        //         GeneratorUIFacade.getInstance().generateModels(new ModuleContext(myLanguage,
        // myProject), params.getModelDescriptors(),
        // GeneratorUIFacade.getInstance().getDefaultGenerationHandler(), true, false);
      }
    }
    super.doRefactoringAction();
  }
  @Override
  public boolean testRefactoring(
      final Project project,
      final SModel sandbox1,
      final SModel sandbox2,
      final Language testRefactoringLanguage,
      final Language testRefactoringTargetLanguage) {
    final String newConceptName = "MyVeryGoodConcept2";
    final ModelAccess modelAccess = project.getRepository().getModelAccess();
    final Wrappers._T<IRefactoring> refactoring = new Wrappers._T<IRefactoring>();
    modelAccess.runReadAction(
        new Runnable() {
          public void run() {
            refactoring.value =
                RefactoringUtil.getRefactoringByClassName(
                    BehaviorReflection.invokeVirtual(
                        String.class,
                        SNodeOperations.getNode(
                            "r:de5b7214-45ee-4f6d-89bf-acde59cdb050(jetbrains.mps.lang.structure.refactorings)",
                            "1347577327951781517"),
                        "virtual_getFqName_1213877404258",
                        new Object[] {}));
          }
        });

    final RefactoringContext refactoringContext =
        new RefactoringContext(project, refactoring.value);
    refactoringContext.setCurrentOperationContext(new ProjectOperationContext(project));

    modelAccess.runReadAction(
        new Runnable() {
          public void run() {
            final SModel structureModelDescriptor =
                testRefactoringLanguage.getStructureModelDescriptor();
            refactoringContext.setParameter(
                RenameConceptRefactoringTester.STRMD, structureModelDescriptor);
            SModel model = structureModelDescriptor;
            SNode concept = SModelOperations.getRootByName(model, "MyVeryGoodConcept1");
            refactoringContext.setSelectedNode(concept);
            refactoringContext.setSelectedModel(structureModelDescriptor);
            refactoringContext.setParameter("newName", newConceptName);
          }
        });
    new RefactoringTestFacade().doExecuteInTest(refactoringContext);
    final boolean[] result = new boolean[] {false};
    ThreadUtils.runInUIThreadAndWait(
        new Runnable() {
          @Override
          public void run() {
            modelAccess.runReadAction(
                new Runnable() {
                  public void run() {
                    try {
                      if (sandbox1.isLoaded()) {
                        System.err.println(
                            "test environment is invalid: model sandbox1 is already initialized, should be not");
                        result[0] = false;
                        return;
                      }
                      SModel sModel = sandbox1;
                      String conceptFqName =
                          sModel.getRootNodes().iterator().next().getConcept().getConceptId();
                      SModel structureModelDescriptor =
                          (SModel)
                              refactoringContext.getParameter(RenameConceptRefactoringTester.STRMD);
                      result[0] =
                          conceptFqName.equals(
                              structureModelDescriptor.getReference().getModelName()
                                  + "."
                                  + newConceptName);
                    } catch (Throwable t) {
                      t.printStackTrace();
                      result[0] = false;
                    }
                  }
                });
          }
        });
    return result[0];
  }
 public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
   try {
     final Wrappers._T<List<String>> packages = new Wrappers._T<List<String>>();
     final Wrappers._T<String> oldPackage = new Wrappers._T<String>();
     ModelAccess modelAccess =
         ((MPSProject) MapSequence.fromMap(_params).get("project"))
             .getRepository()
             .getModelAccess();
     modelAccess.runReadAction(
         new Runnable() {
           public void run() {
             packages.value =
                 SetNodePackage_Action.this.fetchExistingPackages(
                     ((List<SNode>) MapSequence.fromMap(_params).get("nodes")), _params);
             oldPackage.value =
                 SPropertyOperations.getString(
                     ListSequence.fromList(
                             ((List<SNode>) MapSequence.fromMap(_params).get("nodes")))
                         .first(),
                     "virtualPackage");
           }
         });
     final SetNodePackageDialog dialog =
         new SetNodePackageDialog(
             ((MPSProject) MapSequence.fromMap(_params).get("project")), packages.value);
     dialog.setPackage(oldPackage.value);
     dialog.show();
     if (dialog.isCancelled()) {
       return;
     }
     modelAccess.executeCommandInEDT(
         new Runnable() {
           public void run() {
             for (SNode node :
                 ListSequence.fromList(
                     ((List<SNode>) MapSequence.fromMap(_params).get("nodes")))) {
               SPropertyOperations.set(node, "virtualPackage", dialog.getPackage());
               if (SNodeOperations.isInstanceOf(
                   node, "jetbrains.mps.lang.structure.structure.AbstractConceptDeclaration")) {
                 for (SNode aspect :
                     ListSequence.fromList(
                         BehaviorReflection.invokeNonVirtual(
                             (Class<List<SNode>>) ((Class) Object.class),
                             SNodeOperations.cast(
                                 node,
                                 "jetbrains.mps.lang.structure.structure.AbstractConceptDeclaration"),
                             "jetbrains.mps.lang.structure.structure.AbstractConceptDeclaration",
                             "call_findAllAspects_7754459869734028917",
                             new Object[] {}))) {
                   SPropertyOperations.set(
                       ((SNode) aspect), "virtualPackage", dialog.getPackage());
                 }
               }
             }
           }
         });
   } catch (Throwable t) {
     if (LOG.isEnabledFor(Level.ERROR)) {
       LOG.error("User's action execute method failed. Action:" + "SetNodePackage", t);
     }
   }
 }