/* The contents of this file are subject to the THDL Open Community License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License on the THDL web site (http://www.thdl.org/). Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific terms governing rights and limitations under the License. The Initial Developer of this software is the Tibetan and Himalayan Digital Library (THDL). Portions created by the THDL are Copyright 2001-2003 THDL. All Rights Reserved. Contributor(s): ______________________________________. */ package org.thdl.tib.bibl; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Hashtable; import java.util.Iterator; import java.util.Observable; import java.util.Vector; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableCellRenderer; /** * This element is a wrapper element for a JTable that sets up two kinds of tables for editing a Tibbibl--one for * displaying various editions and one for displaying alternative readings. * * @author Than Garson, Tibetan and Himalayan Digital Library */ public class TibTable extends Observable implements TibConstants, ActionListener { // Attributes private JTable theTable; private TibTableModel ttm; private org.jdom.Element app, pagination; private IDFactory idf; private Hashtable pageHash; private int type; private boolean editable; private int mainrow; private TibTableCellRenderer ttcr; // Constants public static int EDS_CONSULTED = 0; public static int APP = 1; // Header Constants private static String ED_NAME_HEAD = "Edition Name"; private static String ED_HEAD = "Edition"; private static String VOL_HEAD = "Volume"; private static String TEXT_HEAD = "Text No."; private static String PAGE_HEAD = "Pagination"; private static String READING = "Reading"; private static String ID_HEAD = "Id. No."; private static String IS_MAIN = "Main Reading"; public static String MAINED = "Set Main Edition"; public static String REMOVE_RDG = "Remove Reading"; public static String SUB = "Done"; public static String CANC = "Cancel"; private static String[] DEFAULT_HEADS = {ED_NAME_HEAD, ID_HEAD,VOL_HEAD,PAGE_HEAD}; private static String[] ED_CON_HEADS = {ED_HEAD,TEXT_HEAD,VOL_HEAD,PAGE_HEAD}; private static String[] APP_HEADS = {ED_HEAD,VOL_HEAD,PAGE_HEAD,READING,IS_MAIN}; // Accessor methods public void setTable(JTable jt) { theTable = jt; ttcr = new TibTableCellRenderer(); ttcr.setMainRow(getMainRow()); String st = new String(); jt.setDefaultRenderer(st.getClass(),ttcr); } public JTable getTable() { return theTable; } public void setTibTableModel(TibTableModel ttm) { this.ttm = ttm; } public TibTableModel getTibTableModel() { return ttm; } public void setApp(org.jdom.Element ap) { app = ap; } public org.jdom.Element getApp() { return app; } public void setIDFactory(IDFactory idf) { if(idf == null) {System.out.println("IDF is null in TibTable!");} this.idf = idf; } public IDFactory getIDFactory() { return idf; } public void setPagination(org.jdom.Element page) { pagination = page; pageHash = new Hashtable(); Iterator it = page.getChildren(NUM).iterator(); while(it.hasNext()) { org.jdom.Element numChild = (org.jdom.Element)it.next(); String ed = numChild.getAttributeValue(CORRESP); String pageNums = TibDoc.cleanString(numChild.getText()); pageHash.put(ed,pageNums); } } public org.jdom.Element getPagination() { return pagination; } public Hashtable getPageHash() { return pageHash; } public void setType(int type) { this.type = type; } public int getType() { return type; } public void setMainRow() { int oldrow = getMainRow(); int row = theTable.getSelectedRow(); String firstCell = (String)theTable.getValueAt(row,0); if(firstCell == null || firstCell.equals("")) {return;} setMainRow(row); ttcr.setMainRow(row); ttm.setValueAt(new Boolean(false),oldrow,4); ttm.setValueAt(new Boolean(true),row,4); theTable.repaint(); } public void setMainRow(int mr) { mainrow = mr; } public int getMainRow() { return mainrow; } public String[] getHeaders() { if(type == EDS_CONSULTED) { return ED_CON_HEADS; } else if(type == APP) { return APP_HEADS; } return DEFAULT_HEADS; } // Helper methods /** *
* This is an "overriding" of the setTable(JTable jt) accessor, which simply sets the table variable * to the given parameter. This method on the other hand sets the table * depending on the {@link #type} of TibTable this is. If this TibTable is constructed * solely with an {@link IDFactory}, then it is an {@link #EDS_CONSULTED} type. If it is * constructed with an Apparatus element, IDFactory, and a Pagination element, then it * is an {@link #APP}, or apparatus, type. This method will construct the table model * accordingly. *
*/
public void setTable()
{
if(type == EDS_CONSULTED) {
Vector eds = idf.getEditionsConsulted();
Object[][] tdata = new Object[eds.size()][ED_CON_HEADS.length];
int e = 0;
Iterator it = eds.iterator();
while(it.hasNext()) {
String ed = (String)it.next();
if(ed.equals(NG)) {continue;}
tdata[e][0] = ed;
tdata[e][1] = idf.getTextNum(ed);
tdata[e][2] = idf.getVolNum(ed);
tdata[e][3] = idf.getPagination(ed);
for(int n=0; n<4; n++) {
if(tdata[e][n] == null) {tdata[e][n] = new String("");}
}
e++;
}
setTibTableModel(new TibTableModel(tdata,ED_CON_HEADS));
setTable(new JTable(getTibTableModel()));
} else if (type == APP) {
doAppTable();
}
}
public void doAppTable()
{
Vector eds = new Vector(idf.getEditionsConsulted());
java.util.List readings = getApp().getChildren();
Object[][] tdata = new Object[eds.size()+1][APP_HEADS.length];
for(int r=0;r