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.DebugStream;
|
---|
37 | import org.greenstone.gatherer.Dictionary;
|
---|
38 | import org.greenstone.gatherer.Gatherer;
|
---|
39 | import org.greenstone.gatherer.collection.CollectionTreeNode;
|
---|
40 | import org.greenstone.gatherer.gui.WarningDialog;
|
---|
41 |
|
---|
42 |
|
---|
43 | public class MetadataValueTableModel
|
---|
44 | extends AbstractTableModel
|
---|
45 | {
|
---|
46 | /** The CollectionTreeNodes this model is built for */
|
---|
47 | private CollectionTreeNode[] file_nodes = null;
|
---|
48 | /** The list of MetadataValueTableEntries in the table */
|
---|
49 | private ArrayList metadata_value_table_entries = new ArrayList();
|
---|
50 |
|
---|
51 | static final private String[] COLUMN_NAMES = { "", Dictionary.get("Metadata.Element"), Dictionary.get("Metadata.Value") };
|
---|
52 |
|
---|
53 |
|
---|
54 | public int addBlankRowForMetadataElement(MetadataElement metadata_element)
|
---|
55 | {
|
---|
56 | MetadataValueTableModelBuilder metadata_value_table_model_builder = new MetadataValueTableModelBuilder();
|
---|
57 | MetadataValue blank_metadata_value = new MetadataValue(metadata_element, new MetadataValueTreeNode(""));
|
---|
58 | return metadata_value_table_model_builder.insertMetadataValue(blank_metadata_value);
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | public int findMetadataValueTableEntryToSelect(MetadataValueTableEntry metadata_value_table_entry)
|
---|
63 | {
|
---|
64 | MetadataElement metadata_element = metadata_value_table_entry.getMetadataElement();
|
---|
65 |
|
---|
66 | // Find the correct entry to select
|
---|
67 | for (int i = 0; i < metadata_value_table_entries.size(); i++) {
|
---|
68 | MetadataValueTableEntry current_metadata_value_table_entry = (MetadataValueTableEntry) metadata_value_table_entries.get(i);
|
---|
69 | int element_comparison = MetadataSetManager.compareMetadataElements(current_metadata_value_table_entry.getMetadataElement(), metadata_element);
|
---|
70 |
|
---|
71 | // We've found the right element, so check if the value already exists
|
---|
72 | if (element_comparison == 0) {
|
---|
73 | int value_comparison = current_metadata_value_table_entry.compareTo(metadata_value_table_entry);
|
---|
74 | if (value_comparison == 0) {
|
---|
75 | // Entry found!
|
---|
76 | return i;
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | // We've just gone past the correct entry to select
|
---|
81 | if (element_comparison > 0) {
|
---|
82 | return i - 1;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | // Have to select the last entry
|
---|
87 | return metadata_value_table_entries.size() - 1;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | public Class getColumnClass(int col)
|
---|
92 | {
|
---|
93 | return getValueAt(0, col).getClass();
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | /** Returns the number of columns in this table. */
|
---|
98 | public int getColumnCount()
|
---|
99 | {
|
---|
100 | return COLUMN_NAMES.length;
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | /** Retrieves the name of the specified column. */
|
---|
105 | public String getColumnName(int col)
|
---|
106 | {
|
---|
107 | return COLUMN_NAMES[col];
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | /* 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. */
|
---|
112 | public synchronized MetadataValueTableEntry getMetadataValueTableEntry(int row)
|
---|
113 | {
|
---|
114 | if (row >= 0 && row < metadata_value_table_entries.size()) {
|
---|
115 | return (MetadataValueTableEntry) metadata_value_table_entries.get(row);
|
---|
116 | }
|
---|
117 |
|
---|
118 | return null;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | /** Returns the number of rows in this table. */
|
---|
123 | public int getRowCount()
|
---|
124 | {
|
---|
125 | return metadata_value_table_entries.size();
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /** Returns the cell value at a given row and column as an Object. */
|
---|
130 | public Object getValueAt(int row, int col)
|
---|
131 | {
|
---|
132 | // Check values are reasonable
|
---|
133 | if (row < 0 || row >= metadata_value_table_entries.size() || col < 0 || col >= COLUMN_NAMES.length) {
|
---|
134 | return null;
|
---|
135 | }
|
---|
136 |
|
---|
137 | MetadataValueTableEntry metadata_value_table_entry = (MetadataValueTableEntry) metadata_value_table_entries.get(row);
|
---|
138 | if (col == 0 && metadata_value_table_entry.isInheritedMetadata()) {
|
---|
139 | return metadata_value_table_entry.getFolderMetadataInheritedFrom();
|
---|
140 | }
|
---|
141 |
|
---|
142 | if (col == 1) {
|
---|
143 | return metadata_value_table_entry.getMetadataElement();
|
---|
144 | }
|
---|
145 |
|
---|
146 | if (col == 2) {
|
---|
147 | return metadata_value_table_entry.getFullValue();
|
---|
148 | }
|
---|
149 |
|
---|
150 | return null;
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | public boolean isCellEditable(int row, int col)
|
---|
155 | {
|
---|
156 | // The inherited and element columns are never editable
|
---|
157 | if (col < 2) {
|
---|
158 | return false;
|
---|
159 | }
|
---|
160 |
|
---|
161 | // Extracted and inherited metadata is not editable
|
---|
162 | MetadataValueTableEntry metadata_value_table_entry = (MetadataValueTableEntry) metadata_value_table_entries.get(row);
|
---|
163 | if (metadata_value_table_entry.getMetadataElement().isExtractedMetadataElement() || metadata_value_table_entry.isInheritedMetadata()) {
|
---|
164 | return false;
|
---|
165 | }
|
---|
166 |
|
---|
167 | return true;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | /** Determine if the given metadata is common to all selected file nodes. */
|
---|
172 | public boolean isCommon(MetadataValueTableEntry metadata_value_table_entry)
|
---|
173 | {
|
---|
174 | return (file_nodes != null && metadata_value_table_entry.getOccurrences() == file_nodes.length);
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | /** Determine if the given metadata is common to all selected file nodes. */
|
---|
179 | public boolean isCommon(int row)
|
---|
180 | {
|
---|
181 | if (row >= 0 && row < metadata_value_table_entries.size()) {
|
---|
182 | return isCommon((MetadataValueTableEntry) metadata_value_table_entries.get(row));
|
---|
183 | }
|
---|
184 |
|
---|
185 | return false;
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | public void rebuild(CollectionTreeNode[] file_nodes)
|
---|
190 | {
|
---|
191 | this.file_nodes = file_nodes;
|
---|
192 | metadata_value_table_entries.clear();
|
---|
193 |
|
---|
194 | // Collection is in a state of flux
|
---|
195 | if (!Gatherer.c_man.ready()) {
|
---|
196 | return;
|
---|
197 | }
|
---|
198 |
|
---|
199 | if (file_nodes == null || file_nodes.length == 0) {
|
---|
200 | return;
|
---|
201 | }
|
---|
202 |
|
---|
203 | // Create model builder
|
---|
204 | MetadataValueTableModelBuilder builder = new MetadataValueTableModelBuilder();
|
---|
205 | builder.run();
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | public void setValueAt(Object new_metadata_value, int row, int col)
|
---|
210 | {
|
---|
211 | MetadataValueTableEntry metadata_value_table_entry = getMetadataValueTableEntry(row);
|
---|
212 |
|
---|
213 | // If nothing has changed no action is necessary
|
---|
214 | String old_metadata_value = metadata_value_table_entry.getFullValue();
|
---|
215 | if (new_metadata_value.equals(old_metadata_value)) {
|
---|
216 | return;
|
---|
217 | }
|
---|
218 |
|
---|
219 | // Lock the interface so nothing can be changed while the metadata edit is being processed
|
---|
220 | Gatherer.g_man.wait(true);
|
---|
221 |
|
---|
222 | // Metadata value added
|
---|
223 | if (old_metadata_value.equals("") && !new_metadata_value.equals("")) {
|
---|
224 | // If we're adding metadata to folders display the warning
|
---|
225 | if (!file_nodes[0].isLeaf()) {
|
---|
226 | WarningDialog dialog = new WarningDialog("warning.DirectoryLevelMetadata", "DirectoryLevelMetadata.Title", Dictionary.get("DirectoryLevelMetadata.Message"), null, true);
|
---|
227 | int dialog_result = dialog.display();
|
---|
228 | dialog.dispose();
|
---|
229 | if (dialog_result != JOptionPane.OK_OPTION) {
|
---|
230 | Gatherer.g_man.wait(false);
|
---|
231 | return;
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | MetadataElement metadata_element = metadata_value_table_entry.getMetadataElement();
|
---|
236 | MetadataValueTreeNode metadata_value_tree_node = metadata_element.addMetadataValue((String) new_metadata_value);
|
---|
237 | MetadataValue metadata_value = new MetadataValue(metadata_element, metadata_value_tree_node);
|
---|
238 | metadata_value.setIsAccumulatingMetadata(true);
|
---|
239 | (new AppendMetadataTask(metadata_value)).run();
|
---|
240 | }
|
---|
241 |
|
---|
242 | // Metadata value removed
|
---|
243 | else if (!old_metadata_value.equals("") && new_metadata_value.equals("")) {
|
---|
244 | (new RemoveMetadataTask(metadata_value_table_entry)).run();
|
---|
245 | }
|
---|
246 |
|
---|
247 | // Metadata value replaced
|
---|
248 | else {
|
---|
249 | MetadataElement metadata_element = metadata_value_table_entry.getMetadataElement();
|
---|
250 | MetadataValueTreeNode metadata_value_tree_node = metadata_element.addMetadataValue((String) new_metadata_value);
|
---|
251 | MetadataValue metadata_value = new MetadataValue(metadata_element, metadata_value_tree_node);
|
---|
252 | metadata_value.setIsAccumulatingMetadata(!metadata_value_table_entry.isInheritedMetadata());
|
---|
253 | (new ReplaceMetadataTask(metadata_value_table_entry, metadata_value)).run();
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 | private class AppendMetadataTask
|
---|
259 | // extends Thread
|
---|
260 | {
|
---|
261 | private MetadataValue metadata_value = null;
|
---|
262 |
|
---|
263 | private AppendMetadataTask(MetadataValue metadata_value)
|
---|
264 | {
|
---|
265 | this.metadata_value = metadata_value;
|
---|
266 | }
|
---|
267 |
|
---|
268 | public void run()
|
---|
269 | {
|
---|
270 | try {
|
---|
271 | // Edit metadata.xml files to add the metadata
|
---|
272 | MetadataXMLFileManager.addMetadata(file_nodes, metadata_value);
|
---|
273 | }
|
---|
274 | catch (Exception exception) {
|
---|
275 | // We need to catch any exceptions here so the interface is unlocked below
|
---|
276 | DebugStream.printStackTrace(exception);
|
---|
277 | }
|
---|
278 |
|
---|
279 | // Operation finished, so turn busy cursor off and unlock interface
|
---|
280 | Gatherer.g_man.wait(false);
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 | private class ReplaceMetadataTask
|
---|
286 | // extends Thread
|
---|
287 | {
|
---|
288 | private MetadataValueTableEntry selected_metadata_value_table_entry = null;
|
---|
289 | private MetadataValue metadata_value = null;
|
---|
290 |
|
---|
291 | private ReplaceMetadataTask(MetadataValueTableEntry selected_metadata_value_table_entry, MetadataValue metadata_value)
|
---|
292 | {
|
---|
293 | this.selected_metadata_value_table_entry = selected_metadata_value_table_entry;
|
---|
294 | this.metadata_value = metadata_value;
|
---|
295 | }
|
---|
296 |
|
---|
297 | public void run()
|
---|
298 | {
|
---|
299 | try {
|
---|
300 | // Edit metadata.xml files to replace the metadata
|
---|
301 | MetadataXMLFileManager.replaceMetadata(file_nodes, selected_metadata_value_table_entry, metadata_value);
|
---|
302 | }
|
---|
303 | catch (Exception exception) {
|
---|
304 | // We need to catch any exceptions here so the interface is unlocked below
|
---|
305 | DebugStream.printStackTrace(exception);
|
---|
306 | }
|
---|
307 |
|
---|
308 | // Operation finished, so turn busy cursor off and unlock interface
|
---|
309 | Gatherer.g_man.wait(false);
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | private class RemoveMetadataTask
|
---|
315 | // extends Thread
|
---|
316 | {
|
---|
317 | private MetadataValueTableEntry selected_metadata_value_table_entry = null;
|
---|
318 |
|
---|
319 | private RemoveMetadataTask(MetadataValueTableEntry selected_metadata_value_table_entry)
|
---|
320 | {
|
---|
321 | this.selected_metadata_value_table_entry = selected_metadata_value_table_entry;
|
---|
322 | }
|
---|
323 |
|
---|
324 | public void run()
|
---|
325 | {
|
---|
326 | try {
|
---|
327 | // Edit metadata.xml files to remove the metadata
|
---|
328 | MetadataXMLFileManager.removeMetadata(file_nodes, selected_metadata_value_table_entry);
|
---|
329 | }
|
---|
330 | catch (Exception exception) {
|
---|
331 | // We need to catch any exceptions here so the interface is unlocked below
|
---|
332 | DebugStream.printStackTrace(exception);
|
---|
333 | }
|
---|
334 |
|
---|
335 | // Operation finished, so turn busy cursor off and unlock interface
|
---|
336 | Gatherer.g_man.wait(false);
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 |
|
---|
341 | private class MetadataValueTableModelBuilder
|
---|
342 | {
|
---|
343 | public void run()
|
---|
344 | {
|
---|
345 | // System.err.println("Building MetadataValueTableModel...");
|
---|
346 |
|
---|
347 | // Build a list of MetadataValueTableEntries that represent the metadata asssigned to the selected files
|
---|
348 | boolean hid_extracted_metadata = false;
|
---|
349 | ArrayList metadata_elements_seen = new ArrayList();
|
---|
350 |
|
---|
351 | // Process each of the selected files in turn
|
---|
352 | for (int i = 0; i < file_nodes.length; i++) {
|
---|
353 | File current_file = file_nodes[i].getFile();
|
---|
354 |
|
---|
355 | // Get the metadata assigned to this file
|
---|
356 | ArrayList assigned_metadata = MetadataXMLFileManager.getMetadataAssignedToFile(current_file);
|
---|
357 | for (int j = 0; j < assigned_metadata.size(); j++) {
|
---|
358 | MetadataValue metadata_value = (MetadataValue) assigned_metadata.get(j);
|
---|
359 | MetadataElement metadata_element = metadata_value.getMetadataElement();
|
---|
360 |
|
---|
361 | // Insert this metadata value into the table, unless it already exists (in which case increment its count)
|
---|
362 | insertMetadataValue(metadata_value);
|
---|
363 |
|
---|
364 | // Remember we have seen this metadata element
|
---|
365 | if (metadata_elements_seen.contains(metadata_element) == false) {
|
---|
366 | metadata_elements_seen.add(metadata_element);
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | // Get the extracted metadata for this file, if desired
|
---|
371 | if (Configuration.get("general.view_extracted_metadata", Configuration.COLLECTION_SPECIFIC) == true) {
|
---|
372 | ArrayList extracted_metadata = DocXMLFileManager.getMetadataExtractedFromFile(current_file);
|
---|
373 | for (int k = 0; k < extracted_metadata.size(); k++) {
|
---|
374 | MetadataValue metadata_value = (MetadataValue) extracted_metadata.get(k);
|
---|
375 |
|
---|
376 | // Insert this metadata value into the table, unless it already exists (in which case increment its count)
|
---|
377 | insertMetadataValue(metadata_value);
|
---|
378 | }
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 | // Make sure each non-extracted metadata element appears in the table (even if blank)
|
---|
383 | ArrayList every_metadata_set_element = MetadataSetManager.getEveryMetadataSetElement();
|
---|
384 | for (int i = 0; i < every_metadata_set_element.size(); i++) {
|
---|
385 | MetadataElement metadata_element = (MetadataElement) every_metadata_set_element.get(i);
|
---|
386 |
|
---|
387 | // If we haven't seen this metadata element and it isn't extracted, add it now
|
---|
388 | if (!metadata_elements_seen.contains(metadata_element) && !metadata_element.isExtractedMetadataElement()) {
|
---|
389 | MetadataValueTableEntry metadata_value_table_entry = new MetadataValueTableEntry(metadata_element, new MetadataValueTreeNode(""));
|
---|
390 |
|
---|
391 | // Blank metadata is common to all selected files (otherwise it wouldn't be blank!)
|
---|
392 | metadata_value_table_entry.setOccurrences(file_nodes.length);
|
---|
393 |
|
---|
394 | // Add it to the table
|
---|
395 | insertMetadataValueTableEntry(metadata_value_table_entry);
|
---|
396 | }
|
---|
397 | }
|
---|
398 |
|
---|
399 | // If extracted metadata was hidden, display the warning
|
---|
400 | if (hid_extracted_metadata) {
|
---|
401 | showExtractedMetadataWarning();
|
---|
402 | }
|
---|
403 | }
|
---|
404 |
|
---|
405 |
|
---|
406 | /** Inserts the new metadata value into the table */
|
---|
407 | private int insertMetadataValue(MetadataValue metadata_value)
|
---|
408 | {
|
---|
409 | return insertMetadataValueTableEntry(new MetadataValueTableEntry(metadata_value));
|
---|
410 | }
|
---|
411 |
|
---|
412 |
|
---|
413 | /** Inserts the new metadata value table entry into the table */
|
---|
414 | private int insertMetadataValueTableEntry(MetadataValueTableEntry metadata_value_table_entry)
|
---|
415 | {
|
---|
416 | MetadataElement metadata_element = metadata_value_table_entry.getMetadataElement();
|
---|
417 |
|
---|
418 | // Find the correct place to insert the table entry
|
---|
419 | for (int i = 0; i < metadata_value_table_entries.size(); i++) {
|
---|
420 | MetadataValueTableEntry current_metadata_value_table_entry = (MetadataValueTableEntry) metadata_value_table_entries.get(i);
|
---|
421 | int element_comparison = MetadataSetManager.compareMetadataElements(current_metadata_value_table_entry.getMetadataElement(), metadata_element);
|
---|
422 |
|
---|
423 | // We've found the right element, so check if the value already exists
|
---|
424 | if (element_comparison == 0) {
|
---|
425 | int value_comparison = current_metadata_value_table_entry.compareTo(metadata_value_table_entry);
|
---|
426 | if (value_comparison == 0) {
|
---|
427 | // Entry already exists, so increment count (except for blank entries)
|
---|
428 | if (!metadata_value_table_entry.getFullValue().equals("")) {
|
---|
429 | current_metadata_value_table_entry.anotherOccurrence();
|
---|
430 | }
|
---|
431 | return i;
|
---|
432 | }
|
---|
433 | }
|
---|
434 |
|
---|
435 | // Found insertion point
|
---|
436 | if (element_comparison > 0) {
|
---|
437 | metadata_value_table_entries.add(i, metadata_value_table_entry);
|
---|
438 | fireTableRowsInserted(i, i);
|
---|
439 | return i;
|
---|
440 | }
|
---|
441 | }
|
---|
442 |
|
---|
443 | // Must go at the end of the table
|
---|
444 | metadata_value_table_entries.add(metadata_value_table_entry);
|
---|
445 | fireTableRowsInserted(metadata_value_table_entries.size() - 1, metadata_value_table_entries.size() - 1);
|
---|
446 | return metadata_value_table_entries.size() - 1;
|
---|
447 | }
|
---|
448 |
|
---|
449 |
|
---|
450 | private void showExtractedMetadataWarning()
|
---|
451 | {
|
---|
452 | Runnable task = new Runnable() {
|
---|
453 | public void run() {
|
---|
454 | WarningDialog dialog = new WarningDialog("warning.ExtractedMetadata", "ExtractedMetadata.Title", Dictionary.get("ExtractedMetadata.Message"), null, false);
|
---|
455 | dialog.display();
|
---|
456 | dialog.dispose();
|
---|
457 | dialog = null;
|
---|
458 | }
|
---|
459 | };
|
---|
460 | SwingUtilities.invokeLater(task);
|
---|
461 | }
|
---|
462 | }
|
---|
463 | }
|
---|