diff options
author | Audrius Meskauskas <audriusa@Bioinformatics.org> | 2006-04-23 18:20:08 +0000 |
---|---|---|
committer | Audrius Meskauskas <audriusa@Bioinformatics.org> | 2006-04-23 18:20:08 +0000 |
commit | 438d175e9116fa55869ae913b6cec75040d4c690 (patch) | |
tree | b3660994d90ab807cb730547ff089e8e8af79eb7 | |
parent | e6314628d7fbbb250218790534c101f295c61b11 (diff) | |
download | classpath-438d175e9116fa55869ae913b6cec75040d4c690.tar.gz |
2006-04-23 Audrius Meskauskas <AudriusA@Bioinformatics.org>
* examples/gnu/classpath/examples/swing/TreeDemo.java:
(createContent): Added check box to swith between single and
multiple selection.
* javax/swing/JTree.java (leadSelectionPath): Removed.
(addSelectionInterval): Explained. (getLeadSelectionPath):
Request the path from model. (getPathsBetweenRows): Explained.
(setLeadSelectionPath): Set the path in model.
* javax/swing/plaf/basic/BasicTreeUI.java
(TreeIncrementAction.actionPerformed, isMultiSelectionEvent,
isToggleSelectionEvent, selectPath, selectPathForEvent): Rewritten.
(MouseHandler.mousePressed): Call selectPathForEvent.
-rw-r--r-- | ChangeLog | 14 | ||||
-rw-r--r-- | examples/gnu/classpath/examples/swing/TreeDemo.java | 24 | ||||
-rw-r--r-- | javax/swing/JTree.java | 42 | ||||
-rw-r--r-- | javax/swing/plaf/basic/BasicTreeUI.java | 76 |
4 files changed, 113 insertions, 43 deletions
@@ -1,3 +1,17 @@ +2006-04-23 Audrius Meskauskas <AudriusA@Bioinformatics.org> + + * examples/gnu/classpath/examples/swing/TreeDemo.java: + (createContent): Added check box to swith between single and + multiple selection. + * javax/swing/JTree.java (leadSelectionPath): Removed. + (addSelectionInterval): Explained. (getLeadSelectionPath): + Request the path from model. (getPathsBetweenRows): Explained. + (setLeadSelectionPath): Set the path in model. + * javax/swing/plaf/basic/BasicTreeUI.java + (TreeIncrementAction.actionPerformed, isMultiSelectionEvent, + isToggleSelectionEvent, selectPath, selectPathForEvent): Rewritten. + (MouseHandler.mousePressed): Call selectPathForEvent. + 2006-04-23 Jeroen Frijters <jeroen@frijters.net> * NEWS: Added entry about new Package constructor. diff --git a/examples/gnu/classpath/examples/swing/TreeDemo.java b/examples/gnu/classpath/examples/swing/TreeDemo.java index 9c6c3b87e..1762f065d 100644 --- a/examples/gnu/classpath/examples/swing/TreeDemo.java +++ b/examples/gnu/classpath/examples/swing/TreeDemo.java @@ -39,10 +39,13 @@ exception statement from your version. */ package gnu.classpath.examples.swing; import java.awt.BorderLayout; +import java.awt.JobAttributes.DefaultSelectionType; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import javax.swing.DebugGraphics; import javax.swing.JButton; +import javax.swing.JCheckBox; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; @@ -52,6 +55,7 @@ import javax.swing.SwingUtilities; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeSelectionModel; import javax.swing.tree.TreePath; +import javax.swing.tree.TreeSelectionModel; public class TreeDemo extends JPanel @@ -153,12 +157,30 @@ public class TreeDemo } } }); - + + final JCheckBox cbSingle = new JCheckBox("single selection"); + cbSingle.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + TreeSelectionModel model = tree.getSelectionModel(); + if (cbSingle.isSelected()) + model.setSelectionMode( + DefaultTreeSelectionModel.SINGLE_TREE_SELECTION); + else + model.setSelectionMode( + DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); + } + }); setLayout(new BorderLayout()); JPanel p2 = new JPanel(); p2.add(add); + p2.add(cbSingle); + + tree.getSelectionModel(). + setSelectionMode(DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); add(p2, BorderLayout.NORTH); add(new JScrollPane(tree), BorderLayout.CENTER); diff --git a/javax/swing/JTree.java b/javax/swing/JTree.java index 87d9d7ea5..8ee4b44ab 100644 --- a/javax/swing/JTree.java +++ b/javax/swing/JTree.java @@ -1402,8 +1402,6 @@ public class JTree extends JComponent implements Scrollable, Accessible private TreePath anchorSelectionPath; - private TreePath leadSelectionPath; - /** * This contains the state of all nodes in the tree. Al/ entries map the * TreePath of a note to to its state. Valid states are EXPANDED and @@ -1568,7 +1566,15 @@ public class JTree extends JComponent implements Scrollable, Accessible TreeUI ui = getUI(); return ui != null ? ui.getPathForRow(this, row) : null; } - + + /** + * Get the pathes that are displayes between the two given rows. + * + * @param index0 the starting row, inclusive + * @param index1 the ending row, inclusive + * + * @return the array of the tree pathes + */ protected TreePath[] getPathBetweenRows(int index0, int index1) { TreeUI ui = getUI(); @@ -2212,7 +2218,15 @@ public class JTree extends JComponent implements Scrollable, Accessible addSelectionPaths(paths); } - + + /** + * Select all rows between the two given indexes, inclusive. The method + * will not select the inner leaves and braches of the currently collapsed + * nodes in this interval. + * + * @param index0 the starting row, inclusive + * @param index1 the ending row, inclusive + */ public void addSelectionInterval(int index0, int index1) { TreePath[] paths = getPathBetweenRows(index0, index1); @@ -2268,7 +2282,10 @@ public class JTree extends JComponent implements Scrollable, Accessible public TreePath getLeadSelectionPath() { - return leadSelectionPath; + if (selectionModel == null) + return null; + else + return selectionModel.getLeadSelectionPath(); } /** @@ -2276,12 +2293,15 @@ public class JTree extends JComponent implements Scrollable, Accessible */ public void setLeadSelectionPath(TreePath path) { - if (leadSelectionPath == path) - return; - - TreePath oldValue = leadSelectionPath; - leadSelectionPath = path; - firePropertyChange(LEAD_SELECTION_PATH_PROPERTY, oldValue, path); + if (selectionModel != null) + { + TreePath oldValue = selectionModel.getLeadSelectionPath(); + if (path.equals(oldValue)) + return; + + selectionModel.addSelectionPath(path); + firePropertyChange(LEAD_SELECTION_PATH_PROPERTY, oldValue, path); + } } /** diff --git a/javax/swing/plaf/basic/BasicTreeUI.java b/javax/swing/plaf/basic/BasicTreeUI.java index 2f30dbec1..dbc8edabb 100644 --- a/javax/swing/plaf/basic/BasicTreeUI.java +++ b/javax/swing/plaf/basic/BasicTreeUI.java @@ -57,6 +57,7 @@ import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; +import java.awt.event.InputEvent; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; @@ -1716,7 +1717,10 @@ public class BasicTreeUI /** * Returning true signifies a mouse event on the node should toggle the - * selection of only the row under the mouse. + * selection of only the row under the mouse. The BasisTreeUI treats the + * event as "toggle selection event" if the CTRL button was pressed while + * clicking. The event is not counted as toggle event if the associated + * tree does not support the multiple selection. * * @param event is the MouseEvent performed on the row. * @return true signifies a mouse event on the node should toggle the @@ -1724,12 +1728,18 @@ public class BasicTreeUI */ protected boolean isToggleSelectionEvent(MouseEvent event) { - return (tree.getSelectionModel().getSelectionMode() == TreeSelectionModel.SINGLE_TREE_SELECTION); + return + (tree.getSelectionModel().getSelectionMode() != + TreeSelectionModel.SINGLE_TREE_SELECTION) && + ((event.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) != 0); } /** * Returning true signifies a mouse event on the node should select from the - * anchor point. + * anchor point. The BasisTreeUI treats the event as "multiple selection + * event" if the SHIFT button was pressed while clicking. The event is not + * counted as multiple selection event if the associated tree does not support + * the multiple selection. * * @param event is the MouseEvent performed on the node. * @return true signifies a mouse event on the node should select from the @@ -1737,7 +1747,10 @@ public class BasicTreeUI */ protected boolean isMultiSelectEvent(MouseEvent event) { - return (tree.getSelectionModel().getSelectionMode() == TreeSelectionModel.CONTIGUOUS_TREE_SELECTION); + return + (tree.getSelectionModel().getSelectionMode() != + TreeSelectionModel.SINGLE_TREE_SELECTION) && + ((event.getModifiersEx() & InputEvent.SHIFT_DOWN_MASK) != 0); } /** @@ -1759,15 +1772,19 @@ public class BasicTreeUI * row. If the even is a toggle selection event, the row is either selected, * or deselected. If the event identifies a multi selection event, the * selection is updated from the anchor point. Otherwise, the row is selected, - * and if the even specified a toggle event the row is expanded/collapsed. + * and the previous selection is cleared.</p> * * @param path is the path selected for an event * @param event is the MouseEvent performed on the path. + * + * @see #isToggleSelectionEvent(MouseEvent) + * @see #isMultiSelectEvent(MouseEvent) */ protected void selectPathForEvent(TreePath path, MouseEvent event) { if (isToggleSelectionEvent(event)) { + // The event selects or unselects the clicked row. if (tree.isPathSelected(path)) tree.removeSelectionPath(path); else @@ -1778,6 +1795,7 @@ public class BasicTreeUI } else if (isMultiSelectEvent(event)) { + // The event extends selection form anchor till the clicked row. TreePath anchor = tree.getAnchorSelectionPath(); if (anchor != null) { @@ -1788,7 +1806,11 @@ public class BasicTreeUI tree.addSelectionPath(path); } else - tree.addSelectionPath(path); + { + // This is an ordinary event that just selects the clicked row. + tree.setSelectionPath(path); + tree.setAnchorSelectionPath(path); + } } /** @@ -2167,23 +2189,22 @@ public class BasicTreeUI startEditTimer.stop(); startEditTimer = new Timer(WAIT_TILL_EDITING, - new ActionListener() - { - public void actionPerformed( - ActionEvent e) - { - startEditing(editPath, - EDIT); - } - }); + new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + startEditing(editPath, EDIT); + } + }); startEditTimer.setRepeats(false); startEditTimer.start(); } else { - selectPath(tree, path); if (e.getClickCount() == 2 && ! isLeaf(row)) toggleExpandState(path); + else + selectPathForEvent(path, e); } } @@ -2648,8 +2669,9 @@ public class BasicTreeUI if (command.equals("selectPreviousChangeLead") && hasPrev) { newPath = treeState.getPathForRow(prevRow); - selectPath(tree, newPath); + tree.setSelectionPath(newPath); tree.setLeadSelectionPath(newPath); + tree.setAnchorSelectionPath(newPath); } else if (command.equals("selectPreviousExtendSelection") && hasPrev) { @@ -2660,12 +2682,12 @@ public class BasicTreeUI else if (command.equals("selectPrevious") && hasPrev) { newPath = treeState.getPathForRow(prevRow); - selectPath(tree, newPath); + tree.setSelectionPath(newPath); } else if (command.equals("selectNext") && hasNext) { newPath = treeState.getPathForRow(nextRow); - selectPath(tree, newPath); + tree.setSelectionPath(newPath); } else if (command.equals("selectNextExtendSelection") && hasNext) { @@ -2676,8 +2698,9 @@ public class BasicTreeUI else if (command.equals("selectNextChangeLead") && hasNext) { newPath = treeState.getPathForRow(nextRow); - selectPath(tree, newPath); + tree.setSelectionPath(newPath); tree.setLeadSelectionPath(newPath); + tree.setAnchorSelectionPath(newPath); } } @@ -3029,17 +3052,8 @@ public class BasicTreeUI { if (path != null) { - if (tree.getSelectionModel().getSelectionMode() == - TreeSelectionModel.SINGLE_TREE_SELECTION) - { - tree.setSelectionPath(path); - tree.setLeadSelectionPath(path); - } - else - { - tree.addSelectionPath(path); - tree.setLeadSelectionPath(path); - } + tree.setSelectionPath(path); + tree.setLeadSelectionPath(path); tree.makeVisible(path); tree.scrollPathToVisible(path); } |