package org.infernus.idea.checkstyle.actions; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.DataKeys; import com.intellij.openapi.actionSystem.Presentation; import com.intellij.openapi.project.Project; import com.intellij.openapi.vcs.changes.Change; import com.intellij.openapi.vcs.changes.ChangeListManager; import com.intellij.openapi.vcs.changes.LocalChangeList; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.wm.ToolWindow; import com.intellij.openapi.wm.ToolWindowManager; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.infernus.idea.checkstyle.CheckStylePlugin; import org.infernus.idea.checkstyle.toolwindow.CheckStyleToolWindowPanel; /** * Scan files in the current change-list. */ public class ScanCurrentChangeList extends BaseAction { private static final Log LOG = LogFactory.getLog(ScanCurrentChangeList.class); @Override public final void actionPerformed(final AnActionEvent event) { Project project = null; try { project = DataKeys.PROJECT.getData(event.getDataContext()); if (project == null) { return; } final ToolWindow toolWindow = ToolWindowManager.getInstance(project) .getToolWindow(CheckStyleToolWindowPanel.ID_TOOLWINDOW); final ChangeListManager changeListManager = ChangeListManager.getInstance(project); project.getComponent(CheckStylePlugin.class).asyncScanFiles(filesFor(changeListManager .getDefaultChangeList()), getSelectedOverride(toolWindow)); } catch (Throwable e) { LOG.error("Modified files scan failed", e); } } private List<VirtualFile> filesFor(final LocalChangeList changeList) { if (changeList == null || changeList.getChanges() == null) { return Collections.emptyList(); } final Collection<VirtualFile> filesInChanges = new HashSet<>(); for (Change change : changeList.getChanges()) { if (change.getVirtualFile() != null) { filesInChanges.add(change.getVirtualFile()); } } return new ArrayList<>(filesInChanges); } @Override public void update(final AnActionEvent event) { super.update(event); Project project = null; try { project = DataKeys.PROJECT.getData(event.getDataContext()); if (project == null) { // check if we're loading... return; } final CheckStylePlugin checkStylePlugin = project.getComponent(CheckStylePlugin.class); if (checkStylePlugin == null) { throw new IllegalStateException("Couldn't get checkstyle plugin"); } final Presentation presentation = event.getPresentation(); final LocalChangeList changeList = ChangeListManager.getInstance(project).getDefaultChangeList(); if (changeList == null || changeList.getChanges() == null || changeList.getChanges().size() == 0) { presentation.setEnabled(false); } else { presentation.setEnabled(!checkStylePlugin.isScanInProgress()); } } catch (Throwable e) { LOG.error("Button update failed.", e); } } }