/*
* Copyright 2008-2013 Andre Pfeiler
*
* This file is part of FindBugs-IDEA.
*
* FindBugs-IDEA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FindBugs-IDEA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FindBugs-IDEA. If not, see <http://www.gnu.org/licenses/>.
*/
package org.twodividedbyzero.idea.findbugs.actions;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.DataKeys;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.content.Content;
import org.twodividedbyzero.idea.findbugs.common.exception.FindBugsPluginException;
import org.twodividedbyzero.idea.findbugs.core.FindBugsPluginImpl;
import org.twodividedbyzero.idea.findbugs.gui.toolwindow.view.ToolWindowPanel;
import org.twodividedbyzero.idea.findbugs.gui.tree.view.BugTree;
import java.awt.event.KeyEvent;
/**
* $Date: 2013-12-05 08:09:56 -0600 (Thu, 05 Dec 2013) $
*
* @author Andre Pfeiler<andrep@dev.java.net>
* @version $Revision: 257 $
* @since 0.9.97
*/
public class ShowQuickSearch extends BaseAction {
private static final Logger LOGGER = Logger.getInstance(ShowQuickSearch.class.getName());
private DataContext _dataContext;
private AnActionEvent _actionEvent;
private boolean _enabled;
private boolean _running;
@Override
public void actionPerformed(final AnActionEvent e) {
_actionEvent = e;
_dataContext = e.getDataContext();
final com.intellij.openapi.project.Project project = DataKeys.PROJECT.getData(_dataContext);
final Presentation presentation = e.getPresentation();
// check a project is loaded
if (isProjectNotLoaded(project, presentation)) {
Messages.showWarningDialog("Project not loaded.", "FindBugs");
return;
}
final ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(getPluginInterface(project).getInternalToolWindowId());
final Content content = toolWindow.getContentManager().getContent(0);
if (content != null) {
final ToolWindowPanel panel = (ToolWindowPanel) content.getComponent();
final BugTree tree = panel.getBugTreePanel().getBugTree();
tree.setSelectionRow(0);
tree.requestFocusInWindow();
tree.dispatchEvent(new KeyEvent(tree, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), KeyEvent.CTRL_DOWN_MASK, KeyEvent.VK_F, 'F'));
}
}
@Override
public void update(final AnActionEvent event) {
try {
_actionEvent = event;
_dataContext = event.getDataContext();
final Project project = DataKeys.PROJECT.getData(_dataContext);
final Presentation presentation = event.getPresentation();
// check a project is loaded
if (isProjectNotLoaded(project, presentation)) {
return;
}
isPluginAccessible(project);
// check if tool window is registered
final ToolWindow toolWindow = isToolWindowRegistered(project);
if (toolWindow == null) {
presentation.setEnabled(false);
presentation.setVisible(false);
return;
}
_enabled = !_running;
presentation.setEnabled(toolWindow.isAvailable() && isEnabled());
presentation.setVisible(true);
} catch (final Throwable e) {
final FindBugsPluginException processed = FindBugsPluginImpl.processError("Action update failed", e);
LOGGER.error("Action update failed", processed);
}
}
@Override
protected boolean isEnabled() {
return _enabled;
}
@Override
protected boolean setEnabled(final boolean enabled) {
final boolean was = _enabled;
if (_enabled != enabled) {
_enabled = enabled;
}
return was;
}
protected boolean isRunning() {
return _running;
}
protected boolean setRunning(final boolean running) {
final boolean was = _running;
if (_running != running) {
_running = running;
}
return was;
}
}