1 7 8 package org.jdesktop.swing; 9 10 import java.lang.reflect.Method ; 11 import java.util.Hashtable ; 12 import java.util.Vector ; 13 14 import java.awt.event.ActionEvent ; 15 16 import javax.swing.ActionMap ; 17 import javax.swing.JTree ; 18 import javax.swing.tree.TreeModel ; 19 import javax.swing.tree.TreeNode ; 20 import javax.swing.tree.TreePath ; 21 22 import org.jdesktop.swing.decorator.ComponentAdapter; 23 import org.jdesktop.swing.decorator.FilterPipeline; 24 import org.jdesktop.swing.decorator.HighlighterPipeline; 25 26 31 public class JXTree extends JTree { 32 33 private Method conversionMethod = null; 34 private final static Class [] methodSignature = new Class [] {Object .class}; 35 private final static Object [] methodArgs = new Object [] {null}; 36 37 protected FilterPipeline filters = null; 38 protected HighlighterPipeline highlighters = null; 39 43 public JXTree() { 44 initActions(); 45 } 46 47 57 public JXTree(Object [] value) { 58 super(value); 59 initActions(); 60 } 61 62 72 public JXTree(Vector value) { 73 super(value); 74 initActions(); 75 } 76 77 88 public JXTree(Hashtable value) { 89 super(value); 90 initActions(); 91 } 92 93 103 public JXTree(TreeNode root) { 104 super(root, false); 105 } 106 107 120 public JXTree(TreeNode root, boolean asksAllowsChildren) { 121 super(root, asksAllowsChildren); 122 initActions(); 123 } 124 125 134 public JXTree(TreeModel newModel) { 135 super(newModel); 136 initActions(); 137 conversionMethod = getValueConversionMethod(newModel); 139 } 140 141 public void setModel(TreeModel newModel) { 142 super.setModel(newModel); 143 conversionMethod = getValueConversionMethod(newModel); 145 } 146 147 private Method getValueConversionMethod(TreeModel model) { 148 try { 149 return model.getClass().getMethod("convertValueToText", methodSignature); 150 } 151 catch (NoSuchMethodException ex) { 152 } 154 return null; 155 } 156 157 public String convertValueToText(Object value, boolean selected, 158 boolean expanded, boolean leaf, 159 int row, boolean hasFocus) { 160 162 if(value != null) { 163 if (conversionMethod == null) { 164 return value.toString(); 165 } 166 else { 167 try { 168 methodArgs[0] = value; 169 return (String ) conversionMethod.invoke(getModel(), methodArgs); 170 } 171 catch (Exception ex) { 172 } 174 } 175 } 176 return ""; 177 } 178 179 private void initActions() { 180 ActionMap map = getActionMap(); 182 map.put("expand-all", new Actions("expand-all")); 183 map.put("collapse-all", new Actions("collapse-all")); 184 } 185 186 190 private class Actions extends UIAction { 191 Actions(String name) { 192 super(name); 193 } 194 195 public void actionPerformed(ActionEvent evt) { 196 if ("expand-all".equals(getName())) { 197 expandAll(); 198 } 199 else if ("collapse-all".equals(getName())) { 200 collapseAll(); 201 } 202 } 203 } 204 205 206 209 public void collapseAll() { 210 for (int i = getRowCount() - 1; i >= 0 ; i--) { 211 collapseRow(i); 212 } 213 } 214 215 218 public void expandAll() { 219 for (int i = 0; i < getRowCount(); i++) { 220 expandRow(i); 221 } 222 } 223 224 public FilterPipeline getFilters() { 225 return filters; 226 } 227 228 public void setFilters(FilterPipeline pipeline) { 229 230 filters = pipeline; 233 } 234 235 public HighlighterPipeline getHighlighters() { 236 return highlighters; 237 } 238 239 public void setHighlighters(HighlighterPipeline pipeline) { 240 highlighters = pipeline; 241 } 242 243 protected ComponentAdapter getComponentAdapter() { 244 return dataAdapter; 245 } 246 247 private final ComponentAdapter dataAdapter = new TreeAdapter(this); 248 249 static class TreeAdapter extends ComponentAdapter { 250 private final JTree tree; 251 private TreePath path; 252 255 261 public TreeAdapter(JTree component) { 262 super(component); 263 tree = component; 264 } 265 public JTree getTree() { 266 return tree; 267 } 268 269 public boolean hasFocus() { 270 return tree.isFocusOwner() && (tree.getLeadSelectionRow() == row); 271 } 272 273 public Object getValueAt(int row, int column) { 274 return path.getLastPathComponent(); 275 } 276 277 public Object getFilteredValueAt(int row, int column) { 278 279 return path.getLastPathComponent(); 280 } 281 282 public boolean isSelected() { 283 return tree.isRowSelected(row); 284 } 285 286 public boolean isExpanded() { 287 return tree.isExpanded(row); 288 } 289 290 public boolean isLeaf() { 291 return tree.getModel().isLeaf(getValue()); 292 } 293 294 public boolean isCellEditable(int row, int column) { 295 return false; 296 } 297 298 public void setValueAt(Object aValue, int row, int column) { 299 300 } 301 } 302 303 } 304 | Popular Tags |