1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
2899 */
2900 public class SelectionModelPropertyChangeHandler implements
2901 PropertyChangeListener {
2902
2903 // NOTE: This class exists only for backward compatibility. All
2904 // its functionality has been moved into Handler. If you need to add
2905 // new functionality add it to the Handler, but make sure this
2906 // class calls into the Handler.
2907
2908 public void propertyChange(PropertyChangeEvent event) {
2909 getHandler().propertyChange(event);
2910 }
2911 } // End of BasicTreeUI.SelectionModelPropertyChangeHandler
2912
2913
2914 /**
2915 * <code>TreeTraverseAction</code> is the action used for left/right keys.
2916 * Will toggle the expandedness of a node, as well as potentially
2917 * incrementing the selection.
2918 */
2919 public class TreeTraverseAction extends AbstractAction {
2920 /** Determines direction to traverse, 1 means expand, -1 means
2921 * collapse. */
2922 protected int direction;
2923 /** True if the selection is reset, false means only the lead path
2924 * changes. */
2925 private boolean changeSelection;
2926
2927 public TreeTraverseAction(int direction, String name) {
2928 this(direction, name, true);
2929 }
2930
2931 private TreeTraverseAction(int direction, String name,
2932 boolean changeSelection) {
2933 this.direction = direction;
2934 this.changeSelection = changeSelection;
2935 }
2936
2937 public void actionPerformed(ActionEvent e) {
2938 if (tree != null) {
2939 SHARED_ACTION.traverse(tree, BasicTreeUI.this, direction,
2940 changeSelection);
2941 }
2942 }
2943
2944 public boolean isEnabled() { return (tree != null &&
2945 tree.isEnabled()); }
2946 } // BasicTreeUI.TreeTraverseAction
2947
2948
2949 /** TreePageAction handles page up and page down events.
2950 */
2951 public class TreePageAction extends AbstractAction {
2952 /** Specifies the direction to adjust the selection by. */
2953 protected int direction;
2954 /** True indicates should set selection from anchor path. */
2955 private boolean addToSelection;
2956 private boolean changeSelection;
2957
2958 public TreePageAction(int direction, String name) {
2959 this(direction, name, false, true);
2960 }
2961
2962 private TreePageAction(int direction, String name,
2963 boolean addToSelection,
2964 boolean changeSelection) {
2965 this.direction = direction;
2966 this.addToSelection = addToSelection;
2967 this.changeSelection = changeSelection;
2968 }
2969
2970 public void actionPerformed(ActionEvent e) {
2971 if (tree != null) {
2972 SHARED_ACTION.page(tree, BasicTreeUI.this, direction,
2973 addToSelection, changeSelection);
2974 }
2975 }
2976
2977 public boolean isEnabled() { return (tree != null &&
2978 tree.isEnabled()); }
2979
2980 } // BasicTreeUI.TreePageAction
2981
2982
2983 /** TreeIncrementAction is used to handle up/down actions. Selection
2984 * is moved up or down based on direction.
2985 */
2986 public class TreeIncrementAction extends AbstractAction {
2987 /** Specifies the direction to adjust the selection by. */
2988 protected int direction;
2989 /** If true the new item is added to the selection, if false the
2990 * selection is reset. */
2991 private boolean addToSelection;
2992 private boolean changeSelection;
2993
2994 public TreeIncrementAction(int direction, String name) {
2995 this(direction, name, false, true);
2996 }
2997
2998 private TreeIncrementAction(int direction, String name,
2999 boolean addToSelection,
3000 boolean changeSelection) {
3001 this.direction = direction;
3002 this.addToSelection = addToSelection;
3003 this.changeSelection = changeSelection;
3004 }
3005
3006 public void actionPerformed(ActionEvent e) {
3007 if (tree != null) {
3008 SHARED_ACTION.increment(tree, BasicTreeUI.this, direction,
3009 addToSelection, changeSelection);
3010 }
3011 }
3012
3013 public boolean isEnabled() { return (tree != null &&
3014 tree.isEnabled()); }
3015
3016 } // End of class BasicTreeUI.TreeIncrementAction
3017
3018 /**
3019 * TreeHomeAction is used to handle end/home actions.
3020 * Scrolls either the first or last cell to be visible based on
3021 * direction.
3022 */
3023 public class TreeHomeAction extends AbstractAction {
3024 protected int direction;
3025 /** Set to true if append to selection. */
3026 private boolean addToSelection;
3027 private boolean changeSelection;
3028
3029 public TreeHomeAction(int direction, String name) {
3030 this(direction, name, false, true);
3031 }
3032
3033 private TreeHomeAction(int direction, String name,
3034 boolean addToSelection,
3035 boolean changeSelection) {
3036 this.direction = direction;
3037 this.changeSelection = changeSelection;
3038 this.addToSelection = addToSelection;
3039 }
3040
3041 public void actionPerformed(ActionEvent e) {
3042 if (tree != null) {
3043 SHARED_ACTION.home(tree, BasicTreeUI.this, direction,
3044 addToSelection, changeSelection);
3045 }
3046 }
3047
3048 public boolean isEnabled() { return (tree != null &&
3049 tree.isEnabled()); }
3050
3051 } // End of class BasicTreeUI.TreeHomeAction
3052
3053
3054 /**
3055 * For the first selected row expandedness will be toggled.
3056 */
3057 public class TreeToggleAction extends AbstractAction {
3058 public TreeToggleAction(String name) {
3059 }
3060
3061 public void actionPerformed(ActionEvent e) {
3062 if(tree != null) {
3063 SHARED_ACTION.toggle(tree, BasicTreeUI.this);
3064 }
3065 }
3066
3067 public boolean isEnabled() { return (tree != null &&
3068 tree.isEnabled()); }
3069
3070 } // End of class BasicTreeUI.TreeToggleAction
3071
3072
3073 /**
3074 * ActionListener that invokes cancelEditing when action performed.
3075 */
3076 public class TreeCancelEditingAction extends AbstractAction {
3077 public TreeCancelEditingAction(String name) {
3078 }
3079
3080 public void actionPerformed(ActionEvent e) {
3081 if(tree != null) {
3082 SHARED_ACTION.cancelEditing(tree, BasicTreeUI.this);
3083 }
3084 }
3085
3086 public boolean isEnabled() { return (tree != null &&
3087 tree.isEnabled() &&
3088 isEditing(tree)); }
3089 } // End of class BasicTreeUI.TreeCancelEditingAction
3090
3091
3092 /**
3093 * MouseInputHandler handles passing all mouse events,
3094 * including mouse motion events, until the mouse is released to
3095 * the destination it is constructed with. It is assumed all the
3168 removeFromSource();
3169 }
3170
3171 protected void removeFromSource() {
3172 if(source != null) {
3173 source.removeMouseListener(this);
3174 source.removeMouseMotionListener(this);
3175 if (focusComponent != null &&
3176 focusComponent == destination && !dispatchedEvent &&
3177 (focusComponent instanceof JTextField)) {
3178 ((JTextField)focusComponent).selectAll();
3179 }
3180 }
3181 source = destination = null;
3182 }
3183
3184 } // End of class BasicTreeUI.MouseInputHandler
3185
3186 private static final TransferHandler defaultTransferHandler = new TreeTransferHandler();
3187
3188 static class TreeTransferHandler extends TransferHandler implements UIResource, Comparator<TreePath> {
3189
3190 private JTree tree;
3191
3192 /**
3193 * Create a Transferable to use as the source for a data transfer.
3194 *
3195 * @param c The component holding the data to be transfered. This
3196 * argument is provided to enable sharing of TransferHandlers by
3197 * multiple components.
3198 * @return The representation of the data to be transfered.
3199 *
3200 */
3201 protected Transferable createTransferable(JComponent c) {
3202 if (c instanceof JTree) {
3203 tree = (JTree) c;
3204 TreePath[] paths = tree.getSelectionPaths();
3205
3206 if (paths == null || paths.length == 0) {
3207 return null;
|
1 /*
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
2899 */
2900 public class SelectionModelPropertyChangeHandler implements
2901 PropertyChangeListener {
2902
2903 // NOTE: This class exists only for backward compatibility. All
2904 // its functionality has been moved into Handler. If you need to add
2905 // new functionality add it to the Handler, but make sure this
2906 // class calls into the Handler.
2907
2908 public void propertyChange(PropertyChangeEvent event) {
2909 getHandler().propertyChange(event);
2910 }
2911 } // End of BasicTreeUI.SelectionModelPropertyChangeHandler
2912
2913
2914 /**
2915 * <code>TreeTraverseAction</code> is the action used for left/right keys.
2916 * Will toggle the expandedness of a node, as well as potentially
2917 * incrementing the selection.
2918 */
2919 @SuppressWarnings("serial") // Superclass is not serializable across versions
2920 public class TreeTraverseAction extends AbstractAction {
2921 /** Determines direction to traverse, 1 means expand, -1 means
2922 * collapse. */
2923 protected int direction;
2924 /** True if the selection is reset, false means only the lead path
2925 * changes. */
2926 private boolean changeSelection;
2927
2928 public TreeTraverseAction(int direction, String name) {
2929 this(direction, name, true);
2930 }
2931
2932 private TreeTraverseAction(int direction, String name,
2933 boolean changeSelection) {
2934 this.direction = direction;
2935 this.changeSelection = changeSelection;
2936 }
2937
2938 public void actionPerformed(ActionEvent e) {
2939 if (tree != null) {
2940 SHARED_ACTION.traverse(tree, BasicTreeUI.this, direction,
2941 changeSelection);
2942 }
2943 }
2944
2945 public boolean isEnabled() { return (tree != null &&
2946 tree.isEnabled()); }
2947 } // BasicTreeUI.TreeTraverseAction
2948
2949
2950 /** TreePageAction handles page up and page down events.
2951 */
2952 @SuppressWarnings("serial") // Superclass is not serializable across versions
2953 public class TreePageAction extends AbstractAction {
2954 /** Specifies the direction to adjust the selection by. */
2955 protected int direction;
2956 /** True indicates should set selection from anchor path. */
2957 private boolean addToSelection;
2958 private boolean changeSelection;
2959
2960 public TreePageAction(int direction, String name) {
2961 this(direction, name, false, true);
2962 }
2963
2964 private TreePageAction(int direction, String name,
2965 boolean addToSelection,
2966 boolean changeSelection) {
2967 this.direction = direction;
2968 this.addToSelection = addToSelection;
2969 this.changeSelection = changeSelection;
2970 }
2971
2972 public void actionPerformed(ActionEvent e) {
2973 if (tree != null) {
2974 SHARED_ACTION.page(tree, BasicTreeUI.this, direction,
2975 addToSelection, changeSelection);
2976 }
2977 }
2978
2979 public boolean isEnabled() { return (tree != null &&
2980 tree.isEnabled()); }
2981
2982 } // BasicTreeUI.TreePageAction
2983
2984
2985 /** TreeIncrementAction is used to handle up/down actions. Selection
2986 * is moved up or down based on direction.
2987 */
2988 @SuppressWarnings("serial") // Superclass is not serializable across versions
2989 public class TreeIncrementAction extends AbstractAction {
2990 /** Specifies the direction to adjust the selection by. */
2991 protected int direction;
2992 /** If true the new item is added to the selection, if false the
2993 * selection is reset. */
2994 private boolean addToSelection;
2995 private boolean changeSelection;
2996
2997 public TreeIncrementAction(int direction, String name) {
2998 this(direction, name, false, true);
2999 }
3000
3001 private TreeIncrementAction(int direction, String name,
3002 boolean addToSelection,
3003 boolean changeSelection) {
3004 this.direction = direction;
3005 this.addToSelection = addToSelection;
3006 this.changeSelection = changeSelection;
3007 }
3008
3009 public void actionPerformed(ActionEvent e) {
3010 if (tree != null) {
3011 SHARED_ACTION.increment(tree, BasicTreeUI.this, direction,
3012 addToSelection, changeSelection);
3013 }
3014 }
3015
3016 public boolean isEnabled() { return (tree != null &&
3017 tree.isEnabled()); }
3018
3019 } // End of class BasicTreeUI.TreeIncrementAction
3020
3021 /**
3022 * TreeHomeAction is used to handle end/home actions.
3023 * Scrolls either the first or last cell to be visible based on
3024 * direction.
3025 */
3026 @SuppressWarnings("serial") // Superclass is not serializable across versions
3027 public class TreeHomeAction extends AbstractAction {
3028 protected int direction;
3029 /** Set to true if append to selection. */
3030 private boolean addToSelection;
3031 private boolean changeSelection;
3032
3033 public TreeHomeAction(int direction, String name) {
3034 this(direction, name, false, true);
3035 }
3036
3037 private TreeHomeAction(int direction, String name,
3038 boolean addToSelection,
3039 boolean changeSelection) {
3040 this.direction = direction;
3041 this.changeSelection = changeSelection;
3042 this.addToSelection = addToSelection;
3043 }
3044
3045 public void actionPerformed(ActionEvent e) {
3046 if (tree != null) {
3047 SHARED_ACTION.home(tree, BasicTreeUI.this, direction,
3048 addToSelection, changeSelection);
3049 }
3050 }
3051
3052 public boolean isEnabled() { return (tree != null &&
3053 tree.isEnabled()); }
3054
3055 } // End of class BasicTreeUI.TreeHomeAction
3056
3057
3058 /**
3059 * For the first selected row expandedness will be toggled.
3060 */
3061 @SuppressWarnings("serial") // Superclass is not serializable across versions
3062 public class TreeToggleAction extends AbstractAction {
3063 public TreeToggleAction(String name) {
3064 }
3065
3066 public void actionPerformed(ActionEvent e) {
3067 if(tree != null) {
3068 SHARED_ACTION.toggle(tree, BasicTreeUI.this);
3069 }
3070 }
3071
3072 public boolean isEnabled() { return (tree != null &&
3073 tree.isEnabled()); }
3074
3075 } // End of class BasicTreeUI.TreeToggleAction
3076
3077
3078 /**
3079 * ActionListener that invokes cancelEditing when action performed.
3080 */
3081 @SuppressWarnings("serial") // Superclass is not serializable across versions
3082 public class TreeCancelEditingAction extends AbstractAction {
3083 public TreeCancelEditingAction(String name) {
3084 }
3085
3086 public void actionPerformed(ActionEvent e) {
3087 if(tree != null) {
3088 SHARED_ACTION.cancelEditing(tree, BasicTreeUI.this);
3089 }
3090 }
3091
3092 public boolean isEnabled() { return (tree != null &&
3093 tree.isEnabled() &&
3094 isEditing(tree)); }
3095 } // End of class BasicTreeUI.TreeCancelEditingAction
3096
3097
3098 /**
3099 * MouseInputHandler handles passing all mouse events,
3100 * including mouse motion events, until the mouse is released to
3101 * the destination it is constructed with. It is assumed all the
3174 removeFromSource();
3175 }
3176
3177 protected void removeFromSource() {
3178 if(source != null) {
3179 source.removeMouseListener(this);
3180 source.removeMouseMotionListener(this);
3181 if (focusComponent != null &&
3182 focusComponent == destination && !dispatchedEvent &&
3183 (focusComponent instanceof JTextField)) {
3184 ((JTextField)focusComponent).selectAll();
3185 }
3186 }
3187 source = destination = null;
3188 }
3189
3190 } // End of class BasicTreeUI.MouseInputHandler
3191
3192 private static final TransferHandler defaultTransferHandler = new TreeTransferHandler();
3193
3194 @SuppressWarnings("serial") // JDK-implementation class
3195 static class TreeTransferHandler extends TransferHandler implements UIResource, Comparator<TreePath> {
3196
3197 private JTree tree;
3198
3199 /**
3200 * Create a Transferable to use as the source for a data transfer.
3201 *
3202 * @param c The component holding the data to be transfered. This
3203 * argument is provided to enable sharing of TransferHandlers by
3204 * multiple components.
3205 * @return The representation of the data to be transfered.
3206 *
3207 */
3208 protected Transferable createTransferable(JComponent c) {
3209 if (c instanceof JTree) {
3210 tree = (JTree) c;
3211 TreePath[] paths = tree.getSelectionPaths();
3212
3213 if (paths == null || paths.length == 0) {
3214 return null;
|