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

Last change on this file since 19223 was 19223, checked in by kjdon, 15 years ago

added code to hide the keyword metadata in the index list

  • Property svn:keywords set to Author Date Id Revision
File size: 10.9 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.Dictionary;
35import org.greenstone.gatherer.Gatherer;
36import org.greenstone.gatherer.gui.DesignPaneHeader;
37import org.greenstone.gatherer.util.StaticStrings;
38
39
40public class SearchMetadataManager
41{
42 private Control controls;
43
44 public static final String TYPE_INDEX = "index";
45 public static final String TYPE_LEVEL = "level";
46 public static final String TYPE_PARTITION = "partition";
47 public static final String TYPE_LANGUAGE = "language";
48
49 // the following are special and should not be translated
50 public static final String METADATA_INDEX = "ex.metadata";
51
52 CollectionMetaManager collmeta_manager = CollectionDesignManager.collectionmeta_manager;
53
54 public SearchMetadataManager() {
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
79 /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
80 * @param mode the new mode as an int
81 */
82 public void modeChanged(int mode) {
83 }
84
85 private class DisplayControl
86 extends JPanel
87 implements Control {
88
89 private SearchMetadataTable metadata_table = null;
90
91 public DisplayControl() {
92 super();
93 this.setComponentOrientation(Dictionary.getOrientation());
94 JPanel header_panel = new DesignPaneHeader("CDM.GUI.SearchMetadata", "searchmetadatasettings");
95
96 metadata_table = new SearchMetadataTable();
97
98 JScrollPane scroll_panel = new JScrollPane(metadata_table);
99 scroll_panel.getViewport().setBackground(Configuration.getColor("coloring.collection_tree_background", false));
100 scroll_panel.setOpaque(true);
101
102 JPanel metadata_table_pane = new JPanel();
103 metadata_table_pane.setComponentOrientation(Dictionary.getOrientation());
104 metadata_table_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
105 metadata_table_pane.setLayout(new BorderLayout());
106 metadata_table_pane.add(scroll_panel, BorderLayout.CENTER);
107
108 setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
109 setLayout(new BorderLayout());
110 add(header_panel, BorderLayout.NORTH);
111 add(metadata_table_pane, BorderLayout.CENTER);
112 }
113
114 public void loseFocus(){
115 // save the last metadata in case its still editing
116 metadata_table.stopEditing();
117 }
118
119
120 public void gainFocus(){
121 // refresh the model as we may have changed indexes in the meantime
122 metadata_table.refreshModel();
123 }
124
125 public void destroy() {}
126 }
127
128 private ArrayList getEntries() {
129 ArrayList entries = new ArrayList();
130
131 ArrayList indexes = CollectionDesignManager.index_manager.getIndexes();
132 if (indexes != null) {
133 int indexes_size = indexes.size();
134 for (int i=0; i<indexes_size; i++) {
135 String index_id = ((Index)indexes.get(i)).getID();
136 if (!index_id.equals(METADATA_INDEX)) {
137 SearchMetadataEntry sme = new SearchMetadataEntry(index_id, TYPE_INDEX);
138 entries.add(sme);
139 }
140 }
141 }
142 ArrayList levels = CollectionDesignManager.index_manager.getLevels();
143 if (levels != null) {
144 int levels_size = levels.size();
145 for (int i=0; i<levels_size; i++) {
146 SearchMetadataEntry sme = new SearchMetadataEntry(((IndexOption)levels.get(i)).getName(), TYPE_LEVEL);
147 entries.add(sme);
148 }
149 }
150
151 ArrayList partitions = CollectionDesignManager.subcollectionindex_manager.getSubcollectionIndexes();
152 if (partitions != null) {
153 int partitions_size = partitions.size();
154 for(int i=0; i<partitions_size; i++) {
155 SearchMetadataEntry sme = new SearchMetadataEntry(((SubcollectionIndex)partitions.get(i)).getID(), TYPE_PARTITION);
156 entries.add(sme);
157 }
158 }
159
160 ArrayList languages = CollectionDesignManager.language_manager.getLanguages();
161 if (languages != null) {
162 int languages_size = languages.size();
163 for (int i=0; i<languages_size; i++) {
164 SearchMetadataEntry sme = new SearchMetadataEntry(((Language)languages.get(i)).getCode(), TYPE_LANGUAGE);
165 entries.add(sme);
166 }
167 }
168 return entries;
169 }
170
171
172 private class SearchMetadataEntry {
173
174 String id;
175 String type;
176 CollectionMeta coll_meta = null;
177 String value;
178
179 public SearchMetadataEntry(String id, String type) {
180 this.id = id;
181 this.type = type;
182 coll_meta = collmeta_manager.getMetadatum(getMetaID());
183 value = coll_meta.getValue(false);
184 }
185
186 public String toString() {
187 return Dictionary.get("CDM.SearchMetadataManager.Type_"+type)+": "+id;
188 }
189
190 public String getMetaID() {
191 if (Gatherer.GS3) {
192 // we don't use any dots in gs3
193 return id;
194 }
195 return StaticStrings.STOP_CHARACTER+id;
196
197 }
198 public String getValue() {
199 return value;
200 }
201 public void setValue(String val) {
202 coll_meta.setValue(val);
203 value = val;
204 }
205 }
206
207 private class SearchMetadataTable
208 extends JTable {
209
210 private SearchMetadataTableModel model = null;
211
212 public SearchMetadataTable() {
213 // create the model
214 this.setComponentOrientation(Dictionary.getOrientation());
215
216 model = new SearchMetadataTableModel();
217 setModel(model);
218
219 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
220
221 // set our own cell renderer
222 TableColumnModel column_model = getColumnModel();
223 TableColumn name_column = column_model.getColumn(0);
224 TableColumn value_column = column_model.getColumn(1);
225
226 SearchMetadataTableCellRenderer cell_renderer = new SearchMetadataTableCellRenderer();
227 name_column.setCellRenderer(cell_renderer);
228 value_column.setCellRenderer(cell_renderer);
229 }
230
231 public void refreshModel() {
232 model.refresh();
233 }
234
235 public void stopEditing() {
236 // Save the current value in the text field, then remove the editor so it doesn't get saved again
237 TableCellEditor table_cell_editor = getCellEditor();
238 if (table_cell_editor != null) {
239 table_cell_editor.stopCellEditing();
240 }
241 }
242 }
243
244 private class SearchMetadataTableModel
245 extends AbstractTableModel {
246
247 // the list of items in the table
248 private ArrayList search_metadata_entries = null;
249
250 final private String[] COLUMN_NAMES = {Dictionary.get("CDM.SearchMetadataManager.Component"), Dictionary.get("CDM.SearchMetadataManager.Component_Name")};
251
252 public SearchMetadataTableModel() {
253 refresh();
254 }
255
256 /** Returns the number of columns in this table. */
257 public int getColumnCount() {
258
259 return COLUMN_NAMES.length;
260 }
261
262 /** Retrieves the name of the specified column. */
263 public String getColumnName(int col) {
264
265 return COLUMN_NAMES[col];
266 }
267
268 /** Returns the number of rows in this table. */
269 public int getRowCount() {
270
271 return search_metadata_entries.size();
272 }
273
274 /** Returns the cell value at a given row and column as an Object. */
275 public Object getValueAt(int row, int col) {
276 // Check values are reasonable
277 if (row < 0 || row >= search_metadata_entries.size() || col < 0 || col >= COLUMN_NAMES.length) {
278 return null;
279 }
280
281 SearchMetadataEntry sme = (SearchMetadataEntry) search_metadata_entries.get(row);
282 if (col == 0) {
283 return sme.toString();
284 }
285 if (col == 1) {
286 return sme.getValue();
287 }
288 return null;
289 }
290
291 public boolean isCellEditable(int row, int col) {
292 if (col == 1) {
293 return true;
294 }
295 return false;
296 }
297
298 public void refresh() {
299 search_metadata_entries = getEntries();
300
301 }
302
303 public void setValueAt(Object new_value, int row, int col) {
304 SearchMetadataEntry sme = (SearchMetadataEntry) search_metadata_entries.get(row);
305 String old_value = sme.getValue();
306 if (!new_value.equals(old_value)) {
307 sme.setValue((String)new_value);
308
309 }
310
311 }
312
313 }
314 private static class SearchMetadataTableCellRenderer
315 extends DefaultTableCellRenderer {
316
317 public void setValue(Object value) {
318
319 setText((String)value);
320 }
321 /** Returns the default table cell renderer.
322 * @param table The <strong>JTable</strong>.
323 * @param value The value to assign to the cell at [row, column] as an <strong>Object</strong>.
324 * @param isSelected <i>true</i> if cell is selected.
325 * @param hasFocus <i>true</i> if cell has focus.
326 * @param row The row of the cell to render as an <i>int</i>.
327 * @param column The column of the cell to render as an <i>int</i>.
328 * @return The default table cell renderer <strong>Component</strong>.
329 */
330 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
331 JComponent component = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
332
333 // 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
334 int real_column = table.convertColumnIndexToModel(column);
335 if (real_column == 1 && isSelected) {
336 table.editCellAt(row, column);
337 if (table.isEditing()) {
338 table.getEditorComponent().requestFocus();
339 }
340 }
341
342 // so we can see the background colour
343 component.setOpaque(true);
344
345 // Background
346 if (isSelected) {
347 component.setBackground(Configuration.getColor("coloring.workspace_heading_background", true));
348 }
349 else {
350 if (real_column == 0) {
351 component.setBackground(Configuration.getColor("coloring.collection_heading_background", true));
352 }
353 else {
354 component.setBackground(Configuration.getColor("coloring.collection_tree_background", true));
355 }
356 }
357
358 // The value column of cells never paints focus
359 if (real_column == 1) {
360 component.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
361 }
362
363 return component;
364 }
365
366 }
367
368}
Note: See TracBrowser for help on using the repository browser.