[4293] | 1 | package org.greenstone.gatherer.mem;
|
---|
| 2 | /**
|
---|
| 3 | *#########################################################################
|
---|
| 4 | *
|
---|
| 5 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
| 6 | * library suite from the New Zealand Digital Library Project at the
|
---|
| 7 | * University of Waikato, New Zealand.
|
---|
| 8 | *
|
---|
| 9 | * <BR><BR>
|
---|
| 10 | *
|
---|
| 11 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 12 | *
|
---|
| 13 | * <BR><BR>
|
---|
| 14 | *
|
---|
| 15 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
| 16 | *
|
---|
| 17 | * <BR><BR>
|
---|
| 18 | *
|
---|
| 19 | * This program is free software; you can redistribute it and/or modify
|
---|
| 20 | * it under the terms of the GNU General Public License as published by
|
---|
| 21 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 22 | * (at your option) any later version.
|
---|
| 23 | *
|
---|
| 24 | * <BR><BR>
|
---|
| 25 | *
|
---|
| 26 | * This program is distributed in the hope that it will be useful,
|
---|
| 27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 29 | * GNU General Public License for more details.
|
---|
| 30 | *
|
---|
| 31 | * <BR><BR>
|
---|
| 32 | *
|
---|
| 33 | * You should have received a copy of the GNU General Public License
|
---|
| 34 | * along with this program; if not, write to the Free Software
|
---|
| 35 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 36 | *########################################################################
|
---|
| 37 | */
|
---|
| 38 | import java.awt.*;
|
---|
| 39 | import java.util.*;
|
---|
| 40 | import javax.swing.*;
|
---|
| 41 | import javax.swing.table.*;
|
---|
| 42 | import org.greenstone.gatherer.Gatherer;
|
---|
| 43 | import org.greenstone.gatherer.mem.Attribute;
|
---|
| 44 | import org.greenstone.gatherer.util.Utility;
|
---|
| 45 | /**
|
---|
| 46 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 47 | * @version 2.3
|
---|
| 48 | */
|
---|
| 49 | public class AttributeTableModel
|
---|
[4365] | 50 | extends AbstractTableModel {
|
---|
| 51 | private Attribute last_attribute = null;
|
---|
| 52 | private int last_index = -1;
|
---|
| 53 | private int longest[] = null;
|
---|
| 54 | private int column_zero_width = 0;
|
---|
| 55 | private JScrollPane scroll_pane = null;
|
---|
| 56 | private JTable table = null;
|
---|
| 57 | private String default_value = null;
|
---|
| 58 | private String names[] = null;
|
---|
| 59 | private TreeSet attributes = null;
|
---|
| 60 | static final private int table_width = 550;
|
---|
| 61 | public AttributeTableModel(TreeSet attributes, String one, String two, String default_value) {
|
---|
| 62 | super();
|
---|
| 63 | this.attributes = attributes;
|
---|
| 64 | this.default_value = default_value;
|
---|
| 65 | this.longest = new int[2];
|
---|
| 66 | this.names = new String[2];
|
---|
| 67 | names[0] = one;
|
---|
| 68 | names[1] = two;
|
---|
| 69 | }
|
---|
[4293] | 70 |
|
---|
[4365] | 71 | public AttributeTableModel(TreeSet attributes, String one, String two, String three, String default_value) {
|
---|
| 72 | super();
|
---|
| 73 | this.attributes = attributes;
|
---|
| 74 | this.default_value = default_value;
|
---|
| 75 | this.longest = new int[3];
|
---|
| 76 | this.names = new String[3];
|
---|
| 77 | names[0] = one;
|
---|
| 78 | names[1] = two;
|
---|
| 79 | names[2] = three;
|
---|
| 80 | }
|
---|
| 81 | public void add(Attribute attribute) {
|
---|
| 82 | attributes.add(attribute);
|
---|
| 83 | // Determine the row index
|
---|
| 84 | boolean found = false;
|
---|
| 85 | int index = 0;
|
---|
| 86 | Iterator iterator = attributes.iterator();
|
---|
| 87 | for(int i = 0; !found && iterator.hasNext(); i++) {
|
---|
| 88 | if(attribute == iterator.next()) {
|
---|
| 89 | found = true;
|
---|
| 90 | index = i;
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | // Fire event
|
---|
| 94 | fireTableRowsInserted(index, index);
|
---|
| 95 | }
|
---|
[4293] | 96 |
|
---|
[4365] | 97 | public boolean contains(String value, int column) {
|
---|
| 98 | boolean result = false;
|
---|
| 99 | Iterator iterator = attributes.iterator();
|
---|
| 100 | while(iterator.hasNext() && !result) {
|
---|
| 101 | Attribute attribute = (Attribute) iterator.next();
|
---|
| 102 | if(value.equals(attribute.get(column))) {
|
---|
| 103 | result = true;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | return result;
|
---|
| 107 | }
|
---|
[4293] | 108 |
|
---|
[4365] | 109 | public Attribute getAttribute(int row) {
|
---|
| 110 | if(row != last_index) {
|
---|
| 111 | last_index = row;
|
---|
| 112 | last_attribute = null;
|
---|
[4293] | 113 | // Retrieve the value from the set using its iterator
|
---|
[4365] | 114 | Iterator iterator = attributes.iterator();
|
---|
| 115 | for(int i = 0; i < row && iterator.hasNext(); i++) {
|
---|
| 116 | iterator.next();
|
---|
| 117 | }
|
---|
| 118 | if(iterator.hasNext()) {
|
---|
| 119 | last_attribute = (Attribute) iterator.next();
|
---|
| 120 | }
|
---|
| 121 | iterator = null;
|
---|
| 122 | }
|
---|
| 123 | return last_attribute;
|
---|
| 124 | }
|
---|
[4293] | 125 |
|
---|
[4365] | 126 | public int getColumnCount() {
|
---|
| 127 | return names.length;
|
---|
| 128 | }
|
---|
[4293] | 129 |
|
---|
[4365] | 130 | public String getColumnName(int col) {
|
---|
| 131 | return names[col];
|
---|
| 132 | }
|
---|
[4293] | 133 |
|
---|
[4365] | 134 | public int getRowCount() {
|
---|
| 135 | int count = 0;
|
---|
| 136 | if(attributes != null) {
|
---|
| 137 | count = attributes.size();
|
---|
| 138 | }
|
---|
| 139 | return count;
|
---|
| 140 | }
|
---|
[4293] | 141 |
|
---|
[4365] | 142 | public Object getValueAt(int row, int col) {
|
---|
| 143 | String value = null;
|
---|
| 144 | Attribute attribute = getAttribute(row);
|
---|
| 145 | if(attribute != null) {
|
---|
| 146 | value = attribute.get(col);
|
---|
| 147 | }
|
---|
| 148 | if(value == null) {
|
---|
| 149 | value = default_value;
|
---|
| 150 | }
|
---|
| 151 | // Update table column width if necessary
|
---|
| 152 | if(value.length() > longest[col]) {
|
---|
| 153 | Component component = table.getCellRenderer(row, col).getTableCellRendererComponent(table, value, false, false, 0, 0);
|
---|
| 154 | int width = component.getPreferredSize().width + 10;
|
---|
| 155 | TableColumn column = table.getColumnModel().getColumn(col);
|
---|
| 156 | if(width > column.getPreferredWidth()) {
|
---|
| 157 | // Calculate default column width
|
---|
| 158 | int default_width = 0;
|
---|
| 159 | int total_width = scroll_pane.getSize().width;
|
---|
| 160 | if(names.length == 2) {
|
---|
| 161 | if(col == 0) {
|
---|
| 162 | default_width = total_width / 3;
|
---|
| 163 | }
|
---|
| 164 | else {
|
---|
| 165 | default_width = 2 * (total_width / 3);
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | else {
|
---|
| 169 | switch(col) {
|
---|
| 170 | case 0:
|
---|
| 171 | default_width = (2 * total_width) / 9;
|
---|
| 172 | break;
|
---|
| 173 | case 1:
|
---|
| 174 | default_width = total_width / 9;
|
---|
| 175 | break;
|
---|
| 176 | default:
|
---|
| 177 | default_width = 2 * (total_width / 3);
|
---|
| 178 | break;
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 | if(width > default_width) {
|
---|
| 182 | column.setPreferredWidth(width);
|
---|
| 183 | }
|
---|
| 184 | else {
|
---|
| 185 | column.setPreferredWidth(default_width);
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | longest[col] = value.length();
|
---|
| 189 | }
|
---|
| 190 | return Utility.stripNL(value);
|
---|
| 191 | }
|
---|
| 192 | public void removeRow(final int row) {
|
---|
| 193 | ///ystem.err.println("Remove row " + row);
|
---|
| 194 | Attribute attribute = getAttribute(row);
|
---|
| 195 | if(attribute != null) {
|
---|
| 196 | attributes.remove(attribute);
|
---|
[4293] | 197 | // Arg... this caused me a headache for hours.
|
---|
[4365] | 198 | last_index = -1;
|
---|
| 199 | fireTableRowsDeleted(row, row);
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 | public void setScrollPane(JScrollPane scroll_pane) {
|
---|
| 203 | this.scroll_pane = scroll_pane;
|
---|
| 204 | }
|
---|
| 205 | public void setTable(JTable table) {
|
---|
| 206 | this.table = table;
|
---|
| 207 | this.longest = new int[getColumnCount()];
|
---|
| 208 | }
|
---|
[4293] | 209 | }
|
---|