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. public class StackView extends TabularView { public StackView(Tabular model, FormulaView view) { super(model,view); int h = model.getHeight(); if (h == 0) checklabels(); // Create "Cmd:" stuff else for (int i = 0; i < h; i++) { // We use CursorPosLabels because they implement HasBaseline. add(new CursorPosLabel((h-i)+":"),i*2); } TabularLayout layout = (TabularLayout)getLayout(); layout.setCols(2); } public void updateView(Object with) { super.updateView(with); checklabels(); } public void addRow(int row) { FormulaView view = FormulaView.get(this); Stack model = (Stack)getModel(); int h = model.getHeight(); add(new CursorPosLabel((h-row)+":"),2 * row); add(view.getView(model.elementAt(row)),1 + 2 * row); TabularLayout layout = (TabularLayout)getLayout(); layout.setRows(h); } public void 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()); } private void 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(new CursorPosLabel("Cmd:")); add(new FormulaView(new Formula())); // 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(); } } public boolean 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; } } return super.keydn(key); } }