source: main/trunk/gli/src/org/greenstone/gatherer/cdm/SearchMetadataManager.java@ 36242

Last change on this file since 36242 was 36242, checked in by kjdon, 2 years ago

gs2, all index/level/sort search metas have the same type. We need to avoid duplicates, so use a LInkedHashSet (no duplicates, and keeps insertion order)

  • Property svn:keywords set to Author Date Id Revision
File size: 14.0 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Copyright (C) 2006 New Zealand Digital Library Project
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *########################################################################
24 */
25package org.greenstone.gatherer.cdm;
26
27import java.awt.*;
28import java.awt.event.*;
29import javax.swing.*;
30import javax.swing.table.*;
31import java.util.ArrayList;
32import java.util.LinkedHashSet;
33
34import org.greenstone.gatherer.Configuration;
35import org.greenstone.gatherer.DebugStream;
36import org.greenstone.gatherer.Dictionary;
37import org.greenstone.gatherer.Gatherer;
38import org.greenstone.gatherer.gui.DesignPaneHeader;
39import org.greenstone.gatherer.util.StaticStrings;
40
41
42public class SearchMetadataManager
43 extends CollectionMetaManager
44{
45 private Control controls;
46
47 // the following are special and should not be translated
48 public static final String METADATA_INDEX = "ex.metadata";
49
50 //CollectionMetaManager collmeta_manager = CollectionDesignManager.collectionmeta_manager;
51
52 public SearchMetadataManager() {
53 super(CollectionDesignManager.collect_config.getDocumentElement(), StaticStrings.SEARCHMETADATA_ELEMENT, new SearchMeta("", ""));
54 DebugStream.println("SearchMetaManager: " + getSize() + " metadata parsed.");
55 }
56
57 /** Destructor. */
58 public void destroy() {
59 if (controls != null) {
60 controls.destroy();
61 controls = null;
62 }
63 }
64
65 public void loseFocus() {
66 }
67
68 public void gainFocus() {
69 }
70
71 public Control getControls() {
72 if (controls == null) {
73 controls = new DisplayControl();
74 }
75 return controls;
76 }
77
78 public SearchMeta getMetadatum(String name, String type) {
79 return getMetadatum(name, type, true);
80 }
81
82 public SearchMeta getMetadatum(String name, String type, boolean add_if_not_found) {
83 int size = getSize();
84 if (size == 0) {
85 return null;
86 }
87 for(int i = 0; i < size; i++) {
88 SearchMeta metadatum = (SearchMeta) getElementAt(i);
89 if(metadatum.getName().equals(name) && metadatum.getType().equals(type) && metadatum.getLanguage().equals(Configuration.getLanguage())) {
90 //DebugStream.println("Found '" + metadatum + "'");
91 return metadatum;
92 }
93 else {
94 //DebugStream.println("No match with: " + metadatum.getName() + " [l=" + metadatum.getLanguage() + "] \"" + metadatum.getValue() + "\"");
95 }
96 metadatum = null;
97 }
98 if(add_if_not_found) {
99 SearchMeta result = new SearchMeta(name, type);
100 addMetadatum(result);
101 //DebugStream.println("Added new metadata: " + name);
102 return result;
103 }
104 else {
105 return null;
106 }
107 }
108
109 /** Retrieve all of the metadata for the given feature and type */
110 public ArrayList getMetadata(String name, String type) {
111 ArrayList result = new ArrayList();
112 int size = getSize(); // Refresh DOM Model
113 for(int i = 0; i < size; i++) {
114 SearchMeta metadata = (SearchMeta) getElementAt(i);
115 if(metadata.getName().equals(name) && metadata.getType().equals(type)) {
116 result.add(metadata);
117 }
118 }
119 return result;
120 }
121
122
123 /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
124 * @param mode the new mode as an int
125 */
126 public void modeChanged(int mode) {
127 }
128
129 public void removeMetadata(String name, String type) {
130 for(int i = getSize(); i != 0; i--) {
131 SearchMeta other = (SearchMeta) getElementAt(i - 1);
132 if(name.equals(other.getName()) && type.equals(other.getType())) {
133 remove(i - 1);
134 }
135 other = null;
136 }
137 }
138
139 private class DisplayControl
140 extends JPanel
141 implements Control {
142
143 private SearchMetadataTable metadata_table = null;
144
145 public DisplayControl() {
146 super();
147 this.setComponentOrientation(Dictionary.getOrientation());
148 JPanel header_panel = new DesignPaneHeader("CDM.GUI.SearchMetadata", "searchmetadatasettings");
149
150 metadata_table = new SearchMetadataTable();
151
152 JScrollPane scroll_panel = new JScrollPane(metadata_table);
153 scroll_panel.getViewport().setBackground(Configuration.getColor("coloring.collection_tree_background", false));
154 scroll_panel.setOpaque(true);
155
156 JPanel metadata_table_pane = new JPanel();
157 metadata_table_pane.setComponentOrientation(Dictionary.getOrientation());
158 metadata_table_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
159 metadata_table_pane.setLayout(new BorderLayout());
160 metadata_table_pane.add(scroll_panel, BorderLayout.CENTER);
161
162 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
163 setLayout(new BorderLayout());
164 add(header_panel, BorderLayout.NORTH);
165 add(metadata_table_pane, BorderLayout.CENTER);
166 }
167
168 public void loseFocus(){
169 // save the last metadata in case its still editing
170 metadata_table.stopEditing();
171 }
172
173
174 public void gainFocus(){
175 // refresh the model as we may have changed indexes in the meantime
176 metadata_table.refreshModel();
177 }
178
179 public void destroy() {}
180 }
181
182 private ArrayList getEntries() {
183 LinkedHashSet entries = new LinkedHashSet();
184
185 ArrayList indexes = CollectionDesignManager.index_manager.getIndexes();
186 if (indexes != null) {
187 int indexes_size = indexes.size();
188 for (int i=0; i<indexes_size; i++) {
189 String index_id = ((Index)indexes.get(i)).getID();
190 if (!index_id.equals(METADATA_INDEX)) {
191 SearchMetadataEntry sme = new SearchMetadataEntry(index_id, SearchMeta.TYPE_INDEX);
192 entries.add(sme);
193 }
194 }
195 }
196 ArrayList sortfields = CollectionDesignManager.index_manager.getSortFields();
197 if (sortfields != null) {
198 int sortfields_size = sortfields.size();
199 for (int i=0; i<sortfields_size; i++) {
200 String sf_id = ((Index)sortfields.get(i)).getID();
201 SearchMetadataEntry sme = new SearchMetadataEntry(sf_id, SearchMeta.TYPE_SORT);
202 entries.add(sme);
203
204 }
205 }
206 ArrayList facets = CollectionDesignManager.index_manager.getFacets();
207 if (facets != null) {
208 int facets_size = facets.size();
209 for (int i=0; i<facets_size; i++) {
210 String facet_id = ((Index)facets.get(i)).getID();
211 SearchMetadataEntry sme = new SearchMetadataEntry(facet_id, SearchMeta.TYPE_FACET);
212 entries.add(sme);
213 }
214 }
215
216 ArrayList levels = CollectionDesignManager.index_manager.getLevels();
217 if (levels != null) {
218 int levels_size = levels.size();
219 for (int i=0; i<levels_size; i++) {
220 SearchMetadataEntry sme = new SearchMetadataEntry(((IndexOption)levels.get(i)).getName(), SearchMeta.TYPE_LEVEL);
221 entries.add(sme);
222 }
223 }
224
225
226 ArrayList partitions = CollectionDesignManager.subcollectionindex_manager.getSubcollectionIndexes();
227 if (partitions != null) {
228 int partitions_size = partitions.size();
229 for(int i=0; i<partitions_size; i++) {
230 SearchMetadataEntry sme = new SearchMetadataEntry(((SubcollectionIndex)partitions.get(i)).getID(), SearchMeta.TYPE_PARTITION);
231 entries.add(sme);
232 }
233 }
234
235 ArrayList languages = CollectionDesignManager.language_manager.getLanguages();
236 if (languages != null) {
237 int languages_size = languages.size();
238 for (int i=0; i<languages_size; i++) {
239 SearchMetadataEntry sme = new SearchMetadataEntry(((Language)languages.get(i)).getCode(), SearchMeta.TYPE_LANGUAGE);
240 entries.add(sme);
241 }
242 }
243 ArrayList list_entries = new ArrayList();
244 list_entries.addAll(entries);
245 return list_entries;
246 }
247
248
249 private class SearchMetadataEntry {
250
251 String id;
252 String type;
253 SearchMeta coll_meta = null;
254 String value;
255
256 public SearchMetadataEntry(String id, String type) {
257 if (!Gatherer.GS3) {
258 type = SearchMeta.TYPE_SEARCH;
259 }
260 this.id = id;
261 this.type = type;
262 //coll_meta = collmeta_manager.getMetadatum(getMetaID(), type);
263 this.coll_meta = getMetadatum(id, type); //getMetaID(), type);
264 if (this.coll_meta != null) {
265 this.value = coll_meta.getValue(CollectionMeta.GREENSTONE);
266 }
267 }
268
269 public String toString() {
270 return Dictionary.get("CDM.SearchMetadataManager.Type_"+type)+": "+id;
271 }
272 public boolean equals(Object sme) {
273 if (sme instanceof SearchMetadataEntry) {
274 if (id.equals(((SearchMetadataEntry)sme).id) && type.equals(((SearchMetadataEntry)sme).type)) {
275 return true;
276 }
277 return false;
278 }
279 return toString().equals(sme.toString());
280
281 }
282 public String getMetaID() {
283 if (Gatherer.GS3) {
284 // we don't use any dots in gs3
285 return id;
286 }
287 return StaticStrings.STOP_CHARACTER+id;
288
289 }
290 public String getValue() {
291 return value;
292 }
293 public void setValue(String val) {
294 coll_meta.setValue(val);
295 value = val;
296 }
297 }
298
299 private class SearchMetadataTable
300 extends JTable {
301
302 private SearchMetadataTableModel model = null;
303
304 public SearchMetadataTable() {
305 // create the model
306 this.setComponentOrientation(Dictionary.getOrientation());
307
308 model = new SearchMetadataTableModel();
309 setModel(model);
310
311 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
312
313 // set our own cell renderer
314 TableColumnModel column_model = getColumnModel();
315 TableColumn name_column = column_model.getColumn(0);
316 TableColumn value_column = column_model.getColumn(1);
317
318 SearchMetadataTableCellRenderer cell_renderer = new SearchMetadataTableCellRenderer();
319 name_column.setCellRenderer(cell_renderer);
320 value_column.setCellRenderer(cell_renderer);
321 }
322
323 public void refreshModel() {
324 model.refresh();
325 }
326
327 public void stopEditing() {
328 // Save the current value in the text field, then remove the editor so it doesn't get saved again
329 TableCellEditor table_cell_editor = getCellEditor();
330 if (table_cell_editor != null) {
331 table_cell_editor.stopCellEditing();
332 }
333 }
334 }
335
336 private class SearchMetadataTableModel
337 extends AbstractTableModel {
338
339 // the list of items in the table
340 private ArrayList search_metadata_entries = null;
341
342 final private String[] COLUMN_NAMES = {Dictionary.get("CDM.SearchMetadataManager.Component"), Dictionary.get("CDM.SearchMetadataManager.Component_Name")};
343
344 public SearchMetadataTableModel() {
345 refresh();
346 }
347
348 /** Returns the number of columns in this table. */
349 public int getColumnCount() {
350
351 return COLUMN_NAMES.length;
352 }
353
354 /** Retrieves the name of the specified column. */
355 public String getColumnName(int col) {
356
357 return COLUMN_NAMES[col];
358 }
359
360 /** Returns the number of rows in this table. */
361 public int getRowCount() {
362
363 return search_metadata_entries.size();
364 }
365
366 /** Returns the cell value at a given row and column as an Object. */
367 public Object getValueAt(int row, int col) {
368 // Check values are reasonable
369 if (row < 0 || row >= search_metadata_entries.size() || col < 0 || col >= COLUMN_NAMES.length) {
370 return null;
371 }
372
373 SearchMetadataEntry sme = (SearchMetadataEntry) search_metadata_entries.get(row);
374 if (col == 0) {
375 return sme.toString();
376 }
377 if (col == 1) {
378 return sme.getValue();
379 }
380 return null;
381 }
382
383 public boolean isCellEditable(int row, int col) {
384 if (col == 1) {
385 return true;
386 }
387 return false;
388 }
389
390 public void refresh() {
391 search_metadata_entries = getEntries();
392
393 }
394
395 public void setValueAt(Object new_value, int row, int col) {
396 SearchMetadataEntry sme = (SearchMetadataEntry) search_metadata_entries.get(row);
397 String old_value = sme.getValue();
398 if (!new_value.equals(old_value)) {
399 sme.setValue((String)new_value);
400
401 }
402
403 }
404
405 }
406 private static class SearchMetadataTableCellRenderer
407 extends DefaultTableCellRenderer {
408
409 public void setValue(Object value) {
410
411 setText((String)value);
412 }
413 /** Returns the default table cell renderer.
414 * @param table The <strong>JTable</strong>.
415 * @param value The value to assign to the cell at [row, column] as an <strong>Object</strong>.
416 * @param isSelected <i>true</i> if cell is selected.
417 * @param hasFocus <i>true</i> if cell has focus.
418 * @param row The row of the cell to render as an <i>int</i>.
419 * @param column The column of the cell to render as an <i>int</i>.
420 * @return The default table cell renderer <strong>Component</strong>.
421 */
422 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
423 JComponent component = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
424
425 // real_column is the column in the model, column is the column in the table - may be different if the user has moved the columns around
426 int real_column = table.convertColumnIndexToModel(column);
427 if (real_column == 1 && isSelected) {
428 table.editCellAt(row, column);
429 if (table.isEditing()) {
430 table.getEditorComponent().requestFocus();
431 }
432 }
433
434 // so we can see the background colour
435 component.setOpaque(true);
436
437 // Background
438 if (isSelected) {
439 component.setBackground(Configuration.getColor("coloring.workspace_heading_background", true));
440 }
441 else {
442 if (real_column == 0) {
443 component.setBackground(Configuration.getColor("coloring.collection_heading_background", true));
444 }
445 else {
446 component.setBackground(Configuration.getColor("coloring.collection_tree_background", true));
447 }
448 }
449
450 // The value column of cells never paints focus
451 if (real_column == 1) {
452 component.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
453 }
454
455 return component;
456 }
457
458 }
459
460}
Note: See TracBrowser for help on using the repository browser.