Java Code Examples for com.intellij.openapi.actionSystem.DataContext#getData()

The following examples show how to use com.intellij.openapi.actionSystem.DataContext#getData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GotoTestRelatedProvider.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public List<? extends GotoRelatedItem> getItems(@Nonnull DataContext context) {
  final PsiFile file = context.getData(LangDataKeys.PSI_FILE);
  List<PsiElement> result;
  final boolean isTest = TestFinderHelper.isTest(file);
  if (isTest) {
    result = TestFinderHelper.findClassesForTest(file);
  } else {
    result = TestFinderHelper.findTestsForClass(file);
  }

  if (!result.isEmpty()) {
    final List<GotoRelatedItem> items = new ArrayList<GotoRelatedItem>();
    for (PsiElement element : result) {
      items.add(new GotoRelatedItem(element, isTest ? "Tests" : "Testee classes"));
    }
    return items;
  }
  return super.getItems(context);
}
 
Example 2
Source File: PasteReferenceProvider.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private static String getCopiedFqn(final DataContext context) {
  Producer<Transferable> producer = context.getData(PasteAction.TRANSFERABLE_PROVIDER);

  if (producer != null) {
    Transferable transferable = producer.produce();
    if (transferable != null) {
      try {
        return (String)transferable.getTransferData(CopyReferenceAction.ourFlavor);
      }
      catch (Exception ignored) { }
    }
    return null;
  }

  return CopyPasteManager.getInstance().getContents(CopyReferenceAction.ourFlavor);
}
 
Example 3
Source File: InlineRefactoringActionHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(@Nonnull final Project project, Editor editor, PsiFile file, DataContext dataContext) {
  editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);

  PsiElement element = dataContext.getData(LangDataKeys.PSI_ELEMENT);
  if (element == null) {
    element = BaseRefactoringAction.getElementAtCaret(editor, file);
  }
  if (element != null) {
    for(InlineActionHandler handler: Extensions.getExtensions(InlineActionHandler.EP_NAME)) {
      if (handler.canInlineElementInEditor(element, editor)) {
        handler.inlineElement(project, editor, element);
        return;
      }
    }

    if (invokeInliner(editor, element)) return;

    String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.wrong.caret.position.method.or.local.name"));
    CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, null);
  }
}
 
Example 4
Source File: RenameLibraryHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void invoke(@Nonnull final Project project, @Nonnull PsiElement[] elements, @Nonnull DataContext dataContext) {
  final Library library = dataContext.getData(LangDataKeys.LIBRARY);
  LOG.assertTrue(library != null);
  Messages.showInputDialog(project,
                           IdeBundle.message("prompt.enter.new.library.name"),
                           IdeBundle.message("title.rename.library"),
                           Messages.getQuestionIcon(),
                           library.getName(),
                           new MyInputValidator(project, library));
}
 
Example 5
Source File: CopyReferenceAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static List<PsiElement> getElementsToCopy(@Nullable final Editor editor, final DataContext dataContext) {
  List<PsiElement> elements = ContainerUtil.newArrayList();
  if (editor != null) {
    PsiReference reference = TargetElementUtil.findReference(editor);
    if (reference != null) {
      ContainerUtil.addIfNotNull(elements, reference.getElement());
    }
  }

  if (elements.isEmpty()) {
    ContainerUtil.addIfNotNull(elements, dataContext.getData(CommonDataKeys.PSI_ELEMENT));
  }

  if (elements.isEmpty() && editor == null) {
    final Project project = dataContext.getData(CommonDataKeys.PROJECT);
    VirtualFile[] files = dataContext.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
    if (project != null && files != null) {
      for (VirtualFile file : files) {
        ContainerUtil.addIfNotNull(elements, PsiManager.getInstance(project).findFile(file));
      }
    }
  }

  return ContainerUtil.mapNotNull(elements, new Function<PsiElement, PsiElement>() {
    @Override
    public PsiElement fun(PsiElement element) {
      return element instanceof PsiFile && !((PsiFile)element).getViewProvider().isPhysical() ? null : adjustElement(element);
    }
  });
}
 
Example 6
Source File: ProjectNameMacro.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public String expand(DataContext dataContext) {
  final Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project == null) {
    return null;
  }
  return project.getName();
}
 
Example 7
Source File: ModuleDirMacro.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public String expand(DataContext dataContext) {
  final Module module = dataContext.getData(LangDataKeys.MODULE);
  if(module == null) {
    return null;
  }
  return module.getModuleDirPath();
}
 
Example 8
Source File: FileListPasteProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void performPaste(@Nonnull DataContext dataContext) {
  final Project project = dataContext.getData(CommonDataKeys.PROJECT);
  final IdeView ideView = dataContext.getData(LangDataKeys.IDE_VIEW);
  if (project == null || ideView == null) return;

  if (!FileCopyPasteUtil.isFileListFlavorAvailable()) return;

  final Transferable contents = CopyPasteManager.getInstance().getContents();
  if (contents == null) return;
  final List<File> fileList = FileCopyPasteUtil.getFileList(contents);
  if (fileList == null) return;

  final List<PsiElement> elements = new ArrayList<PsiElement>();
  for (File file : fileList) {
    final VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
    if (vFile != null) {
      final PsiManager instance = PsiManager.getInstance(project);
      PsiFileSystemItem item = vFile.isDirectory() ? instance.findDirectory(vFile) : instance.findFile(vFile);
      if (item != null) {
        elements.add(item);
      }
    }
  }

  if (elements.size() > 0) {
    final PsiDirectory dir = ideView.getOrChooseDirectory();
    if (dir != null) {
      final boolean move = LinuxDragAndDropSupport.isMoveOperation(contents);
      if (move) {
        new MoveFilesOrDirectoriesHandler().doMove(PsiUtilCore.toPsiElementArray(elements), dir);
      }
      else {
        new CopyFilesOrDirectoriesHandler().doCopy(PsiUtilCore.toPsiElementArray(elements), dir);
      }
    }
  }
}
 
Example 9
Source File: ExportTestResultsAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean isEnabled(DataContext dataContext) {
  if (myModel == null) {
    return false;
  }

  if (dataContext.getData(CommonDataKeys.PROJECT) == null) {
    return false;
  }

  return !myModel.getRoot().isInProgress();
}
 
Example 10
Source File: FileRelativeDirMacro.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public String expand(DataContext dataContext) {
  final VirtualFile baseDir = dataContext.getData(PlatformDataKeys.PROJECT_FILE_DIRECTORY);
  if (baseDir == null) {
    return null;
  }

  VirtualFile dir = getVirtualDirOrParent(dataContext);
  if (dir == null) return null;
  return FileUtil.getRelativePath(VfsUtil.virtualToIoFile(baseDir), VfsUtil.virtualToIoFile(dir));
}
 
Example 11
Source File: DesktopWindowWatcher.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * This method should get notifications abount changes of focused window.
 * Only <code>focusedWindow</code> property is acceptable.
 *
 * @throws IllegalArgumentException if property name isn't <code>focusedWindow</code>.
 */
@Override
public final void propertyChange(final PropertyChangeEvent e) {
  if (LOG.isDebugEnabled()) {
    LOG.debug("enter: propertyChange(" + e + ")");
  }
  if (!FOCUSED_WINDOW_PROPERTY.equals(e.getPropertyName())) {
    throw new IllegalArgumentException("unknown property name: " + e.getPropertyName());
  }
  synchronized (myLock) {
    final consulo.ui.Window window = TargetAWT.from((Window)e.getNewValue());
    if (window == null || myApplication.isDisposed()) {
      return;
    }
    if (!myWindow2Info.containsKey(window)) {
      myWindow2Info.put(window, new WindowInfo(window, true));
    }
    myFocusedWindow = window;
    final Project project = DataManager.getInstance().getDataContext(myFocusedWindow).getData(CommonDataKeys.PROJECT);
    for (Iterator<consulo.ui.Window> i = myFocusedWindows.iterator(); i.hasNext(); ) {
      final consulo.ui.Window w = i.next();
      final DataContext dataContext = DataManager.getInstance().getDataContext(TargetAWT.to(w));
      if (project == dataContext.getData(CommonDataKeys.PROJECT)) {
        i.remove();
      }
    }
    myFocusedWindows.add(myFocusedWindow);
    // Set new root frame
    final IdeFrame frame = IdeFrameUtil.findRootIdeFrame(window);

    if (frame != null) {
      JOptionPane.setRootFrame((Frame)TargetAWT.to(frame.getWindow()));
    }
  }
  if (LOG.isDebugEnabled()) {
    LOG.debug("exit: propertyChange()");
  }
}
 
Example 12
Source File: XDebuggerEditBreakpointActionHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isEnabled(@Nonnull Project project, AnActionEvent event) {
  DataContext dataContext = event.getDataContext();
  Editor editor = dataContext.getData(CommonDataKeys.EDITOR);
  if (editor == null) return false;
  final Pair<GutterIconRenderer,Object> pair = XBreakpointUtil.findSelectedBreakpoint(project, editor);
  return pair.first != null && pair.second instanceof XLineBreakpointImpl;
}
 
Example 13
Source File: CollapseAllAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected TreeExpander getExpander(DataContext dataContext) {
  return dataContext.getData(PlatformDataKeys.TREE_EXPANDER);
}
 
Example 14
Source File: HighlightManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void requestHideHighlights(final DataContext dataContext) {
  final Editor editor = dataContext.getData(PlatformDataKeys.EDITOR);
  if (editor == null) return;
  hideHighlights(editor, HIDE_BY_ANY_KEY);
}
 
Example 15
Source File: MoveModulesOutsideGroupAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
  final DataContext dataContext = e.getDataContext();
  final Module[] modules = dataContext.getData(LangDataKeys.MODULE_CONTEXT_ARRAY);
  MoveModulesToGroupAction.doMove(modules, null, dataContext);
}
 
Example 16
Source File: ExportToHTMLManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Should be invoked in event dispatch thread
 */
@RequiredUIAccess
public static void executeExport(final DataContext dataContext) throws FileNotFoundException {
  PsiDirectory psiDirectory = null;
  PsiElement psiElement = dataContext.getData(LangDataKeys.PSI_ELEMENT);
  if(psiElement instanceof PsiDirectory) {
    psiDirectory = (PsiDirectory)psiElement;
  }
  final PsiFile psiFile = dataContext.getData(LangDataKeys.PSI_FILE);
  Project project = dataContext.getData(CommonDataKeys.PROJECT);
  String shortFileName = null;
  String directoryName = null;
  if(psiFile != null || psiDirectory != null) {
    if(psiFile != null) {
      shortFileName = psiFile.getVirtualFile().getName();
      if(psiDirectory == null) {
        psiDirectory = psiFile.getContainingDirectory();
      }
    }
    if(psiDirectory != null) {
      directoryName = psiDirectory.getVirtualFile().getPresentableUrl();
    }
  }

  Editor editor = dataContext.getData(PlatformDataKeys.EDITOR);
  boolean isSelectedTextEnabled = false;
  if(editor != null && editor.getSelectionModel().hasSelection()) {
    isSelectedTextEnabled = true;
  }
  ExportToHTMLDialog exportToHTMLDialog = new ExportToHTMLDialog(shortFileName, directoryName, isSelectedTextEnabled, project);

  ExportToHTMLSettings exportToHTMLSettings = ExportToHTMLSettings.getInstance(project);
  if(exportToHTMLSettings.OUTPUT_DIRECTORY == null) {
    final VirtualFile baseDir = project.getBaseDir();

    if (baseDir != null) {
      exportToHTMLSettings.OUTPUT_DIRECTORY = baseDir.getPresentableUrl() + File.separator + "exportToHTML";
    }
    else {
      exportToHTMLSettings.OUTPUT_DIRECTORY = "";
    }
  }
  exportToHTMLDialog.reset();
  exportToHTMLDialog.show();
  if(!exportToHTMLDialog.isOK()) {
    return;
  }
  try {
    exportToHTMLDialog.apply();
  }
  catch (ConfigurationException e) {
    Messages.showErrorDialog(project, e.getMessage(), CommonBundle.getErrorTitle());
  }

  PsiDocumentManager.getInstance(project).commitAllDocuments();
  final String outputDirectoryName = exportToHTMLSettings.OUTPUT_DIRECTORY;
  if(exportToHTMLSettings.getPrintScope() != PrintSettings.PRINT_DIRECTORY) {
    if(psiFile == null || psiFile.getText() == null) {
      return;
    }
    final String dirName = constructOutputDirectory(psiFile, outputDirectoryName);
    HTMLTextPainter textPainter = new HTMLTextPainter(psiFile, project, dirName, exportToHTMLSettings.PRINT_LINE_NUMBERS);
    if(exportToHTMLSettings.getPrintScope() == PrintSettings.PRINT_SELECTED_TEXT && editor != null && editor.getSelectionModel().hasSelection()) {
      int firstLine = editor.getDocument().getLineNumber(editor.getSelectionModel().getSelectionStart());
      textPainter.setSegment(editor.getSelectionModel().getSelectionStart(), editor.getSelectionModel().getSelectionEnd(), firstLine);
    }
    textPainter.paint(null, psiFile.getFileType());
    if (exportToHTMLSettings.OPEN_IN_BROWSER) {
      BrowserUtil.browse(textPainter.getHTMLFileName());
    }
  }
  else {
    myLastException = null;
    ExportRunnable exportRunnable = new ExportRunnable(exportToHTMLSettings, psiDirectory, outputDirectoryName, project);
    ProgressManager.getInstance().runProcessWithProgressSynchronously(exportRunnable, CodeEditorBundle.message("export.to.html.title"), true, project);
    if (myLastException != null) {
      throw myLastException;
    }
  }
}
 
Example 17
Source File: FileListPasteProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isPasteEnabled(@Nonnull DataContext dataContext) {
  return dataContext.getData(LangDataKeys.IDE_VIEW) != null &&
         FileCopyPasteUtil.isFileListFlavorAvailable();
}
 
Example 18
Source File: CreateTemplateInPackageAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected boolean isAvailable(final DataContext dataContext) {
  final Project project = dataContext.getData(CommonDataKeys.PROJECT);
  final IdeView view = dataContext.getData(LangDataKeys.IDE_VIEW);
  if (project == null || view == null || view.getDirectories().length == 0) {
    return false;
  }

  final Module module = dataContext.getData(LangDataKeys.MODULE);
  if (module == null) {
    return false;
  }

  final Class moduleExtensionClass = getModuleExtensionClass();
  if (moduleExtensionClass != null && ModuleUtilCore.getExtension(module, moduleExtensionClass) == null) {
    return false;
  }

  if (!myInSourceOnly) {
    return true;
  }

  PsiPackageSupportProvider[] extensions = PsiPackageSupportProvider.EP_NAME.getExtensions();

  ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
  for (PsiDirectory dir : view.getDirectories()) {
    boolean accepted = false;
    for (PsiPackageSupportProvider provider : extensions) {
      if (provider.acceptVirtualFile(module, dir.getVirtualFile())) {
        accepted = true;
        break;
      }
    }

    if (accepted && projectFileIndex.isInSourceContent(dir.getVirtualFile()) && checkPackageExists(dir)) {
      return true;
    }
  }

  return false;
}
 
Example 19
Source File: FileEncodingMacro.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public String expand(DataContext dataContext) {
  VirtualFile file = dataContext.getData(PlatformDataKeys.VIRTUAL_FILE);
  if (file == null) return null;
  return file.getCharset().displayName();
}
 
Example 20
Source File: ChangeViewTypeActionBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected static TypeHierarchyBrowserBase getTypeHierarchyBrowser(final DataContext context) {
  return context.getData(TypeHierarchyBrowserBase.DATA_KEY);
}