package gve.calc.formula;
import java.awt.*;
import awt.Button;
import java.awt.event.*;
publicclassMatrixextendsTabular {
privatePart [][] elements;
publicMatrix(int height,int width) {
if (height <= 0) thrownew Error("invalid matrix height");
if (width <= 0) thrownew Error("invalid matrix width");
elements = newPart[height][width];
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++) {
elements[i][j] = newIdentifier("");
elements[i][j].parent = this;
}
}
publicint getHeight() { return elements.length; }
publicint getWidth() { return elements[0].length; }
publicPart elementAt(int i,int j) { return elements[i][j]; }
publicvoid setElementAt(int i,int j,Part el) { elements[i][j] = el; el.setParent(this); }
public Object clone() {
int h = getHeight();
int w = getWidth();
Matrix result = newMatrix(h,w);
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
result.elements[i][j] = (Part)elements[i][j].clone();
}
return result;
}
publicPart evaluate(Evaluator ev) {
return (Part)clone();
}
pos is the index of the new to be created row
publicvoid insertRow(int pos) {
int w = getWidth(), h = getHeight();
Part [][] newel = newPart[h+1][w];
for (int i = 0; i < h; i++) {
if (i < pos) System.arraycopy(elements[i],0,newel[i],0,w);
else System.arraycopy(elements[i],0,newel[i+1],0,w);
}
elements = newel;
for (int j = 0; j < w; j++) setElementAt(pos,j,newIdentifier());
super.insertRow(pos);
}
publicvoid insertCol(int pos) {
int w = getWidth(), h = getHeight();
Part [][] newel = newPart[h][w+1];
for (int j = 0; j < w; j++) {
int jtarget = j<pos ? j : j+1;
for (int i = 0; i < h; i++) newel[i][jtarget] = elements[i][j];
}
elements = newel;
for (int i = 0; i < h; i++) setElementAt(i,pos,newIdentifier(""));
super.insertCol(pos);
}
publicvoid removeRow(int pos) {
int w = getWidth(), h = getHeight();
Part [][] newel = newPart[h-1][w];
for (int i = 0; i < h; i++) {
if (i < pos) System.arraycopy(elements[i],0,newel[i],0,w);
elseif (i == pos) ;
else System.arraycopy(elements[i],0,newel[i-1],0,w);
}
elements = newel;
super.removeRow(pos);
}
public Component createView(FormulaView view) {
returnnewMatrixView(this,view);
}
}