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

Last change on this file since 12095 was 12073, checked in by kjdon, 18 years ago

a new manager for search form menu item metadata - i.e. all the '.' collection metadata

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