1 19 package org.openide.explorer.view; 20 21 import org.openide.awt.HtmlRenderer; 22 import org.openide.awt.ListPane; 23 import org.openide.nodes.Node; 24 import java.awt.Component ; 25 import java.awt.Container ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 29 import javax.swing.*; 30 import javax.swing.tree.TreeCellRenderer ; 31 32 33 40 public class NodeRenderer extends Object implements TreeCellRenderer , ListCellRenderer { 41 private static NodeRenderer instance = null; 42 43 47 48 private static VisualizerNode draggedOver; 49 50 51 private boolean bigIcons = false; 52 private HtmlRenderer.Renderer renderer = HtmlRenderer.createRenderer(); 53 54 55 public NodeRenderer() { 56 } 57 58 64 public @Deprecated NodeRenderer(boolean bigIcons) { 65 this.bigIcons = bigIcons; 66 } 67 68 76 public static @Deprecated NodeRenderer sharedInstance() { 77 if (instance == null) { 78 instance = new NodeRenderer(); 79 } 80 81 IllegalStateException ise = new IllegalStateException ( 82 "NodeRenderer." + "sharedInstance() is deprecated. Create an instance of NodeRenderer" + "instead" 83 ); 84 Logger.getLogger(NodeRenderer.class.getName()).log(Level.WARNING, null, ise); 85 86 return instance; 87 } 88 89 94 public Component getTreeCellRendererComponent( 95 JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus 96 ) { 97 VisualizerNode vis = findVisualizerNode(value); 98 99 if (vis == draggedOver) { 100 sel = true; 101 } 102 103 String text = vis.getHtmlDisplayName(); 104 boolean isHtml = text != null; 105 106 if (!isHtml) { 107 text = vis.getDisplayName(); 108 } 109 110 Component result = renderer.getTreeCellRendererComponent(tree, text, sel, expanded, leaf, row, hasFocus); 113 114 result.setEnabled(tree.isEnabled()); 115 renderer.setHtml(isHtml); 116 117 configureFrom(renderer, tree, expanded, sel, vis); 120 121 return result; 122 } 123 124 127 public Component getListCellRendererComponent( 128 JList list, Object value, int index, boolean sel, boolean cellHasFocus 129 ) { 130 VisualizerNode vis = findVisualizerNode(value); 131 132 if (vis == draggedOver) { 133 sel = true; 134 } 135 136 String text = vis.getHtmlDisplayName(); 137 boolean isHtml = text != null; 138 139 if (!isHtml) { 140 text = vis.getDisplayName(); 141 } 142 143 Component result = renderer.getListCellRendererComponent( 146 list, text, index, sel, cellHasFocus || (value == draggedOver) 147 ); 148 renderer.setHtml(isHtml); 149 result.setEnabled(list.isEnabled()); 150 151 int iconWidth = configureFrom(renderer, list, false, sel, vis); 154 155 boolean bigIcons = this.bigIcons || list instanceof ListPane; 156 157 if (bigIcons) { 158 renderer.setCentered(true); 159 } else { 160 if (list.getModel() instanceof NodeListModel && (((NodeListModel) list.getModel()).getDepth() > 1)) { 164 int indent = iconWidth * NodeListModel.findVisualizerDepth(list.getModel(), vis); 165 166 renderer.setIndent(indent); 167 } 168 } 169 170 return result; 171 } 172 173 178 private int configureFrom( 179 HtmlRenderer.Renderer ren, Container target, boolean useOpenedIcon, boolean sel, VisualizerNode vis 180 ) { 181 Icon icon = vis.getIcon(useOpenedIcon, bigIcons); 182 183 if (icon.getIconWidth() > 0) { 184 ren.setIconTextGap(24 - icon.getIconWidth()); 188 } else { 189 ren.setIndent(26); 194 } 195 196 ren.setIcon(icon); 197 198 if (target instanceof TreeTable.TreeTableCellRenderer) { 199 TreeTable tt = ((TreeTable.TreeTableCellRenderer) target).getTreeTable(); 200 ren.setParentFocused(tt.hasFocus() || tt.isEditing()); 201 } 202 203 return (icon.getIconWidth() == 0) ? 24 : icon.getIconWidth(); 204 } 205 206 208 private static final VisualizerNode findVisualizerNode(Object value) { 209 VisualizerNode vis = (value instanceof Node) ? VisualizerNode.getVisualizer(null, (Node) value) 210 : (VisualizerNode) value; 211 212 if (vis == null) { 213 vis = VisualizerNode.EMPTY; 214 } 215 216 return vis; 217 } 218 219 222 static void dragEnter(Object dragged) { 223 draggedOver = (VisualizerNode) dragged; 224 } 225 226 227 static void dragExit() { 228 draggedOver = null; 229 } 230 } 231 | Popular Tags |