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

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

modified to use teh new SearchMeta

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