1 19 20 package org.netbeans.swing.outline; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Insets ; 25 import javax.swing.BorderFactory ; 26 import javax.swing.Icon ; 27 import javax.swing.JTable ; 28 import javax.swing.UIManager ; 29 import javax.swing.border.Border ; 30 import javax.swing.table.DefaultTableCellRenderer ; 31 import javax.swing.tree.AbstractLayoutCache ; 32 import javax.swing.tree.TreePath ; 33 34 39 public class DefaultOutlineCellRenderer extends DefaultTableCellRenderer { 40 private boolean expanded = false; 41 private boolean leaf = true; 42 private boolean showHandle = true; 43 private int nestingDepth = 0; 44 private static final Border expansionBorder = new ExpansionHandleBorder(); 45 46 47 public DefaultOutlineCellRenderer() { 48 } 49 50 54 public final void setBorder (Border b) { 55 if (b == expansionBorder) { 56 super.setBorder(b); 57 } else { 58 super.setBorder(BorderFactory.createCompoundBorder (b, expansionBorder)); 59 } 60 } 61 62 private static Icon getDefaultOpenIcon() { 63 return UIManager.getIcon("Tree.openIcon"); } 65 66 private static Icon getDefaultClosedIcon() { 67 return UIManager.getIcon("Tree.closedIcon"); } 69 70 private static Icon getDefaultLeafIcon() { 71 return UIManager.getIcon("Tree.leafIcon"); } 73 74 private static Icon getExpandedIcon() { 75 return UIManager.getIcon ("Tree.collapsedIcon"); } 77 78 private static Icon getCollapsedIcon() { 79 return UIManager.getIcon ("Tree.expandedIcon"); } 81 82 static int getNestingWidth() { 83 return getExpansionHandleWidth(); 84 } 85 86 static int getExpansionHandleWidth() { 87 return getExpandedIcon().getIconWidth(); 88 } 89 90 static int getExpansionHandleHeight() { 91 return getExpandedIcon().getIconHeight(); 92 } 93 94 private void setNestingDepth (int i) { 95 nestingDepth = i; 96 } 97 98 private void setExpanded (boolean val) { 99 expanded = val; 100 } 101 102 private void setLeaf (boolean val) { 103 leaf = val; 104 } 105 106 private void setShowHandle (boolean val) { 107 showHandle = val; 108 } 109 110 private boolean isLeaf () { 111 return leaf; 112 } 113 114 private boolean isExpanded () { 115 return expanded; 116 } 117 118 private boolean isShowHandle() { 119 return showHandle; 120 } 121 122 126 private int getNestingDepth() { 127 return nestingDepth; 128 } 129 130 139 public Component getTableCellRendererComponent(JTable table, Object value, 140 boolean isSelected, boolean hasFocus, int row, 141 int column) { 142 143 Component c = (DefaultOutlineCellRenderer) super.getTableCellRendererComponent( 144 table, value, isSelected, hasFocus, row, column); 145 Outline tbl = (Outline) table; 146 if (value != null && tbl.isTreeColumnIndex(column)) { 150 AbstractLayoutCache layout = tbl.getLayoutCache(); 151 152 boolean leaf = tbl.getOutlineModel().isLeaf(value); 153 setLeaf(leaf); 154 setShowHandle(true); 155 TreePath path = layout.getPathForRow(row); 156 boolean expanded = !layout.isExpanded(path); 157 setExpanded (expanded); 158 setNestingDepth (path.getPathCount() - 1); 159 RenderDataProvider rendata = tbl.getRenderDataProvider(); 160 Icon icon = null; 161 if (rendata != null) { 162 String displayName = rendata.getDisplayName(value); 163 if (displayName != null) { 164 setText (displayName); 165 } 166 setToolTipText (rendata.getTooltipText(value)); 167 Color bg = rendata.getBackground(value); 168 Color fg = rendata.getForeground(value); 169 if (bg != null && !isSelected) { 170 setBackground (bg); 171 } else { 172 setBackground (isSelected ? 173 tbl.getSelectionBackground() : tbl.getBackground()); 174 } 175 if (fg != null && !isSelected) { 176 setForeground (fg); 177 } else { 178 setForeground (isSelected ? 179 tbl.getSelectionForeground() : tbl.getForeground()); 180 } 181 icon = rendata.getIcon(value); 182 } 183 if (icon == null) { 184 if (!leaf) { 185 if (expanded) { 186 setIcon (getDefaultClosedIcon()); 187 } else { 188 setIcon (getDefaultOpenIcon()); 189 } 190 } else { 191 setIcon (getDefaultLeafIcon()); 192 } 193 } 194 195 } else { 196 setIcon(null); 197 setShowHandle(false); 198 } 199 return this; 200 } 201 202 private static class ExpansionHandleBorder implements Border { 203 private Insets insets = new Insets (0,0,0,0); 204 public Insets getBorderInsets(Component c) { 205 DefaultOutlineCellRenderer ren = (DefaultOutlineCellRenderer) c; 206 if (ren.isShowHandle()) { 207 insets.left = getExpansionHandleWidth() + (ren.getNestingDepth() * 208 getNestingWidth()); 209 insets.top = 1; 211 insets.right = 1; 212 insets.bottom = 1; 213 } else { 214 insets.left = 1; 216 insets.top = 1; 217 insets.right = 1; 218 insets.bottom = 1; 219 } 220 return insets; 221 } 222 223 public boolean isBorderOpaque() { 224 return false; 225 } 226 227 public void paintBorder(Component c, java.awt.Graphics g, int x, int y, int width, int height) { 228 DefaultOutlineCellRenderer ren = (DefaultOutlineCellRenderer) c; 229 if (ren.isShowHandle() && !ren.isLeaf()) { 230 Icon icon = ren.isExpanded() ? getExpandedIcon() : getCollapsedIcon(); 231 int iconY; 232 int iconX = ren.getNestingDepth() * getNestingWidth(); 233 if (icon.getIconHeight() < height) { 234 iconY = (height / 2) - (icon.getIconHeight() / 2); 235 } else { 236 iconY = 0; 237 } 238 icon.paintIcon(c, g, iconX, iconY); 239 } 240 } 241 } 242 } 243 | Popular Tags |