aboutsummaryrefslogtreecommitdiffstats
path: root/sd-plugin/src/main/java/org/intellij/sdk/language/structure/SdStructureViewElement.java
blob: a5f45e6b26bdf65081b787ce7f95af2e367a4c0e (plain) (blame)
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
package org.intellij.sdk.language.structure;

import com.intellij.ide.projectView.PresentationData;
import com.intellij.ide.structureView.StructureViewTreeElement;
import com.intellij.ide.util.treeView.smartTree.SortableTreeElement;
import com.intellij.ide.util.treeView.smartTree.TreeElement;
import com.intellij.navigation.ItemPresentation;
import com.intellij.psi.NavigatablePsiElement;
import com.intellij.psi.PsiElement;
import org.intellij.sdk.language.SdUtil;
import org.intellij.sdk.language.psi.SdDocumentDefinition;
import org.intellij.sdk.language.psi.SdDocumentStructDefinition;
import org.intellij.sdk.language.psi.SdDocumentSummaryDefinition;
import org.intellij.sdk.language.psi.SdFile;
import org.intellij.sdk.language.psi.SdRankProfileDefinition;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class SdStructureViewElement implements StructureViewTreeElement, SortableTreeElement {

    private final NavigatablePsiElement myElement;

    public SdStructureViewElement(NavigatablePsiElement element) {
        this.myElement = element;
    }

    @Override
    public Object getValue() {
        return myElement;
    }

    @Override
    public void navigate(boolean requestFocus) {
        myElement.navigate(requestFocus);
    }

    @Override
    public boolean canNavigate() {
        return myElement.canNavigate();
    }

    @Override
    public boolean canNavigateToSource() {
        return myElement.canNavigateToSource();
    }

    @NotNull
    @Override
    public String getAlphaSortKey() {
        String name = myElement.getName();
        return name != null ? name : "";
    }

    @NotNull
    @Override
    public ItemPresentation getPresentation() {
        ItemPresentation presentation = myElement.getPresentation();
        return presentation != null ? presentation : new PresentationData();
    }

    @Override
    public TreeElement @NotNull [] getChildren() {
        List<PsiElement> children = new ArrayList<>();;
        if (myElement instanceof SdFile) {
            children = SdUtil.findSchemaChildren(myElement);
        } else if (myElement instanceof SdDocumentDefinition) {
            children = SdUtil.findDocumentChildren(myElement);
        } else if (myElement instanceof SdDocumentStructDefinition) {
            children = SdUtil.findDocumentStructChildren(myElement);
        } else if (myElement instanceof SdRankProfileDefinition) {
            children = SdUtil.findRankProfileChildren(myElement);
        } else if (myElement instanceof SdDocumentSummaryDefinition) {
            children = SdUtil.findDocumentSummaryChildren(myElement);
        }
        
        List<TreeElement> treeElements = new ArrayList<>(children.size());
        for (PsiElement child : children) {
            treeElements.add(new SdStructureViewElement((NavigatablePsiElement) child));
        }
        return treeElements.toArray(new TreeElement[0]);
    }
}