1 | package org.greenstone.gatherer.gui.metaaudit;
|
---|
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 de.qfs.lib.gui.*;
|
---|
39 | import java.awt.*;
|
---|
40 | import java.util.ArrayList;
|
---|
41 | import javax.swing.JTable;
|
---|
42 | import javax.swing.event.TableModelEvent;
|
---|
43 | import javax.swing.table.*;
|
---|
44 | import org.greenstone.gatherer.Gatherer;
|
---|
45 | import org.greenstone.gatherer.file.FileNode;
|
---|
46 | import org.greenstone.gatherer.gui.metaaudit.Filter;
|
---|
47 | import org.greenstone.gatherer.gui.metaaudit.HeaderListener;
|
---|
48 | import org.greenstone.gatherer.gui.metaaudit.HeaderRenderer;
|
---|
49 | import org.greenstone.gatherer.gui.metaaudit.MetaAuditFrame;
|
---|
50 | import org.greenstone.gatherer.gui.metaaudit.MetaAuditModel;
|
---|
51 | import org.greenstone.gatherer.gui.metaaudit.MetaAuditRenderer;
|
---|
52 | /**
|
---|
53 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
54 | * @version 2.3
|
---|
55 | */
|
---|
56 | public class MetaAuditTable
|
---|
57 | extends JTable
|
---|
58 | implements TableModelSorterListener {
|
---|
59 |
|
---|
60 | private boolean initial_state;
|
---|
61 |
|
---|
62 | private boolean ready;
|
---|
63 |
|
---|
64 | private Filter filter;
|
---|
65 |
|
---|
66 | private HeaderRenderer header_renderer;
|
---|
67 |
|
---|
68 | private MetaAuditFrame parent_frame;
|
---|
69 |
|
---|
70 | private MetaAuditModel model;
|
---|
71 |
|
---|
72 | private SortedTableHelper helper;
|
---|
73 |
|
---|
74 | static final private int MARGIN = 10;
|
---|
75 |
|
---|
76 | public MetaAuditTable(MetaAuditFrame parent_frame) {
|
---|
77 | super();
|
---|
78 | this.header_renderer = new HeaderRenderer();
|
---|
79 | this.parent_frame = parent_frame;
|
---|
80 | getTableHeader().addMouseListener(new HeaderListener(parent_frame, this));
|
---|
81 | setAutoResizeMode(AUTO_RESIZE_OFF);
|
---|
82 | setDefaultRenderer(ArrayList.class, new MetaAuditRenderer());
|
---|
83 | ready = true;
|
---|
84 | }
|
---|
85 |
|
---|
86 | public Filter getFilter() {
|
---|
87 | return filter;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public MetaAuditModel getOriginalModel() {
|
---|
91 | return model;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public TableModelSorter getSorter() {
|
---|
95 | return helper.getTableModelSorter();
|
---|
96 | }
|
---|
97 |
|
---|
98 | public boolean isFiltered(int column) {
|
---|
99 | if(filter == null) {
|
---|
100 | return false;
|
---|
101 | }
|
---|
102 | return filter.isFiltered(column);
|
---|
103 | }
|
---|
104 |
|
---|
105 | public void newModel(FileNode records[]) {
|
---|
106 | if(records == null || records.length == 0) {
|
---|
107 | return;
|
---|
108 | }
|
---|
109 | wait(true);
|
---|
110 | initial_state = true;
|
---|
111 | ///ystem.err.println("Build new Model - should only be called once per click!");
|
---|
112 | model = new MetaAuditModel(records);
|
---|
113 | setModel(model);
|
---|
114 | filter = new Filter(model.getColumnCount());
|
---|
115 | helper = new SortedTableHelper (this, filter, new DefaultTableModelSorter(), header_renderer, null);
|
---|
116 | helper.prepareTable();
|
---|
117 | // Add renderer to columns.
|
---|
118 | TableColumnModel column_model = getColumnModel();
|
---|
119 | for(int i = 0; i < column_model.getColumnCount(); i++) {
|
---|
120 | column_model.getColumn(i).setHeaderRenderer(header_renderer);
|
---|
121 | }
|
---|
122 | wait(false);
|
---|
123 | initial_state = false;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void tableChanged(TableModelEvent e) {
|
---|
127 | wait(true);
|
---|
128 | super.tableChanged(e);
|
---|
129 | if(ready) {
|
---|
130 | // Create storage area
|
---|
131 | ArrayList heights[] = new ArrayList[dataModel.getRowCount()];
|
---|
132 | int widths_value[] = new int[dataModel.getColumnCount()];
|
---|
133 | ArrayList widths[] = new ArrayList[dataModel.getColumnCount()];
|
---|
134 | // Iterate through cell values finding tallest and widest for each row and column respectively.
|
---|
135 | for(int j = 0; j < dataModel.getRowCount(); j++) {
|
---|
136 | for(int k = 0; k < dataModel.getColumnCount(); k++) {
|
---|
137 | Object object = dataModel.getValueAt(j, k);
|
---|
138 | if(object instanceof ArrayList) {
|
---|
139 | ArrayList data = (ArrayList) object;
|
---|
140 | if(heights[j] == null || data.size() > heights[j].size()) {
|
---|
141 | heights[j] = data;
|
---|
142 | }
|
---|
143 | String longest = "";
|
---|
144 | for(int i = 0; i < data.size(); i++) {
|
---|
145 | String temp = data.get(i).toString();
|
---|
146 | if(temp.length() > longest.length()) {
|
---|
147 | longest = temp;
|
---|
148 | }
|
---|
149 | temp = null;
|
---|
150 | }
|
---|
151 | if(widths[k] == null || longest.length() > widths_value[k]) {
|
---|
152 | widths_value[k] = longest.length();
|
---|
153 | widths[k] = data;
|
---|
154 | }
|
---|
155 | longest = null;
|
---|
156 | data = null;
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 | // Now assign the dimensions we have determined to be the largest.
|
---|
161 | TableCellRenderer renderer = getDefaultRenderer(ArrayList.class);
|
---|
162 | for(int l = 0; l < heights.length; l++) {
|
---|
163 | if(heights[l] != null) {
|
---|
164 | Component component = renderer.getTableCellRendererComponent(this, heights[l], false, false, 0, 0);
|
---|
165 | Dimension size = component.getPreferredSize();
|
---|
166 | setRowHeight(l, size.height);
|
---|
167 | size = null;
|
---|
168 | component = null;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | heights = null;
|
---|
172 | for(int m = 0; m < widths.length; m++) {
|
---|
173 | if(widths[m] != null) {
|
---|
174 | Component component = renderer.getTableCellRendererComponent(this, widths[m], false, false, 0, 0);
|
---|
175 | Dimension size = component.getPreferredSize();
|
---|
176 | int width = size.width + MARGIN;
|
---|
177 | if(width > (2 * parent_frame.getSize().width) / 3) {
|
---|
178 | width = (2 * parent_frame.getSize().width) / 3;
|
---|
179 | }
|
---|
180 | Component header = header_renderer.getTableCellRendererComponent(this, dataModel.getColumnName(m), false, false, 0, 0);
|
---|
181 | if(header.getPreferredSize().width + MARGIN > width) {
|
---|
182 | width = header.getPreferredSize().width + MARGIN;
|
---|
183 | }
|
---|
184 | columnModel.getColumn(m).setPreferredWidth(width);
|
---|
185 | size = null;
|
---|
186 | component = null;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | widths = null;
|
---|
190 | renderer = null;
|
---|
191 | }
|
---|
192 | wait(initial_state);
|
---|
193 | }
|
---|
194 |
|
---|
195 | public void sortOrderChanged(TableModelSorterEvent event) {
|
---|
196 | }
|
---|
197 |
|
---|
198 | private void wait(boolean waiting) {
|
---|
199 | if(parent_frame != null) {
|
---|
200 | parent_frame.wait(waiting);
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|