00001 /* 00002 * Name: TTreeSelectionModel.java 00003 * Author: Philip Milne 00004 * Scott Violet 00005 * Maintainer: Rafael Jesus Alcantara Perez 00006 * Summary: Tree selection model. 00007 * Date: $Date: 2003/09/28 23:22:05 $ 00008 * Revision: $Revision: 1.1 $ 00009 * 00010 * Copyright (C) 2003 Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com> 00011 * 00012 * This program is free software; you can redistribute it and/or modify 00013 * it under the terms of the GNU General Public License as published by 00014 * the Free Software Foundation; either version 2 of the License, or 00015 * (at your option) any later version. 00016 * 00017 * This program is distributed in the hope that it will be useful, 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 * GNU General Public License for more details. 00021 * 00022 * You should have received a copy of the GNU General Public License 00023 * along with this program; if not, write to the Free Software 00024 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 00025 * MA 02111-1307, USA. 00026 * 00027 * 00028 * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved. 00029 * 00030 * Redistribution and use in source and binary forms, with or 00031 * without modification, are permitted provided that the following 00032 * conditions are met: 00033 * 00034 * - Redistributions of source code must retain the above copyright 00035 * notice, this list of conditions and the following disclaimer. 00036 * 00037 * - Redistribution in binary form must reproduce the above 00038 * copyright notice, this list of conditions and the following 00039 * disclaimer in the documentation and/or other materials 00040 * provided with the distribution. 00041 * 00042 * Neither the name of Sun Microsystems, Inc. or the names of 00043 * contributors may be used to endorse or promote products derived 00044 * from this software without specific prior written permission. 00045 * 00046 * This software is provided "AS IS," without a warranty of any 00047 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND 00048 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 00049 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY 00050 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY 00051 * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR 00052 * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR 00053 * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE 00054 * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, 00055 * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER 00056 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF 00057 * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS 00058 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 00059 * 00060 * You acknowledge that this software is not designed, licensed or 00061 * intended for use in the design, construction, operation or 00062 * maintenance of any nuclear facility. 00063 */ 00064 00066 package org.mpcl.nui.treetable; 00067 00068 import javax.swing.ListSelectionModel; 00069 import javax.swing.event.ListSelectionEvent; 00070 import javax.swing.event.ListSelectionListener; 00071 import javax.swing.tree.DefaultTreeSelectionModel; 00072 import javax.swing.tree.TreePath; 00073 00074 00081 public class TTreeSelectionModel extends DefaultTreeSelectionModel 00082 { 00083 00088 protected class TListSelectionListener implements ListSelectionListener 00089 { 00095 public void valueChanged (ListSelectionEvent tLIST_SELECTION_EVENT) 00096 { 00097 updateSelectedPathsFromSelectedRows(); 00098 } 00099 } 00100 00102 protected boolean gIsUpdatingListSelectionModel; 00103 00105 protected TTree tTree; 00106 00107 00108 // 00109 // C O N S T R U C T O R S 00110 // 00111 00113 public TTreeSelectionModel (TTree tTREE) 00114 { 00115 super(); 00116 tTree = tTREE; 00117 getListSelectionModel().addListSelectionListener (createListSelectionListener()); 00118 } 00119 00125 public void resetRowSelection() 00126 { 00127 00128 // 00129 // Notice how it does not message super if gIsUpdatingListSelectionModel 00130 // is true. If gIsUpdatingListSelectionModel is true, it implies the 00131 // ListSelectionModel has already been updated and the paths are the only 00132 // thing that needs to be updated. 00133 // 00134 if ( !gIsUpdatingListSelectionModel ) 00135 { 00136 gIsUpdatingListSelectionModel = true; 00137 try 00138 { 00139 super.resetRowSelection(); 00140 } 00141 finally 00142 { 00143 gIsUpdatingListSelectionModel = false; 00144 } 00145 } 00146 00147 } // resetRowSelection() 00148 00153 protected void updateSelectedPathsFromSelectedRows() 00154 { 00155 00156 if ( !gIsUpdatingListSelectionModel ) 00157 { 00158 gIsUpdatingListSelectionModel = true; 00159 try 00160 { 00161 // 00162 // This is way expensive, ListSelectionModel needs an enumerator 00163 // for iterating. 00164 // 00165 00166 TreePath tSelectedTreePath; 00167 int iMax = listSelectionModel.getMaxSelectionIndex(); 00168 int iMin = listSelectionModel.getMinSelectionIndex(); 00169 00170 clearSelection(); 00171 if ( ( iMin != -1 ) && ( iMax != -1 ) ) 00172 { 00173 for (int I = iMin; ( I <= iMax ) ;++I) 00174 { 00175 if ( listSelectionModel.isSelectedIndex (I) ) 00176 { 00177 tSelectedTreePath = tTree.getPathForRow (I); 00178 if ( tSelectedTreePath != null ) 00179 { 00180 addSelectionPath (tSelectedTreePath); 00181 } 00182 } 00183 } 00184 } 00185 } 00186 finally 00187 { 00188 gIsUpdatingListSelectionModel = false; 00189 } 00190 } 00191 00192 } // updateSelectedPathsFromSelectedRows() 00193 00194 00195 // 00196 // S E L E C T O R S 00197 // 00198 00203 protected ListSelectionListener createListSelectionListener() 00204 { 00205 return new TListSelectionListener(); 00206 } 00207 00208 00209 // 00210 // S E L E C T O R S 00211 // 00212 00219 public ListSelectionModel getListSelectionModel() 00220 { 00221 return listSelectionModel; 00222 } 00223 00224 } // class TTreeSelectionModel