summaryrefslogtreecommitdiff
path: root/javax
diff options
context:
space:
mode:
authorAudrius Meskauskas <audriusa@Bioinformatics.org>2006-04-26 12:31:07 +0000
committerAudrius Meskauskas <audriusa@Bioinformatics.org>2006-04-26 12:31:07 +0000
commit42da653f1960e3412d46e945a709800d0587e26e (patch)
treedc6b40725786996c52586dfcba516cbb58e2c239 /javax
parente94ee062e444b58b9b1b38b9a825b9d0249f12ea (diff)
downloadclasspath-42da653f1960e3412d46e945a709800d0587e26e.tar.gz
2006-04-26 Audrius Meskauskas <AudriusA@Bioinformatics.org>
* javax/swing/JTree.java (setLeadSelectionPath): Repaint the new and old lead pathes. * javax/swing/plaf/basic/BasicTreeUI.java (FocusHandler): Repaint the lead row when focus changes. (PropertyChangeHandler): Use existing constants, not the string literals for the property names. (TreeIncrementAction): Shrink the selection when moving from the selection edge to the selection anchor. (TreeSelectionHandler.valueChanged): Repaint the new and old lead pathes. (paintRow): Treat row as focused only if it is the lead row. * javax/swing/tree/DefaultTreeCellRenderer.java (getTreeCellRendererComponent): Set the vertical alignment to CENTER. (paint): Rewritten. * javax/swing/tree/DefaultTreeSelectionModel.java (addSelectionPath): Event construction fix (old and new lead were always the same). (addSelectionPaths): Likewise. * javax/swing/JComponent.java (setOpaque): Explained. * javax/swing/tree/FixedHeightLayoutCache.java (getBounds): Accept null. * javax/swing/tree/VariableHeightLayoutCache.java (getBounds): Accept null.
Diffstat (limited to 'javax')
-rw-r--r--javax/swing/JComponent.java7
-rw-r--r--javax/swing/JTree.java13
-rw-r--r--javax/swing/plaf/basic/BasicTreeUI.java87
-rw-r--r--javax/swing/tree/DefaultTreeCellRenderer.java29
-rw-r--r--javax/swing/tree/DefaultTreeSelectionModel.java20
-rw-r--r--javax/swing/tree/FixedHeightLayoutCache.java2
-rw-r--r--javax/swing/tree/VariableHeightLayoutCache.java2
7 files changed, 123 insertions, 37 deletions
diff --git a/javax/swing/JComponent.java b/javax/swing/JComponent.java
index 8f5c22405..21904b566 100644
--- a/javax/swing/JComponent.java
+++ b/javax/swing/JComponent.java
@@ -2924,9 +2924,12 @@ public abstract class JComponent extends Container implements Serializable
}
/**
- * Set the value of the {@link #opaque} property.
+ * Set if the component should paint all pixels withing its bounds.
+ * If this property is set to false, the component expects the cleared
+ * background.
*
- * @param isOpaque The new value of the property
+ * @param isOpaque if true, paint all pixels. If false, expect the clean
+ * background.
*
* @see ComponentUI#update
*/
diff --git a/javax/swing/JTree.java b/javax/swing/JTree.java
index dd3e65c9f..d90ad171b 100644
--- a/javax/swing/JTree.java
+++ b/javax/swing/JTree.java
@@ -2298,8 +2298,17 @@ public class JTree extends JComponent implements Scrollable, Accessible
TreePath oldValue = selectionModel.getLeadSelectionPath();
if (path.equals(oldValue))
return;
-
- selectionModel.addSelectionPath(path);
+
+ // Repaint the previous and current rows with the lead selection path.
+ if (path != null)
+ {
+ repaint(getPathBounds(path));
+ selectionModel.addSelectionPath(path);
+ }
+
+ if (oldValue!=null)
+ repaint(getPathBounds(oldValue));
+
firePropertyChange(LEAD_SELECTION_PATH_PROPERTY, oldValue, path);
}
}
diff --git a/javax/swing/plaf/basic/BasicTreeUI.java b/javax/swing/plaf/basic/BasicTreeUI.java
index 5df5c0587..d9aeb9d2c 100644
--- a/javax/swing/plaf/basic/BasicTreeUI.java
+++ b/javax/swing/plaf/basic/BasicTreeUI.java
@@ -2053,24 +2053,38 @@ public class BasicTreeUI
/**
* Invoked when focus is activated on the tree we're in, redraws the lead
- * row. Invoked when a component gains the keyboard focus.
+ * row. Invoked when a component gains the keyboard focus. The method
+ * repaints the lead row that is shown differently when the tree is in
+ * focus.
*
* @param e is the focus event that is activated
*/
public void focusGained(FocusEvent e)
{
- // TODO: Implement this properly.
+ repaintLeadRow();
}
/**
* Invoked when focus is deactivated on the tree we're in, redraws the lead
- * row. Invoked when a component loses the keyboard focus.
+ * row. Invoked when a component loses the keyboard focus. The method
+ * repaints the lead row that is shown differently when the tree is in
+ * focus.
*
* @param e is the focus event that is deactivated
*/
public void focusLost(FocusEvent e)
{
- // TODO: Implement this properly.
+ repaintLeadRow();
+ }
+
+ /**
+ * Repaint the lead row.
+ */
+ void repaintLeadRow()
+ {
+ TreePath lead = tree.getLeadSelectionPath();
+ if (lead!=null)
+ tree.repaint(tree.getPathBounds(lead));
}
}
@@ -2479,21 +2493,22 @@ public class BasicTreeUI
*/
public void propertyChange(PropertyChangeEvent event)
{
- if ((event.getPropertyName()).equals("rootVisible"))
+ String property = event.getPropertyName();
+ if (property.equals(JTree.ROOT_VISIBLE_PROPERTY))
{
validCachedPreferredSize = false;
treeState.setRootVisible(tree.isRootVisible());
tree.repaint();
}
- else if ((event.getPropertyName()).equals("selectionModel"))
+ else if (property.equals(JTree.SELECTION_MODEL_PROPERTY))
{
- TreeSelectionModel model = tree.getSelectionModel();
- model.setRowMapper(treeState);
+ treeSelectionModel = tree.getSelectionModel();
+ treeSelectionModel.setRowMapper(treeState);
}
- else if ((event.getPropertyName()).equals("model"))
+ else if (property.equals(JTree.TREE_MODEL_PROPERTY))
{
- TreeModel model = tree.getModel();
- model.addTreeModelListener(treeModelListener);
+ treeModel = tree.getModel();
+ treeModel.addTreeModelListener(treeModelListener);
}
}
}
@@ -2699,12 +2714,20 @@ public class BasicTreeUI
{
newPath = treeState.getPathForRow(prevRow);
tree.setSelectionPath(newPath);
- tree.setLeadSelectionPath(newPath);
tree.setAnchorSelectionPath(newPath);
+ tree.setLeadSelectionPath(newPath);
}
else if (command.equals("selectPreviousExtendSelection") && hasPrev)
{
newPath = treeState.getPathForRow(prevRow);
+
+ // If the new path is already selected, the selection shrinks,
+ // unselecting the previously current path.
+ if (tree.isPathSelected(newPath))
+ tree.getSelectionModel().removeSelectionPath(currentPath);
+
+ // This must be called in any case because it updates the model
+ // lead selection index.
tree.addSelectionPath(newPath);
tree.setLeadSelectionPath(newPath);
}
@@ -2721,15 +2744,24 @@ public class BasicTreeUI
else if (command.equals("selectNextExtendSelection") && hasNext)
{
newPath = treeState.getPathForRow(nextRow);
+
+ // If the new path is already selected, the selection shrinks,
+ // unselecting the previously current path.
+ if (tree.isPathSelected(newPath))
+ tree.getSelectionModel().removeSelectionPath(currentPath);
+
+ // This must be called in any case because it updates the model
+ // lead selection index.
tree.addSelectionPath(newPath);
+
tree.setLeadSelectionPath(newPath);
}
else if (command.equals("selectNextChangeLead") && hasNext)
{
newPath = treeState.getPathForRow(nextRow);
tree.setSelectionPath(newPath);
+ tree.setAnchorSelectionPath(newPath);
tree.setLeadSelectionPath(newPath);
- tree.setAnchorSelectionPath(newPath);
}
}
@@ -2894,7 +2926,24 @@ public class BasicTreeUI
public void valueChanged(TreeSelectionEvent event)
{
if (tree.isEditing())
- tree.stopEditing();
+ tree.cancelEditing();
+
+ TreePath op = event.getOldLeadSelectionPath();
+ TreePath np = event.getNewLeadSelectionPath();
+
+ // Repaint of the changed lead selection path.
+ if (op != np)
+ {
+ Rectangle o = treeState.getBounds(event.getOldLeadSelectionPath(),
+ new Rectangle());
+ Rectangle n = treeState.getBounds(event.getNewLeadSelectionPath(),
+ new Rectangle());
+
+ if (o!=null)
+ tree.repaint(o);
+ if (n!=null)
+ tree.repaint(n);
+ }
}
}// TreeSelectionHandler
@@ -3082,7 +3131,7 @@ public class BasicTreeUI
if (path != null)
{
tree.setSelectionPath(path);
- tree.setLeadSelectionPath(path);
+ tree.setLeadSelectionPath(path);
tree.makeVisible(path);
tree.scrollPathToVisible(path);
}
@@ -3311,9 +3360,15 @@ public class BasicTreeUI
if (dtcr == null)
dtcr = createDefaultCellRenderer();
+ boolean focused = false;
+ if (treeSelectionModel!= null)
+ focused = treeSelectionModel.getLeadSelectionRow() == row
+ && tree.isFocusOwner();
+
Component c = dtcr.getTreeCellRendererComponent(tree, node, selected,
isExpanded, isLeaf, row,
- tree.hasFocus());
+ focused);
+
rendererPane.paintComponent(g, c, c.getParent(), bounds);
}
diff --git a/javax/swing/tree/DefaultTreeCellRenderer.java b/javax/swing/tree/DefaultTreeCellRenderer.java
index df70ba7fb..5e93145ae 100644
--- a/javax/swing/tree/DefaultTreeCellRenderer.java
+++ b/javax/swing/tree/DefaultTreeCellRenderer.java
@@ -407,7 +407,7 @@ public class DefaultTreeCellRenderer
this.hasFocus = hasFocus;
setHorizontalAlignment(LEFT);
setOpaque(false);
- setVerticalAlignment(TOP);
+ setVerticalAlignment(CENTER);
setEnabled(true);
super.setFont(UIManager.getFont("Tree.font"));
@@ -445,8 +445,7 @@ public class DefaultTreeCellRenderer
/**
* Paints the value. The background is filled based on selected.
*
- * @param g
- * the graphics device.
+ * @param g the graphics device.
*/
public void paint(Graphics g)
{
@@ -468,17 +467,27 @@ public class DefaultTreeCellRenderer
getHorizontalTextPosition(), vr, ir, tr,
getIconTextGap());
+ // Reusing one rectangle.
+ Rectangle bounds = getBounds(ir);
+
+ bounds.x = tr.x - insets.left;
+ bounds.width = tr.width + insets.left+insets.right;
+
g.setColor(super.getBackground());
- g.fillRect(tr.x, tr.y, tr.width, tr.height - insets.top - insets.bottom);
+ g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
- // paint border
- Color b = getBorderSelectionColor();
- if (b != null)
+ super.paint(g);
+
+ // Paint the border of the focused element only (lead selection)
+ if (hasFocus)
{
- g.setColor(b);
- g.drawRect(tr.x, tr.y, tr.width, tr.height - insets.top - insets.bottom);
+ Color b = getBorderSelectionColor();
+ if (b != null)
+ {
+ g.setColor(b);
+ g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height - 1);
+ }
}
- super.paint(g);
}
/**
diff --git a/javax/swing/tree/DefaultTreeSelectionModel.java b/javax/swing/tree/DefaultTreeSelectionModel.java
index 82832da49..54b4f638d 100644
--- a/javax/swing/tree/DefaultTreeSelectionModel.java
+++ b/javax/swing/tree/DefaultTreeSelectionModel.java
@@ -341,6 +341,9 @@ public class DefaultTreeSelectionModel
* is already selected and doesn't add the same path twice. If this changes
* the selection the registered TreeSelectionListeners are notified.
*
+ * The lead path is changed to the added path. This also happen if the
+ * passed path was already selected before.
+ *
* @param path the path to add to the selection
*/
public void addSelectionPath(TreePath path)
@@ -358,11 +361,16 @@ public class DefaultTreeSelectionModel
selection = new TreePath[temp.length];
System.arraycopy(temp, 0, selection, 0, temp.length);
}
+ }
+
+ if (path!=leadPath)
+ {
+ TreePath oldLead = leadPath;
leadPath = path;
leadRow = getRow(path);
leadIndex = selection.length - 1;
- fireValueChanged(new TreeSelectionEvent(this, path, true, leadPath,
- path));
+ fireValueChanged(new TreeSelectionEvent(this, path, true, oldLead,
+ leadPath));
}
}
@@ -396,12 +404,13 @@ public class DefaultTreeSelectionModel
selection = new TreePath[temp.length];
System.arraycopy(temp, 0, selection, 0, temp.length);
}
+ TreePath oldLead = leadPath;
leadPath = paths[paths.length - 1];
leadRow = getRow(leadPath);
leadIndex = selection.length - 1;
fireValueChanged(new TreeSelectionEvent(this, v0, true,
- leadPath, paths[0]));
+ oldLead, leadPath));
}
}
insureRowContinuity();
@@ -732,10 +741,7 @@ public class DefaultTreeSelectionModel
*/
public int getLeadSelectionRow()
{
- if ((rowMapper == null) || (leadPath == null))
- return - 1;
- else
- return getRow(leadPath);
+ return leadRow;
}
/**
diff --git a/javax/swing/tree/FixedHeightLayoutCache.java b/javax/swing/tree/FixedHeightLayoutCache.java
index 6cd7d90d2..993f81f23 100644
--- a/javax/swing/tree/FixedHeightLayoutCache.java
+++ b/javax/swing/tree/FixedHeightLayoutCache.java
@@ -326,6 +326,8 @@ public class FixedHeightLayoutCache
*/
public Rectangle getBounds(TreePath path, Rectangle rect)
{
+ if (path == null)
+ return null;
if (dirty)
update();
Object last = path.getLastPathComponent();
diff --git a/javax/swing/tree/VariableHeightLayoutCache.java b/javax/swing/tree/VariableHeightLayoutCache.java
index 0599a0dc3..0e09d259f 100644
--- a/javax/swing/tree/VariableHeightLayoutCache.java
+++ b/javax/swing/tree/VariableHeightLayoutCache.java
@@ -325,6 +325,8 @@ public class VariableHeightLayoutCache
*/
public Rectangle getBounds(TreePath path, Rectangle rect)
{
+ if (path == null)
+ return null;
if (dirty)
update();
Object last = path.getLastPathComponent();