Index: javax/swing/DefaultCellEditor.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/DefaultCellEditor.java,v retrieving revision 1.12 diff -u -r1.12 DefaultCellEditor.java --- javax/swing/DefaultCellEditor.java 18 Aug 2005 20:50:39 -0000 1.12 +++ javax/swing/DefaultCellEditor.java 23 Aug 2005 18:32:04 -0000 @@ -369,9 +369,17 @@ delegate = new EditorDelegate(); ((JTextField)editorComponent).addActionListener(delegate); } - else + else if (editorComponent instanceof JCheckBox) { - // TODO + ((JCheckBox)editorComponent).setText(value.toString()); + delegate = new EditorDelegate(); + ((JCheckBox)editorComponent).addActionListener(delegate); + } + else if (editorComponent instanceof JComboBox) + { + ((JComboBox)editorComponent).setSelectedItem(value.toString()); + delegate = new EditorDelegate(); + ((JComboBox)editorComponent).addActionListener(delegate); } return editorComponent; Index: javax/swing/plaf/basic/BasicTreeUI.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicTreeUI.java,v retrieving revision 1.66 diff -u -r1.66 BasicTreeUI.java --- javax/swing/plaf/basic/BasicTreeUI.java 23 Aug 2005 14:14:02 -0000 1.66 +++ javax/swing/plaf/basic/BasicTreeUI.java 23 Aug 2005 18:32:04 -0000 @@ -259,8 +259,6 @@ treeExpansionListener = createTreeExpansionListener(); treeModelListener = createTreeModelListener(); - createdRenderer = true; - createdCellEditor = true; editingRow = -1; lastSelectedRow = -1; } @@ -1205,6 +1203,9 @@ currentCellRenderer = createDefaultCellRenderer(); rendererPane = createCellRendererPane(); + createdRenderer = true; + createdCellEditor = true; + TreeModel mod = tree.getModel(); setModel(mod); tree.setRootVisible(true); @@ -2420,6 +2421,7 @@ */ public void valueChanged(TreeSelectionEvent event) { + // FIXME: not implemented } }// TreeSelectionHandler Index: javax/swing/tree/DefaultTreeCellEditor.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/tree/DefaultTreeCellEditor.java,v retrieving revision 1.8 diff -u -r1.8 DefaultTreeCellEditor.java --- javax/swing/tree/DefaultTreeCellEditor.java 19 Aug 2005 21:48:54 -0000 1.8 +++ javax/swing/tree/DefaultTreeCellEditor.java 23 Aug 2005 18:32:04 -0000 @@ -49,12 +49,16 @@ import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.MouseEvent; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.EventObject; +import javax.swing.DefaultCellEditor; import javax.swing.Icon; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JTextField; import javax.swing.JTree; @@ -130,6 +134,7 @@ Rectangle cr = tr.union(ir); tr.width += offset; + // paint icon DefaultTreeCellEditor.this.editingIcon.paintIcon(DefaultTreeCellEditor.this.editingComponent, g, cr.x, cr.y); @@ -228,10 +233,15 @@ String s = getText(); Font f = getFont(); - FontMetrics fm = getToolkit().getFontMetrics(f); - return new Dimension(SwingUtilities.computeStringWidth(fm, s), - fm.getHeight()); + if (f != null) + { + FontMetrics fm = getToolkit().getFontMetrics(f); + + return new Dimension(SwingUtilities.computeStringWidth(fm, s), + fm.getHeight()); + } + return renderer.getPreferredSize(); } } @@ -303,7 +313,7 @@ * Font to paint with, null indicates font of renderer is to be used. */ protected Font font; - + /** * Constructs a DefaultTreeCellEditor object for a JTree using the * specified renderer and a default editor. (Use this constructor @@ -314,9 +324,7 @@ */ public DefaultTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer) { - this.tree = tree; - this.renderer = renderer; - // FIXME: Not fully implemented. + this(tree, renderer, null); } /** @@ -331,10 +339,33 @@ public DefaultTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer, TreeCellEditor editor) { - this.tree = tree; + setTree(tree); this.renderer = renderer; - this.realEditor = editor; - // FIXME: Not fully implemented. + + if (editor == null) + editor = createTreeCellEditor(); + + if (tree != null) + { + lastPath = tree.getSelectionPath(); + if (lastPath != null) + { + lastRow = tree.getRowForPath(lastPath); + Object val = lastPath.getLastPathComponent(); + boolean isLeaf = tree.getModel().isLeaf(val); + boolean expanded = tree.isExpanded(lastPath); + determineOffset(tree, val, true, expanded, isLeaf, lastRow); + editingComponent = editor.getTreeCellEditorComponent( + tree, val, true, expanded, isLeaf, lastRow); + } + } + + editingContainer = createContainer(); + UIDefaults defaults = UIManager.getLookAndFeelDefaults(); + setFont(defaults.getFont("Tree.font")); + setBorderSelectionColor(defaults.getColor("Tree.selectionBorderColor")); + editingIcon = renderer.getIcon(); + timer = new javax.swing.Timer(1200, this); } /** @@ -406,10 +437,16 @@ /** * Configures the editor. Passed onto the realEditor. + * Sets an initial value for the editor. This will cause + * the editor to stopEditing and lose any partially edited value + * if the editor is editing when this method is called. + * Returns the component that should be added to the client's Component + * hierarchy. Once installed in the client's hierarchy this component will + * then be able to draw and receive user input. * * @param tree - the JTree that is asking the editor to edit; this parameter can be null * @param value - the value of the cell to be edited - * @param isSelected - true is the cell is to be renderer with selection highlighting + * @param isSelected - true is the cell is to be rendered with selection highlighting * @param expanded - true if the node is expanded * @param leaf - true if the node is a leaf node * @param row - the row index of the node being edited @@ -420,7 +457,36 @@ boolean isSelected, boolean expanded, boolean leaf, int row) { - return null; // TODO + if (tree != null && value != null) + { + editingComponent = (Component) value; + if (tree.isEditing()) + tree.stopEditing(); + if (editingComponent instanceof JTextField) + { + ((JTextField) editingComponent).setText(value.toString()); + realEditor = this; + ((JTextField) editingComponent).addActionListener(this); + canEdit = true; + } + else if (editingComponent instanceof JComboBox) + { + ((JComboBox) editingComponent).setSelectedItem(value.toString()); + realEditor = this; + ((JComboBox) editingComponent).addActionListener((JComboBox) + editingComponent); + canEdit = true; + } + else if (editingComponent instanceof JCheckBox) + { + ((JCheckBox) editingComponent).setText(value.toString()); + realEditor = this; + ((JCheckBox) editingComponent).addActionListener(this); + canEdit = true; + } + } + + return editingComponent; } /** @@ -430,9 +496,9 @@ */ public Object getCellEditorValue() { - return null; // TODO + return editingComponent; } - + /** * If the realEditor returns true to this message, prepareForEditing * is messaged and true is returned. @@ -442,29 +508,40 @@ */ public boolean isCellEditable(EventObject event) { - return false; // TODO + if (canEdit && canEditImmediately(event) && realEditor.isCellEditable(event)) + { + prepareForEditing(); + return true; + } + return false; } /** * Messages the realEditor for the return value. * - * @param event - the event the editor should use to start editing - * @return true if the editor would like the editing cell to be - * selected; otherwise returns false + * @param event - + * the event the editor should use to start editing + * @return true if the editor would like the editing cell to be selected; + * otherwise returns false */ public boolean shouldSelectCell(EventObject event) { - return false; // TODO + return realEditor.shouldSelectCell(event); } /** * If the realEditor will allow editing to stop, the realEditor * is removed and true is returned, otherwise false is returned. - * @return true if editing was stopped; false otherwise + * @return true if editing was stopped; false otherwise */ public boolean stopCellEditing() { - return false; // TODO + if (realEditor.stopCellEditing()) + { + realEditor = null; + return true; + } + return false; } /** @@ -473,7 +550,8 @@ */ public void cancelCellEditing() { - // TODO + realEditor.cancelCellEditing(); + realEditor = null; } /** @@ -515,7 +593,12 @@ */ public void valueChanged(TreeSelectionEvent e) { - // TODO + lastPath = e.getNewLeadSelectionPath(); + lastRow = tree.getRowForPath(lastPath); + TreeSelectionListener[] listeners = tree.getTreeSelectionListeners(); + + for (int index = 0; index < listeners.length; ++index) + listeners[index].valueChanged(e); } /** @@ -525,7 +608,8 @@ */ public void actionPerformed(ActionEvent e) { - // TODO + if (tree != null && lastPath != null) + tree.startEditingAtPath(lastPath); } /** @@ -540,14 +624,19 @@ } /** - * Returns true if event is a MouseEvent and the click count is 1. + * Returns true if event is a MouseEvent and the click count is 1. * * @param event - the event being studied * @return true if editing should start */ protected boolean shouldStartEditingTimer(EventObject event) { - return false; // TODO + if (!(event instanceof MouseEvent) && + inHitRegion(((MouseEvent) event).getX(), + ((MouseEvent) event).getY()) && ((MouseEvent) event). + getClickCount() == 1) + return true; + return false; } /** @@ -555,7 +644,9 @@ */ protected void startEditingTimer() { - // TODO + if (timer == null) + timer = new javax.swing.Timer(1200, this); + timer.start(); } /** @@ -566,9 +657,15 @@ * @return true if event is null, or it is a MouseEvent with * a click count > 2 and inHitRegion returns true */ - protected boolean canEditImmediately(EventObject value0) + protected boolean canEditImmediately(EventObject event) { - return false; // TODO + if (event == null || (!(event instanceof MouseEvent) && (((MouseEvent) event). + getClickCount() > 2 || inHitRegion(((MouseEvent) event).getX(), + ((MouseEvent) event).getY())))) + return true; + else if (shouldStartEditingTimer(event)) + startEditingTimer(); + return false; } /** @@ -586,7 +683,16 @@ */ protected boolean inHitRegion(int x, int y) { - return false; // TODO + Dimension d = editingContainer.getPreferredSize(); + int textX = offset; + + if (editingIcon != null) + textX += editingIcon.getIconWidth() + 4; + + if ((d.width > textX && x >= textX && x <= d.width) + && (y >= 0 && y <= d.height)) + return true; + return false; } /** @@ -601,7 +707,13 @@ protected void determineOffset(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) { - // TODO + renderer.getTreeCellRendererComponent(tree, value, isSelected, expanded, + leaf, row, true); + Icon c = renderer.getIcon(); + if (c != null) + offset = renderer.getIconTextGap() + c.getIconWidth(); + else + offset = 0; } /** @@ -610,7 +722,7 @@ */ protected void prepareForEditing() { - // TODO + editingContainer.add(editingComponent); } /** @@ -620,7 +732,7 @@ */ protected Container createContainer() { - return null; // TODO + return new DefaultTreeCellEditor.EditorContainer(); } /** @@ -631,6 +743,9 @@ */ protected TreeCellEditor createTreeCellEditor() { - return null; // TODO + UIDefaults defaults = UIManager.getLookAndFeelDefaults(); + return new DefaultCellEditor(new DefaultTreeCellEditor. + DefaultTextField(defaults.getBorder + ("Tree.selectionBorder"))); } }