diff options
Diffstat (limited to 'libjava/classpath/javax/swing/plaf/basic/BasicTreeUI.java')
-rw-r--r-- | libjava/classpath/javax/swing/plaf/basic/BasicTreeUI.java | 529 |
1 files changed, 314 insertions, 215 deletions
diff --git a/libjava/classpath/javax/swing/plaf/basic/BasicTreeUI.java b/libjava/classpath/javax/swing/plaf/basic/BasicTreeUI.java index 9a193986ac5..5b0ffce09b6 100644 --- a/libjava/classpath/javax/swing/plaf/basic/BasicTreeUI.java +++ b/libjava/classpath/javax/swing/plaf/basic/BasicTreeUI.java @@ -38,17 +38,16 @@ package javax.swing.plaf.basic; -import gnu.classpath.NotImplementedException; import gnu.javax.swing.tree.GnuPath; import java.awt.Color; import java.awt.Component; +import java.awt.Container; import java.awt.Dimension; -import java.awt.Font; -import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Insets; import java.awt.Label; +import java.awt.Point; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -196,7 +195,7 @@ public class BasicTreeUI protected AbstractLayoutCache treeState; /** Used for minimizing the drawing of vertical lines. */ - protected Hashtable drawingCache; + protected Hashtable<TreePath, Boolean> drawingCache; /** * True if doing optimizations for a largeModel. Subclasses that don't support @@ -275,13 +274,6 @@ public class BasicTreeUI TreeModelListener treeModelListener; /** - * This timer fires the editing action after about 1200 ms if not reset during - * that time. It handles the editing start with the single mouse click (and - * not the double mouse click) on the selected tree node. - */ - Timer startEditTimer; - - /** * The zero size icon, used for expand controls, if they are not visible. */ static Icon nullIcon; @@ -428,6 +420,7 @@ public class BasicTreeUI { if (largeModel != this.largeModel) { + completeEditing(); tree.removeComponentListener(componentListener); this.largeModel = largeModel; tree.addComponentListener(componentListener); @@ -451,6 +444,7 @@ public class BasicTreeUI */ protected void setRowHeight(int rowHeight) { + completeEditing(); if (rowHeight == 0) rowHeight = getMaxHeight(tree); treeState.setRowHeight(rowHeight); @@ -544,6 +538,7 @@ public class BasicTreeUI */ protected void setRootVisible(boolean newValue) { + completeEditing(); tree.setRootVisible(newValue); } @@ -590,8 +585,7 @@ public class BasicTreeUI */ protected void setCellEditor(TreeCellEditor editor) { - cellEditor = editor; - createdCellEditor = true; + updateCellEditor(); } /** @@ -611,7 +605,7 @@ public class BasicTreeUI */ protected void setEditable(boolean newValue) { - tree.setEditable(newValue); + updateCellEditor(); } /** @@ -632,6 +626,7 @@ public class BasicTreeUI */ protected void setSelectionModel(TreeSelectionModel newLSM) { + completeEditing(); if (newLSM != null) { treeSelectionModel = newLSM; @@ -787,12 +782,13 @@ public class BasicTreeUI */ public boolean stopEditing(JTree tree) { - if (isEditing(tree)) + boolean ret = false; + if (editingComponent != null && cellEditor.stopCellEditing()) { completeEditing(false, false, true); - finish(); + ret = true; } - return ! isEditing(tree); + return ret; } /** @@ -805,8 +801,8 @@ public class BasicTreeUI // There is no need to send the cancel message to the editor, // as the cancellation event itself arrives from it. This would // only be necessary when cancelling the editing programatically. - completeEditing(false, false, false); - finish(); + if (editingComponent != null) + completeEditing(false, true, false); } /** @@ -818,7 +814,9 @@ public class BasicTreeUI */ public void startEditingAtPath(JTree tree, TreePath path) { - startEditing(path, null); + tree.scrollPathToVisible(path); + if (path != null && tree.isVisible(path)) + startEditing(path, null); } /** @@ -842,6 +840,7 @@ public class BasicTreeUI preferredSize = new Dimension(); largeModel = tree.isLargeModel(); preferredSize = new Dimension(); + stopEditingInCompleteEditing = true; setModel(tree.getModel()); } @@ -1136,6 +1135,7 @@ public class BasicTreeUI */ protected void updateExpandedDescendants(TreePath path) { + completeEditing(); Enumeration expanded = tree.getExpandedDescendants(path); while (expanded.hasMoreElements()) treeState.setExpandedState((TreePath) expanded.nextElement(), true); @@ -1167,9 +1167,33 @@ public class BasicTreeUI */ protected void updateCellEditor() { - if (tree.isEditable() && cellEditor == null) - setCellEditor(createDefaultCellEditor()); - createdCellEditor = true; + completeEditing(); + TreeCellEditor newEd = null; + if (tree != null && tree.isEditable()) + { + newEd = tree.getCellEditor(); + if (newEd == null) + { + newEd = createDefaultCellEditor(); + if (newEd != null) + { + tree.setCellEditor(newEd); + createdCellEditor = true; + } + } + } + // Update listeners. + if (newEd != cellEditor) + { + if (cellEditor != null && cellEditorListener != null) + cellEditor.removeCellEditorListener(cellEditorListener); + cellEditor = newEd; + if (cellEditorListener == null) + cellEditorListener = createCellEditorListener(); + if (cellEditor != null && cellEditorListener != null) + cellEditor.addCellEditorListener(cellEditorListener); + createdCellEditor = false; + } } /** @@ -1563,12 +1587,15 @@ public class BasicTreeUI for (int i = startIndex; i <= endIndex; i++, k++) { path[k] = treeState.getPathForRow(i); - isLeaf[k] = treeModel.isLeaf(path[k].getLastPathComponent()); - isExpanded[k] = tree.isExpanded(path[k]); - bounds[k] = getPathBounds(tree, path[k]); - - paintHorizontalPartOfLeg(g, clip, insets, bounds[k], path[k], i, - isExpanded[k], false, isLeaf[k]); + if (path[k] != null) + { + isLeaf[k] = treeModel.isLeaf(path[k].getLastPathComponent()); + isExpanded[k] = tree.isExpanded(path[k]); + bounds[k] = getPathBounds(tree, path[k]); + + paintHorizontalPartOfLeg(g, clip, insets, bounds[k], path[k], + i, isExpanded[k], false, isLeaf[k]); + } if (isLastChild(path[k])) paintVerticalPartOfLeg(g, clip, insets, path[k]); } @@ -1576,8 +1603,9 @@ public class BasicTreeUI k = 0; for (int i = startIndex; i <= endIndex; i++, k++) { - paintRow(g, clip, insets, bounds[k], path[k], i, isExpanded[k], - false, isLeaf[k]); + if (path[k] != null) + paintRow(g, clip, insets, bounds[k], path[k], i, isExpanded[k], + false, isLeaf[k]); } } } @@ -1587,7 +1615,9 @@ public class BasicTreeUI */ private boolean isLastChild(TreePath path) { - if (path instanceof GnuPath) + if (path == null) + return false; + else if (path instanceof GnuPath) { // Except the seldom case when the layout cache is changed, this // optimized code will be executed. @@ -1719,6 +1749,10 @@ public class BasicTreeUI */ protected void completeEditing() { + if (tree.getInvokesStopCellEditing() && stopEditingInCompleteEditing + && editingComponent != null) + cellEditor.stopCellEditing(); + completeEditing(false, true, false); } @@ -1736,28 +1770,35 @@ public class BasicTreeUI boolean messageTree) { // Make no attempt to complete the non existing editing session. - if (!isEditing(tree)) - return; - - if (messageStop) + if (stopEditingInCompleteEditing && editingComponent != null) { - getCellEditor().stopCellEditing(); - stopEditingInCompleteEditing = true; - } - - if (messageCancel) - { - getCellEditor().cancelCellEditing(); - stopEditingInCompleteEditing = true; - } + Component comp = editingComponent; + TreePath p = editingPath; + editingComponent = null; + editingPath = null; + if (messageStop) + cellEditor.stopCellEditing(); + else if (messageCancel) + cellEditor.cancelCellEditing(); + + tree.remove(comp); + + if (editorHasDifferentSize) + { + treeState.invalidatePathBounds(p); + updateSize(); + } + else + { + // Need to refresh the tree. + Rectangle b = getPathBounds(tree, p); + tree.repaint(0, b.y, tree.getWidth(), b.height); + } - if (messageTree) - { - TreeCellEditor editor = getCellEditor(); - if (editor != null) + if (messageTree) { - Object value = editor.getCellEditorValue(); - treeModel.valueForPathChanged(tree.getLeadSelectionPath(), value); + Object value = cellEditor.getCellEditorValue(); + treeModel.valueForPathChanged(p, value); } } } @@ -1772,47 +1813,105 @@ public class BasicTreeUI */ protected boolean startEditing(TreePath path, MouseEvent event) { - updateCellEditor(); - TreeCellEditor ed = getCellEditor(); + // Maybe cancel editing. + if (isEditing(tree) && tree.getInvokesStopCellEditing() + && ! stopEditing(tree)) + return false; - if (ed != null && (event == EDIT || ed.shouldSelectCell(event)) - && ed.isCellEditable(event)) + completeEditing(); + TreeCellEditor ed = cellEditor; + if (ed != null && tree.isPathEditable(path)) { - Rectangle bounds = getPathBounds(tree, path); - - // Extend the right boundary till the tree width. - bounds.width = tree.getWidth() - bounds.x; - - editingPath = path; - editingRow = tree.getRowForPath(editingPath); - - Object value = editingPath.getLastPathComponent(); - - stopEditingInCompleteEditing = false; - boolean expanded = tree.isExpanded(editingPath); - isEditing = true; - editingComponent = ed.getTreeCellEditorComponent(tree, value, true, - expanded, - isLeaf(editingRow), - editingRow); - - // Remove all previous components (if still present). Only one - // container with the editing component inside is allowed in the tree. - tree.removeAll(); - - // The editing component must be added to its container. We add the - // container, not the editing component itself. - Component container = editingComponent.getParent(); - container.setBounds(bounds); - tree.add(container); - editingComponent.requestFocus(); + if (ed.isCellEditable(event)) + { + editingRow = getRowForPath(tree, path); + Object value = path.getLastPathComponent(); + boolean isSelected = tree.isPathSelected(path); + boolean isExpanded = tree.isExpanded(editingPath); + boolean isLeaf = treeModel.isLeaf(value); + editingComponent = ed.getTreeCellEditorComponent(tree, value, + isSelected, + isExpanded, + isLeaf, + editingRow); + + Rectangle bounds = getPathBounds(tree, path); + + Dimension size = editingComponent.getPreferredSize(); + int rowHeight = getRowHeight(); + if (size.height != bounds.height && rowHeight > 0) + size.height = rowHeight; + + if (size.width != bounds.width || size.height != bounds.height) + { + editorHasDifferentSize = true; + treeState.invalidatePathBounds(path); + updateSize(); + } + else + editorHasDifferentSize = false; + + // The editing component must be added to its container. We add the + // container, not the editing component itself. + tree.add(editingComponent); + editingComponent.setBounds(bounds.x, bounds.y, size.width, + size.height); + editingComponent.validate(); + editingPath = path; + + if (ed.shouldSelectCell(event)) + { + stopEditingInCompleteEditing = false; + tree.setSelectionRow(editingRow); + stopEditingInCompleteEditing = true; + } + + editorRequestFocus(editingComponent); + // Register MouseInputHandler to redispatch initial mouse events + // correctly. + if (event instanceof MouseEvent) + { + Point p = SwingUtilities.convertPoint(tree, event.getX(), event.getY(), + editingComponent); + Component active = + SwingUtilities.getDeepestComponentAt(editingComponent, p.x, p.y); + if (active != null) + { + MouseInputHandler ih = new MouseInputHandler(tree, active, event); + + } + } - return true; + return true; + } + else + editingComponent = null; } return false; } /** + * Requests focus on the editor. The method is necessary since the + * DefaultTreeCellEditor returns a container that contains the + * actual editor, and we want to request focus on the editor, not the + * container. + */ + private void editorRequestFocus(Component c) + { + if (c instanceof Container) + { + // TODO: Maybe do something more reasonable here, like queriying the + // FocusTraversalPolicy. + Container cont = (Container) c; + if (cont.getComponentCount() > 0) + cont.getComponent(0).requestFocus(); + } + else if (c.isFocusable()) + c.requestFocus(); + + } + + /** * If the <code>mouseX</code> and <code>mouseY</code> are in the expand or * collapse region of the row, this will toggle the row. * @@ -2180,7 +2279,7 @@ public class BasicTreeUI */ public void editingStopped(ChangeEvent e) { - stopEditing(tree); + completeEditing(false, false, true); } /** @@ -2191,7 +2290,7 @@ public class BasicTreeUI */ public void editingCanceled(ChangeEvent e) { - cancelEditing(tree); + completeEditing(false, false, false); } } // CellEditorHandler @@ -2347,9 +2446,15 @@ public class BasicTreeUI * events. */ public class MouseHandler - extends MouseAdapter - implements MouseMotionListener + extends MouseAdapter + implements MouseMotionListener { + + /** + * If the cell has been selected on mouse press. + */ + private boolean selectedOnPress; + /** * Constructor */ @@ -2365,76 +2470,15 @@ public class BasicTreeUI */ public void mousePressed(MouseEvent e) { - // Any mouse click cancels the previous waiting edit action, initiated - // by the single click on the selected node. - if (startEditTimer != null) + if (! e.isConsumed()) { - startEditTimer.stop(); - startEditTimer = null; + handleEvent(e); + selectedOnPress = true; } - - if (tree != null && tree.isEnabled()) + else { - // Always end the current editing session if clicked on the - // tree and outside the bounds of the editing component. - if (isEditing(tree)) - if (!stopEditing(tree)) - // Return if we have failed to cancel the editing session. - return; - - int x = e.getX(); - int y = e.getY(); - TreePath path = getClosestPathForLocation(tree, x, y); - - if (path != null) - { - Rectangle bounds = getPathBounds(tree, path); - if (SwingUtilities.isLeftMouseButton(e)) - checkForClickInExpandControl(path, x, y); - - if (x > bounds.x && x <= (bounds.x + bounds.width)) - { - TreePath currentLead = tree.getLeadSelectionPath(); - if (currentLead != null && currentLead.equals(path) - && e.getClickCount() == 1 && tree.isEditable()) - { - // Schedule the editing session. - final TreePath editPath = path; - - // The code below handles the required click-pause-click - // functionality which must be present in the tree UI. - // If the next click comes after the - // time longer than the double click interval AND - // the same node stays focused for the WAIT_TILL_EDITING - // duration, the timer starts the editing session. - if (startEditTimer != null) - startEditTimer.stop(); - - startEditTimer = new Timer(WAIT_TILL_EDITING, - new ActionListener() - { - public void actionPerformed(ActionEvent e) - { - startEditing(editPath, EDIT); - } - }); - - startEditTimer.setRepeats(false); - startEditTimer.start(); - } - else - { - if (e.getClickCount() == 2) - toggleExpandState(path); - else - selectPathForEvent(path, e); - } - } - } + selectedOnPress = false; } - - // We need to request the focus. - tree.requestFocusInWindow(); } /** @@ -2446,9 +2490,8 @@ public class BasicTreeUI * @param e is the mouse event that occured */ public void mouseDragged(MouseEvent e) - throws NotImplementedException { - // TODO: What should be done here, if anything? + // Nothing to do here. } /** @@ -2458,9 +2501,8 @@ public class BasicTreeUI * @param e the mouse event that occured */ public void mouseMoved(MouseEvent e) - throws NotImplementedException { - // TODO: What should be done here, if anything? + // Nothing to do here. } /** @@ -2469,9 +2511,46 @@ public class BasicTreeUI * @param e is the mouse event that occured */ public void mouseReleased(MouseEvent e) - throws NotImplementedException { - // TODO: What should be done here, if anything? + if (! e.isConsumed() && ! selectedOnPress) + handleEvent(e); + } + + /** + * Handles press and release events. + * + * @param e the mouse event + */ + private void handleEvent(MouseEvent e) + { + if (tree != null && tree.isEnabled()) + { + // Maybe stop editing. + if (isEditing(tree) && tree.getInvokesStopCellEditing() + && ! stopEditing(tree)) + return; + + // Explicitly request focus. + tree.requestFocusInWindow(); + + int x = e.getX(); + int y = e.getY(); + TreePath path = getClosestPathForLocation(tree, x, y); + if (path != null) + { + Rectangle b = getPathBounds(tree, path); + if (y <= b.y + b.height) + { + if (SwingUtilities.isLeftMouseButton(e)) + checkForClickInExpandControl(path, x, y); + if (x > b.x && x <= b.x + b.width) + { + if (! startEditing(path, e)) + selectPathForEvent(path, e); + } + } + } + } } } @@ -2501,6 +2580,9 @@ public class BasicTreeUI { this.source = source; this.destination = destination; + source.addMouseListener(this); + source.addMouseMotionListener(this); + dispatch(e); } /** @@ -2510,9 +2592,8 @@ public class BasicTreeUI * @param e mouse event that occured */ public void mouseClicked(MouseEvent e) - throws NotImplementedException { - // TODO: What should be done here, if anything? + dispatch(e); } /** @@ -2521,9 +2602,8 @@ public class BasicTreeUI * @param e mouse event that occured */ public void mousePressed(MouseEvent e) - throws NotImplementedException { - // TODO: What should be done here, if anything? + // Nothing to do here. } /** @@ -2532,9 +2612,9 @@ public class BasicTreeUI * @param e mouse event that occured */ public void mouseReleased(MouseEvent e) - throws NotImplementedException { - // TODO: What should be done here, if anything? + dispatch(e); + removeFromSource(); } /** @@ -2543,9 +2623,9 @@ public class BasicTreeUI * @param e mouse event that occured */ public void mouseEntered(MouseEvent e) - throws NotImplementedException { - // TODO: What should be done here, if anything? + if (! SwingUtilities.isLeftMouseButton(e)) + removeFromSource(); } /** @@ -2554,9 +2634,9 @@ public class BasicTreeUI * @param e mouse event that occured */ public void mouseExited(MouseEvent e) - throws NotImplementedException { - // TODO: What should be done here, if anything? + if (! SwingUtilities.isLeftMouseButton(e)) + removeFromSource(); } /** @@ -2568,9 +2648,8 @@ public class BasicTreeUI * @param e mouse event that occured */ public void mouseDragged(MouseEvent e) - throws NotImplementedException { - // TODO: What should be done here, if anything? + dispatch(e); } /** @@ -2580,18 +2659,37 @@ public class BasicTreeUI * @param e mouse event that occured */ public void mouseMoved(MouseEvent e) - throws NotImplementedException { - // TODO: What should be done here, if anything? + removeFromSource(); } /** * Removes event from the source */ protected void removeFromSource() - throws NotImplementedException { - // TODO: Implement this properly. + if (source != null) + { + source.removeMouseListener(this); + source.removeMouseMotionListener(this); + } + source = null; + destination = null; + } + + /** + * Redispatches mouse events to the destination. + * + * @param e the mouse event to redispatch + */ + private void dispatch(MouseEvent e) + { + if (destination != null) + { + MouseEvent e2 = SwingUtilities.convertMouseEvent(source, e, + destination); + destination.dispatchEvent(e2); + } } } @@ -2627,24 +2725,42 @@ public class BasicTreeUI public Rectangle getNodeDimensions(Object cell, int row, int depth, boolean expanded, Rectangle size) { - if (size == null || cell == null) - return null; - - String s = cell.toString(); - Font f = tree.getFont(); - FontMetrics fm = tree.getToolkit().getFontMetrics(f); - - if (s != null) + Dimension prefSize; + if (editingComponent != null && editingRow == row) + { + // Editing, ask editor for preferred size. + prefSize = editingComponent.getPreferredSize(); + int rowHeight = getRowHeight(); + if (rowHeight > 0 && rowHeight != prefSize.height) + prefSize.height = rowHeight; + } + else + { + // Not editing, ask renderer for preferred size. + Component rend = + currentCellRenderer.getTreeCellRendererComponent(tree, cell, + tree.isRowSelected(row), + expanded, + treeModel.isLeaf(cell), + row, false); + // Make sure the layout is valid. + rendererPane.add(rend); + rend.validate(); + prefSize = rend.getPreferredSize(); + } + if (size != null) { - TreePath path = treeState.getPathForRow(row); size.x = getRowX(row, depth); - size.width = SwingUtilities.computeStringWidth(fm, s); - size.width = size.width + getCurrentControlIcon(path).getIconWidth() - + gap + getNodeIcon(path).getIconWidth(); - size.height = getMaxHeight(tree); - size.y = size.height * row; + // FIXME: This should be handled by the layout cache. + size.y = prefSize.height * row; + size.width = prefSize.width; + size.height = prefSize.height; } - + else + // FIXME: The y should be handled by the layout cache. + size = new Rectangle(getRowX(row, depth), prefSize.height * row, prefSize.width, + prefSize.height); + return size; } @@ -2706,6 +2822,9 @@ public class BasicTreeUI if (treeState != null) treeState.invalidateSizes(); } + else if (property.equals(JTree.EDITABLE_PROPERTY)) + setEditable(((Boolean) event.getNewValue()).booleanValue()); + } } @@ -2714,7 +2833,7 @@ public class BasicTreeUI * properties of the model change. */ public class SelectionModelPropertyChangeHandler - implements PropertyChangeListener + implements PropertyChangeListener { /** @@ -2732,9 +2851,8 @@ public class BasicTreeUI * the property that has changed. */ public void propertyChange(PropertyChangeEvent event) - throws NotImplementedException { - // TODO: What should be done here, if anything? + treeSelectionModel.resetRowSelection(); } } @@ -2804,6 +2922,7 @@ public class BasicTreeUI */ public void treeCollapsed(TreeExpansionEvent event) { + completeEditing(); validCachedPreferredSize = false; treeState.setExpandedState(event.getPath(), false); // The maximal cell height may change @@ -3269,8 +3388,7 @@ public class BasicTreeUI */ public void valueChanged(TreeSelectionEvent event) { - if (tree.isEditing()) - tree.cancelEditing(); + completeEditing(); TreePath op = event.getOldLeadSelectionPath(); TreePath np = event.getNewLeadSelectionPath(); @@ -3808,25 +3926,6 @@ public class BasicTreeUI } /** - * Finish the editing session. - */ - void finish() - { - treeState.invalidatePathBounds(treeState.getPathForRow(editingRow)); - editingPath = null; - editingRow = - 1; - stopEditingInCompleteEditing = false; - isEditing = false; - Rectangle bounds = editingComponent.getParent().getBounds(); - tree.removeAll(); - validCachedPreferredSize = false; - // Repaint the region, where was the editing component. - tree.repaint(bounds); - editingComponent = null; - tree.requestFocus(); - } - - /** * Returns the amount to indent the given row * * @return amount to indent the given row. |