001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: KTreeModelTreeTableAdapter.java,v $ 023 Revision 1.1 2004/05/31 07:30:26 markl 024 Final cleanup and bugfixes of kiwi.ui.model. 025 026 Revision 1.5 2004/03/10 00:58:43 markl 027 javadoc corrections 028 029 Revision 1.4 2003/01/19 09:33:06 markl 030 Javadoc & comment header updates. 031 032 Revision 1.3 2001/03/12 04:54:33 markl 033 Source code cleanup. 034 035 Revision 1.2 1999/01/10 03:12:19 markl 036 added GPL header & RCS tag 037 ---------------------------------------------------------------------------- 038*/ 039 040package kiwi.ui.model; 041 042import java.awt.*; 043import java.awt.event.*; 044import java.util.*; 045import javax.swing.*; 046import javax.swing.event.*; 047import javax.swing.table.*; 048import javax.swing.tree.*; 049 050import kiwi.event.*; 051 052/** An adapter that allows a Kiwi <code>KTreeTable</code> to be used 053 * with a <code>KTreeModel</code>. The adapter translates messages 054 * and events between the <code>KTreeTable</code> and the 055 * <code>KTreeModel</code> implementation. This class is for internal 056 * use, and does not normally need to be instantiated by application 057 * code. 058 * 059 * @see kiwi.ui.KTreeTable 060 * 061 * @author Mark Lindner 062 * @since Kiwi 2.0 063 */ 064 065public class KTreeModelTreeTableAdapter implements TableModel, 066 KTreeModelListener 067 { 068 private KTreeModel model = null; 069 private Vector listeners; 070 private JTree _tree; 071 private String columns[]; 072 073 /** Construct a new tree model table adapter. 074 * 075 */ 076 077 public KTreeModelTreeTableAdapter(JTree tree) 078 { 079 _tree = tree; 080 listeners = new Vector(); 081 082 tree.addTreeExpansionListener(new TreeExpansionListener() 083 { 084 public void treeExpanded(TreeExpansionEvent event) 085 { 086 fireTableDataChanged(); 087 } 088 089 public void treeCollapsed(TreeExpansionEvent event) 090 { 091 fireTableDataChanged(); 092 } 093 094 } 095 ); 096 } 097 098 /** Set the tree model to be used by this adapter. The adapter adds itself 099 * as a listener of the tree model. If a model was already set prior to this 100 * call, it is replaced, and the adapter removes itself as a listener from 101 * that model. 102 * 103 * @param model The model to set. 104 * @see #getTreeModel 105 */ 106 107 public void setTreeModel(KTreeModel model) 108 { 109 if(this.model != null) 110 this.model.removeTreeModelListener(this); 111 this.model = model; 112 _init(); 113 } 114 115 /** Get the tree model in use by this adapter. Returns the tree model 116 * currently associated with this adapter (may be <code>null</code> if no 117 * model has been set). 118 * 119 * @see #setTreeModel 120 */ 121 122 public KTreeModel getTreeModel() 123 { 124 return(model); 125 } 126 127 /* initialization code */ 128 129 private void _init() 130 { 131 if(model != null) 132 { 133 model.addTreeModelListener(this); 134 135 columns = (String [])model.getValueForProperty( 136 null, ModelProperties.COLUMN_NAMES_PROPERTY); 137 } 138 else 139 columns = null; 140 141 fireTableDataChanged(); 142 } 143 144 /** Add a table model listener. Adds a <code>TableModelListener</code> to 145 * this adapter's list of table model listeners. 146 * 147 * @param listener The listener to add. 148 * @see #removeTableModelListener 149 */ 150 151 public void addTableModelListener(TableModelListener listener) 152 { 153 listeners.addElement(listener); 154 } 155 156 /** Remove a table model listener. Removes a <code>TableModelListener</code> 157 * from this adapter's list of table model listeners. 158 * 159 * @param listener The listener to remove. 160 * @see #addTableModelListener 161 */ 162 163 public void removeTableModelListener(TableModelListener listener) 164 { 165 listeners.removeElement(listener); 166 } 167 168 /** Get the column count for this model. (Implementation of 169 * <code>TableModel</code>.) 170 */ 171 172 public int getColumnCount() 173 { 174 if(columns != null) 175 return(columns.length); 176 else 177 return(0); 178 } 179 180 /** Set the value at the given row and column in the table model. 181 * (Implementation of <code>TableModel</code>.) In this implementation, this 182 * method does nothing, since <code>TreeTable</code>s are currently not 183 * editable. 184 */ 185 186 public void setValueAt(Object obj, int row, int col) 187 { 188 // does nothing 189 } 190 191 /** Get the value at the given row and column in the table model. 192 * (Implementation of <code>TableModel</code>.) 193 * 194 * @param row The row. 195 * @param col The column. 196 */ 197 198 public Object getValueAt(int row, int col) 199 { 200 if(model == null) 201 return(null); 202 203 Object node = nodeForRow(row); 204 205 return(model.getValueForProperty(node, columns[col])); 206 } 207 208 209 /** Determine if a given cell is editable. In this implementation, the method 210 * always returns <code>false</code> because <code>TreeTable</code>s are 211 * currently not editable. 212 */ 213 214 public boolean isCellEditable(int row, int col) 215 { 216 return(col == 0); 217 } 218 219 /** Get the row count for this table model. (Implementation of 220 * <code>TableModel</code>.) 221 */ 222 223 public int getRowCount() 224 { 225 return(_tree.getRowCount()); 226 } 227 228 /** Get the class type for the given column. In this implementation, this 229 * method always returns <code>Object.class</code>. This may change in a 230 * future implementation to reflect the actual data type. 231 */ 232 233 public Class getColumnClass(int col) 234 { 235 return(Object.class); 236 } 237 238 /** Get the name of a given column. (Implementation of 239 * <code>TableModel</code>.) 240 * 241 * @param col The index of the column. 242 * 243 * @return The name of the column. 244 */ 245 246 public String getColumnName(int col) 247 { 248 if(columns == null) 249 return(null); 250 251 return(columns[col]); 252 } 253 254 /** Notify listeners that the table data has changed. 255 */ 256 257 protected void fireTableDataChanged() 258 { 259 TableModelEvent evt = null; 260 Enumeration e = listeners.elements(); 261 while(e.hasMoreElements()) 262 { 263 if(evt == null) 264 evt = new TableModelEvent(this /*, first, last, 265 TableModelEvent.ALL_COLUMNS, 266 TableModelEvent.DELETE */); 267 TableModelListener l = (TableModelListener)e.nextElement(); 268 l.tableChanged(evt); 269 } 270 } 271 272 /** Get the node at the specified row. 273 * 274 * @param row The row number 275 * @return The object at the specified row. 276 */ 277 278 protected Object nodeForRow(int row) 279 { 280 TreePath path = _tree.getPathForRow(row); 281 return(path.getLastPathComponent()); 282 } 283 284 /** Invoked after a node has been added to the model. */ 285 286 public void nodesAdded(KTreeModelEvent evt) 287 { 288 fireTableDataChanged(); 289 } 290 291 /** Invoked after a node has been removed from the model. */ 292 293 public void nodesRemoved(KTreeModelEvent evt) 294 { 295 fireTableDataChanged(); 296 } 297 298 /** Invoked when nodes are changed in the model. */ 299 300 public void nodesChanged(KTreeModelEvent evt) 301 { 302 fireTableDataChanged(); 303 } 304 305 /** Invoked when the structure of a portion of the model is changed. */ 306 307 public void structureChanged(KTreeModelEvent evt) 308 { 309 fireTableDataChanged(); 310 } 311 312 /** Invoked when the structure of the entire model has changed. */ 313 314 public void dataChanged(KTreeModelEvent evt) 315 { 316 fireTableDataChanged(); 317 } 318 319 /** Dispose of the adapter. Causes the adapter to detach its listeners from 320 * its associated <code>JTree</code> component, and then null out its 321 * references to the <code>JTree</code> and to the associated 322 * <code>KTreeModel</code>. 323 */ 324 325 public void dispose() 326 { 327 if(model != null) 328 model.removeTreeModelListener(this); 329 330 model = null; 331 _tree = null; 332 } 333 334 } 335 336/* end of source file */