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); } }
@Override public void actionPerformed(AnActionEvent e) { Project project = e.getData(PlatformDataKeys.PROJECT); Editor editor = e.getData(PlatformDataKeys.EDITOR); if (editor == null) { return; } if (!editor.getDocument().isWritable()) { return; } Document document = editor.getDocument(); SelectionModel selection = editor.getSelectionModel(); String selectedText = selection.getSelectedText(); String autoAlignedText; int startOffset; int endOffset; if (selectedText != null) { // just align the selected text autoAlignedText = aligner.align(selectedText); startOffset = selection.getSelectionStart(); endOffset = selection.getSelectionEnd(); } else { // auto-align the whole document autoAlignedText = aligner.align(document.getText()); startOffset = 0; endOffset = document.getTextLength(); } replaceString(project, document, autoAlignedText, startOffset, endOffset); }