/**
*#########################################################################
*
* A component of the Gatherer application, part of the Greenstone digital
* library suite from the New Zealand Digital Library Project at the
* University of Waikato, New Zealand.
*
*
*
* Author: Shaoqun Wu, Greenstone Digital Library, University of Waikato
*
*
*
* Copyright (C) 2006 New Zealand Digital Library Project
*
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*########################################################################
*/
package org.greenstone.gatherer.gems;
import javax.swing.table.*;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.awt.Rectangle;
import javax.swing.JOptionPane;
import javax.swing.DefaultCellEditor;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import org.greenstone.gatherer.Dictionary;
public class AttributeTable
extends JTable
implements MetadataElementListener, MetadataSetListener, ActionListener {
// self reference
JTable this_table;
private ArrayList listeners = new ArrayList();
private JTextField metadata_value_text_field = new JTextField();
private boolean language_dependent;
public AttributeTable(boolean lang_dependent){
super();
setRowHeight(20);
this.setComponentOrientation(Dictionary.getOrientation());
metadata_value_text_field.setComponentOrientation(Dictionary.getOrientation());
language_dependent = lang_dependent;
metadata_value_text_field.setBorder(null);
DefaultCellEditor cellEditor = new DefaultCellEditor(metadata_value_text_field);
cellEditor.setClickCountToStart(1);
setDefaultEditor(String.class,cellEditor);
this_table = this;
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
/* not currently used */
public void actionPerformed(ActionEvent e){
String command = e.getActionCommand();
if (command.equals(GEMSConstants.ADD_ATTRIBUTE)){
addAttribute();
return ;
}
if (command.equals(GEMSConstants.DELETE_ATTRIBUTE)){
deleteAttribute();
}
}
public void addNewLanguage(String lang) {
AttributeTableModel model = (AttributeTableModel)getModel();
model.addNewLanguage(lang);
}
public void metadataElementChanged(MetadataElementEvent mee){
MetadataElementModel model = mee.getMetadataElementModel();
if (model == null){ //this model has been deleted
setModel(new DefaultTableModel());
return;
}
populateAttributeTable(model);
}
public void metadataSetChanged(MetadataSetEvent mse){
MetadataSetInfo info = mse.getMetadataSetInfo();
if (info == null){
setModel(new DefaultTableModel());
return;
}
populateAttributeTable(info);
}
public void addAttributeListener(AttributeListener al){
if (!listeners.contains(al)){
listeners.add(al);
}
}
public void removeAttributeListener(AttributeListener al){
listeners.remove(al);
}
private void addAttribute(){
AttributeTableModel model = (AttributeTableModel)getModel();
model.addNewRow();
}
private void deleteAttribute(){
int index = getSelectedRow();
AttributeTableModel model = (AttributeTableModel)getModel();
model.deleteRow(index);
}
private void populateAttributeTable(AttributeContainer container){
AttributeTableModel model = new AttributeTableModel(container, language_dependent);
setModel(model);
TableColumnModel tcm = getColumnModel();
if (language_dependent) {
tcm.getColumn(0).setPreferredWidth(100);
tcm.getColumn(1).setPreferredWidth(50);
tcm.getColumn(2).setPreferredWidth(350);
}
else {
tcm.getColumn(0).setPreferredWidth(100);
tcm.getColumn(1).setPreferredWidth(400);
}
}
private void notifyListeners(Attribute attr){
AttributeEvent ae = new AttributeEvent(attr);
for(int i=0;i