package gve.calc.logic; import awt.*; import java.awt.*; import awt.Button; import java.awt.event.*; import gve.calc.formula.*; public class TableauPartView extends Container implements View,KeydnListener,HasCursorPos,CreatesPopup,ActionListener { private TableauPart model; public TableauPartView(TableauPart model,FormulaView view) { this.model = model; add(view.getView(model.top)); add(view.getView(model.left)); if (model.right != null) add(view.getView(model.right)); setLayout(new TableauPartLayout()); MVC.registerView(model,this); } public Object getModel() { return model; } public void updateView(Object with) { removeAll(); FormulaView view = FormulaView.get(this); add(view.getView(model.top)); add(view.getView(model.left)); if (model.right != null) add(view.getView(model.right)); validate(); } public void setCursorPos(int pos) { FormulaView view = FormulaView.get(this); switch (pos) { case Somewhere_TOP: view.setCursorPart(model.top); break; case Somewhere_BOTTOM: view.setCursorPart(model.left); break; default: break; } } public int getCursorPos() { return 0; } public void activate() {} public void deactivate() {} public void paint(Graphics g) { super.paint(g); Dimension siz = getSize(); Dimension tsiz = getComponent(0).getSize(); int y = tsiz.height; System.out.println("TableauPart "+siz+" top "+tsiz); if (getComponentCount() == 2) { g.drawLine(siz.width/2,y,siz.width/2,y+10); } else { Dimension lsiz = getComponent(1).getSize(); Dimension rsiz = getComponent(2).getSize(); g.drawLine(siz.width/2,y,siz.width/2,y+5); g.drawLine(lsiz.width/2,y+5,siz.width-rsiz.width/2,y+5); g.drawLine(lsiz.width/2,y+5,lsiz.width/2,y+10); g.drawLine(siz.width-rsiz.width/2,y+5,siz.width-rsiz.width/2,y+10); } } public boolean keydn(int key) { FormulaView fview = FormulaView.get(this); switch (key) { case Key_DOWN: if (Part.isDescendantOr(fview.getCursorPart(),model.top)) { fview.setCursorPart(model.left,Somewhere_TOP); return true; } break; case Key_UP: if (Part.isDescendantOr(fview.getCursorPart(),model.left)) { fview.setCursorPart(model.top,Somewhere_BOTTOM); return true; } else if (model.right!=null && Part.isDescendantOr(fview.getCursorPart(),model.right)) { fview.setCursorPart(model.top,Somewhere_BOTTOM); return true; } break; case Key_BS: if (fview.getCursorPart()==model.left && Part.isEmpty(model.left)) { // delete left branch Formula f = fview.getFormula(); Identifier dummy = new Identifier(); f.replace(model,dummy); if (model.right == null) { f.replace(dummy,model.top); } else { f.replace(dummy,new TableauPart(model.top,model.right)); } fview.setCursorPart(model.top); return true; } break; } return false; } public Component createPopup(int x,int y) { Button b; { String name = null; if (model.right == null) name = "Make double branch"; else if (Part.isEmpty(model.left) || Part.isEmpty(model.right)) name = "Make single branch"; if (name != null) { b = new Button(name); b.addActionListener(this); return b; } } return null; } public void actionPerformed(ActionEvent evt) { String cmd = evt.getActionCommand(); FormulaView view = FormulaView.get(this); Formula formula = view.getFormula(); if (cmd.equals("Make double branch")) { model.right = new Identifier(""); model.right.setParent(model); MVC.changed(model); view.setCursorPart(model.right); } else if (cmd.equals("Make single branch")) { if (Part.isEmpty(model.left)) { // Remove left branch if (model.right != null) { if (Part.isDescendantOr(view.getCursorPart(),model.left)) view.setCursorPart(model.right); model.left = model.right; model.right = null; MVC.changed(model); } else { if (Part.isDescendantOr(view.getCursorPart(),model.left)) view.setCursorPart(model.top); formula.replace(model.left,new Identifier("")); } } else if (Part.isEmpty(model.right)) { // Remove right branch if (Part.isDescendantOr(view.getCursorPart(),model.right)) view.setCursorPart(model.top); model.right = null; MVC.changed(model); } } } }