Package com.intellij.codeInsight.actions

Source Code of com.intellij.codeInsight.actions.CodeInsightAction

/*
* Copyright 2000-2007 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.intellij.codeInsight.actions;

import com.intellij.codeInsight.CodeInsightActionHandler;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.Nullable;

/**
* @author Dmitry Avdeev
*/
public abstract class CodeInsightAction extends AnAction {

  public void actionPerformed(AnActionEvent e) {
    DataContext dataContext = e.getDataContext();
    Project project = DataKeys.PROJECT.getData(dataContext);
    Editor editor = getEditor(dataContext, project);
    actionPerformedImpl(project, editor);
  }

  @Nullable
  protected Editor getEditor(final DataContext dataContext, final Project project) {
    return DataKeys.EDITOR.getData(dataContext);
  }

  public void actionPerformedImpl(final Project project, final Editor editor) {
    if (editor == null) return;
    final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    if (psiFile == null) return;
    CommandProcessor.getInstance().executeCommand(project, new Runnable() {
      public void run() {
        final CodeInsightActionHandler handler = getHandler();
        final Runnable action = new Runnable() {
          public void run() {
            if (!ApplicationManager.getApplication().isUnitTestMode() && !editor.getContentComponent().isShowing()) return;
            handler.invoke(project, editor, psiFile);
          }
        };
        if (handler.startInWriteAction()) {
          ApplicationManager.getApplication().runWriteAction(action);
        }
        else {
          action.run();
        }
      }
    }, getCommandName(), editor.getDocument());
  }

  public void update(AnActionEvent event) {
    Presentation presentation = event.getPresentation();
    DataContext dataContext = event.getDataContext();

    Project project = DataKeys.PROJECT.getData(dataContext);
    if (project == null) {
      presentation.setEnabled(false);
      return;
    }

    Editor editor = getEditor(dataContext, project);
    if (editor == null) {
      presentation.setEnabled(false);
      return;
    }

    PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    if (file == null || !isValidForFile(project, editor, file)) {
      presentation.setEnabled(false);
    }
    else {
      presentation.setEnabled(isEnabledForFile(project, editor, file));
    }
  }

  protected boolean isValidForFile(Project project, Editor editor, final PsiFile file) {
    return true;
  }

  protected boolean isEnabledForFile(Project project, Editor editor, final PsiFile file) {
    return true;
  }

  protected abstract CodeInsightActionHandler getHandler();

  protected String getCommandName() {
    String text = getTemplatePresentation().getText();
    return text != null ? text : "";
  }
}
TOP

Related Classes of com.intellij.codeInsight.actions.CodeInsightAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.