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

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

more modifications for RTL GLI, thanks to Amin Hedjazi

  • Property svn:keywords set to Author Date Id Revision
File size: 10.6 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 model = new SearchMetadataTableModel();
210 setModel(model);
211
212 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
213
214 // set our own cell renderer
215 TableColumnModel column_model = getColumnModel();
216 TableColumn name_column = column_model.getColumn(0);
217 TableColumn value_column = column_model.getColumn(1);
218
219 SearchMetadataTableCellRenderer cell_renderer = new SearchMetadataTableCellRenderer();
220 name_column.setCellRenderer(cell_renderer);
221 value_column.setCellRenderer(cell_renderer);
222 }
223
224 public void refreshModel() {
225 model.refresh();
226 }
227
228 public void stopEditing() {
229 // Save the current value in the text field, then remove the editor so it doesn't get saved again
230 TableCellEditor table_cell_editor = getCellEditor();
231 if (table_cell_editor != null) {
232 table_cell_editor.stopCellEditing();
233 }
234 }
235 }
236
237 private class SearchMetadataTableModel
238 extends AbstractTableModel {
239
240 // the list of items in the table
241 private ArrayList search_metadata_entries = null;
242
243 final private String[] COLUMN_NAMES = {Dictionary.get("CDM.SearchMetadataManager.Component"), Dictionary.get("CDM.SearchMetadataManager.Component_Name")};
244
245 public SearchMetadataTableModel() {
246 refresh();
247 }
248
249 /** Returns the number of columns in this table. */
250 public int getColumnCount() {
251
252 return COLUMN_NAMES.length;
253 }
254
255 /** Retrieves the name of the specified column. */
256 public String getColumnName(int col) {
257
258 return COLUMN_NAMES[col];
259 }
260
261 /** Returns the number of rows in this table. */
262 public int getRowCount() {
263
264 return search_metadata_entries.size();
265 }
266
267 /** Returns the cell value at a given row and column as an Object. */
268 public Object getValueAt(int row, int col) {
269 // Check values are reasonable
270 if (row < 0 || row >= search_metadata_entries.size() || col < 0 || col >= COLUMN_NAMES.length) {
271 return null;
272 }
273
274 SearchMetadataEntry sme = (SearchMetadataEntry) search_metadata_entries.get(row);
275 if (col == 0) {
276 return sme.toString();
277 }
278 if (col == 1) {
279 return sme.getValue();
280 }
281 return null;
282 }
283
284 public boolean isCellEditable(int row, int col) {
285 if (col == 1) {
286 return true;
287 }
288 return false;
289 }
290
291 public void refresh() {
292 search_metadata_entries = getEntries();
293
294 }
295
296 public void setValueAt(Object new_value, int row, int col) {
297 SearchMetadataEntry sme = (SearchMetadataEntry) search_metadata_entries.get(row);
298 String old_value = sme.getValue();
299 if (!new_value.equals(old_value)) {
300 sme.setValue((String)new_value);
301
302 }
303
304 }
305
306 }
307 private static class SearchMetadataTableCellRenderer
308 extends DefaultTableCellRenderer {
309
310 public void setValue(Object value) {
311
312 setText((String)value);
313 }
314 /** Returns the default table cell renderer.
315 * @param table The <strong>JTable</strong>.
316 * @param value The value to assign to the cell at [row, column] as an <strong>Object</strong>.
317 * @param isSelected <i>true</i> if cell is selected.
318 * @param hasFocus <i>true</i> if cell has focus.
319 * @param row The row of the cell to render as an <i>int</i>.
320 * @param column The column of the cell to render as an <i>int</i>.
321 * @return The default table cell renderer <strong>Component</strong>.
322 */
323 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
324 JComponent component = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
325
326 // 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
327 int real_column = table.convertColumnIndexToModel(column);
328 if (real_column == 1 && isSelected) {
329 table.editCellAt(row, column);
330 if (table.isEditing()) {
331 table.getEditorComponent().requestFocus();
332 }
333 }
334
335 // so we can see the background colour
336 component.setOpaque(true);
337
338 // Background
339 if (isSelected) {
340 component.setBackground(Configuration.getColor("coloring.workspace_heading_background", true));
341 }
342 else {
343 if (real_column == 0) {
344 component.setBackground(Configuration.getColor("coloring.collection_heading_background", true));
345 }
346 else {
347 component.setBackground(Configuration.getColor("coloring.collection_tree_background", true));
348 }
349 }
350
351 // The value column of cells never paints focus
352 if (real_column == 1) {
353 component.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
354 }
355
356 return component;
357 }
358
359 }
360
361}
Note: See TracBrowser for help on using the repository browser.