1 package com.ca.directory.jxplorer.tree; 2 3 import com.ca.commons.cbutil.*; 4 import com.ca.commons.naming.RDN; 5 6 import javax.naming.NamingException ; 7 import javax.swing.*; 8 import javax.swing.border.Border ; 9 import javax.swing.tree.*; 10 import java.awt.*; 11 import java.awt.event.ActionEvent ; 12 import java.awt.event.ActionListener ; 13 import java.util.EventObject ; 14 15 20 21 public class SmartTreeCellEditor extends DefaultTreeCellEditor 22 { 23 Panel display; 24 TextField standard; 25 26 boolean useIcons = true; 27 28 Object value = null; 29 30 36 37 public SmartTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer) 38 { 39 super(tree, renderer); 40 display = new Panel(); 41 display.setBackground(Color.green); 42 display.add(standard = new TextField("bloop")); 43 } 44 45 46 53 54 protected TreeCellEditor createTreeCellEditor() 55 { 56 MyTreeCellEditor editor = new MyTreeCellEditor(); 57 editor.setClickCountToStart(1); 59 return editor; 60 61 } 62 63 67 68 public void actionPerformed(ActionEvent e) 69 { 70 if (((SmartTree)tree).dragging == true) 71 return; 72 73 super.actionPerformed(e); 74 } 75 76 81 82 protected void determineOffset(JTree tree, Object value, 83 boolean isSelected, boolean expanded, 84 boolean leaf, int row) 85 { 86 if(value != null) 87 { 88 editingIcon = ((SmartNode)value).getIcon(); 89 90 if (editingIcon != null) 91 offset = 4 + editingIcon.getIconWidth(); 92 else 93 offset = 4; 94 } 95 else 96 { 97 editingIcon = null; 98 offset = 0; 99 } 100 } 101 102 106 107 public Object getCellEditorValue() 108 { 109 try 110 { 111 SmartNode node = (SmartNode)value; 112 if (node.isMultiValued()) 113 { 114 return value.toString(); 115 } 116 } 117 catch (Exception e) {} 119 return super.getCellEditorValue(); 120 } 121 122 125 126 public boolean isCellEditable(EventObject event) 127 { 128 if (tree == null) return false; 131 TreePath path = tree.getSelectionPath(); 132 133 if (path == null) return false; 135 136 SmartNode node = (SmartNode)path.getLastPathComponent(); 137 138 if (node == null) return false; 140 141 142 146 147 if (node != null && node.isDummy() || node.isStructural() || node.isRoot()) 148 return false; 149 150 return super.isCellEditable(event); 151 } 152 153 154 155 156 157 162 163 class CellEditorGUI extends CBPanel 164 { 165 ActionListener listener; 167 boolean multi = false; 169 JTextField first; 171 Border thinBorder; 173 SmartNode node; 175 RDN displayRDN; 177 184 185 CellEditorGUI() 186 { 187 makeHeavy(); 188 thinBorder = UIManager.getBorder("Tree.editorBorder"); 189 first = new JTextField(); 190 first.setBorder(thinBorder); 191 first.setText("bloop!"); 192 add(first); 193 194 } 195 196 197 205 206 void setValue(SmartNode node) 207 { 208 this.node = node; 209 210 RDN rdn = node.getRDN(); 211 212 int size = rdn.size(); 213 214 multi = node.isMultiValued(); 215 216 trimExcessComponents(); 218 Dimension prefSize = renderer.getPreferredSize(); 219 220 setSize(prefSize); 221 222 first.setText(rdn.getRawVal(0)); 224 229 230 for (int i=1; i<size; i++) 231 { 232 makeLight(); 233 JLabel plus = new JLabel("+"); 234 plus.setForeground(Color.red); 235 add(plus); 236 makeHeavy(); 237 JTextField nextField = new JTextField(rdn.getRawVal(i)); 238 nextField.setBorder(thinBorder); 239 nextField.addActionListener(listener); 240 add(nextField); 241 } 242 243 } 244 245 248 249 protected void trimExcessComponents() 250 { 251 for (int i=getComponentCount()-1; i>0; i--) 252 { 253 remove(i); 254 } 255 } 256 257 258 263 264 RDN getValue() 265 { 266 RDN returnRDN = node.getRDN(); 267 268 try 269 { 270 279 int size = getComponentCount(); 280 for (int i=0; i<size; i = i+2) 281 { 282 JTextField currentTextField = (JTextField) getComponent(i); 283 String text = currentTextField.getText(); 284 text = text.trim(); 285 if (text.length() == 0) 286 text = " "; 287 returnRDN.setRawVal(text, (i/2)); 288 } 289 return returnRDN; 290 } 292 catch (NamingException e) 293 { 294 CBUtility.error(CBIntText.get("Invalid Name"), null); 295 297 298 } 299 return returnRDN; 300 } 301 302 314 315 void addActionListener(ActionListener l) 316 { 317 listener = l; 318 first.addActionListener(l); 319 } 320 321 327 328 public Dimension getPreferredSize() 329 { 330 Dimension size = super.getPreferredSize(); 331 332 if(renderer != null ) 334 { 335 Dimension rSize = renderer.getPreferredSize(); 336 size.height = rSize.height; 337 } 338 339 if (multi) 340 size.width += 30; 342 return size; 343 } 344 } 345 346 353 354 public class MyTreeCellEditor extends DefaultCellEditor 355 { 356 357 CellEditorGUI cellDisplay; 358 359 363 364 public MyTreeCellEditor() 365 { 366 super(new JCheckBox()); 368 cellDisplay = new CellEditorGUI(); 369 370 editorComponent = cellDisplay; 371 372 this.clickCountToStart = 2; 373 374 delegate = new EditorDelegate() 375 { 376 public void setValue(Object value) 377 { 378 cellDisplay.setValue((SmartNode)value); 379 } 380 381 public Object getCellEditorValue() 382 { 383 return cellDisplay.getValue(); 384 } 385 }; 386 387 cellDisplay.addActionListener(delegate); 388 } 389 390 395 396 public Component getTreeCellEditorComponent(JTree tree, Object value, 397 boolean isSelected, 398 boolean expanded, 399 boolean leaf, int row) 400 { 401 cellDisplay.setBackground(tree.getBackground()); 402 delegate.setValue(value); 403 return editorComponent; 404 } 405 406 407 408 409 } 410 411 } | Popular Tags |