Copyright (C) 2000, 2001, Geert Vernaeve. All Rights Reserved.
package gve.calc.formula;
import java.awt.*;
Controls a binary infix operator.
publicclassInfixBinaryOpControllerextendsOperatorController {
publicstaticHasCursorPos getOperatorView(Container comp) {
return (HasCursorPos)comp.getComponent(1);
}
publicstaticboolean keydn(int key,Container comp) {
BinaryOp model = (BinaryOp)((View)comp).getModel();
FormulaView view = FormulaView.get(comp);
switch (key) {
caseKeydnListener.Key_LEFT:
if (comp == view.getCursorComp()) {
// actual cursor pos is stored in middle child
HasCursorPos operatorComp = getOperatorView(comp);
int cp = operatorComp.getCursorPos();
if (cp > 0) {
operatorComp.setCursorPos(cp - 1);
} else {
view.setCursorPart(model.left,HasCursorPos.Somewhere_RIGHT);
}
return true;
} elseif (Part.isDescendantOr(view.getCursorPart(),model.right)) {
// Cannot go more to the left in a (sub)child on the right side
// so move to the middle
view.setCursorComp(comp); // disactivate old cursor
getOperatorView(comp).setCursorPos(HasCursorPos.Somewhere_RIGHT);
return true;
}
return false;
caseKeydnListener.Key_RIGHT:
if (comp == view.getCursorComp()) {
// actual cursor pos is stored in middle child
HasCursorPos operatorComp = getOperatorView(comp);
int cp = operatorComp.getCursorPos();
if (cp < model.getName().length()) {
operatorComp.setCursorPos(cp + 1);
} else {
view.setCursorPart(model.right,HasCursorPos.Somewhere_LEFT);
}
return true;
} elseif (Part.isDescendantOr(view.getCursorPart(),model.left)) {
// Cannot go more to the right in a (sub)child on the left side
// so move to the middle
view.cursorOff(); // disactivate old cursor; may cause the child to recognize operator
Formula f = view.getFormula();
if (!f.contains(model)) { // e.g. the child recognized it was an operator upon deactivate()
if (f.contains(model.right))
// try at least to showe some cursor
view.setCursorPart(model.right);
return true;
}
// Next two lines are obsoleted by the introduction of MYLEFT
// view.setCursorComp(comp); // disactivate old cursor
// getOperatorView(comp).setCursorPos(HasCursorPos.Somewhere_LEFT);
view.setCursorPart(model,HasCursorPos.Somewhere_MYLEFT);
return true;
}
return false;
caseKeydnListener.Key_BS:
if (comp == view.getCursorComp()) {
HasCursorPos operatorComp = getOperatorView(comp);
int cp = operatorComp.getCursorPos();
if (cp == 0) { // push backspace when cursor is at leftmost position in operator name
// => go to left child and push backspace there.
operatorComp.deactivate();
view.setCursorPart(model.left,HasCursorPos.Somewhere_RIGHT);
return ((KeydnListener)view.getCursorComp()).keydn(key);
}
}
}
returnOperatorController.keydn(key,comp); // dismantle operator
}
}