Copyright (C) 2000, 2001, Geert Vernaeve. All Rights Reserved.
package
gve.calc.formula;
import
java.awt.*;
public
class
PrefixUnaryOpView
extends
HasBaselineContainer
implements
HasCursorPos
,
KeydnListener
,
View
,
MouseSensitive
{
private
PrefixUnaryOp
model;
public
PrefixUnaryOpView
(
PrefixUnaryOp
model,
FormulaView
view) { this (model,view,
new
CursorPosLabel
(model.getName())); }
public
PrefixUnaryOpView
(
PrefixUnaryOp
model,
FormulaView
view,Component operatorView) { this.model = model; add(operatorView); add(view.getView(model.child)); setLayout(
new
HorizontalLayoutMgr
()); }
public
void
updateView(Object with) { System.out.println(
"PrefixUnaryOpView.updateView() error"
); }
public
Object getModel() {
return
model; }
public
void
setCursorPos(
int
pos) {
switch
(pos) {
case
Somewhere_LEFT: getOperatorView().setCursorPos(Somewhere_LEFT);
break
;
case
Somewhere_RIGHT:
FormulaView
.get(this).setCursorPart(model.child,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); } }
/* OperatorController expects this method to return a cursor position when the cursor * is on us */
public
int
getCursorPos() {
int
cp = getOperatorView().getCursorPos();
if
(cp >= 0)
return
cp;
return
Somewhere; }
public
void
activate() {}
public
void
deactivate() { getOperatorView().deactivate(); }
HasCursorPos
getOperatorView() {
return
(
HasCursorPos
)getComponent(0); }
public
boolean
keydn(
int
key) {
FormulaView
fview =
FormulaView
.get(this);
HasCursorPos
opView = getOperatorView();
int
cp = opView.getCursorPos();
if
(key == Key_LEFT) {
if
(cp > 0) { opView.setCursorPos(cp - 1);
return
true; }
else
if
(cp == 0) {
return
false; }
else
if
(
Part
.isDescendantOr(fview.getCursorPart(),model.child)) {
// Cannot go more to the left in a (sub)child on the right side
// so move to the middle
fview.setCursorComp(this,Somewhere_LEFT); opView.setCursorPos(model.getName().length());
return
true; }
return
false; }
else
if
(key == Key_RIGHT) {
if
(cp < 0)
return
false;
if
(cp < model.getName().length()) { opView.setCursorPos(cp + 1);
return
true; }
else
{ fview.setCursorPart(model.child,Somewhere_LEFT);
return
true; } }
return
OperatorController
.keydn(key,this); }
public
boolean
mousePressed(
int
flags,
int
x,
int
y) {
return
false; }
// See also InfixBinaryOpView.mouseDown
public
void
mouseDown(
int
flags,
int
x,
int
y) {
FormulaView
view =
FormulaView
.get(this);
int
w = getComponent(0).getSize().width/2; view.setCursorComp(this,x<w ? Somewhere_MYLEFT : Somewhere_MYRIGHT); } }