5d80cae1084bc2fcd161656847fd36f11f7dd3a6
[fedora-idea.git] / platform / platform-impl / src / com / intellij / ide / actions / tree / BaseTreeNodeAction.java
blob5d80cae1084bc2fcd161656847fd36f11f7dd3a6
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.ide.actions.tree;
18 import com.intellij.openapi.actionSystem.AnAction;
19 import com.intellij.openapi.actionSystem.AnActionEvent;
20 import com.intellij.openapi.actionSystem.PlatformDataKeys;
21 import com.intellij.ui.treeStructure.treetable.TreeTable;
23 import javax.swing.*;
25 abstract class BaseTreeNodeAction extends AnAction {
26 public BaseTreeNodeAction() {
27 setEnabledInModalContext(true);
30 public void actionPerformed(AnActionEvent e) {
31 Object sourceComponent = getSourceComponent(e);
32 if (sourceComponent instanceof JTree) {
33 performOn((JTree)sourceComponent);
35 else if (sourceComponent instanceof TreeTable) {
36 performOn(((TreeTable)sourceComponent).getTree());
40 protected abstract void performOn(JTree tree);
42 public void update(AnActionEvent e) {
43 e.getPresentation().setEnabled(enabledOn(getSourceComponent(e)));
46 private static boolean enabledOn(Object sourceComponent) {
47 if (sourceComponent instanceof JTree) {
48 return true;
50 if (sourceComponent instanceof TreeTable) {
51 return true;
53 return false;
56 private static Object getSourceComponent(AnActionEvent e) {
57 return PlatformDataKeys.CONTEXT_COMPONENT.getData(e.getDataContext());