|
| 1 | +/* |
| 2 | + * Copyright 2019 Cybrosis |
| 3 | + * |
| 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 |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 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. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.cybrosis.catdea.navigator; |
| 18 | + |
| 19 | +import com.android.tools.idea.navigator.AndroidProjectViewPane; |
| 20 | +import com.cybrosis.catdea.CatdeaService; |
| 21 | +import com.cybrosis.catdea.files.CatdeaFileType; |
| 22 | +import com.cybrosis.catdea.icons.CatdeaIcons; |
| 23 | +import com.cybrosis.catdea.lang.psi.PsiCatdeaFile; |
| 24 | +import com.intellij.ide.projectView.PresentationData; |
| 25 | +import com.intellij.ide.projectView.ProjectViewNode; |
| 26 | +import com.intellij.ide.projectView.TreeStructureProvider; |
| 27 | +import com.intellij.ide.projectView.ViewSettings; |
| 28 | +import com.intellij.ide.projectView.impl.ProjectAbstractTreeStructureBase; |
| 29 | +import com.intellij.ide.projectView.impl.ProjectTreeStructure; |
| 30 | +import com.intellij.ide.projectView.impl.ProjectViewPane; |
| 31 | +import com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode; |
| 32 | +import com.intellij.ide.projectView.impl.nodes.PsiFileNode; |
| 33 | +import com.intellij.ide.util.treeView.AbstractTreeNode; |
| 34 | +import com.intellij.openapi.project.Project; |
| 35 | +import com.intellij.openapi.vfs.VirtualFile; |
| 36 | +import com.intellij.psi.PsiFile; |
| 37 | +import com.intellij.psi.PsiManager; |
| 38 | +import com.intellij.psi.search.FileTypeIndex; |
| 39 | +import com.intellij.psi.search.GlobalSearchScope; |
| 40 | +import org.jetbrains.annotations.NotNull; |
| 41 | + |
| 42 | +import javax.swing.*; |
| 43 | +import java.util.ArrayList; |
| 44 | +import java.util.Collection; |
| 45 | +import java.util.Collections; |
| 46 | +import java.util.List; |
| 47 | + |
| 48 | +public class CatdeaProjectViewPane extends ProjectViewPane { |
| 49 | + public static final String ID = "CatdeaLogs"; |
| 50 | + |
| 51 | + public class CatdeaStructureProvider implements TreeStructureProvider { |
| 52 | + @NotNull |
| 53 | + @Override |
| 54 | + public Collection<AbstractTreeNode> modify(@NotNull AbstractTreeNode parent, |
| 55 | + @NotNull Collection<AbstractTreeNode> children, |
| 56 | + ViewSettings settings) { |
| 57 | + if (parent instanceof RootNode) return children; |
| 58 | + |
| 59 | + final Project project = parent.getProject(); |
| 60 | + if (project == null) return children; |
| 61 | + |
| 62 | + final List<AbstractTreeNode> result = new ArrayList<>(children); |
| 63 | + |
| 64 | + if (parent instanceof ProjectViewProjectNode && CatdeaService.getInstance(myProject).available()) { |
| 65 | + result.add(new RootNode(project, settings)); |
| 66 | + return result; |
| 67 | + } |
| 68 | + |
| 69 | + result.removeIf(child -> { |
| 70 | + return child instanceof PsiFileNode && |
| 71 | + ((PsiFileNode) child).getValue() instanceof PsiCatdeaFile; |
| 72 | + }); |
| 73 | + |
| 74 | + return result; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private class RootNode extends ProjectViewNode<String> { |
| 79 | + RootNode(Project project, ViewSettings viewSettings) { |
| 80 | + super(project, "Logs", viewSettings); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean contains(@NotNull VirtualFile file) { |
| 85 | + return file.getFileType() instanceof CatdeaFileType; |
| 86 | + } |
| 87 | + |
| 88 | + @NotNull |
| 89 | + @Override |
| 90 | + public Collection<? extends AbstractTreeNode> getChildren() { |
| 91 | + final List<AbstractTreeNode> result = new ArrayList<>(); |
| 92 | + |
| 93 | + final PsiManager psiManager = PsiManager.getInstance(myProject); |
| 94 | + |
| 95 | + FileTypeIndex.processFiles( |
| 96 | + CatdeaFileType.INSTANCE, |
| 97 | + file -> { |
| 98 | + final PsiFile psiFile = psiManager.findFile(file); |
| 99 | + if (psiFile != null) { |
| 100 | + result.add(new Node(getProject(), psiFile, getSettings())); |
| 101 | + } |
| 102 | + return true; |
| 103 | + }, |
| 104 | + GlobalSearchScope.projectScope(myProject) |
| 105 | + ); |
| 106 | + |
| 107 | + return result; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + protected void update(@NotNull PresentationData presentation) { |
| 112 | + presentation.setPresentableText(CatdeaProjectViewPane.this.getTitle()); |
| 113 | + presentation.setIcon(CatdeaProjectViewPane.this.getIcon()); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private static class Node extends PsiFileNode { |
| 118 | + Node(Project project, @NotNull PsiFile psiFile, ViewSettings viewSettings) { |
| 119 | + super(project, psiFile, viewSettings); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public boolean contains(@NotNull VirtualFile file) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public Collection<AbstractTreeNode> getChildrenImpl() { |
| 129 | + return Collections.emptyList(); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void update(@NotNull PresentationData presentation) { |
| 134 | + presentation.setPresentableText(getValue().getName()); |
| 135 | + presentation.setIcon(CatdeaIcons.FILE_TYPE); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + public CatdeaProjectViewPane(@NotNull Project project) { |
| 141 | + super(project); |
| 142 | + } |
| 143 | + |
| 144 | + @NotNull |
| 145 | + @Override |
| 146 | + public String getTitle() { |
| 147 | + return "Catdea Logs"; |
| 148 | + } |
| 149 | + |
| 150 | + @NotNull |
| 151 | + @Override |
| 152 | + public Icon getIcon() { |
| 153 | + return CatdeaIcons.FOLDER; |
| 154 | + } |
| 155 | + |
| 156 | + @NotNull |
| 157 | + @Override |
| 158 | + public String getId() { |
| 159 | + return ID; |
| 160 | + } |
| 161 | + |
| 162 | + @SuppressWarnings("DanglingJavadoc") |
| 163 | + @Override |
| 164 | + public int getWeight() { |
| 165 | + /** @see AndroidProjectViewPane#getWeight() */ |
| 166 | + return 141; |
| 167 | + } |
| 168 | + |
| 169 | + @NotNull |
| 170 | + @Override |
| 171 | + protected ProjectAbstractTreeStructureBase createStructure() { |
| 172 | + return new ProjectTreeStructure(myProject, ID) { |
| 173 | + @Override |
| 174 | + protected AbstractTreeNode createRoot(@NotNull Project project, @NotNull ViewSettings settings) { |
| 175 | + return new RootNode(project, settings); |
| 176 | + } |
| 177 | + }; |
| 178 | + } |
| 179 | +} |
0 commit comments