source: trunk/gli/src/org/greenstone/gatherer/metadata/MetadataValueTableModel.java@ 13398

Last change on this file since 13398 was 13398, checked in by kjdon, 17 years ago

new warningdialog now takes a string for the title rather than a dictionary key

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