PredicateTableModel.java

00001 /* 
00002 ** Author(s): Miguel Calejo
00003 ** Contact:   interprolog@declarativa.com, http://www.declarativa.com
00004 ** Copyright (C) Declarativa, Portugal, 2000-2002
00005 ** Use and distribution, without any warranties, under the terms of the 
00006 ** GNU Library General Public License, readable in http://www.fsf.org/copyleft/lgpl.html
00007 */
00008 package com.declarativa.interprolog.gui;
00009 import com.declarativa.interprolog.*;
00010 import javax.swing.table.*;
00011 import javax.swing.event.*;
00012 import javax.swing.*;
00013 import java.io.Serializable;
00014 
00015 
00016 public class PredicateTableModel implements Serializable, TableModel{
00017         TermModel template;
00018         TermModel[] tuples;
00019         int arity;
00020         String functor;
00021         
00022         public static ObjectExamplePair example(){
00023                 TermModel[] Atuples = new TermModel[1];
00024                 Atuples[0]=new TermModel("c");
00025                 TermModel[] Xtuples = new TermModel[1];
00026                 Xtuples[0] = new TermModel("a");
00027                 PredicateTableModel ptm1 = new PredicateTableModel(null,Xtuples);
00028                 PredicateTableModel ptm2 = new PredicateTableModel(new TermModel("b",Atuples),null);
00029                 return new ObjectExamplePair("PredicateTableModel",ptm1,ptm2);
00030         }
00031         
00032         public PredicateTableModel(TermModel template,TermModel[] tuples){
00033                 if (template == null && tuples==null) 
00034                         throw new RuntimeException("The PredicateTableModel constructor needs a non-null argument");
00035                 if (template == null && tuples.length==0)
00036                         throw new RuntimeException("The PredicateTableModel constructor needs some tuple");
00037                 if (template!=null) {
00038                         arity=template.getChildCount();
00039                         functor=template.node.toString();
00040                 } else {
00041                         arity=tuples[0].getChildCount();
00042                         functor=tuples[0].node.toString();
00043                 }
00044                 if (tuples==null) tuples = new TermModel[0];
00045                 for (int t=0; t<tuples.length; t++) {
00046                         if (tuples[t]==null)
00047                                 throw new RuntimeException("Null tuple in PredicateTableModel tuple "+t);
00048                         if (arity!=tuples[t].getChildCount())
00049                                 throw new RuntimeException("Conflicting arity in PredicateTableModel tuple "+t);
00050                 }
00051                 this.template = template;
00052                 this.tuples = tuples;
00053         }
00054         
00055         public String toString(){
00056                 return functor+"/"+arity+" ("+template+")";
00057         }
00058 
00059         // TableModel methods:
00060         
00061         public void addTableModelListener(TableModelListener l){
00062                 System.out.println("Should add a TableModelListener...");
00063         }
00064         
00065         public void removeTableModelListener(TableModelListener l){
00066                 System.out.println("Should remove a TableModelListener...");
00067         }
00068                 
00069         public Class getColumnClass(int columnIndex){
00070                 return Object.class;
00071         }
00072         
00073         public boolean isCellEditable(int rowIndex,int columnIndex){
00074                 return false;
00075         }
00076         
00077         public void setValueAt(Object aValue,int rowIndex,int columnIndex){
00078                 throw new Error("PredicateTableModel can not be edited!");
00079     }
00080         public String getColumnName(int columnIndex){
00081                 if (template==null) return "Arg"+columnIndex;
00082                 else {
00083                         return template.children[columnIndex].toString();
00084                 }
00085         }
00086         public int getRowCount(){
00087                 return tuples.length;
00088         }
00089         public int getColumnCount(){
00090                 return arity;
00091         }
00092         public Object getValueAt(int row, int column){
00093                 return tuples[row].children[column];
00094         }
00095 }

Generated on Wed Jul 26 13:30:44 2006 for XSB by  doxygen 1.4.5