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

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

a few more mods for RTL gli

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