summaryrefslogtreecommitdiff
path: root/plugins/xpath/xpath-view/src/org/intellij/plugins/xpathView/XPathAction.java
blob: 20e3597c8f2f1ae99969aa19c88b6e69b8467888 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright 2002-2005 Sascha Weinreuter
 *
 * 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 org.intellij.plugins.xpathView;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.xml.XmlFile;

public abstract class XPathAction extends AnAction {
    protected final XPathAppComponent myComponent;

    protected XPathAction() {
        myComponent = XPathAppComponent.getInstance();
    }

    public void update(AnActionEvent event) {
        super.update(event);
        final Presentation presentation = event.getPresentation();

        // keep track of enabled status
        presentation.setEnabled(isEnabled(event, true));

        // provide icon for toolbar
        if (ActionPlaces.MAIN_TOOLBAR.equals(event.getPlace())) {
            updateToolbar(event);
        } else if (ActionPlaces.MAIN_MENU.equals(event.getPlace())) {
            updateMainMenu(event);
        } else if (ActionPlaces.EDITOR_POPUP.equals(event.getPlace())) {
            presentation.setVisible(presentation.isEnabled());
        }
    }

    protected void updateMainMenu(AnActionEvent event) {
        final boolean b = myComponent.getConfig().SHOW_IN_MAIN_MENU;
        event.getPresentation().setVisible(b && isEnabled(event, false));
    }

    protected void updateToolbar(AnActionEvent event) {
        event.getPresentation().setVisible(myComponent.getConfig().SHOW_IN_TOOLBAR);
    }

    protected boolean isEnabled(AnActionEvent event, boolean checkAvailable) {
        final Project project = CommonDataKeys.PROJECT.getData(event.getDataContext());
        if (project == null) {
            // no active project
            return false;
        }

        Editor editor = CommonDataKeys.EDITOR.getData(event.getDataContext());
        if (editor == null) {
            FileEditorManager fem = FileEditorManager.getInstance(project);
            editor = fem.getSelectedTextEditor();
        }
        if (editor == null) {
            return false;
        }

        // do we have an xml file?
        final PsiDocumentManager cem = PsiDocumentManager.getInstance(project);
        final PsiFile psiFile = cem.getPsiFile(editor.getDocument());
        // this is also true for DTD documents...
        if (!(psiFile instanceof XmlFile)) {
            return false;
        } else if (psiFile.getLanguage() == StdFileTypes.DTD.getLanguage()) {
            return false;
        }

        return !checkAvailable || isEnabledAt((XmlFile)psiFile, editor.getCaretModel().getOffset());
    }

    protected abstract boolean isEnabledAt(XmlFile xmlFile, int offset);
}