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

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

made SearchMetadataTableCellRenderer a static class

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