Copyright (C) 2000, 2001, Geert Vernaeve.
package gve.calc.formula; import awt.*; import java.awt.*; public abstract class Tabular extends Part { public Tabular() {} public abstract int getHeight(); public abstract int getWidth();
get element at row i and column j
public abstract Part elementAt(int i,int j); public abstract void setElementAt(int i,int j,Part el); public Part evaluate(Evaluator ev) { return (Part)clone(); } public void replaceChild(Part child,Part byThat) { int w = getWidth(), h = getHeight(); for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) if (elementAt(i,j) == child) { setElementAt(i,j,byThat); return; } }
Returns Point(x=col,y=row) or null
public Point whichChild(Part ch) { int w = getWidth(), h = getHeight(); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (ch == elementAt(i,j)) return new Point(j,i); } } return null; } public Point whichOffspring(Part ch) { while (true) { if (ch == null) return null; if (ch.parent == this) return whichChild(ch); ch = ch.parent; } }
pos is the index of the new to be created row
public void insertRow(int pos) { MVC.changed(this,new TabularModified(pos,true,true)); }
Subclasses overriding this method should do their work and at the end call super.insertCol(pos)
public void insertCol(int pos) { MVC.changed(this,new TabularModified(pos,true,false)); } public void removeCol(int pos) { MVC.changed(this,new TabularModified(pos,false,false)); } public void removeRow(int pos) { MVC.changed(this,new TabularModified(pos,false,true)); } public void writeTable(java.io.Writer w) throws java.io.IOException { int width = getWidth(), height = getHeight(); for (int i = 0; i < height; i++) for (int j = 0; j < width; j++) elementAt(i,j).write(w); }
Utility method for subclasses
public void readTable(java.io.BufferedReader r) throws java.io.IOException, ClassNotFoundException,NoSuchMethodException, java.lang.reflect.InvocationTargetException,IllegalAccessException { int width = getWidth(), height = getHeight(); for (int i = 0; i < height; i++) for (int j = 0; j < width; j++) setElementAt(i,j,Part.read(r)); } public static final Part [] removeRow(Part []array,int pos) { Part [] result = new Part[array.length-1]; for (int i = pos-1; i>=0; i--) result[i] = array[i]; for (int i = array.length-1; i>pos; i--) result[i-1] = array[i]; return result; }
Utility method for subclasses. Returns a new array with an empty row inserted.
public final Part [] insertRow(Part []array,int pos) { Part [] result = new Part[array.length+1]; for (int i = pos-1; i>=0; i--) result[i] = array[i]; for (int i = array.length-1; i>=pos; i--) result[i+1] = array[i]; result[pos] = new Identifier(""); result[pos].parent = this; return result; } public Part getChild(int count) { if (count < 0) return null; int width = getWidth(); int row = count / width; if (row >= getHeight()) return null; return elementAt(row,count % width); } public boolean same(Object o) { if (!(o instanceof Tabular)) return false; Tabular obj = (Tabular)o; int width = getWidth(); int height = getHeight(); if (width != obj.getWidth()) return false; if (height != obj.getHeight()) return false; for (int i = height-1; i>=0; i--) { for (int j = width-1; j>=0; j--) { if (!elementAt(i,j).same(obj.elementAt(i,j))) return false; } } return true; } public Component createView(FormulaView f) { return new TabularView(this,f); } }