source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/metadata/MetadataValueTableModel.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 17.3 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
142 if(metadata_value_table_entry == null) {
143 System.err.println("\n@@@@@ MetadataValueTableModel.getValueAt(): metadata_value_table_entry is unexpectedly null!\n");
144 return null;
145 }
146
147 if (col == 0 && metadata_value_table_entry.isInheritedMetadata()) {
148 return metadata_value_table_entry.getFolderMetadataInheritedFrom();
149 }
150
151 if (col == 1) {
152 return metadata_value_table_entry.getMetadataElement();
153 }
154
155 if (col == 2)
156 {
157 return decodeHTMLEntities(metadata_value_table_entry.getFullValue());
158 }
159
160 return null;
161 }
162
163 public String decodeHTMLEntities(String text)
164 {
165 text = text.replace("&amp;#40;", "(");
166 text = text.replace("&amp;#41;", ")");
167 text = text.replace("&amp;#44;", ",");
168 text = text.replace("&amp;#60;", "<");
169 text = text.replace("&amp;#62;", ">");
170 text = text.replace("&amp;#91;", "[");
171 text = text.replace("&amp;#93;", "]");
172 text = text.replace("&amp;#123;", "{");
173 text = text.replace("&amp;#125;", "}");
174 return text;
175 }
176
177 public boolean isCellEditable(int row, int col)
178 {
179 // The inherited and element columns are never editable
180 if (col < 2) {
181 return false;
182 }
183
184 // Extracted and inherited metadata is not editable
185 MetadataValueTableEntry metadata_value_table_entry = (MetadataValueTableEntry) metadata_value_table_entries.get(row);
186 if (metadata_value_table_entry.getMetadataElement().isExtractedMetadataElement() || metadata_value_table_entry.isInheritedMetadata()) {
187 return false;
188 }
189
190 return true;
191 }
192
193
194 /** Determine if the given metadata is common to all selected file nodes. */
195 public boolean isCommon(MetadataValueTableEntry metadata_value_table_entry)
196 {
197 return (file_nodes != null && metadata_value_table_entry.getOccurrences() == file_nodes.length);
198 }
199
200
201 /** Determine if the given metadata is common to all selected file nodes. */
202 public boolean isCommon(int row)
203 {
204 if (row >= 0 && row < metadata_value_table_entries.size()) {
205 return isCommon((MetadataValueTableEntry) metadata_value_table_entries.get(row));
206 }
207
208 return false;
209 }
210
211
212 public void rebuild(CollectionTreeNode[] file_nodes)
213 {
214 this.file_nodes = file_nodes;
215 metadata_value_table_entries.clear();
216
217 // Collection is in a state of flux
218 if (!Gatherer.c_man.ready()) {
219 return;
220 }
221
222 if (file_nodes == null || file_nodes.length == 0) {
223 return;
224 }
225
226 // Create model builder
227 MetadataValueTableModelBuilder builder = new MetadataValueTableModelBuilder();
228 builder.run();
229 }
230
231
232 public void setValueAt(Object new_metadata_value, int row, int col)
233 {
234 MetadataValueTableEntry metadata_value_table_entry = getMetadataValueTableEntry(row);
235 if(metadata_value_table_entry == null) {
236 System.err.println("\n@@@@@ MetadataValueTableModel.setValueAt(): metadata_value_table_entry is unexpectedly null!\n");
237 return;
238 }
239
240 // If nothing has changed no action is necessary
241 String old_metadata_value = metadata_value_table_entry.getFullValue();
242 if (new_metadata_value.equals(old_metadata_value)) {
243 return;
244 }
245
246 // Lock the interface so nothing can be changed while the metadata edit is being processed
247 Gatherer.g_man.wait(true);
248
249
250 // Check for a restricted metadata element
251 MetadataElement metadata_element = metadata_value_table_entry.getMetadataElement();
252 if (!new_metadata_value.equals("") && metadata_element.isRestricted()) {
253 if (metadata_element.getMetadataValueTreeNode((String)new_metadata_value)==null) {
254 WarningDialog dialog = new WarningDialog("warning.InvalidMetadata", Dictionary.get("InvalidMetadata.Title"), Dictionary.get("InvalidMetadata.Message"), null, false);
255 int dialog_result = dialog.display();
256 dialog.dispose();
257 Gatherer.g_man.wait(false);
258 return;
259
260 }
261 }
262
263 // Metadata value added
264 if (old_metadata_value.equals("") && !new_metadata_value.equals("")) {
265 // If we're adding metadata to folders display the warning
266 if (!file_nodes[0].isLeaf()) {
267 WarningDialog dialog = new WarningDialog("warning.DirectoryLevelMetadata", Dictionary.get("DirectoryLevelMetadata.Title"), Dictionary.get("DirectoryLevelMetadata.Message"), null, true);
268 int dialog_result = dialog.display();
269 dialog.dispose();
270 if (dialog_result != JOptionPane.OK_OPTION) {
271 Gatherer.g_man.wait(false);
272 return;
273 }
274 }
275
276 MetadataValueTreeNode metadata_value_tree_node = metadata_element.addMetadataValue((String) new_metadata_value);
277 MetadataValue metadata_value = new MetadataValue(metadata_element, metadata_value_tree_node);
278 metadata_value.setIsAccumulatingMetadata(true);
279 (new AppendMetadataTask(metadata_value)).run();
280 }
281
282 // Metadata value removed
283 else if (!old_metadata_value.equals("") && new_metadata_value.equals("")) {
284 (new RemoveMetadataTask(metadata_value_table_entry)).run();
285 }
286
287 // Metadata value replaced
288 else {
289 MetadataValueTreeNode metadata_value_tree_node = metadata_element.addMetadataValue((String) new_metadata_value);
290 MetadataValue metadata_value = new MetadataValue(metadata_element, metadata_value_tree_node);
291 metadata_value.setIsAccumulatingMetadata(!metadata_value_table_entry.isInheritedMetadata());
292 (new ReplaceMetadataTask(metadata_value_table_entry, metadata_value)).run();
293 }
294 }
295
296
297 private class AppendMetadataTask
298 // extends Thread
299 {
300 private MetadataValue metadata_value = null;
301
302 private AppendMetadataTask(MetadataValue metadata_value)
303 {
304 this.metadata_value = metadata_value;
305 }
306
307 public void run()
308 {
309 try {
310 // Edit metadata.xml files to add the metadata
311 MetadataXMLFileManager.addMetadata(file_nodes, metadata_value);
312 }
313 catch (Exception exception) {
314 // We need to catch any exceptions here so the interface is unlocked below
315 DebugStream.printStackTrace(exception);
316 }
317
318 // Operation finished, so turn busy cursor off and unlock interface
319 Gatherer.g_man.wait(false);
320 }
321 }
322
323
324 private class ReplaceMetadataTask
325 // extends Thread
326 {
327 private MetadataValueTableEntry selected_metadata_value_table_entry = null;
328 private MetadataValue metadata_value = null;
329
330 private ReplaceMetadataTask(MetadataValueTableEntry selected_metadata_value_table_entry, MetadataValue metadata_value)
331 {
332 this.selected_metadata_value_table_entry = selected_metadata_value_table_entry;
333 this.metadata_value = metadata_value;
334 }
335
336 public void run()
337 {
338 try {
339 // Edit metadata.xml files to replace the metadata
340 MetadataXMLFileManager.replaceMetadata(file_nodes, selected_metadata_value_table_entry, metadata_value);
341 }
342 catch (Exception exception) {
343 // We need to catch any exceptions here so the interface is unlocked below
344 DebugStream.printStackTrace(exception);
345 }
346
347 // Operation finished, so turn busy cursor off and unlock interface
348 Gatherer.g_man.wait(false);
349 }
350 }
351
352
353 private class RemoveMetadataTask
354 // extends Thread
355 {
356 private MetadataValueTableEntry selected_metadata_value_table_entry = null;
357
358 private RemoveMetadataTask(MetadataValueTableEntry selected_metadata_value_table_entry)
359 {
360 this.selected_metadata_value_table_entry = selected_metadata_value_table_entry;
361 }
362
363 public void run()
364 {
365 try {
366 // Edit metadata.xml files to remove the metadata
367 MetadataXMLFileManager.removeMetadata(file_nodes, selected_metadata_value_table_entry);
368 }
369 catch (Exception exception) {
370 // We need to catch any exceptions here so the interface is unlocked below
371 DebugStream.printStackTrace(exception);
372 }
373
374 // Operation finished, so turn busy cursor off and unlock interface
375 Gatherer.g_man.wait(false);
376 }
377 }
378
379
380 private class MetadataValueTableModelBuilder
381 {
382 public void run()
383 {
384 // System.err.println("Building MetadataValueTableModel...");
385
386 // Build a list of MetadataValueTableEntries that represent the metadata asssigned to the selected files
387 boolean hid_extracted_metadata = false;
388 ArrayList metadata_elements_seen = new ArrayList();
389
390 // Process each of the selected files in turn
391 for (int i = 0; i < file_nodes.length; i++) {
392 File current_file = file_nodes[i].getFile();
393
394 // Get the metadata assigned to this file
395 ArrayList assigned_metadata = MetadataXMLFileManager.getMetadataAssignedToFile(current_file);
396 for (int j = 0; j < assigned_metadata.size(); j++) {
397 MetadataValue metadata_value = (MetadataValue) assigned_metadata.get(j);
398 MetadataElement metadata_element = metadata_value.getMetadataElement();
399
400 // Insert this metadata value into the table, unless it already exists (in which case increment its count)
401 insertMetadataValue(metadata_value);
402
403 // Remember we have seen this metadata element
404 if (metadata_elements_seen.contains(metadata_element) == false) {
405 metadata_elements_seen.add(metadata_element);
406 }
407 }
408
409 // Get the extracted metadata for this file, if desired
410 if (Configuration.get("general.view_extracted_metadata", Configuration.COLLECTION_SPECIFIC) == true) {
411 ArrayList extracted_metadata = DocXMLFileManager.getMetadataExtractedFromFile(current_file);
412 for (int k = 0; k < extracted_metadata.size(); k++) {
413 MetadataValue metadata_value = (MetadataValue) extracted_metadata.get(k);
414
415 // Insert this metadata value into the table, unless it already exists (in which case increment its count)
416 insertMetadataValue(metadata_value);
417 }
418 }
419 }
420
421 // Make sure each non-extracted metadata element appears in the table (even if blank)
422 ArrayList every_metadata_set_element = MetadataSetManager.getEveryMetadataSetElement();
423 for (int i = 0; i < every_metadata_set_element.size(); i++) {
424 MetadataElement metadata_element = (MetadataElement) every_metadata_set_element.get(i);
425
426 // If we haven't seen this metadata element and it isn't extracted, add it now
427 if (!metadata_elements_seen.contains(metadata_element) && !metadata_element.isExtractedMetadataElement()) {
428 MetadataValueTableEntry metadata_value_table_entry = new MetadataValueTableEntry(metadata_element, new MetadataValueTreeNode(""));
429
430 // Blank metadata is common to all selected files (otherwise it wouldn't be blank!)
431 metadata_value_table_entry.setOccurrences(file_nodes.length);
432
433 // Add it to the table
434 insertMetadataValueTableEntry(metadata_value_table_entry);
435 }
436 }
437
438 // If extracted metadata was hidden, display the warning
439 if (hid_extracted_metadata) {
440 showExtractedMetadataWarning();
441 }
442 }
443
444
445 /** Inserts the new metadata value into the table */
446 private int insertMetadataValue(MetadataValue metadata_value)
447 {
448 return insertMetadataValueTableEntry(new MetadataValueTableEntry(metadata_value));
449 }
450
451
452 /** Inserts the new metadata value table entry into the table */
453 private int insertMetadataValueTableEntry(MetadataValueTableEntry metadata_value_table_entry)
454 {
455 MetadataElement metadata_element = metadata_value_table_entry.getMetadataElement();
456
457 // Find the correct place to insert the table entry
458 for (int i = 0; i < metadata_value_table_entries.size(); i++) {
459 MetadataValueTableEntry current_metadata_value_table_entry = (MetadataValueTableEntry) metadata_value_table_entries.get(i);
460 int element_comparison = MetadataSetManager.compareMetadataElements(current_metadata_value_table_entry.getMetadataElement(), metadata_element);
461
462 // We've found the right element, so check if the value already exists
463 if (element_comparison == 0) {
464 int value_comparison = current_metadata_value_table_entry.compareTo(metadata_value_table_entry);
465 if (value_comparison == 0) {
466 // Entry already exists, so increment count (except for blank entries)
467 if (!metadata_value_table_entry.getFullValue().equals("")) {
468 current_metadata_value_table_entry.anotherOccurrence();
469 }
470 return i;
471 }
472 }
473
474 // Found insertion point
475 if (element_comparison > 0) {
476 metadata_value_table_entries.add(i, metadata_value_table_entry);
477 fireTableRowsInserted(i, i);
478 return i;
479 }
480 }
481
482 // Must go at the end of the table
483 metadata_value_table_entries.add(metadata_value_table_entry);
484 fireTableRowsInserted(metadata_value_table_entries.size() - 1, metadata_value_table_entries.size() - 1);
485 return metadata_value_table_entries.size() - 1;
486 }
487
488
489 private void showExtractedMetadataWarning()
490 {
491 Runnable task = new Runnable() {
492 public void run() {
493 WarningDialog dialog = new WarningDialog("warning.ExtractedMetadata", Dictionary.get("ExtractedMetadata.Title"), Dictionary.get("ExtractedMetadata.Message"), null, false);
494 dialog.display();
495 dialog.dispose();
496 dialog = null;
497 }
498 };
499 SwingUtilities.invokeLater(task);
500 }
501 }
502}
Note: See TracBrowser for help on using the repository browser.