package text;
import org.popsul.util.Key;
import org.popsul.util.Split;
import org.popsul.util.Replace;
import java.util.Vector;
import java.util.Enumeration;
import javax.microedition.lcdui.*;
class EditorCanvas extends Canvas{
public static final int ENGLISH_SMALL = 0x0000;
public static final int ENGLISH_BIG = 0x0001;
Vector text = null;
int startLine, curx, cury = 0;
//int MARGIN = 22;
int x = 0;// = MARGIN;
Font f;// = Font.getDefaultFont();
int fontH;// = f.getHeight();
int widthSymScreen = 0;
int hscrol = 0;
int maxLines, maxChars = 0;
int sWidth;// = this.getWidth();
int sHeight;// = this.getHeight();
int resStart, resEnd = 0;
int currentSearchPosition = 0;
int numberWidth = 0;
Editor parent = null;
EditorFont efont = null;
String _dkey = null;
String _searchString = null;
boolean enteredLine = false;
boolean lineExists = false;
String enterLine = "";
int currentLayOut = EditorCanvas.ENGLISH_SMALL;
public EditorCanvas(String text, Editor parent){
this.parent = parent;
this.setText(text, false);
this.setFullScreenMode(true);
this.efont = new EditorFont();
sWidth = this.getWidth();
sHeight = this.getHeight();
this.setFont(efont.getCurrent());
}
public void paint(Graphics g){
int y, len, sw, xmarg = 0;
int MARGIN = this.calculateWidth();
int cux = x + MARGIN;
y = 12;
String _t, _s = null;
g.setFont(f);
g.setColor(0xffffff);
g.fillRect(0, 0, sWidth, sHeight);
g.setColor(0x000000);
char[] sym = null;
for(int i = startLine, k = 0; i < text.size() && k < this.maxLines; i++, k++){
_t = text.elementAt(i).toString();
if(cury == i){
len = _t.length();
sym = new char[len+1];
sym[len] = ' ';
_t.getChars(0, len, sym, 0);
g.setColor(0xdddddd);
g.fillRect(0, y, sWidth, fontH);
g.setColor(0x000000);
for(int j = 0; j < sym.length; j++){
_s = Editor.A(sym[j]);
sw = f.stringWidth(_s);
if(this.curx == j){
g.setColor(0xff0000);
g.drawString("|", (cux + xmarg)-1, y, g.LEFT | g.TOP);
g.setColor(0x000000);
xmarg++;
}
g.drawString(_s, cux + xmarg, y, g.LEFT | g.TOP);
xmarg += sw;
}
} else {
g.drawString(_t, cux, y, g.LEFT | g.TOP);
}
y += fontH;
}
g.setColor(0xddeedd);
g.fillRect(0, 0, MARGIN, sHeight);
g.setColor(0x000000);
y = 12;
for(int i = 0; i < this.maxLines; i++){
g.drawString(ts(this.startLine + i + 1), 0, y, g.LEFT | g.TOP);
y += fontH;
}
if(this._dkey != null){
g.drawString(_dkey, 2, y, g.LEFT | g.TOP);
}
if(enteredLine){
if(lineExists){
g.setColor(0x00ff00);
} else {
g.setColor(0xff0000);
}
g.drawRect(sWidth - 2 - MARGIN, sHeight - 2 - fontH, MARGIN, fontH);
g.setColor(0x000000);
g.drawString(this.enterLine, sWidth - MARGIN, sHeight - fontH, g.LEFT | g.TOP);
}
}
public void setText(String text, boolean repainted){
this.curx = 0;
this.cury = 0;
this.text = null;
System.gc();
text = Replace.string(text, "\r\n", "\n");
text = Replace.string(text, "\r", "\n");
this.text = Split.string(text, "\n");
if(repainted)
this.repaint();
}
public String getText(){
if(this.text == null){
return null;
}
return this.getString(0, this.text.size());
}
private void enter(int k){
int n = -1;
if(Key.is0(k))
n = 0;
else if(Key.is1(k))
n = 1;
else if(Key.is2(k))
n = 2;
else if(Key.is3(k))
n = 3;
else if(Key.is4(k))
n = 4;
else if(Key.is5(k))
n = 5;
else if(Key.is6(k))
n = 6;
else if(Key.is7(k))
n = 7;
else if(Key.is8(k))
n = 8;
else if(Key.is9(k))
n = 9;
else if(Key.isPound(k)){
this.enteredLine = false;
} else if(Key.isLeftSoft(k) || Key.isGreen(k)){
if(this.enterLine.length() < 1){
return;
} else {
this.enterLine = this.enterLine.substring(0, this.enterLine.length() - 1);
}
}
if(n != -1 && this.enterLine.length() < 4){
this.enterLine = this.enterLine.concat(ts(n));
}
int line = -1;
try{
line = Integer.parseInt(this.enterLine);
line--;
}catch(Exception e){}
if(line < 0){
this.lineExists = false;
} else if(line > this.text.size() - 1){
this.lineExists = false;
} else {
lineExists = true;
this.startLine = line;
this.cury = line;
}
this.repaint();
}
/* QWERTY ONLY */
void edit(int key){
char _x = (char)0x00;
if(Key.isBackspace(key)){
this.deleteChar();
} else if((_x = getCharFromKey(key)) != 0x00){
this.insertChar(_x);
}
}
void deleteChar(){
String _tmp = null;
String _tmp2 = null;
int _l = 0;
if(curx == 0 && cury == 0){
return;
}
if(curx == 0){
_tmp = this.text.elementAt(cury - 1).toString();
_l = _tmp.length();
_tmp += this.text.elementAt(cury).toString();
this.text.removeElementAt(cury);
this.text.setElementAt(_tmp, cury - 1);
cury--;
curx = _l;
} else {
_tmp = this.text.elementAt(cury).toString();
_tmp2 = _tmp.substring(0, curx-1);
_tmp2 += _tmp.substring(curx);
this.text.setElementAt(_tmp2, cury);
curx--;
}
}
void insertChar(char c){
String _tmp = null;
String _tmp2 = null;
if(c == '\n'){
_tmp = this.text.elementAt(cury).toString();
_tmp2 = _tmp.substring(0, curx);
_tmp = _tmp.substring(curx);
this.text.setElementAt(_tmp2, cury);
this.text.insertElementAt(_tmp, cury + 1);
this.cury++;
this.curx = 0;
} else {
_tmp = this.text.elementAt(cury).toString();
_tmp2 = _tmp.substring(0, curx);
_tmp2 += String.valueOf(c);
_tmp2 += _tmp.substring(curx);
this.text.setElementAt(_tmp2, cury);
curx++;
}
}
char getCharFromKey(int k){
char out = (char) 0x00;
if(Key.isSpace(k)){
out = ' ';
} else if(Key.isEnter(k)){
out = '\n';
} else if(k > 31 && k < 127){
out = (char) k;
}
return out;
}
/* END QWERTY ONLY */
protected void keyRepeated(int keyCode){ keyPressed(keyCode); }
protected void keyPressed(int k){
this._dkey = String.valueOf(k);
int _l = 0;
boolean _repaint = true;
if(enteredLine){
this.enter(k);
return;
}
if (Key.isUp(k)){
if(cury > 0){
cury--;
if((_l = l(cury)) < curx){
curx = _l;
}
}
} else if (Key.isDown(k)){
if(cury < text.size() - 1){
cury++;
if((_l = l(cury)) < curx){
curx = _l;
}
}
} else if (Key.isLeft(k)){
curx--;
if(curx < 0){
if(cury > 0){
this.curx = l(cury - 1);
this.cury--;
} else {
curx = 0;
}
}
} else if (Key.isRight(k)){
curx++;
if(curx > l(cury)){
if(cury < text.size()-1){
curx = 0;
cury++;
x = 0;
} else if(curx >= l(cury) + 1) {
curx--;
}
}
} else if (Key.isFire(k) || Key.is5(k)){
this.resStart = cury - 1;
if(resStart < 0)
resStart = 0;
this.resEnd = cury + 1;
if(resEnd > this.text.size() - 1)
resEnd = this.text.size() - 1;
this.parent.openEdit(this.getString(resStart, resEnd));
_repaint = false;
} else if (Key.isStar(k)){
this.setFont(efont.getNext());
} else if (Key.isPound(k)){
this.lineExists = false;
this.enterLine = "";
this.enteredLine = !this.enteredLine;
} else if (Key.is1(k)){
cury -= this.maxLines;
if(cury < 0){
cury = 0;
}
} else if (Key.is7(k)){
cury += this.maxLines;
if(cury >= this.text.size()){
cury = this.text.size() - 1;
}
} else if (Key.is8(k)){
this.parent.callbackSearch();
_repaint = false;
} else if(Key.is9(k)){
this.nextSearch();
} else if(Key.is3(k)){
this.parent.showEditorReplace();
} else if(Key.is4(k) || Key.is2(k) || Key.is6(k)) {
//do nothing
} else {
this.edit(k);
}
if(_repaint){
this.updatePos();
repaint();
System.gc();
}
}
void setFont(Font font){
this.f = font;
this.fontH = this.f.getHeight();
if(this.fontH == 0){
Editor.D("Пиздец какой то");
} else {
this.maxLines = (int)((this.sHeight - 24) / this.fontH);
}
this.hscrol = this.f.stringWidth("M");
this.maxChars = (int)(this.sWidth / this.hscrol);
//this.MARGIN = (this.w("7") * 3) + 3;
this.numberWidth = this.w("7") + 1;
}
int calculateWidth(){
//костыль
int _w = String.valueOf(this.startLine + this.maxLines).length();
return this.numberWidth * _w;
}
public void restore(String s){
Editor.D("Restore: " + s);
Vector v = Split.string(s, "\n");
for(int i = resEnd; i >= resStart; i--){
this.text.removeElementAt(i);
}
for(int i = v.size() - 1; i >= 0; i--){
this.text.insertElementAt(v.elementAt(i).toString(), resStart);
}
this.updatePos();
this.repaint();
}
private void updatePos(){
int clen = l(cury);
int _l = w(s(cury).substring(0, curx));
int _m = this.calculateWidth();
if(_l - x > this.sWidth - (10 + _m)){
x = 0 - (_l - this.sWidth) - (10 + _m);
} else {
x = 0;
}
if(cury >= this.maxLines + this.startLine){
startLine=cury-this.maxLines+1;
} else if(cury < this.startLine){
startLine=cury;
} else if(cury < this.maxLines) {
startLine = 0;
}
if(startLine < 0){
startLine = 0;
}
if(startLine >= this.text.size()){
startLine = this.text.size() - 2;
}
}
public void search(String s){
this._searchString = s;
this.currentSearchPosition = 0;
this.nextSearch();
}
private void nextSearch(){
if(this._searchString == null){
return;
}
boolean repeated = false;
int pos = 0;
for(int i = this.currentSearchPosition;; i++){
if(i >= this.text.size()){
if(repeated)
return;
repeated = true;
i = 0;
}
String s = this.text.elementAt(i).toString();
if((pos = s.indexOf(this._searchString)) != -1){
this.currentSearchPosition = i+1;
this.startLine = i;
this.cury = i;
this.curx = pos;
break;
}
}
}
public void replace(String fs, String rs){
if(fs == null || rs == null || fs.length() < 1 || rs.length() < 1)
return;
for(int i = 0; i <= this.text.size() - 1; i++){
this.text.setElementAt(Replace.string(s(i), fs, rs), i);
}
}
private int w(String s){
return f.stringWidth(s);
}
private String s(int p){
return text.elementAt(p).toString();
}
private String ts(int s){
return String.valueOf(s);
}
private int l(int p){
return this.text.elementAt(p).toString().length();
}
private String getString(int s, int e){
if(e <= s){
return null;
}
if(s < 0)
s = 0;
if(e >= text.size())
e = text.size() - 1;
StringBuffer sbuf = new StringBuffer();
for(int i = s; i<=e; i++){
sbuf.append(this.text.elementAt(i).toString());
if(i<e){
sbuf.append("\n");
}
}
return sbuf.toString();
}
}