View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
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.atlassian.theplugin.idea.ui.tree.file;
18  
19  import com.atlassian.theplugin.commons.BambooFileInfo;
20  import com.atlassian.theplugin.idea.ui.tree.AtlassianClickAction;
21  import com.atlassian.theplugin.idea.ui.tree.AtlassianTreeNode;
22  import com.intellij.openapi.fileTypes.FileType;
23  import com.intellij.openapi.fileTypes.FileTypeManager;
24  import com.intellij.openapi.vcs.AbstractVcs;
25  import com.intellij.psi.PsiFile;
26  import com.intellij.ui.ColoredTreeCellRenderer;
27  import com.intellij.ui.SimpleTextAttributes;
28  import com.intellij.vcsUtil.VcsUtil;
29  import org.apache.commons.io.FilenameUtils;
30  
31  import javax.swing.*;
32  import javax.swing.tree.TreeCellRenderer;
33  
34  public class BambooFileNode extends FileNode {
35  
36      private BambooFileInfo file;
37      private static final TreeCellRenderer MY_RENDERER = new BambooFileNodeRenderer();
38      /**
39       * null when there is not corresponding PsiFile in currently open project
40       */
41      private PsiFile psiFile;
42  
43      public BambooFileNode(final BambooFileInfo f, final PsiFile psiFile) {
44          this(f, AtlassianClickAction.EMPTY_ACTION, psiFile);
45      }
46  
47  	public BambooFileNode(final BambooFileNode node) {
48  		this(node.file, node.psiFile);
49  	}
50  
51  	public PsiFile getPsiFile() {
52          return psiFile;
53      }
54  
55      public BambooFileNode(BambooFileInfo file, AtlassianClickAction action, PsiFile psiFile) {
56          super(FilenameUtils.getName(file.getFileDescriptor().getUrl()), action);
57          this.file = file;
58          this.psiFile = psiFile;
59      }
60  
61      public BambooFileInfo getBambooFileInfo() {
62          return file;
63      }
64  
65      public String getRevision() {
66          return file.getFileDescriptor().getRevision();
67      }
68  
69      @Override
70      public TreeCellRenderer getTreeCellRenderer() {
71  		return MY_RENDERER;
72      }
73  
74  	@Override
75  	public AtlassianTreeNode getClone() {
76  		return new BambooFileNode(this);
77  	}
78  
79  	private static class BambooFileNodeRenderer extends ColoredTreeCellRenderer {
80  
81  		@Override
82          public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded,
83                  boolean leaf, int row, boolean hasFocus) {
84              BambooFileNode node = (BambooFileNode) value;
85              append(node.getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
86  
87              StringBuilder txt = new StringBuilder();
88              txt.append(" (rev: ");
89              txt.append(node.getRevision());
90              txt.append(",");
91              append(txt.toString(), SimpleTextAttributes.GRAY_ITALIC_ATTRIBUTES);
92              if (node.getPsiFile() != null) {
93                  try {
94                      AbstractVcs vcs = VcsUtil.getVcsFor(node.getPsiFile().getProject(), node.getPsiFile().getVirtualFile());
95                      if (vcs == null) {
96                          throw new NullPointerException("no VCS info");
97                      }
98                      String revision = vcs.getDiffProvider().getCurrentRevision(node.getPsiFile().getVirtualFile()).asString();
99                      append(" loc rev: " + revision, SimpleTextAttributes.GRAY_ITALIC_ATTRIBUTES);
100                 } catch (NullPointerException e) {
101                     append(e.getMessage(), SimpleTextAttributes.ERROR_ATTRIBUTES);
102                 }
103             } else {
104                 append(" no corresponding file in the project", SimpleTextAttributes.ERROR_ATTRIBUTES);
105             }
106 
107             append(")", SimpleTextAttributes.GRAY_ITALIC_ATTRIBUTES);
108             FileTypeManager mgr = FileTypeManager.getInstance();
109             FileType type = mgr.getFileTypeByFileName(node.getName());
110             setIcon(type.getIcon());
111         }
112 
113 	}
114 }