source: gli/trunk/src/org/greenstone/gatherer/gui/metaaudit/MetaAuditTable.java@ 18370

Last change on this file since 18370 was 18370, checked in by kjdon, 15 years ago

committed code submitted by Amin Hedjazi for making the GLI right to left. I worked on this code on the rtl-gli branch, then merged the branch back to the trunk at revision 18368. The branch code was slightly different in a couple of places where it shouldn't have been. So don't use the branch code next time. Start a new branch.

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