src/share/classes/javax/swing/plaf/basic/BasicTreeUI.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2011, 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
  23  * questions.
  24  */
  25 
  26 package javax.swing.plaf.basic;
  27 
  28 import javax.swing.*;
  29 import javax.swing.event.*;

  30 import java.awt.*;
  31 import java.awt.event.*;
  32 import java.awt.datatransfer.*;
  33 import java.beans.*;
  34 import java.util.Enumeration;
  35 import java.util.Hashtable;
  36 import java.util.ArrayList;
  37 import java.util.Collections;
  38 import java.util.Comparator;
  39 import javax.swing.plaf.ComponentUI;
  40 import javax.swing.plaf.UIResource;
  41 import javax.swing.plaf.TreeUI;
  42 import javax.swing.tree.*;
  43 import javax.swing.text.Position;
  44 import javax.swing.plaf.basic.DragRecognitionSupport.BeforeDrag;

  45 import sun.swing.SwingUtilities2;
  46 
  47 import sun.swing.DefaultLookup;
  48 import sun.swing.UIAction;
  49 
  50 /**
  51  * The basic L&F for a hierarchical data structure.
  52  * <p>
  53  *
  54  * @author Scott Violet
  55  * @author Shannon Hickey (drag and drop)
  56  */
  57 
  58 public class BasicTreeUI extends TreeUI
  59 {
  60     private static final StringBuilder BASELINE_COMPONENT_KEY =
  61         new StringBuilder("Tree.baselineComponent");
  62 
  63     // Old actions forward to an instance of this.
  64     static private final Actions SHARED_ACTION = new Actions();


2148                    editorSize.height != nodeBounds.height) {
2149                     // Editor wants different width or height, invalidate
2150                     // treeState and relayout.
2151                     editorHasDifferentSize = true;
2152                     treeState.invalidatePathBounds(path);
2153                     updateSize();
2154                     // To make sure x/y are updated correctly, fetch
2155                     // the bounds again.
2156                     nodeBounds = getPathBounds(tree, path);
2157                     if (nodeBounds == null) {
2158                         return false;
2159                     }
2160                 }
2161                 else
2162                     editorHasDifferentSize = false;
2163                 tree.add(editingComponent);
2164                 editingComponent.setBounds(nodeBounds.x, nodeBounds.y,
2165                                            nodeBounds.width,
2166                                            nodeBounds.height);
2167                 editingPath = path;


2168                 if (editingComponent instanceof JComponent) {
2169                     ((JComponent)editingComponent).revalidate();




2170                 } else {
2171                     editingComponent.validate();
2172                 }
2173                 editingComponent.repaint();
2174                 if(cellEditor.shouldSelectCell(event)) {
2175                     stopEditingInCompleteEditing = false;
2176                     tree.setSelectionRow(row);
2177                     stopEditingInCompleteEditing = true;
2178                 }
2179 
2180                 Component focusedComponent = SwingUtilities2.
2181                                  compositeRequestFocus(editingComponent);
2182                 boolean selectAll = true;
2183 
2184                 if(event != null) {
2185                     /* Find the component that will get forwarded all the
2186                        mouse events until mouseReleased. */
2187                     Point          componentPoint = SwingUtilities.convertPoint
2188                         (tree, new Point(event.getX(), event.getY()),
2189                          editingComponent);


2200                         MouseInputHandler handler =
2201                             new MouseInputHandler(tree, activeComponent,
2202                                                   event, focusedComponent);
2203 
2204                         if (releaseEvent != null) {
2205                             handler.mouseReleased(releaseEvent);
2206                         }
2207 
2208                         selectAll = false;
2209                     }
2210                 }
2211                 if (selectAll && focusedComponent instanceof JTextField) {
2212                     ((JTextField)focusedComponent).selectAll();
2213                 }
2214                 return true;
2215             }
2216             else
2217                 editingComponent = null;
2218         }
2219         return false;































2220     }
2221 
2222     //
2223     // Following are primarily for handling mouse events.
2224     //
2225 
2226     /**
2227      * If the <code>mouseX</code> and <code>mouseY</code> are in the
2228      * expand/collapse region of the <code>row</code>, this will toggle
2229      * the row.
2230      */
2231     protected void checkForClickInExpandControl(TreePath path,
2232                                                 int mouseX, int mouseY) {
2233       if (isLocationInExpandControl(path, mouseX, mouseY)) {
2234           handleExpandControlClick(path, mouseX, mouseY);
2235         }
2236     }
2237 
2238     /**
2239      * Returns true if <code>mouseX</code> and <code>mouseY</code> fall


   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
  23  * questions.
  24  */
  25 
  26 package javax.swing.plaf.basic;
  27 
  28 import javax.swing.*;
  29 import javax.swing.event.*;
  30 import java.applet.Applet;
  31 import java.awt.*;
  32 import java.awt.event.*;
  33 import java.awt.datatransfer.*;
  34 import java.beans.*;
  35 import java.util.Enumeration;
  36 import java.util.Hashtable;
  37 import java.util.ArrayList;
  38 import java.util.Collections;
  39 import java.util.Comparator;
  40 import javax.swing.plaf.ComponentUI;
  41 import javax.swing.plaf.UIResource;
  42 import javax.swing.plaf.TreeUI;
  43 import javax.swing.tree.*;
  44 import javax.swing.text.Position;
  45 import javax.swing.plaf.basic.DragRecognitionSupport.BeforeDrag;
  46 import sun.awt.AWTAccessor;
  47 import sun.swing.SwingUtilities2;
  48 
  49 import sun.swing.DefaultLookup;
  50 import sun.swing.UIAction;
  51 
  52 /**
  53  * The basic L&F for a hierarchical data structure.
  54  * <p>
  55  *
  56  * @author Scott Violet
  57  * @author Shannon Hickey (drag and drop)
  58  */
  59 
  60 public class BasicTreeUI extends TreeUI
  61 {
  62     private static final StringBuilder BASELINE_COMPONENT_KEY =
  63         new StringBuilder("Tree.baselineComponent");
  64 
  65     // Old actions forward to an instance of this.
  66     static private final Actions SHARED_ACTION = new Actions();


2150                    editorSize.height != nodeBounds.height) {
2151                     // Editor wants different width or height, invalidate
2152                     // treeState and relayout.
2153                     editorHasDifferentSize = true;
2154                     treeState.invalidatePathBounds(path);
2155                     updateSize();
2156                     // To make sure x/y are updated correctly, fetch
2157                     // the bounds again.
2158                     nodeBounds = getPathBounds(tree, path);
2159                     if (nodeBounds == null) {
2160                         return false;
2161                     }
2162                 }
2163                 else
2164                     editorHasDifferentSize = false;
2165                 tree.add(editingComponent);
2166                 editingComponent.setBounds(nodeBounds.x, nodeBounds.y,
2167                                            nodeBounds.width,
2168                                            nodeBounds.height);
2169                 editingPath = path;
2170 
2171                 Container validateRoot = null;
2172                 if (editingComponent instanceof JComponent) {
2173                     validateRoot = getValidateRoot((Container)editingComponent, true);
2174                 }
2175 
2176                 if (validateRoot != null) {
2177                     AWTAccessor.getContainerAccessor().validateUnconditionally(validateRoot);
2178                 } else {
2179                     editingComponent.validate();
2180                 }
2181                 editingComponent.repaint();
2182                 if(cellEditor.shouldSelectCell(event)) {
2183                     stopEditingInCompleteEditing = false;
2184                     tree.setSelectionRow(row);
2185                     stopEditingInCompleteEditing = true;
2186                 }
2187 
2188                 Component focusedComponent = SwingUtilities2.
2189                                  compositeRequestFocus(editingComponent);
2190                 boolean selectAll = true;
2191 
2192                 if(event != null) {
2193                     /* Find the component that will get forwarded all the
2194                        mouse events until mouseReleased. */
2195                     Point          componentPoint = SwingUtilities.convertPoint
2196                         (tree, new Point(event.getX(), event.getY()),
2197                          editingComponent);


2208                         MouseInputHandler handler =
2209                             new MouseInputHandler(tree, activeComponent,
2210                                                   event, focusedComponent);
2211 
2212                         if (releaseEvent != null) {
2213                             handler.mouseReleased(releaseEvent);
2214                         }
2215 
2216                         selectAll = false;
2217                     }
2218                 }
2219                 if (selectAll && focusedComponent instanceof JTextField) {
2220                     ((JTextField)focusedComponent).selectAll();
2221                 }
2222                 return true;
2223             }
2224             else
2225                 editingComponent = null;
2226         }
2227         return false;
2228     }
2229 
2230     // The implementation of the method is copied from SwingUtilities
2231     private Container getValidateRoot(Container c, boolean visibleOnly) {
2232         Container root = null;
2233 
2234         for (; c != null; c = c.getParent())
2235         {
2236             if (!c.isDisplayable() || c instanceof CellRendererPane) {
2237                 return null;
2238             }
2239             if (c.isValidateRoot()) {
2240                 root = c;
2241                 break;
2242             }
2243         }
2244 
2245         if (root == null) {
2246             return null;
2247         }
2248 
2249         for (; c != null; c = c.getParent()) {
2250             if (!c.isDisplayable() || (visibleOnly && !c.isVisible())) {
2251                 return null;
2252             }
2253             if (c instanceof Window || c instanceof Applet) {
2254                 return root;
2255             }
2256         }
2257 
2258         return null;
2259     }
2260 
2261     //
2262     // Following are primarily for handling mouse events.
2263     //
2264 
2265     /**
2266      * If the <code>mouseX</code> and <code>mouseY</code> are in the
2267      * expand/collapse region of the <code>row</code>, this will toggle
2268      * the row.
2269      */
2270     protected void checkForClickInExpandControl(TreePath path,
2271                                                 int mouseX, int mouseY) {
2272       if (isLocationInExpandControl(path, mouseX, mouseY)) {
2273           handleExpandControlClick(path, mouseX, mouseY);
2274         }
2275     }
2276 
2277     /**
2278      * Returns true if <code>mouseX</code> and <code>mouseY</code> fall