package
gve.calc.formula;
import
java.awt.*;
Controls an operator.
public
class
OperatorController
{
Note that this method has much in common with IdentifierView.keydn()
public
static
boolean
keydn(
int
key,Container comp) {
Operator
model = (
Operator
)((
View
)comp).getModel();
FormulaView
view =
FormulaView
.get(comp);
int
cp = ((
HasCursorPos
)comp).getCursorPos(); String str = model.getName();
switch
(key) {
case
KeydnListener
.Key_BS:
if
(comp == view.getCursorComp()) {
if
(comp
instanceof
HasCursorPos
) { str = str.substring(0,cp-1) + str.substring(cp);
Identifier
ident; view.setCursorPart(ident = model.dismantle(str,view.getFormula()),cp-1);
if
(ident.getParent()
instanceof
OperatorSpace
) {
OperatorSpace
spat = (
OperatorSpace
)ident.getParent(); spat.recognizeOp(view); } } }
return
true;
case
KeydnListener
.Key_DEL:
if
(cp != str.length()) { str = str.substring(0,cp) + str.substring(cp+1); view.setCursorPart(model.dismantle(str,view.getFormula()),cp); }
return
true;
default
:
if
(key < 32)
return
false; str =
new
String(
new
StringBuffer(str).insert(cp,(
char
)key));
if
(str.equals(
" "
)) {
if
(key ==
' '
) str =
" "
;
/* e.g. typed "x y", cursor on OperatorSpace, then press Space again (6-dec-2000 bug) */
else
str =
""
; cp = -1; }
// Dismantle operator
Identifier
ident; view.setCursorPart(ident = model.dismantle(str,view.getFormula()),cp+1);
// Maybe a new operator name has been formed; recognize
// In case of a n-ary operator, dismantle() has added
// n OperatorSpaces as parent, parent's parent, ... of
// the Identifier. We suppose that n==1 or n==2
Part
par = ident.getParent();
Part
parpar = ident.getParent();
if
(par
instanceof
OperatorSpace
) {
OperatorSpace
spat = (
OperatorSpace
)par;
if
(spat.recognizeOp(view.getFormula(),view)) parpar = null; }
if
(parpar
instanceof
OperatorSpace
) {
OperatorSpace
spat = (
OperatorSpace
)parpar; spat.recognizeOp(view.getFormula(),view); }
return
true; } } }