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

Last change on this file since 8013 was 8013, checked in by mdewsnip, 20 years ago

More new code, this time for the MetadataAuditTable.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1package org.greenstone.gatherer.metadata;
2
3
4import java.awt.*;
5import java.io.*;
6import java.util.*;
7import javax.swing.*;
8import javax.swing.table.*;
9import org.greenstone.gatherer.Configuration;
10import org.greenstone.gatherer.Dictionary;
11import org.greenstone.gatherer.Gatherer;
12import org.greenstone.gatherer.file.FileNode;
13import org.greenstone.gatherer.gui.WarningDialog;
14
15
16public class MetadataValueTableModel
17 extends AbstractTableModel
18{
19 /** The FileNodes this model is built for */
20 private FileNode[] file_nodes = null;
21 /** The list of MetadataValueTableEntries in the table */
22 private ArrayList metadata_value_table_entries = new ArrayList();
23
24 static final private String[] COLUMN_NAMES = { "", Dictionary.get("Metadata.Element"), Dictionary.get("Metadata.Value") };
25
26
27 /** Returns the number of columns in this table. */
28 public int getColumnCount()
29 {
30 return COLUMN_NAMES.length;
31 }
32
33
34 /** Retrieves the name of the specified column. */
35 public String getColumnName(int col)
36 {
37 return COLUMN_NAMES[col];
38 }
39
40
41 /* 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. */
42 public synchronized MetadataValueTableEntry getMetadataValueTableEntry(int row)
43 {
44 if (row >= 0 && row < metadata_value_table_entries.size()) {
45 return (MetadataValueTableEntry) metadata_value_table_entries.get(row);
46 }
47
48 return null;
49 }
50
51
52 /** Returns the number of rows in this table. */
53 public int getRowCount()
54 {
55 return metadata_value_table_entries.size();
56 }
57
58
59 /** Returns the cell value at a given row and column as an Object. */
60 public Object getValueAt(int row, int col)
61 {
62 // Check values are reasonable
63 if (row < 0 || row >= metadata_value_table_entries.size() || col < 0 || col >= COLUMN_NAMES.length) {
64 return null;
65 }
66
67 MetadataValueTableEntry metadata_value_table_entry = (MetadataValueTableEntry) metadata_value_table_entries.get(row);
68 if (col == 0 && metadata_value_table_entry.isInheritedMetadata()) {
69 return metadata_value_table_entry.getFolderMetadataInheritedFrom();
70 }
71
72 if (col == 1) {
73 return metadata_value_table_entry.getMetadataElement();
74 }
75
76 if (col == 2) {
77 return metadata_value_table_entry.getFullValue();
78 }
79
80 return null;
81 }
82
83
84 /** Determine if the given metadata is common to all selected file nodes given the context of the current view. */
85 public boolean isCommon(MetadataValueTableEntry metadata_value_table_entry)
86 {
87 return (file_nodes != null && metadata_value_table_entry.getOccurrences() == file_nodes.length);
88 }
89
90
91 /** Determine if the given metadata is common to all selected file nodes given the context of the current view. */
92 public boolean isCommon(int row)
93 {
94 if (row >= 0 && row < metadata_value_table_entries.size()) {
95 return isCommon((MetadataValueTableEntry) metadata_value_table_entries.get(row));
96 }
97
98 return false;
99 }
100
101
102 public void rebuild(FileNode[] file_nodes)
103 {
104 metadata_value_table_entries.clear();
105
106 if (!Gatherer.c_man.ready()) {
107 Gatherer.println("Not ready!");
108 return;
109 }
110
111 this.file_nodes = file_nodes;
112 if (file_nodes != null && file_nodes.length > 0) {
113 // Create model builder
114 MetadataValueTableModelBuilder builder = new MetadataValueTableModelBuilder();
115 builder.run();
116 }
117 }
118
119
120 private class MetadataValueTableModelBuilder
121 {
122 public void run()
123 {
124 // Build a list of MetadataValueTableEntries that represent the metadata asssigned to the selected files
125 boolean hid_extracted_metadata = false;
126 boolean has_inherited_metadata = false;
127 ArrayList metadata_elements_seen = new ArrayList();
128
129 // Process each of the selected files in turn
130 for (int i = 0; i < file_nodes.length; i++) {
131 File current_file = file_nodes[i].getFile();
132
133 // Get the metadata assigned to this file
134 ArrayList assigned_metadata = MetadataXMLFileManager.getMetadataAssignedToFile(current_file);
135 for (int j = 0; j < assigned_metadata.size(); j++) {
136 MetadataValue metadata_value = (MetadataValue) assigned_metadata.get(j);
137 MetadataElement metadata_element = metadata_value.getMetadataElement();
138
139 // Note if there is inherited (folder-level) metadata in the table
140 has_inherited_metadata = has_inherited_metadata || metadata_value.isInheritedMetadata();
141
142 // Insert this metadata value into the table, unless it already exists (in which case increment its count)
143 insertMetadataValueIntoTable(metadata_value);
144
145 // Remember we have seen this metadata element
146 if (metadata_elements_seen.contains(metadata_element) == false) {
147 metadata_elements_seen.add(metadata_element);
148 }
149 }
150
151 // Get the extracted metadata for this file, if desired
152 if (Gatherer.config.get("general.view_extracted_metadata", Configuration.COLLECTION_SPECIFIC) == true) {
153 ArrayList extracted_metadata = DocXMLFileManager.getMetadataExtractedFromFile(current_file);
154 for (int k = 0; k < extracted_metadata.size(); k++) {
155 MetadataValue metadata_value = (MetadataValue) extracted_metadata.get(k);
156
157 // Insert this metadata value into the table, unless it already exists (in which case increment its count)
158 insertMetadataValueIntoTable(metadata_value);
159 }
160 }
161 }
162
163 // Make sure each non-extracted metadata element appears in the table (even if blank)
164 ArrayList every_metadata_set_element = MetadataSetManager.getEveryMetadataSetElement();
165 for (int i = 0; i < every_metadata_set_element.size(); i++) {
166 MetadataElement metadata_element = (MetadataElement) every_metadata_set_element.get(i);
167
168 // If we haven't seen this metadata element and it isn't extracted, add it now
169 if (!metadata_elements_seen.contains(metadata_element) && !metadata_element.isExtractedMetadata()) {
170 MetadataValueTableEntry metadata_value_table_entry = new MetadataValueTableEntry(metadata_element, new MetadataValueTreeNode(""));
171
172 // Blank metadata is common to all selected files (otherwise it wouldn't be blank!)
173 metadata_value_table_entry.setOccurrences(file_nodes.length);
174
175 // Add it to the table
176 insertMetadataValueIntoTable(metadata_value_table_entry);
177 }
178 }
179
180 // If extracted metadata was hidden, display the warning
181 if (hid_extracted_metadata) {
182 showExtractedMetadataWarning();
183 }
184
185 // If there is inherited metadata, display the warning
186 if (has_inherited_metadata) {
187 showInheritedMetadataWarning();
188 }
189 }
190
191
192 /** Alphabetically inserts the new metadata value into the table */
193 private void insertMetadataValueIntoTable(MetadataValue metadata_value)
194 {
195 for (int i = 0; i < metadata_value_table_entries.size(); i++) {
196 MetadataValueTableEntry current_metadata_value_table_entry = (MetadataValueTableEntry) metadata_value_table_entries.get(i);
197 int c = current_metadata_value_table_entry.compareTo(metadata_value);
198
199 // Insert value before existing entry
200 if (c > 0) {
201 metadata_value_table_entries.add(i, new MetadataValueTableEntry(metadata_value));
202 fireTableRowsInserted(i, i);
203 return;
204 }
205
206 // Entry already exists (increment existing count)
207 if (c == 0) {
208 current_metadata_value_table_entry.anotherOccurrence();
209 return;
210 }
211 }
212
213 // Must go at the end of the table
214 metadata_value_table_entries.add(new MetadataValueTableEntry(metadata_value));
215 fireTableRowsInserted(metadata_value_table_entries.size() - 1, metadata_value_table_entries.size() - 1);
216 }
217
218
219 private void showExtractedMetadataWarning()
220 {
221 Runnable task = new Runnable() {
222 public void run() {
223 WarningDialog dialog = new WarningDialog("warning.ExtractedMetadata", false);
224 dialog.display();
225 dialog.dispose();
226 dialog = null;
227 }
228 };
229 SwingUtilities.invokeLater(task);
230 }
231
232
233 private void showInheritedMetadataWarning()
234 {
235 Runnable task = new Runnable() {
236 public void run() {
237 WarningDialog dialog = new WarningDialog("warning.InheritedMetadata", false);
238 dialog.display();
239 dialog.dispose();
240 dialog = null;
241 }
242 };
243 SwingUtilities.invokeLater(task);
244 }
245 }
246}
Note: See TracBrowser for help on using the repository browser.