@NotNull
  @Override
  public RelativePoint guessBestPopupLocation(@NotNull DataContext dataContext) {
    Component component = PlatformDataKeys.CONTEXT_COMPONENT.getData(dataContext);
    JComponent focusOwner = component instanceof JComponent ? (JComponent) component : null;

    if (focusOwner == null) {
      Project project = PlatformDataKeys.PROJECT.getData(dataContext);
      IdeFrameImpl frame =
          project == null
              ? null
              : ((WindowManagerEx) WindowManager.getInstance()).getFrame(project);
      focusOwner = frame == null ? null : frame.getRootPane();
      if (focusOwner == null) {
        throw new IllegalArgumentException("focusOwner cannot be null");
      }
    }

    final Point point = PlatformDataKeys.CONTEXT_MENU_POINT.getData(dataContext);
    if (point != null) {
      return new RelativePoint(focusOwner, point);
    }

    Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
    if (editor != null && focusOwner == editor.getContentComponent()) {
      return guessBestPopupLocation(editor);
    } else {
      return guessBestPopupLocation(focusOwner);
    }
  }
  public void update(final AnActionEvent event) {
    super.update(event);

    final Presentation presentation = event.getPresentation();
    final DataContext context = event.getDataContext();
    Module module = (Module) context.getData(LangDataKeys.MODULE.getName());
    PsiFile currentFile = (PsiFile) context.getData(LangDataKeys.PSI_FILE.getName());
    Editor editor = (Editor) context.getData(PlatformDataKeys.EDITOR.getName());
    // VirtualFile currentFile = (VirtualFile)
    // context.getData(PlatformDataKeys.VIRTUAL_FILE.getName());
    if (currentFile != null && editor != null) {
      boolean isSSFile =
          currentFile
              .getFileType()
              .getDefaultExtension()
              .equals(SilverStripeFileType.DEFAULT_EXTENSION);
      SelectionModel selectionModel = editor.getSelectionModel();
      String selectedText = selectionModel.getSelectedText();
      this.selectedText = selectedText;
      this.selectonModel = selectionModel;
      this.editor = editor;
      this.currentFile = currentFile;
      if (selectedText == null) {
        presentation.setEnabled(false);
      }
      if (!isSSFile) {
        presentation.setEnabled(false);
        presentation.setVisible(false);
      }
    } else {
      presentation.setEnabled(false);
      presentation.setVisible(false);
    }
  }
Пример #3
0
 public void playBack(DataContext context) {
   Editor editor = PlatformDataKeys.EDITOR.getData(context);
   final TypedAction typedAction = EditorActionManager.getInstance().getTypedAction();
   char chars[] = myText.toCharArray();
   for (int i = 0; i < chars.length; i++) {
     typedAction.actionPerformed(editor, chars[i], context);
   }
 }
 public void update(AnActionEvent e) {
   DataContext dataContext = e.getDataContext();
   Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
   boolean enabled =
       (editor != null && FileDocumentManager.getInstance().getFile(editor.getDocument()) != null)
           || getElementToCopy(editor, dataContext) != null;
   e.getPresentation().setEnabled(enabled);
   if (ActionPlaces.isPopupPlace(e.getPlace())) {
     e.getPresentation().setVisible(enabled);
   } else {
     e.getPresentation().setVisible(true);
   }
 }
Пример #5
0
  public void update(AnActionEvent event) {
    Presentation presentation = event.getPresentation();
    DataContext dataContext = event.getDataContext();
    Project project = PlatformDataKeys.PROJECT.getData(dataContext);
    presentation.setEnabled(false);
    if (project == null) {
      return;
    }

    Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
    if (editor != null) {
      updateForEditor(dataContext, presentation);
    } else {
      String id = ToolWindowManager.getInstance(project).getActiveToolWindowId();
      updateForToolWindow(id, dataContext, presentation);
    }
  }
  @Override
  public void actionPerformed(AnActionEvent e) {

    DataContext context = e.getDataContext();
    Editor editor = PlatformDataKeys.EDITOR.getData(context);
    PsiFile psiFile = LangDataKeys.PSI_FILE.getData(context);
    if (psiFile == null) return;

    List<GotoRelatedItem> items = getItems(psiFile, editor, context);
    if (items.isEmpty()) return;
    if (items.size() == 1 && items.get(0).getElement() != null) {
      items.get(0).navigate();
      return;
    }
    if (ApplicationManager.getApplication().isUnitTestMode()) {
      System.out.println(items);
    }
    createPopup(items, "Go to Related Files").showInBestPositionFor(context);
  }
  @Override
  public Object getData(final String dataId) {
    final Editor e = validateCurrentEditor();
    if (e == null) return null;

    if (!myProject.isDisposed()) {
      final Object o =
          ((FileEditorManagerImpl) FileEditorManager.getInstance(myProject))
              .getData(dataId, e, myFile);
      if (o != null) return o;
    }

    if (PlatformDataKeys.EDITOR.is(dataId)) {
      return e;
    }
    if (PlatformDataKeys.VIRTUAL_FILE.is(dataId)) {
      return myFile.isValid() ? myFile : null; // fix for SCR 40329
    }
    return null;
  }
  public void actionPerformed(AnActionEvent e) {
    DataContext dataContext = e.getDataContext();
    Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
    Project project = PlatformDataKeys.PROJECT.getData(dataContext);
    PsiElement element = getElementToCopy(editor, dataContext);

    if (!doCopy(element, project, editor) && editor != null) {
      Document document = editor.getDocument();
      PsiFile file = PsiDocumentManager.getInstance(project).getCachedPsiFile(document);
      if (file != null) {
        String toCopy =
            getFileFqn(file) + ":" + (editor.getCaretModel().getLogicalPosition().line + 1);
        CopyPasteManager.getInstance().setContents(new StringSelection(toCopy));
        setStatusBarText(project, toCopy + " has been copied");
      }
      return;
    }

    HighlightManager highlightManager = HighlightManager.getInstance(project);
    EditorColorsManager manager = EditorColorsManager.getInstance();
    TextAttributes attributes =
        manager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
    if (element != null && editor != null) {
      PsiElement nameIdentifier = HighlightUsagesHandler.getNameIdentifier(element);
      if (nameIdentifier != null) {
        highlightManager.addOccurrenceHighlights(
            editor, new PsiElement[] {nameIdentifier}, attributes, true, null);
      } else {
        PsiReference reference =
            TargetElementUtilBase.findReference(editor, editor.getCaretModel().getOffset());
        if (reference != null) {
          highlightManager.addOccurrenceHighlights(
              editor, new PsiReference[] {reference}, attributes, true, null);
        } else {
          highlightManager.addOccurrenceHighlights(
              editor, new PsiElement[] {element}, attributes, true, null);
        }
      }
    }
  }
Пример #9
0
  protected void updateForEditor(DataContext dataContext, Presentation presentation) {
    Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
    if (editor == null) {
      presentation.setVisible(false);
      return;
    }

    Project project = PlatformDataKeys.PROJECT.getData(dataContext);
    if (project == null) {
      return;
    }
    PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());

    PsiElement element = getTargetElement(editor, project);
    boolean result = element != null && CopyHandler.canCopy(new PsiElement[] {element});

    if (!result && file != null) {
      result = CopyHandler.canCopy(new PsiElement[] {file});
    }

    presentation.setEnabled(result);
    presentation.setVisible(true);
  }
Пример #10
0
  public void actionPerformed(AnActionEvent e) {
    final DataContext dataContext = e.getDataContext();
    final Project project = PlatformDataKeys.PROJECT.getData(dataContext);
    if (project == null) {
      return;
    }

    CommandProcessor.getInstance()
        .executeCommand(
            project,
            new Runnable() {
              public void run() {
                PsiDocumentManager.getInstance(project).commitAllDocuments();
              }
            },
            "",
            null);
    final Editor editor = PlatformDataKeys.EDITOR.getData(dataContext);
    PsiElement[] elements;

    PsiDirectory defaultTargetDirectory;
    if (editor != null) {
      PsiElement aElement = getTargetElement(editor, project);
      PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
      if (file == null) return;
      elements = new PsiElement[] {aElement};
      if (aElement == null || !CopyHandler.canCopy(elements)) {
        elements = new PsiElement[] {file};
      }
      defaultTargetDirectory = file.getContainingDirectory();
    } else {
      PsiElement element = LangDataKeys.TARGET_PSI_ELEMENT.getData(dataContext);
      defaultTargetDirectory = element instanceof PsiDirectory ? (PsiDirectory) element : null;
      elements = LangDataKeys.PSI_ELEMENT_ARRAY.getData(dataContext);
    }
    doCopy(elements, defaultTargetDirectory);
  }
Пример #11
0
 private static Editor getEditor(final AnActionEvent e) {
   return PlatformDataKeys.EDITOR.getData(e.getDataContext());
 }