1 | /**
|
---|
2 | *############################################################################
|
---|
3 | * A component of the Greenstone Librarian Interface, part of the Greenstone
|
---|
4 | * digital library suite from the New Zealand Digital Library Project at the
|
---|
5 | * University of Waikato, New Zealand.
|
---|
6 | *
|
---|
7 | * Author: Michael Dewsnip, NZDL Project, University of Waikato, NZ
|
---|
8 | *
|
---|
9 | * Copyright (C) 2004 New Zealand Digital Library Project
|
---|
10 | *
|
---|
11 | * This program is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * This program is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with this program; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | *############################################################################
|
---|
25 | */
|
---|
26 |
|
---|
27 | package org.greenstone.gatherer.metadata;
|
---|
28 |
|
---|
29 |
|
---|
30 | import java.awt.*;
|
---|
31 | import java.io.*;
|
---|
32 | import java.util.*;
|
---|
33 | import javax.swing.*;
|
---|
34 | import javax.swing.table.*;
|
---|
35 | import org.greenstone.gatherer.Configuration;
|
---|
36 | import org.greenstone.gatherer.Dictionary;
|
---|
37 | import org.greenstone.gatherer.Gatherer;
|
---|
38 | import org.greenstone.gatherer.file.FileNode;
|
---|
39 | import org.greenstone.gatherer.gui.WarningDialog;
|
---|
40 |
|
---|
41 |
|
---|
42 | public class MetadataValueTableModel
|
---|
43 | extends AbstractTableModel
|
---|
44 | {
|
---|
45 | /** The FileNodes this model is built for */
|
---|
46 | private FileNode[] file_nodes = null;
|
---|
47 | /** The list of MetadataValueTableEntries in the table */
|
---|
48 | private ArrayList metadata_value_table_entries = new ArrayList();
|
---|
49 |
|
---|
50 | static final private String[] COLUMN_NAMES = { "", Dictionary.get("Metadata.Element"), Dictionary.get("Metadata.Value") };
|
---|
51 |
|
---|
52 |
|
---|
53 | /** Returns the number of columns in this table. */
|
---|
54 | public int getColumnCount()
|
---|
55 | {
|
---|
56 | return COLUMN_NAMES.length;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | /** Retrieves the name of the specified column. */
|
---|
61 | public String getColumnName(int col)
|
---|
62 | {
|
---|
63 | return COLUMN_NAMES[col];
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | /* Called to retrieve the MetadataValueTableEntry at a certain row. Usually caused by the user selecting a row in the table. It is synchronized so that the model doesn't up and change while we're trying to retrieve the indicated element. */
|
---|
68 | public synchronized MetadataValueTableEntry getMetadataValueTableEntry(int row)
|
---|
69 | {
|
---|
70 | if (row >= 0 && row < metadata_value_table_entries.size()) {
|
---|
71 | return (MetadataValueTableEntry) metadata_value_table_entries.get(row);
|
---|
72 | }
|
---|
73 |
|
---|
74 | return null;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /** Returns the number of rows in this table. */
|
---|
79 | public int getRowCount()
|
---|
80 | {
|
---|
81 | return metadata_value_table_entries.size();
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | /** Returns the cell value at a given row and column as an Object. */
|
---|
86 | public Object getValueAt(int row, int col)
|
---|
87 | {
|
---|
88 | // Check values are reasonable
|
---|
89 | if (row < 0 || row >= metadata_value_table_entries.size() || col < 0 || col >= COLUMN_NAMES.length) {
|
---|
90 | return null;
|
---|
91 | }
|
---|
92 |
|
---|
93 | MetadataValueTableEntry metadata_value_table_entry = (MetadataValueTableEntry) metadata_value_table_entries.get(row);
|
---|
94 | if (col == 0 && metadata_value_table_entry.isInheritedMetadata()) {
|
---|
95 | return metadata_value_table_entry.getFolderMetadataInheritedFrom();
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (col == 1) {
|
---|
99 | return metadata_value_table_entry.getMetadataElement();
|
---|
100 | }
|
---|
101 |
|
---|
102 | if (col == 2) {
|
---|
103 | return metadata_value_table_entry.getFullValue();
|
---|
104 | }
|
---|
105 |
|
---|
106 | return null;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | /** Determine if the given metadata is common to all selected file nodes given the context of the current view. */
|
---|
111 | public boolean isCommon(MetadataValueTableEntry metadata_value_table_entry)
|
---|
112 | {
|
---|
113 | return (file_nodes != null && metadata_value_table_entry.getOccurrences() == file_nodes.length);
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /** Determine if the given metadata is common to all selected file nodes given the context of the current view. */
|
---|
118 | public boolean isCommon(int row)
|
---|
119 | {
|
---|
120 | if (row >= 0 && row < metadata_value_table_entries.size()) {
|
---|
121 | return isCommon((MetadataValueTableEntry) metadata_value_table_entries.get(row));
|
---|
122 | }
|
---|
123 |
|
---|
124 | return false;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | public void rebuild(FileNode[] file_nodes)
|
---|
129 | {
|
---|
130 | this.file_nodes = file_nodes;
|
---|
131 | metadata_value_table_entries.clear();
|
---|
132 |
|
---|
133 | // Collection is in a state of flux
|
---|
134 | if (!Gatherer.c_man.ready()) {
|
---|
135 | return;
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (file_nodes == null || file_nodes.length == 0) {
|
---|
139 | return;
|
---|
140 | }
|
---|
141 |
|
---|
142 | // Create model builder
|
---|
143 | MetadataValueTableModelBuilder builder = new MetadataValueTableModelBuilder();
|
---|
144 | builder.run();
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | private class MetadataValueTableModelBuilder
|
---|
149 | {
|
---|
150 | public void run()
|
---|
151 | {
|
---|
152 | // Build a list of MetadataValueTableEntries that represent the metadata asssigned to the selected files
|
---|
153 | boolean hid_extracted_metadata = false;
|
---|
154 | boolean has_inherited_metadata = false;
|
---|
155 | ArrayList metadata_elements_seen = new ArrayList();
|
---|
156 |
|
---|
157 | // Process each of the selected files in turn
|
---|
158 | for (int i = 0; i < file_nodes.length; i++) {
|
---|
159 | File current_file = file_nodes[i].getFile();
|
---|
160 |
|
---|
161 | // Get the metadata assigned to this file
|
---|
162 | ArrayList assigned_metadata = MetadataXMLFileManager.getMetadataAssignedToFile(current_file);
|
---|
163 | for (int j = 0; j < assigned_metadata.size(); j++) {
|
---|
164 | MetadataValue metadata_value = (MetadataValue) assigned_metadata.get(j);
|
---|
165 | MetadataElement metadata_element = metadata_value.getMetadataElement();
|
---|
166 |
|
---|
167 | // Note if there is inherited (folder-level) metadata in the table
|
---|
168 | has_inherited_metadata = has_inherited_metadata || metadata_value.isInheritedMetadata();
|
---|
169 |
|
---|
170 | // Insert this metadata value into the table, unless it already exists (in which case increment its count)
|
---|
171 | insertMetadataValueIntoTable(metadata_value);
|
---|
172 |
|
---|
173 | // Remember we have seen this metadata element
|
---|
174 | if (metadata_elements_seen.contains(metadata_element) == false) {
|
---|
175 | metadata_elements_seen.add(metadata_element);
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | // Get the extracted metadata for this file, if desired
|
---|
180 | if (Configuration.get("general.view_extracted_metadata", Configuration.COLLECTION_SPECIFIC) == true) {
|
---|
181 | ArrayList extracted_metadata = DocXMLFileManager.getMetadataExtractedFromFile(current_file);
|
---|
182 | for (int k = 0; k < extracted_metadata.size(); k++) {
|
---|
183 | MetadataValue metadata_value = (MetadataValue) extracted_metadata.get(k);
|
---|
184 |
|
---|
185 | // Insert this metadata value into the table, unless it already exists (in which case increment its count)
|
---|
186 | insertMetadataValueIntoTable(metadata_value);
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | // Make sure each non-extracted metadata element appears in the table (even if blank)
|
---|
192 | ArrayList every_metadata_set_element = MetadataSetManager.getEveryMetadataSetElement();
|
---|
193 | for (int i = 0; i < every_metadata_set_element.size(); i++) {
|
---|
194 | MetadataElement metadata_element = (MetadataElement) every_metadata_set_element.get(i);
|
---|
195 |
|
---|
196 | // If we haven't seen this metadata element and it isn't extracted, add it now
|
---|
197 | if (!metadata_elements_seen.contains(metadata_element) && !metadata_element.isExtractedMetadata()) {
|
---|
198 | MetadataValueTableEntry metadata_value_table_entry = new MetadataValueTableEntry(metadata_element, new MetadataValueTreeNode(""));
|
---|
199 |
|
---|
200 | // Blank metadata is common to all selected files (otherwise it wouldn't be blank!)
|
---|
201 | metadata_value_table_entry.setOccurrences(file_nodes.length);
|
---|
202 |
|
---|
203 | // Add it to the table
|
---|
204 | insertMetadataValueIntoTable(metadata_value_table_entry);
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | // If extracted metadata was hidden, display the warning
|
---|
209 | if (hid_extracted_metadata) {
|
---|
210 | showExtractedMetadataWarning();
|
---|
211 | }
|
---|
212 |
|
---|
213 | // If there is inherited metadata, display the warning
|
---|
214 | if (has_inherited_metadata) {
|
---|
215 | showInheritedMetadataWarning();
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | /** Alphabetically inserts the new metadata value into the table */
|
---|
221 | private void insertMetadataValueIntoTable(MetadataValue metadata_value)
|
---|
222 | {
|
---|
223 | insertMetadataValueIntoTable(new MetadataValueTableEntry(metadata_value));
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|
227 | /** Alphabetically inserts the new metadata value into the table */
|
---|
228 | private void insertMetadataValueIntoTable(MetadataValueTableEntry metadata_value_table_entry)
|
---|
229 | {
|
---|
230 | for (int i = 0; i < metadata_value_table_entries.size(); i++) {
|
---|
231 | MetadataValueTableEntry current_metadata_value_table_entry = (MetadataValueTableEntry) metadata_value_table_entries.get(i);
|
---|
232 | int c = current_metadata_value_table_entry.compareTo(metadata_value_table_entry);
|
---|
233 |
|
---|
234 | // Insert value before existing entry
|
---|
235 | if (c > 0) {
|
---|
236 | metadata_value_table_entries.add(i, metadata_value_table_entry);
|
---|
237 | fireTableRowsInserted(i, i);
|
---|
238 | return;
|
---|
239 | }
|
---|
240 |
|
---|
241 | // Entry already exists (increment existing count)
|
---|
242 | if (c == 0) {
|
---|
243 | current_metadata_value_table_entry.anotherOccurrence();
|
---|
244 | return;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | // Must go at the end of the table
|
---|
249 | metadata_value_table_entries.add(metadata_value_table_entry);
|
---|
250 | fireTableRowsInserted(metadata_value_table_entries.size() - 1, metadata_value_table_entries.size() - 1);
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | private void showExtractedMetadataWarning()
|
---|
255 | {
|
---|
256 | Runnable task = new Runnable() {
|
---|
257 | public void run() {
|
---|
258 | WarningDialog dialog = new WarningDialog("warning.ExtractedMetadata", null, false);
|
---|
259 | dialog.display();
|
---|
260 | dialog.dispose();
|
---|
261 | dialog = null;
|
---|
262 | }
|
---|
263 | };
|
---|
264 | SwingUtilities.invokeLater(task);
|
---|
265 | }
|
---|
266 |
|
---|
267 |
|
---|
268 | private void showInheritedMetadataWarning()
|
---|
269 | {
|
---|
270 | Runnable task = new Runnable() {
|
---|
271 | public void run() {
|
---|
272 | WarningDialog dialog = new WarningDialog("warning.InheritedMetadata", null, false);
|
---|
273 | dialog.display();
|
---|
274 | dialog.dispose();
|
---|
275 | dialog = null;
|
---|
276 | }
|
---|
277 | };
|
---|
278 | SwingUtilities.invokeLater(task);
|
---|
279 | }
|
---|
280 | }
|
---|
281 | }
|
---|