jdk/src/java.desktop/share/classes/javax/swing/JTree.java
changeset 29250 f9edd0824de6
parent 28059 e576535359cc
child 31657 6b5f36a9a3c0
equal deleted inserted replaced
28999:86a1284f1128 29250:f9edd0824de6
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   870         if(treeModel != null) {
   870         if(treeModel != null) {
   871             if(treeModelListener == null)
   871             if(treeModelListener == null)
   872                 treeModelListener = createTreeModelListener();
   872                 treeModelListener = createTreeModelListener();
   873             if(treeModelListener != null)
   873             if(treeModelListener != null)
   874                 treeModel.addTreeModelListener(treeModelListener);
   874                 treeModel.addTreeModelListener(treeModelListener);
       
   875 
   875             // Mark the root as expanded, if it isn't a leaf.
   876             // Mark the root as expanded, if it isn't a leaf.
   876             if(treeModel.getRoot() != null &&
   877             Object treeRoot = treeModel.getRoot();
   877                !treeModel.isLeaf(treeModel.getRoot())) {
   878             if(treeRoot != null &&
   878                 expandedState.put(new TreePath(treeModel.getRoot()),
   879                !treeModel.isLeaf(treeRoot)) {
   879                                   Boolean.TRUE);
   880                 expandedState.put(new TreePath(treeRoot),
       
   881                                     Boolean.TRUE);
   880             }
   882             }
   881         }
   883         }
   882         firePropertyChange(TREE_MODEL_PROPERTY, oldModel, treeModel);
   884         firePropertyChange(TREE_MODEL_PROPERTY, oldModel, treeModel);
   883         invalidate();
   885         invalidate();
   884     }
   886     }
  2998 
  3000 
  2999     /**
  3001     /**
  3000      * Expands the root path, assuming the current TreeModel has been set.
  3002      * Expands the root path, assuming the current TreeModel has been set.
  3001      */
  3003      */
  3002     private void expandRoot() {
  3004     private void expandRoot() {
  3003         TreeModel              model = getModel();
  3005         TreeModel   model = getModel();
  3004 
       
  3005         if(model != null && model.getRoot() != null) {
  3006         if(model != null && model.getRoot() != null) {
  3006             expandPath(new TreePath(model.getRoot()));
  3007             expandPath(new TreePath(model.getRoot()));
  3007         }
  3008         }
  3008     }
  3009     }
  3009 
  3010 
  3269 
  3270 
  3270         if(model == null)
  3271         if(model == null)
  3271             return null;
  3272             return null;
  3272 
  3273 
  3273         int          count = indexs.length;
  3274         int          count = indexs.length;
       
  3275 
  3274         Object       parent = model.getRoot();
  3276         Object       parent = model.getRoot();
       
  3277         if (parent == null)
       
  3278             return null;
       
  3279 
  3275         TreePath     parentPath = new TreePath(parent);
  3280         TreePath     parentPath = new TreePath(parent);
  3276 
       
  3277         for(int counter = 0; counter < count; counter++) {
  3281         for(int counter = 0; counter < count; counter++) {
  3278             parent = model.getChild(parent, indexs[counter]);
  3282             parent = model.getChild(parent, indexs[counter]);
  3279             if(parent == null)
  3283             if(parent == null)
  3280                 return null;
  3284                 return null;
  3281             parentPath = parentPath.pathByAddingChild(parent);
  3285             parentPath = parentPath.pathByAddingChild(parent);
  3858                 return;
  3862                 return;
  3859 
  3863 
  3860             if (parent.getPathCount() == 1) {
  3864             if (parent.getPathCount() == 1) {
  3861                 // New root, remove everything!
  3865                 // New root, remove everything!
  3862                 clearToggledPaths();
  3866                 clearToggledPaths();
  3863                 if(treeModel.getRoot() != null &&
  3867 
  3864                    !treeModel.isLeaf(treeModel.getRoot())) {
  3868               Object treeRoot = treeModel.getRoot();
       
  3869 
       
  3870               if(treeRoot != null &&
       
  3871                 !treeModel.isLeaf(treeRoot)) {
  3865                     // Mark the root as expanded, if it isn't a leaf.
  3872                     // Mark the root as expanded, if it isn't a leaf.
  3866                     expandedState.put(parent, Boolean.TRUE);
  3873                     expandedState.put(parent, Boolean.TRUE);
  3867                 }
  3874                 }
  3868             }
  3875             }
  3869             else if(expandedState.get(parent) != null) {
  3876             else if(expandedState.get(parent) != null) {
  4349             // and then get the renderer component and return it
  4356             // and then get the renderer component and return it
  4350             TreeModel model = JTree.this.getModel();
  4357             TreeModel model = JTree.this.getModel();
  4351             if (model == null) {
  4358             if (model == null) {
  4352                 return null;
  4359                 return null;
  4353             }
  4360             }
  4354             TreePath path = new TreePath(model.getRoot());
  4361 
       
  4362             Object treeRoot = model.getRoot();
       
  4363             if (treeRoot == null) {
       
  4364                 return null;
       
  4365             }
       
  4366             TreePath path = new TreePath(treeRoot);
  4355             if (JTree.this.isVisible(path)) {
  4367             if (JTree.this.isVisible(path)) {
  4356                 TreeCellRenderer r = JTree.this.getCellRenderer();
  4368                 TreeCellRenderer r = JTree.this.getCellRenderer();
  4357                 TreeUI ui = JTree.this.getUI();
  4369                 TreeUI ui = JTree.this.getUI();
  4358                 if (ui != null) {
  4370                 if (ui != null) {
  4359                     int row = ui.getRowForPath(JTree.this, path);
  4371                     int row = ui.getRowForPath(JTree.this, path);
  4362                                        && (lsr == row);
  4374                                        && (lsr == row);
  4363                     boolean selected = JTree.this.isPathSelected(path);
  4375                     boolean selected = JTree.this.isPathSelected(path);
  4364                     boolean expanded = JTree.this.isExpanded(path);
  4376                     boolean expanded = JTree.this.isExpanded(path);
  4365 
  4377 
  4366                     return r.getTreeCellRendererComponent(JTree.this,
  4378                     return r.getTreeCellRendererComponent(JTree.this,
  4367                         model.getRoot(), selected, expanded,
  4379                         treeRoot, selected, expanded,
  4368                         model.isLeaf(model.getRoot()), row, hasFocus);
  4380                         model.isLeaf(treeRoot), row, hasFocus);
  4369                 }
  4381                 }
  4370             }
  4382             }
  4371             return null;
  4383             return null;
  4372         }
  4384         }
  4373 
  4385 
  4416             }
  4428             }
  4417             if (isRootVisible()) {
  4429             if (isRootVisible()) {
  4418                 return 1;    // the root node
  4430                 return 1;    // the root node
  4419             }
  4431             }
  4420 
  4432 
       
  4433             Object treeRoot = model.getRoot();
       
  4434             if (treeRoot == null)
       
  4435                 return 0;
       
  4436 
  4421             // return the root's first set of children count
  4437             // return the root's first set of children count
  4422             return model.getChildCount(model.getRoot());
  4438             return model.getChildCount(treeRoot);
  4423         }
  4439         }
  4424 
  4440 
  4425         /**
  4441         /**
  4426          * Return the nth Accessible child of the object.
  4442          * Return the nth Accessible child of the object.
  4427          *
  4443          *
  4431         public Accessible getAccessibleChild(int i) {
  4447         public Accessible getAccessibleChild(int i) {
  4432             TreeModel model = JTree.this.getModel();
  4448             TreeModel model = JTree.this.getModel();
  4433             if (model == null) {
  4449             if (model == null) {
  4434                 return null;
  4450                 return null;
  4435             }
  4451             }
       
  4452 
       
  4453             Object treeRoot = model.getRoot();
       
  4454             if (treeRoot == null) {
       
  4455                 return null;
       
  4456             }
       
  4457 
  4436             if (isRootVisible()) {
  4458             if (isRootVisible()) {
  4437                 if (i == 0) {    // return the root node Accessible
  4459                 if (i == 0) {    // return the root node Accessible
  4438                     Object[] objPath = { model.getRoot() };
  4460                     Object[] objPath = { treeRoot };
  4439                     TreePath path = new TreePath(objPath);
  4461                     TreePath path = new TreePath(objPath);
  4440                     return new AccessibleJTreeNode(JTree.this, path, JTree.this);
  4462                     return new AccessibleJTreeNode(JTree.this, path, JTree.this);
  4441                 } else {
  4463                 } else {
  4442                     return null;
  4464                     return null;
  4443                 }
  4465                 }
  4444             }
  4466             }
  4445 
  4467 
  4446             // return Accessible for one of root's child nodes
  4468             // return Accessible for one of root's child nodes
  4447             int count = model.getChildCount(model.getRoot());
  4469             int count = model.getChildCount(treeRoot);
  4448             if (i < 0 || i >= count) {
  4470             if (i < 0 || i >= count) {
  4449                 return null;
  4471                 return null;
  4450             }
  4472             }
  4451             Object obj = model.getChild(model.getRoot(), i);
  4473             Object obj = model.getChild(treeRoot, i);
  4452             Object[] objPath = { model.getRoot(), obj };
  4474             if (obj == null)
       
  4475                 return null;
       
  4476 
       
  4477             Object[] objPath = {treeRoot, obj };
       
  4478 
  4453             TreePath path = new TreePath(objPath);
  4479             TreePath path = new TreePath(objPath);
  4454             return new AccessibleJTreeNode(JTree.this, path, JTree.this);
  4480             return new AccessibleJTreeNode(JTree.this, path, JTree.this);
  4455         }
  4481         }
  4456 
  4482 
  4457         /**
  4483         /**
  4486          * @return the number of items currently selected.
  4512          * @return the number of items currently selected.
  4487          */
  4513          */
  4488         public int getAccessibleSelectionCount() {
  4514         public int getAccessibleSelectionCount() {
  4489             Object[] rootPath = new Object[1];
  4515             Object[] rootPath = new Object[1];
  4490             rootPath[0] = treeModel.getRoot();
  4516             rootPath[0] = treeModel.getRoot();
       
  4517             if (rootPath[0] == null)
       
  4518                 return 0;
       
  4519 
  4491             TreePath childPath = new TreePath(rootPath);
  4520             TreePath childPath = new TreePath(rootPath);
  4492             if (JTree.this.isPathSelected(childPath)) {
  4521             if (JTree.this.isPathSelected(childPath)) {
  4493                 return 1;
  4522                 return 1;
  4494             } else {
  4523             } else {
  4495                 return 0;
  4524                 return 0;
  4508         public Accessible getAccessibleSelection(int i) {
  4537         public Accessible getAccessibleSelection(int i) {
  4509             // The JTree can have only one accessible child, the root.
  4538             // The JTree can have only one accessible child, the root.
  4510             if (i == 0) {
  4539             if (i == 0) {
  4511                 Object[] rootPath = new Object[1];
  4540                 Object[] rootPath = new Object[1];
  4512                 rootPath[0] = treeModel.getRoot();
  4541                 rootPath[0] = treeModel.getRoot();
       
  4542                 if (rootPath[0] == null)
       
  4543                     return null;
       
  4544 
  4513                 TreePath childPath = new TreePath(rootPath);
  4545                 TreePath childPath = new TreePath(rootPath);
  4514                 if (JTree.this.isPathSelected(childPath)) {
  4546                 if (JTree.this.isPathSelected(childPath)) {
  4515                     return new AccessibleJTreeNode(JTree.this, childPath, JTree.this);
  4547                     return new AccessibleJTreeNode(JTree.this, childPath, JTree.this);
  4516                 }
  4548                 }
  4517             }
  4549             }
  4527         public boolean isAccessibleChildSelected(int i) {
  4559         public boolean isAccessibleChildSelected(int i) {
  4528             // The JTree can have only one accessible child, the root.
  4560             // The JTree can have only one accessible child, the root.
  4529             if (i == 0) {
  4561             if (i == 0) {
  4530                 Object[] rootPath = new Object[1];
  4562                 Object[] rootPath = new Object[1];
  4531                 rootPath[0] = treeModel.getRoot();
  4563                 rootPath[0] = treeModel.getRoot();
       
  4564                 if (rootPath[0] == null)
       
  4565                     return false;
       
  4566 
  4532                 TreePath childPath = new TreePath(rootPath);
  4567                 TreePath childPath = new TreePath(rootPath);
  4533                 return JTree.this.isPathSelected(childPath);
  4568                 return JTree.this.isPathSelected(childPath);
  4534             } else {
  4569             } else {
  4535                 return false;
  4570                 return false;
  4536             }
  4571             }
  4547          */
  4582          */
  4548         public void addAccessibleSelection(int i) {
  4583         public void addAccessibleSelection(int i) {
  4549            TreeModel model = JTree.this.getModel();
  4584            TreeModel model = JTree.this.getModel();
  4550            if (model != null) {
  4585            if (model != null) {
  4551                if (i == 0) {
  4586                if (i == 0) {
  4552                    Object[] objPath = {model.getRoot()};
  4587                     Object[] objPath = {model.getRoot()};
       
  4588                     if (objPath[0] == null)
       
  4589                         return;
       
  4590 
  4553                    TreePath path = new TreePath(objPath);
  4591                    TreePath path = new TreePath(objPath);
  4554                    JTree.this.addSelectionPath(path);
  4592                    JTree.this.addSelectionPath(path);
  4555                 }
  4593                 }
  4556             }
  4594             }
  4557         }
  4595         }
  4566         public void removeAccessibleSelection(int i) {
  4604         public void removeAccessibleSelection(int i) {
  4567             TreeModel model = JTree.this.getModel();
  4605             TreeModel model = JTree.this.getModel();
  4568             if (model != null) {
  4606             if (model != null) {
  4569                 if (i == 0) {
  4607                 if (i == 0) {
  4570                     Object[] objPath = {model.getRoot()};
  4608                     Object[] objPath = {model.getRoot()};
       
  4609                     if (objPath[0] == null)
       
  4610                         return;
       
  4611 
  4571                     TreePath path = new TreePath(objPath);
  4612                     TreePath path = new TreePath(objPath);
  4572                     JTree.this.removeSelectionPath(path);
  4613                     JTree.this.removeSelectionPath(path);
  4573                 }
  4614                 }
  4574             }
  4615             }
  4575         }
  4616         }
  4591          */
  4632          */
  4592         public void selectAllAccessibleSelection() {
  4633         public void selectAllAccessibleSelection() {
  4593             TreeModel model = JTree.this.getModel();
  4634             TreeModel model = JTree.this.getModel();
  4594             if (model != null) {
  4635             if (model != null) {
  4595                 Object[] objPath = {model.getRoot()};
  4636                 Object[] objPath = {model.getRoot()};
       
  4637                 if (objPath[0] == null)
       
  4638                     return;
       
  4639 
  4596                 TreePath path = new TreePath(objPath);
  4640                 TreePath path = new TreePath(objPath);
  4597                 JTree.this.addSelectionPath(path);
  4641                 JTree.this.addSelectionPath(path);
  4598             }
  4642             }
  4599         }
  4643         }
  4600 
  4644