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