1 7 8 package javax.swing.plaf.metal; 9 10 import javax.swing.*; 11 import javax.swing.event.*; 12 import java.awt.*; 13 import java.awt.event.*; 14 import java.beans.*; 15 import java.io.*; 16 import java.util.*; 17 import javax.swing.plaf.*; 18 import javax.swing.tree.*; 19 20 import javax.swing.plaf.basic.*; 21 22 62 public class MetalTreeUI extends BasicTreeUI { 63 64 private static Color lineColor; 65 66 private static final String LINE_STYLE = "JTree.lineStyle"; 67 68 private static final String LEG_LINE_STYLE_STRING = "Angled"; 69 private static final String HORIZ_STYLE_STRING = "Horizontal"; 70 private static final String NO_STYLE_STRING = "None"; 71 72 private static final int LEG_LINE_STYLE = 2; 73 private static final int HORIZ_LINE_STYLE = 1; 74 private static final int NO_LINE_STYLE = 0; 75 76 private int lineStyle = LEG_LINE_STYLE; 77 private PropertyChangeListener lineStyleListener = new LineListener(); 78 79 public static ComponentUI createUI(JComponent x) { 81 return new MetalTreeUI (); 82 } 83 84 public MetalTreeUI() 85 { 86 super(); 87 } 88 89 protected int getHorizontalLegBuffer() 90 { 91 return 4; 92 } 93 94 public void installUI( JComponent c ) { 95 super.installUI( c ); 96 lineColor = UIManager.getColor( "Tree.line" ); 97 98 Object lineStyleFlag = c.getClientProperty( LINE_STYLE ); 99 decodeLineStyle(lineStyleFlag); 100 c.addPropertyChangeListener(lineStyleListener); 101 102 } 103 104 public void uninstallUI( JComponent c) { 105 c.removePropertyChangeListener(lineStyleListener); 106 super.uninstallUI(c); 107 } 108 109 113 protected void decodeLineStyle(Object lineStyleFlag) { 114 if ( lineStyleFlag == null || 115 lineStyleFlag.equals(LEG_LINE_STYLE_STRING)){ 116 lineStyle = LEG_LINE_STYLE; } else { 118 if ( lineStyleFlag.equals(NO_STYLE_STRING) ) { 119 lineStyle = NO_LINE_STYLE; 120 } else if ( lineStyleFlag.equals(HORIZ_STYLE_STRING) ) { 121 lineStyle = HORIZ_LINE_STYLE; 122 } 123 } 124 125 } 126 127 protected boolean isLocationInExpandControl(int row, int rowLevel, 128 int mouseX, int mouseY) { 129 if(tree != null && !isLeaf(row)) { 130 int boxWidth; 131 132 if(getExpandedIcon() != null) 133 boxWidth = getExpandedIcon().getIconWidth() + 6; 134 else 135 boxWidth = 8; 136 137 Insets i = tree.getInsets(); 138 int boxLeftX = (i != null) ? i.left : 0; 139 140 141 boxLeftX += (((rowLevel + depthOffset - 1) * totalChildIndent) + 142 getLeftChildIndent()) - boxWidth/2; 143 144 int boxRightX = boxLeftX + boxWidth; 145 146 return mouseX >= boxLeftX && mouseX <= boxRightX; 147 } 148 return false; 149 } 150 151 public void paint(Graphics g, JComponent c) { 152 super.paint( g, c ); 153 154 155 if (lineStyle == HORIZ_LINE_STYLE && !largeModel) { 157 paintHorizontalSeparators(g,c); 158 } 159 } 160 161 protected void paintHorizontalSeparators(Graphics g, JComponent c) { 162 g.setColor( lineColor ); 163 164 Rectangle clipBounds = g.getClipBounds(); 165 166 int beginRow = getRowForPath(tree, getClosestPathForLocation 167 (tree, 0, clipBounds.y)); 168 int endRow = getRowForPath(tree, getClosestPathForLocation 169 (tree, 0, clipBounds.y + clipBounds.height - 1)); 170 171 if ( beginRow <= -1 || endRow <= -1 ) { 172 return; 173 } 174 175 for ( int i = beginRow; i <= endRow; ++i ) { 176 TreePath path = getPathForRow(tree, i); 177 178 if(path != null && path.getPathCount() == 2) { 179 Rectangle rowBounds = getPathBounds(tree,getPathForRow 180 (tree, i)); 181 182 if(rowBounds != null) 184 g.drawLine(clipBounds.x, rowBounds.y, 185 clipBounds.x + clipBounds.width, rowBounds.y); 186 } 187 } 188 189 } 190 191 protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds, 192 Insets insets, TreePath path) { 193 if (lineStyle == LEG_LINE_STYLE) { 194 super.paintVerticalPartOfLeg(g, clipBounds, insets, path); 195 } 196 } 197 198 protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds, 199 Insets insets, Rectangle bounds, 200 TreePath path, int row, 201 boolean isExpanded, 202 boolean hasBeenExpanded, boolean 203 isLeaf) { 204 if (lineStyle == LEG_LINE_STYLE) { 205 super.paintHorizontalPartOfLeg(g, clipBounds, insets, bounds, 206 path, row, isExpanded, 207 hasBeenExpanded, isLeaf); 208 } 209 } 210 211 212 class LineListener implements PropertyChangeListener { 213 public void propertyChange(PropertyChangeEvent e) { 214 String name = e.getPropertyName(); 215 if ( name.equals( LINE_STYLE ) ) { 216 decodeLineStyle(e.getNewValue()); 217 } 218 } 219 } 221 } 222 | Popular Tags |