Пример #1
0
  /**
   * Insert the method's description here. Creation date: (21.12.2000 22:24:23)
   *
   * @return boolean
   * @param object com.cosylab.vdct.graphics.objects.VisibleObject
   * @param zoomOnHilited sets the option whether the hilited object should be zoomed in
   */
  public boolean setAsHilited(VisibleObject object, boolean zoomOnHilited) {
    this.zoomOnHilited = zoomOnHilited;

    if (zoomOnHilited) {
      DrawingSurface.getInstance().repaint();
      hilitedObjects.clear();
      if (hilitedObject != null) {
        hilitedObjects.add(hilitedObject);
      }
    }

    if (object == null) {
      hilitedObjects.clear();
      hilitedObject = null;
      return false;
    }

    if (object != hilitedObject || hilitedObjects.size() == 1) {

      hilitedObject = object;

      // initialization
      hilitedObjects.clear();
      hilitedObjects.add(hilitedObject);
      Object obj = hilitedObject;

      // inlinks
      Vector outlinks = null;
      if (obj instanceof MultiInLink) {
        outlinks = ((MultiInLink) obj).getOutlinks();
      } else if (obj instanceof InLink) {
        outlinks = new Vector();
        outlinks.add(((InLink) obj).getOutput());
      }

      if (!zoomOnHilited) {
        if (outlinks != null)
          for (int i = 0; i < outlinks.size(); i++) {
            obj = outlinks.elementAt(i);
            hilitedObjects.add(obj);
            while (obj instanceof InLink) {
              obj = ((InLink) obj).getOutput();
              hilitedObjects.add(obj);
            }
          }

        // outLinks
        obj = hilitedObject;
        while (obj instanceof OutLink) {
          obj = ((OutLink) obj).getInput();
          hilitedObjects.add(obj);
        }
      }
      return true;
    } else return false;
  }
  @Override
  public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file)
      throws IncorrectOperationException {
    if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;

    PsiMethod[] constructors = myClass.getConstructors();
    if (constructors.length == 0) {
      final AddDefaultConstructorFix defaultConstructorFix = new AddDefaultConstructorFix(myClass);
      ApplicationManager.getApplication()
          .runWriteAction(
              new Runnable() {
                @Override
                public void run() {
                  defaultConstructorFix.invoke(project, editor, file);
                }
              });
      constructors = myClass.getConstructors();
    }
    Arrays.sort(
        constructors,
        new Comparator<PsiMethod>() {
          @Override
          public int compare(PsiMethod c1, PsiMethod c2) {
            final PsiMethod cc1 = RefactoringUtil.getChainedConstructor(c1);
            final PsiMethod cc2 = RefactoringUtil.getChainedConstructor(c2);
            if (cc1 == c2) return 1;
            if (cc2 == c1) return -1;
            if (cc1 == null) {
              return cc2 == null ? 0 : compare(c1, cc2);
            } else {
              return cc2 == null ? compare(cc1, c2) : compare(cc1, cc2);
            }
          }
        });
    final ArrayList<PsiMethod> constrs =
        filterConstructorsIfFieldAlreadyAssigned(constructors, getField());
    if (constrs.size() > 1) {
      final PsiMethodMember[] members = new PsiMethodMember[constrs.size()];
      int i = 0;
      for (PsiMethod constructor : constrs) {
        members[i++] = new PsiMethodMember(constructor);
      }
      final List<PsiMethodMember> elements;
      if (ApplicationManager.getApplication().isUnitTestMode()) {
        elements = Arrays.asList(members);
      } else {
        final MemberChooser<PsiMethodMember> chooser =
            new MemberChooser<PsiMethodMember>(members, false, true, project);
        chooser.setTitle("Choose constructors to add parameter to");
        chooser.show();
        elements = chooser.getSelectedElements();
        if (elements == null) return;
      }

      for (PsiMethodMember member : elements) {
        if (!addParameterToConstructor(
            project, file, editor, member.getElement(), new PsiField[] {getField()})) break;
      }

    } else if (!constrs.isEmpty()) {
      final Collection<SmartPsiElementPointer<PsiField>> fieldsToFix = getFieldsToFix();
      try {
        final PsiMethod constructor = constrs.get(0);
        final LinkedHashSet<PsiField> fields = new LinkedHashSet<PsiField>();
        getFieldsToFix().add(myField);
        for (SmartPsiElementPointer<PsiField> elementPointer : fieldsToFix) {
          final PsiField field = elementPointer.getElement();
          if (field != null
              && isAvailable(field)
              && filterConstructorsIfFieldAlreadyAssigned(new PsiMethod[] {constructor}, field)
                  .contains(constructor)) {
            fields.add(field);
          }
        }
        if (constrs.size() == constructors.length
            && fields.size() > 1
            && !ApplicationManager.getApplication().isUnitTestMode()) {
          PsiFieldMember[] members = new PsiFieldMember[fields.size()];
          int i = 0;
          for (PsiField field : fields) {
            members[i++] = new PsiFieldMember(field);
          }
          MemberChooser<PsiElementClassMember> chooser =
              new MemberChooser<PsiElementClassMember>(members, false, true, project);
          chooser.setTitle("Choose Fields to Generate Constructor Parameters for");
          chooser.show();
          if (chooser.getExitCode() != DialogWrapper.OK_EXIT_CODE) return;
          final List<PsiElementClassMember> selectedElements = chooser.getSelectedElements();
          if (selectedElements == null) return;
          fields.clear();
          for (PsiElementClassMember member : selectedElements) {
            fields.add((PsiField) member.getElement());
          }
        }

        addParameterToConstructor(
            project,
            file,
            editor,
            constructor,
            constrs.size() == constructors.length
                ? fields.toArray(new PsiField[fields.size()])
                : new PsiField[] {getField()});
      } finally {
        fieldsToFix.clear();
      }
    }
  }