Copyright (C) 2001, Geert Vernaeve. All Rights Reserved.
Created 29-jan-2001
package gve.calc.formula;
import java.awt.*;
// PROBLEM:
// We want to add a "Cmd: " item to the stack where the user enters commands.
// This command is to be an editable Part tree that is *not* a child of the Stack model.
// Currently, the system is not designed to handle this kind of stuff.
publicclassStackViewextendsTabularView {
publicStackView(Tabular model, FormulaView view) {
super(model,view);
int h = model.getHeight();
if (h == 0) checklabels(); // Create "Cmd:" stuff
elsefor (int i = 0; i < h; i++) {
// We use CursorPosLabels because they implement HasBaseline.
add(newCursorPosLabel((h-i)+":"),i*2);
}
TabularLayout layout = (TabularLayout)getLayout();
layout.setCols(2);
}
publicvoid updateView(Object with) {
super.updateView(with);
checklabels();
}
publicvoid addRow(int row) {
FormulaView view = FormulaView.get(this);
Stack model = (Stack)getModel();
int h = model.getHeight();
add(newCursorPosLabel((h-row)+":"),2 * row);
add(view.getView(model.elementAt(row)),1 + 2 * row);
TabularLayout layout = (TabularLayout)getLayout();
layout.setRows(h);
}
publicvoid remRow(int row) {
Stack model = (Stack)getModel();
remove(2*row+1); // remove stack element
remove(2*row); // remove label
TabularLayout layout = (TabularLayout)getLayout();
layout.setRows(model.getHeight());
}
privatevoid checklabels() {
Stack model = (Stack)getModel();
int h = model.getHeight();
if (h == 0) { // Special case
if (getComponentCount() == 2) {
CursorPosLabel lab = (CursorPosLabel)getComponent(0);
if (!lab.getText().equals("Cmd:")) {
lab.setText("Cmd:");
lab.invalidate();
}
} else {
TabularLayout layout = (TabularLayout)getLayout();
removeAll();
add(newCursorPosLabel("Cmd:"));
add(newFormulaView(newFormula()));
// add(new CursorPosLabel("???")); // PENDING: must be an IdentifierView ...
layout.setRows(1);
}
return;
}
for (int i = h-1; i>=0; i--) {
CursorPosLabel lab = (CursorPosLabel)getComponent(i*2);
String shouldbe = (h-i)+":";
if (lab.getText().equals(shouldbe)) continue;
lab.setText(shouldbe);
lab.invalidate();
}
}
publicboolean keydn(int key) {
switch (key) {
case '\n': {
Stack model = (Stack)getModel();
FormulaView view = FormulaView.get(this);
int child;
{ Point p = model.whichOffspring(view.getCursorPart());
if (p == null) break;
child = p.y;
}
// if (child == model.getHeight()) break;
model.insertRow(child+1);
view.setCursorPart(model.elementAt(child+1,0));
return true;
}
}
returnsuper.keydn(key);
}
}