1 /* 2 * UnBBayes 3 * Copyright (C) 2002, 2008 Universidade de Brasilia - http://www.unb.br 4 * 5 * This file is part of UnBBayes. 6 * 7 * UnBBayes is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * UnBBayes is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with UnBBayes. If not, see <http://www.gnu.org/licenses/>. 19 * 20 */ 21 package unbbayes.gui.table; 22 23 import java.util.ArrayList; 24 import java.util.Iterator; 25 import java.util.Vector; 26 27 import javax.swing.table.DefaultTableColumnModel; 28 import javax.swing.table.TableColumn; 29 30 // Steve Webb 16/09/04 swebb99_uk@hotmail.com 31 32 /** 33 * Class which extends the functionality of DefaultColumnTableModel to 34 * also provide capabilities to group columns. This can be used for 35 * instance to aid in the layout of groupable table headers. 36 */ 37 public class GroupableTableColumnModel extends DefaultTableColumnModel { 38 39 /** 40 * Hold the list of ColumnGroups which define what group each normal 41 * column is within, if any. 42 */ 43 protected ArrayList columnGroups = new ArrayList(); 44 45 46 /** 47 * Add a new columngroup. 48 * @param columnGroup new ColumnGroup 49 */ 50 public void addColumnGroup(ColumnGroup columnGroup) { 51 columnGroups.add(columnGroup); 52 } 53 54 /** 55 * Provides an Iterator to iterate over the 56 * ColumnGroup list. 57 * @return Iterator over ColumnGroups 58 */ 59 public Iterator columnGroupIterator() { 60 return columnGroups.iterator(); 61 } 62 63 /** 64 * Returns a ColumnGroup specified by an index. 65 * @param index index of ColumnGroup 66 * @return ColumnGroup 67 */ 68 public ColumnGroup getColumnGroup(int index) { 69 if(index >= 0 && index < columnGroups.size()) { 70 return (ColumnGroup)columnGroups.get(index); 71 } 72 return null; 73 } 74 75 /** 76 * Provides and iterator for accessing the ColumnGroups 77 * associated with a column. 78 * @param col Column 79 * @return ColumnGroup iterator 80 */ 81 public Iterator getColumnGroups(TableColumn col) { 82 if (columnGroups.isEmpty()) return null; 83 Iterator iter = columnGroups.iterator(); 84 while (iter.hasNext()) { 85 ColumnGroup cGroup = (ColumnGroup)iter.next(); 86 Vector v_ret = (Vector)cGroup.getColumnGroups(col,new Vector()); 87 if (v_ret != null) { 88 return v_ret.iterator(); 89 } 90 } 91 return null; 92 } 93 } 94 95