From c27a09473f3c5fd222cb9df37628645c326d4b7d Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sun, 1 Nov 2020 15:26:12 +0000 Subject: [PATCH] fix tree renderer for collection trees --- .../muwire/core/collections/PathTree.groovy | 1 + .../com/muwire/gui/CollectionTabView.groovy | 1 + .../muwire/gui/CollectionWizardView.groovy | 2 +- .../com/muwire/gui/PathTreeRenderer.groovy | 38 +++++++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 gui/src/main/groovy/com/muwire/gui/PathTreeRenderer.groovy diff --git a/core/src/main/groovy/com/muwire/core/collections/PathTree.groovy b/core/src/main/groovy/com/muwire/core/collections/PathTree.groovy index 03ec15f9..3369622f 100644 --- a/core/src/main/groovy/com/muwire/core/collections/PathTree.groovy +++ b/core/src/main/groovy/com/muwire/core/collections/PathTree.groovy @@ -7,6 +7,7 @@ class PathTree { final PathNode root PathTree(String root) { this.root = new PathNode(root, null) + this.root.setUserObject(root) keyToNode.put(this.root.key(), this.root) } diff --git a/gui/griffon-app/views/com/muwire/gui/CollectionTabView.groovy b/gui/griffon-app/views/com/muwire/gui/CollectionTabView.groovy index 37dedb42..35474473 100644 --- a/gui/griffon-app/views/com/muwire/gui/CollectionTabView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/CollectionTabView.groovy @@ -98,6 +98,7 @@ class CollectionTabView { borderLayout() scrollPane(constraints : BorderLayout.CENTER, border : etchedBorder()) { itemsTree = new JTree(model.fileTreeModel) + itemsTree.setCellRenderer(new PathTreeRenderer()) tree(rowHeight : rowHeight, rootVisible : true, expandsSelectedPaths : true, itemsTree) } } diff --git a/gui/griffon-app/views/com/muwire/gui/CollectionWizardView.groovy b/gui/griffon-app/views/com/muwire/gui/CollectionWizardView.groovy index a3a662ba..a437cc05 100644 --- a/gui/griffon-app/views/com/muwire/gui/CollectionWizardView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/CollectionWizardView.groovy @@ -83,7 +83,7 @@ class CollectionWizardView { label(text : trans("COLLECTION_REVIEW_TITLE"), constraints : BorderLayout.NORTH) scrollPane(constraints : BorderLayout.CENTER) { jTree = new JTree(model.tree) -// jtree.setCellRenderer(new SharedTreeRenderer()) // TODO: create new renderer + jTree.setCellRenderer(new PathTreeRenderer()) tree(id : "preview-tree", rowHeight : rowHeight, rootVisible : true, expandsSelectedPaths : true, jTree) } panel(constraints : BorderLayout.SOUTH) { diff --git a/gui/src/main/groovy/com/muwire/gui/PathTreeRenderer.groovy b/gui/src/main/groovy/com/muwire/gui/PathTreeRenderer.groovy new file mode 100644 index 00000000..d4ef9051 --- /dev/null +++ b/gui/src/main/groovy/com/muwire/gui/PathTreeRenderer.groovy @@ -0,0 +1,38 @@ +package com.muwire.gui + +import java.awt.Component + +import javax.swing.ImageIcon +import javax.swing.JTree +import javax.swing.tree.DefaultTreeCellRenderer + +import com.muwire.core.collections.FileCollectionItem + +class PathTreeRenderer extends DefaultTreeCellRenderer { + + private final ImageIcon commentIcon + + public PathTreeRenderer() { + commentIcon = new ImageIcon((URL) PathTreeRenderer.class.getResource("/comment.png")) + } + + public Component getTreeCellRendererComponent(JTree tree, Object value, + boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { + def userObject = value.getUserObject() + + def defaultRenderer = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus) + if (userObject == null || userObject instanceof String) { + return defaultRenderer + } + + FileCollectionItem item = (FileCollectionItem) userObject + String fileName = item.pathElements.get(item.pathElements.size() - 1) + setText(fileName) + setEnabled(true) + if (item.comment != "" && item.comment != null) { + setIcon(commentIcon) + } + + this + } +}