Extract inplace comments logic from ProjectViewTree to reuse it in other places, see IDEA-CR-56496

GitOrigin-RevId: d3be97e3a5ffa67135354e24f1b5afca7279fe37
This commit is contained in:
Evgeniy Stepanov
2019-12-23 22:34:56 +00:00
committed by intellij-monorepo-bot
parent 86e98b428e
commit aa56dc465c
2 changed files with 79 additions and 61 deletions
@@ -0,0 +1,78 @@
package com.intellij.ide.projectView.impl
import com.intellij.ide.projectView.ProjectViewNode
import com.intellij.ide.ui.UISettings.Companion.instance
import com.intellij.ide.util.treeView.NodeRenderer
import com.intellij.openapi.fileEditor.impl.IdeDocumentHistoryImpl
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.registry.Registry
import com.intellij.openapi.util.text.StringUtil
import com.intellij.openapi.vfs.VfsUtilCore
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFileSystemItem
import com.intellij.ui.SimpleTextAttributes
import com.intellij.util.text.DateFormatUtil
import com.intellij.util.ui.tree.TreeUtil
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.attribute.BasicFileAttributes
import javax.swing.JTree
open class ProjectViewRenderer : NodeRenderer() {
init {
isOpaque = false
isIconOpaque = false
isTransparentIconBackground = true
}
override fun customizeCellRenderer(tree: JTree,
value: Any?,
selected: Boolean,
expanded: Boolean,
leaf: Boolean,
row: Int,
hasFocus: Boolean) {
super.customizeCellRenderer(tree, value, selected, expanded, leaf, row, hasFocus)
val userObject = TreeUtil.getUserObject(value)
if (userObject is ProjectViewNode<*> && instance.showInplaceComments) {
appendInplaceComments(userObject)
}
}
fun appendInplaceComments(project: Project?, file: VirtualFile?) {
val ioFile =
if (file == null || file.isDirectory || !file.isInLocalFileSystem) null
else VfsUtilCore.virtualToIoFile(file)
val attr =
try {
if (ioFile == null) null
else Files.readAttributes(Paths.get(ioFile.toURI()), BasicFileAttributes::class.java)
} catch (ex: Exception) {
null
}
if (attr != null) {
append(" ")
val attributes = SimpleTextAttributes.GRAYED_SMALL_ATTRIBUTES
append(DateFormatUtil.formatDateTime(attr.lastModifiedTime().toMillis()), attributes)
append(", " + StringUtil.formatFileSize(attr.size()), attributes)
}
if (Registry.`is`("show.last.visited.timestamps") && file != null && project != null) {
IdeDocumentHistoryImpl.appendTimestamp(project, this, file)
}
}
fun appendInplaceComments(node: ProjectViewNode<*>) {
val parentNode = node.parent
val content = node.value
if (content is PsiFileSystemItem || content !is PsiElement || parentNode != null && parentNode.value is PsiDirectory) {
appendInplaceComments(node.project, node.virtualFile)
}
}
}
@@ -4,28 +4,20 @@ package com.intellij.ide.projectView.impl;
import com.intellij.ide.dnd.aware.DnDAwareTree;
import com.intellij.ide.projectView.ProjectViewNode;
import com.intellij.ide.ui.UISettings;
import com.intellij.ide.util.treeView.AbstractTreeNode;
import com.intellij.ide.util.treeView.NodeDescriptor;
import com.intellij.ide.util.treeView.NodeRenderer;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.impl.IdeDocumentHistoryImpl;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.newvfs.VfsPresentationUtil;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiDirectoryContainer;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFileSystemItem;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.ui.popup.HintUpdateSupply;
import com.intellij.ui.tabs.FileColorManagerImpl;
import com.intellij.util.ObjectUtils;
import com.intellij.util.text.DateFormatUtil;
import com.intellij.util.ui.tree.TreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -36,10 +28,6 @@ import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayDeque;
/**
@@ -65,55 +53,7 @@ public class ProjectViewTree extends DnDAwareTree {
*/
@NotNull
protected TreeCellRenderer createCellRenderer() {
final NodeRenderer cellRenderer = new NodeRenderer() {
@Override
protected void doPaint(Graphics2D g) {
super.doPaint(g);
setOpaque(false);
}
@Override
public void customizeCellRenderer(@NotNull JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus) {
super.customizeCellRenderer(tree, value, selected, expanded, leaf, row, hasFocus);
Object object = TreeUtil.getUserObject(value);
if (object instanceof ProjectViewNode && UISettings.getInstance().getShowInplaceComments()) {
ProjectViewNode<?> node = (ProjectViewNode<?>)object;
AbstractTreeNode<?> parentNode = node.getParent();
Object content = node.getValue();
VirtualFile file =
content instanceof PsiFileSystemItem ||
!(content instanceof PsiElement) ||
parentNode != null && parentNode.getValue() instanceof PsiDirectory ?
node.getVirtualFile() : null;
File ioFile = file == null || file.isDirectory() || !file.isInLocalFileSystem() ? null : VfsUtilCore.virtualToIoFile(file);
BasicFileAttributes attr = null;
try {
attr = ioFile == null ? null : Files.readAttributes(Paths.get(ioFile.toURI()), BasicFileAttributes.class);
}
catch (Exception ignored) { }
if (attr != null) {
append(" ");
SimpleTextAttributes attributes = SimpleTextAttributes.GRAYED_SMALL_ATTRIBUTES;
append(DateFormatUtil.formatDateTime(attr.lastModifiedTime().toMillis()), attributes);
append(", " + StringUtil.formatFileSize(attr.size()), attributes);
}
if (Registry.is("show.last.visited.timestamps") && file != null && node.getProject() != null) {
IdeDocumentHistoryImpl.appendTimestamp(node.getProject(), this, file);
}
}
}
};
cellRenderer.setOpaque(false);
cellRenderer.setIconOpaque(false);
cellRenderer.setTransparentIconBackground(true);
return cellRenderer;
return new ProjectViewRenderer();
}
/**