source: trunk/gli/src/org/greenstone/gatherer/gems/AttributeTableModel.java@ 8270

Last change on this file since 8270 was 8270, checked in by mdewsnip, 20 years ago

Source files for the Greenstone Editor for Metadata Sets (GEMS). This is currently just the old MetadataEditorManager, modified to run stand-alone. It will be substantially improved by Attila Aros.

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