Back to index

wims  3.65+svn20090927
DataTableModel.java
Go to the documentation of this file.
00001 /*
00002     WIMSchem Elements: Chemistry molecular diagram drawing tool.
00003     
00004     (c) 2008 Dr. Alex M. Clark
00005     
00006     Released as GNUware, under the Gnu Public License (GPL)
00007     
00008     See www.gnu.org for details.
00009 */
00010 
00011 package WIMSchem.ds;
00012 
00013 import WIMSchem.*;
00014 
00015 import java.util.*;
00016 import java.awt.*;
00017 import java.awt.event.*;
00018 import javax.swing.*;
00019 import javax.swing.table.*;
00020 import javax.swing.border.*;
00021 
00022 /*
00023     Data model to reconcile a DataSheet instance with the editing performed by a JTable component.
00024 */
00025 
00026 public class DataTableModel extends AbstractTableModel 
00027 {
00028     // Class implemented specially to draw molecules on a table.
00029 
00030     class MoleculeRenderer extends EditorPane implements TableCellRenderer
00031     {
00032        Border focusBorder=null;
00033 
00034        public MoleculeRenderer() 
00035        {
00036             setOpaque(true);
00037 
00038            SetEditable(false);
00039            SetAutoScale(true);
00040        }
00041 
00042        public Component getTableCellRendererComponent(JTable table,Object mol,boolean isSelected,boolean hasFocus,int row,int col)
00043        {
00044            if (mol!=null) this.Replace((Molecule)mol); else this.Clear();
00045 
00046            setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
00047 
00048            //SetBorder(hasFocus);
00049            if (hasFocus) 
00050            {
00051               if (focusBorder==null) 
00052                   focusBorder=BorderFactory.createMatteBorder(1,1,1,1,table.getSelectionBackground().darker());
00053               setBorder(focusBorder);
00054            }
00055            else setBorder(null);
00056 
00057            return this;
00058        }
00059     }
00060     public MoleculeRenderer NewMoleculeRenderer() {return new MoleculeRenderer();}
00061     
00062     // Class implemented for editing of molecules: just sends an event notice to the parent, rather than actually doing the work.
00063     
00064     class MoleculeEditor extends AbstractCellEditor implements TableCellEditor
00065     {
00066        ActionListener actlist;
00067     
00068        public MoleculeEditor(ActionListener ActList)
00069        {
00070            actlist=ActList;
00071        }
00072        
00073        public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected,int row,int col) 
00074        {
00075            actlist.actionPerformed(new ActionEvent(table,0,row+","+col));
00076            fireEditingStopped();
00077            return null;
00078        }
00079        public Object getCellEditorValue() {return null;}
00080     }
00081     //JOptionPane.showMessageDialog(null,e.toString(),"Clipboard Read Failed",JOptionPane.ERROR_MESSAGE);
00082     public MoleculeEditor NewMoleculeEditor(ActionListener ActList) {return new MoleculeEditor(ActList);}
00083 
00084     // main implementation
00085 
00086     DataSheet ds=null;
00087     DataSheetCache cache=null;
00088     int temporaryEdit=-1;
00089 
00090     public DataTableModel(DataSheet DS,DataSheetCache Cache) {ds=DS; cache=Cache;}
00091     public void SetDataSheet(DataSheet DS,DataSheetCache Cache) {ds=DS; cache=Cache;}
00092     
00093     public void SetTemporaryEdit(int Col) {temporaryEdit=Col;}
00094     public void ClearTemporaryEdit() {temporaryEdit=-1;}
00095     
00096     public int getColumnCount() {return ds.NumCols();}
00097     public int getRowCount() {return ds.NumRows();}
00098     public String getColumnName(int col) {return ds.ColName(col);}
00099     public Object getValueAt(int row,int col) 
00100     {
00101        int t=ds.ColType(col);
00102        if (ds.IsNull(row,col))
00103        {
00104            if (t==DataSheet.COLTYPE_MOLECULE) return null;
00105            if (t==DataSheet.COLTYPE_BOOLEAN) return new Boolean(false); // (no UI concept of null)
00106            return ""; // null
00107        }
00108        else
00109        {
00110            if (t==DataSheet.COLTYPE_MOLECULE) return ds.GetMolecule(row,col);
00111            if (t==DataSheet.COLTYPE_STRING) return ds.GetString(row,col);
00112            if (t==DataSheet.COLTYPE_INTEGER) return new Integer(ds.GetInteger(row,col));
00113            if (t==DataSheet.COLTYPE_REAL) return new Double(ds.GetReal(row,col));
00114            if (t==DataSheet.COLTYPE_BOOLEAN) return new Boolean(ds.GetBoolean(row,col));
00115        }
00116        return "";
00117     }
00118     public Class getColumnClass(int col) 
00119     {
00120        int t=ds.ColType(col);
00121        if (t==DataSheet.COLTYPE_MOLECULE) return new Molecule().getClass();
00122        if (t==DataSheet.COLTYPE_STRING) return new String("").getClass();
00123        if (t==DataSheet.COLTYPE_INTEGER) return new String("").getClass();
00124        if (t==DataSheet.COLTYPE_REAL) return new String("").getClass();
00125        if (t==DataSheet.COLTYPE_BOOLEAN) return new Boolean(false).getClass();
00126        return new String("").getClass();
00127     }
00128     public boolean isCellEditable(int row,int col) 
00129     {
00130        // (molecule only) return temporaryEdit==col || ds.ColType(col)!=DataSheet.COLTYPE_MOLECULE;
00131        return temporaryEdit==col;
00132     }
00133     public void setValueAt(Object val,int row,int col) 
00134     {
00135        int t=ds.ColType(col);
00136        try
00137        {
00138            cache.CacheUndo(ds);
00139        
00140            if (t==DataSheet.COLTYPE_MOLECULE) ds.SetMolecule(row,col,(Molecule)val);
00141            else if (t==DataSheet.COLTYPE_STRING) ds.SetString(row,col,(String)val);
00142            else if (t==DataSheet.COLTYPE_INTEGER) 
00143            {
00144               String str=(String)val;
00145               if (str.length()==0) ds.SetToNull(row,col);
00146               else ds.SetInteger(row,col,Integer.parseInt(str));
00147            }
00148            else if (t==DataSheet.COLTYPE_REAL) 
00149            {
00150               String str=(String)val;
00151               if (str.length()==0) ds.SetToNull(row,col);
00152               else ds.SetReal(row,col,Double.parseDouble(str));
00153            }
00154            else if (t==DataSheet.COLTYPE_BOOLEAN) ds.SetBoolean(row,col,((Boolean)val).booleanValue());
00155            
00156            ds.SetDirty();
00157        }
00158        catch (NumberFormatException ex) {} // (do nothing --> change gets rejected)
00159     }
00160     
00161     // normally implemented in JTable, but passed along to this class; returns default editing/rendering properties for unit datatypes,
00162     // and a special one for molecules...
00163     public TableCellRenderer getCellRenderer(int row,int col,TableCellRenderer def) 
00164     {
00165        if (ds.ColType(col)==DataSheet.COLTYPE_MOLECULE)
00166        {
00167            // !! if column height is set to one-line, use a 'molecular formula' renderer...
00168            return new MoleculeRenderer();
00169        }
00170        
00171        return def;
00172     }
00173 }
00174 
00175 
00176