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