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

Last change on this file since 9864 was 9864, checked in by mdewsnip, 19 years ago

Changed WarningDialog to take the direct message text, rather than the message text key. This makes it more flexible, allowing arguments in the message text (eg. used by PluginManager).

  • Property svn:keywords set to Author Date Id Revision
File size: 15.9 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 /** 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 return;
231 }
232 }
233
234 MetadataElement metadata_element = metadata_value_table_entry.getMetadataElement();
235 MetadataValueTreeNode metadata_value_tree_node = metadata_element.addMetadataValue((String) new_metadata_value);
236 MetadataValue metadata_value = new MetadataValue(metadata_element, metadata_value_tree_node);
237 metadata_value.setIsAccumulatingMetadata(true);
238 (new AppendMetadataTask(metadata_value)).run();
239 }
240
241 // Metadata value removed
242 else if (!old_metadata_value.equals("") && new_metadata_value.equals("")) {
243 (new RemoveMetadataTask(metadata_value_table_entry)).run();
244 }
245
246 // Metadata value replaced
247 else {
248 MetadataElement metadata_element = metadata_value_table_entry.getMetadataElement();
249 MetadataValueTreeNode metadata_value_tree_node = metadata_element.addMetadataValue((String) new_metadata_value);
250 MetadataValue metadata_value = new MetadataValue(metadata_element, metadata_value_tree_node);
251 metadata_value.setIsAccumulatingMetadata(!metadata_value_table_entry.isInheritedMetadata());
252 (new ReplaceMetadataTask(metadata_value_table_entry, metadata_value)).run();
253 }
254 }
255
256
257 private class AppendMetadataTask
258 // extends Thread
259 {
260 private MetadataValue metadata_value = null;
261
262 private AppendMetadataTask(MetadataValue metadata_value)
263 {
264 this.metadata_value = metadata_value;
265 }
266
267 public void run()
268 {
269 try {
270 // Edit metadata.xml files to add the metadata
271 MetadataXMLFileManager.addMetadata(file_nodes, metadata_value);
272 }
273 catch (Exception exception) {
274 // We need to catch any exceptions here so the interface is unlocked below
275 DebugStream.printStackTrace(exception);
276 }
277
278 // Operation finished, so turn busy cursor off and unlock interface
279 Gatherer.g_man.wait(false);
280 }
281 }
282
283
284 private class ReplaceMetadataTask
285 // extends Thread
286 {
287 private MetadataValueTableEntry selected_metadata_value_table_entry = null;
288 private MetadataValue metadata_value = null;
289
290 private ReplaceMetadataTask(MetadataValueTableEntry selected_metadata_value_table_entry, MetadataValue metadata_value)
291 {
292 this.selected_metadata_value_table_entry = selected_metadata_value_table_entry;
293 this.metadata_value = metadata_value;
294 }
295
296 public void run()
297 {
298 try {
299 // Edit metadata.xml files to replace the metadata
300 MetadataXMLFileManager.replaceMetadata(file_nodes, selected_metadata_value_table_entry, metadata_value);
301 }
302 catch (Exception exception) {
303 // We need to catch any exceptions here so the interface is unlocked below
304 DebugStream.printStackTrace(exception);
305 }
306
307 // Operation finished, so turn busy cursor off and unlock interface
308 Gatherer.g_man.wait(false);
309 }
310 }
311
312
313 private class RemoveMetadataTask
314 // extends Thread
315 {
316 private MetadataValueTableEntry selected_metadata_value_table_entry = null;
317
318 private RemoveMetadataTask(MetadataValueTableEntry selected_metadata_value_table_entry)
319 {
320 this.selected_metadata_value_table_entry = selected_metadata_value_table_entry;
321 }
322
323 public void run()
324 {
325 try {
326 // Edit metadata.xml files to remove the metadata
327 MetadataXMLFileManager.removeMetadata(file_nodes, selected_metadata_value_table_entry);
328 }
329 catch (Exception exception) {
330 // We need to catch any exceptions here so the interface is unlocked below
331 DebugStream.printStackTrace(exception);
332 }
333
334 // Operation finished, so turn busy cursor off and unlock interface
335 Gatherer.g_man.wait(false);
336 }
337 }
338
339
340 private class MetadataValueTableModelBuilder
341 {
342 public void run()
343 {
344 // System.err.println("Building MetadataValueTableModel...");
345
346 // Build a list of MetadataValueTableEntries that represent the metadata asssigned to the selected files
347 boolean hid_extracted_metadata = false;
348 ArrayList metadata_elements_seen = new ArrayList();
349
350 // Process each of the selected files in turn
351 for (int i = 0; i < file_nodes.length; i++) {
352 File current_file = file_nodes[i].getFile();
353
354 // Get the metadata assigned to this file
355 ArrayList assigned_metadata = MetadataXMLFileManager.getMetadataAssignedToFile(current_file);
356 for (int j = 0; j < assigned_metadata.size(); j++) {
357 MetadataValue metadata_value = (MetadataValue) assigned_metadata.get(j);
358 MetadataElement metadata_element = metadata_value.getMetadataElement();
359
360 // Insert this metadata value into the table, unless it already exists (in which case increment its count)
361 insertMetadataValue(metadata_value);
362
363 // Remember we have seen this metadata element
364 if (metadata_elements_seen.contains(metadata_element) == false) {
365 metadata_elements_seen.add(metadata_element);
366 }
367 }
368
369 // Get the extracted metadata for this file, if desired
370 if (Configuration.get("general.view_extracted_metadata", Configuration.COLLECTION_SPECIFIC) == true) {
371 ArrayList extracted_metadata = DocXMLFileManager.getMetadataExtractedFromFile(current_file);
372 for (int k = 0; k < extracted_metadata.size(); k++) {
373 MetadataValue metadata_value = (MetadataValue) extracted_metadata.get(k);
374
375 // Insert this metadata value into the table, unless it already exists (in which case increment its count)
376 insertMetadataValue(metadata_value);
377 }
378 }
379 }
380
381 // Make sure each non-extracted metadata element appears in the table (even if blank)
382 ArrayList every_metadata_set_element = MetadataSetManager.getEveryMetadataSetElement();
383 for (int i = 0; i < every_metadata_set_element.size(); i++) {
384 MetadataElement metadata_element = (MetadataElement) every_metadata_set_element.get(i);
385
386 // If we haven't seen this metadata element and it isn't extracted, add it now
387 if (!metadata_elements_seen.contains(metadata_element) && !metadata_element.isExtractedMetadataElement()) {
388 MetadataValueTableEntry metadata_value_table_entry = new MetadataValueTableEntry(metadata_element, new MetadataValueTreeNode(""));
389
390 // Blank metadata is common to all selected files (otherwise it wouldn't be blank!)
391 metadata_value_table_entry.setOccurrences(file_nodes.length);
392
393 // Add it to the table
394 insertMetadataValueTableEntry(metadata_value_table_entry);
395 }
396 }
397
398 // If extracted metadata was hidden, display the warning
399 if (hid_extracted_metadata) {
400 showExtractedMetadataWarning();
401 }
402 }
403
404
405 /** Inserts the new metadata value into the table */
406 private int insertMetadataValue(MetadataValue metadata_value)
407 {
408 return insertMetadataValueTableEntry(new MetadataValueTableEntry(metadata_value));
409 }
410
411
412 /** Inserts the new metadata value table entry into the table */
413 private int insertMetadataValueTableEntry(MetadataValueTableEntry metadata_value_table_entry)
414 {
415 MetadataElement metadata_element = metadata_value_table_entry.getMetadataElement();
416
417 // Find the correct place to insert the table entry
418 for (int i = 0; i < metadata_value_table_entries.size(); i++) {
419 MetadataValueTableEntry current_metadata_value_table_entry = (MetadataValueTableEntry) metadata_value_table_entries.get(i);
420 int element_comparison = MetadataSetManager.compareMetadataElements(current_metadata_value_table_entry.getMetadataElement(), metadata_element);
421
422 // We've found the right element, so check if the value already exists
423 if (element_comparison == 0) {
424 int value_comparison = current_metadata_value_table_entry.compareTo(metadata_value_table_entry);
425 if (value_comparison == 0) {
426 // Entry already exists, so increment count (except for blank entries)
427 if (!metadata_value_table_entry.getFullValue().equals("")) {
428 current_metadata_value_table_entry.anotherOccurrence();
429 }
430 return i;
431 }
432 }
433
434 // Found insertion point
435 if (element_comparison > 0) {
436 metadata_value_table_entries.add(i, metadata_value_table_entry);
437 fireTableRowsInserted(i, i);
438 return i;
439 }
440 }
441
442 // Must go at the end of the table
443 metadata_value_table_entries.add(metadata_value_table_entry);
444 fireTableRowsInserted(metadata_value_table_entries.size() - 1, metadata_value_table_entries.size() - 1);
445 return metadata_value_table_entries.size() - 1;
446 }
447
448
449 private void showExtractedMetadataWarning()
450 {
451 Runnable task = new Runnable() {
452 public void run() {
453 WarningDialog dialog = new WarningDialog("warning.ExtractedMetadata", "ExtractedMetadata.Title", Dictionary.get("ExtractedMetadata.Message"), null, false);
454 dialog.display();
455 dialog.dispose();
456 dialog = null;
457 }
458 };
459 SwingUtilities.invokeLater(task);
460 }
461 }
462}
Note: See TracBrowser for help on using the repository browser.