Copyright (C) 2000, 2001, Geert Vernaeve. All Rights Reserved.
package gve.calc.formula; import java.awt.*; import java.awt.event.*; import awt.Button;
@author Geert Vernaeve
public class InfixBinaryOpView extends HasBaselineContainer implements HasCursorPos,KeydnListener,View,CreatesPopup,ActionListener,MouseSensitive { private InfixBinaryOp model; public InfixBinaryOpView(InfixBinaryOp model,FormulaView view) { this(model,view,new CursorPosLabel(model.getName())); } public InfixBinaryOpView(InfixBinaryOp model,FormulaView view,Component operatorView) { this.model = model; add(view.getView(model.left)); add(operatorView); add(view.getView(model.right)); setLayout(new HorizontalLayoutMgr()); MVC.registerView(model,this); }
Called when menu "Swap Operands" is chosen.
public void updateView(Object with) { FormulaView view = FormulaView.get(this); // Note: we may not do the following: // remove(0); add(getView(left),0); // remove (2); add(getView(right),0); // because remove(0) unlinks the right component // off the tree, so getView(right) would give us null. Component lview = view.getView(model.left); Component rview = view.getView(model.right); // Note that // remove(0); add(lview,0); remove(2); add(lview,2); // also not works. This is because adding lview at position 0 // removes it at the position where it was (being position 2), // so remove(2); would fail. remove(2); remove(0); add(lview,0); add(rview,2); validate(); } public Object getModel() { return model; } public void setCursorPos(int pos) { switch (pos) { case Somewhere_LEFT: FormulaView.get(this).setCursorPart(model.left,Somewhere_LEFT); break; case Somewhere_RIGHT: FormulaView.get(this).setCursorPart(model.right,Somewhere_RIGHT); break; case Somewhere_MYLEFT: getOperatorView().setCursorPos(Somewhere_LEFT); break; case Somewhere_MYRIGHT: getOperatorView().setCursorPos(Somewhere_RIGHT); break; default: getOperatorView().setCursorPos(pos >=0 ? pos : Somewhere); } } public int getCursorPos() { FormulaView view = FormulaView.get(this); Part cursorpart = view.getCursorPart(); if (Part.isDescendantOr(cursorpart,model.left)) return Somewhere_LEFT; if (Part.isDescendantOr(cursorpart,model.right)) return Somewhere_RIGHT; if (cursorpart == model) return getOperatorView().getCursorPos(); return Somewhere; } public void activate() { if (getComponentCount() == 3) getOperatorView().activate(); } public void deactivate() { // While dismantling etc., left or right child could // already be removed. In that case, do nothing if (getComponentCount() == 3) getOperatorView().deactivate(); }
Return the View of the operator itself (i.e. without the left and right arguments). This is our middle child.
HasCursorPos getOperatorView() { return (HasCursorPos)getComponent(1); } public boolean keydn(int key) { return InfixBinaryOpController.keydn(key,this); } public Component createPopup(int x,int y) { Button b = new Button("Swap operands"); b.addActionListener(this); return b; } public void actionPerformed(ActionEvent evt) { String cmd = evt.getActionCommand(); if (cmd.equals("Swap operands")) { Part swap = model.left; model.left = model.right; model.right = swap; MVC.changed(model); } } public boolean mousePressed(int flags,int x,int y) { return false; } public void mouseDown(int flags,int x,int y) { FormulaView view = FormulaView.get(this); int w = getComponent(0).getSize().width + getComponent(1).getSize().width/2; view.setCursorComp(this,x<w ? Somewhere_MYLEFT : Somewhere_MYRIGHT); } }