modified JFastReader.java

Initial working version
This commit is contained in:
mbirth 2004-12-08 10:16:50 +00:00
parent 5faaf21b84
commit b84bfd2c04

View File

@ -24,78 +24,86 @@ import java.awt.Scrollbar;
import java.awt.SystemColor; import java.awt.SystemColor;
import java.awt.TextField; import java.awt.TextField;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class JFastReader extends Frame implements ActionListener { public class JFastReader extends Frame implements ActionListener {
final static int WND_W=208, WND_H=276; // initial window size final int WND_W=208, WND_H=276; // initial window size
final static String APPNAME="jFastReader"; final String APPNAME="jFastReader";
final static String APPVERSION="1.0"; final String APPVERSION="1.0";
private static JFastReader jFastReader = null; private static JFastReader jFastReader = null;
static Font ftPlain8 = new Font("Dialog", Font.PLAIN, 8); Font ftPlain8 = new Font("Dialog", Font.PLAIN, 8);
static Font ftPlain10 = new Font("Dialog", Font.PLAIN, 10); Font ftPlain10 = new Font("Dialog", Font.PLAIN, 10);
static Font ftBold12 = new Font("Dialog", Font.BOLD, 12); Font ftBold12 = new Font("Dialog", Font.BOLD, 12);
static String curFile = ""; File curFile;
int curWord = 1;
int maxWord = 1;
MySeeker seeker = new MySeeker();
static Panel pnMain = new Panel(new BorderLayout()); Panel pnMain = new Panel(new BorderLayout());
static Panel pnTop = new Panel(new FlowLayout(FlowLayout.CENTER,1,1)); Panel pnTop = new Panel(new BorderLayout());
static Panel pnBottom = new Panel(new BorderLayout()); Panel pnTopLeft = new Panel(new FlowLayout(FlowLayout.LEFT,1,1));
static Panel pnBottom2 = new Panel(new GridLayout(3,2,2,2)); Panel pnTopRight = new Panel(new FlowLayout(FlowLayout.RIGHT,1,1));
static Panel pnSpeed = new Panel(new BorderLayout()); Panel pnBottom = new Panel(new BorderLayout());
static Panel pnOptBook = new Panel(new GridLayout(1,2,1,1)); Panel pnBottom2 = new Panel(new GridLayout(3,2,2,1));
static Panel pnSlowFast = new Panel(new GridLayout(1,2,1,1)); Panel pnSpeed = new Panel(new BorderLayout());
static Panel pnBackForw = new Panel(new GridLayout(1,2,1,1)); Panel pnOptBook = new Panel(new GridLayout(1,2,1,1));
static Panel pnReadPaus = new Panel(new GridLayout(1,2,1,1)); Panel pnSlowFast = new Panel(new GridLayout(1,2,1,1));
static Panel pnReptButt = new Panel(new GridLayout(1,2,1,1)); Panel pnBackForw = new Panel(new GridLayout(1,2,1,1));
static Panel pnButtons = new Panel(new GridLayout(2,1,1,1)); Panel pnReadPaus = new Panel(new GridLayout(1,2,1,1));
Panel pnReptButt = new Panel(new GridLayout(1,2,1,1));
Panel pnButtons = new Panel(new GridLayout(2,1,1,1));
static Label lbGo = new Label("Word:", Label.RIGHT); Label lbGo = new Label("Word:", Label.RIGHT);
static TextField tfGo = new TextField("", 4); TextField tfGo = new TextField("", 4);
static Button btGo = new Button("Go"); Button btGo = new Button("Go");
static Button btAbout = new Button("About"); Button btAbout = new Button("?");
static Button btText = new Button("Text"); Button btText = new Button("Go Text");
static MyCanvas cvFR = new MyCanvas(); MyCanvas cvFR = new MyCanvas();
static Scrollbar sbProgress = new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, 0, 0); Scrollbar sbProgress = new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, 0, 0);
static Label lbSpeedTag = new Label("Speed (wpm.)", Label.LEFT); Label lbSpeedTag = new Label("Speed (wpm.)", Label.LEFT);
static MyProgBar cvSpeed = new MyProgBar(); MyProgBar cvSpeed = new MyProgBar();
static Label lbSpeed = new Label("---", Label.LEFT); Label lbSpeed = new Label("---", Label.LEFT);
static Button btOptions = new Button("Options"); Button btOptions = new Button("Opt.");
static Button btBookmark = new Button("Bookmark"); Button btBookmark = new Button("Bkmk");
static Button btSlower = new Button("<<\nSlower"); Button btSlower = new Button("<<\nSlwr");
static Button btFaster = new Button(">>\nFaster"); Button btFaster = new Button(">>\nFstr");
static Button btBack = new Button("<<\nBKWD"); Button btBack = new Button("<\nBKW");
static Button btForw = new Button(">>\nFWD"); Button btForw = new Button(">\nFWD");
static Button btRead = new Button("READ"); Button btRead = new Button(">\nREAD");
static Button btPause = new Button("PAUSE"); Button btPause = new Button("||\nPAUS");
static Button btRepeat = new Button("REPEAT"); Button btRepeat = new Button("REPT");
static Button btLoad = new Button("Load File"); Button btLoad = new Button("Load");
static Button btQuit = new Button("Exit"); Button btQuit = new Button("Exit");
transient Dialog diAbout = new Dialog(this, "About...", true); Dialog diAbout = new Dialog(this, "About...", true);
static Panel pnAboutText = new Panel(new GridLayout(0,1,0,0)); Panel pnAboutText = new Panel(new GridLayout(0,1,0,0));
static Panel pnAboutButt = new Panel(new FlowLayout(FlowLayout.RIGHT)); Panel pnAboutButt = new Panel(new FlowLayout(FlowLayout.RIGHT));
static Label lbAbout1 = new Label(APPNAME, Label.CENTER); Label lbAbout1 = new Label(APPNAME, Label.CENTER);
static Label lbAbout2 = new Label("by Markus Birth", Label.CENTER); Label lbAbout2 = new Label("by Markus Birth", Label.CENTER);
static Label lbAbout3 = new Label("mbirth@webwriters.de", Label.CENTER); Label lbAbout3 = new Label("mbirth@webwriters.de", Label.CENTER);
static Label lbAbout4 = new Label("http://www.webwriters.de/mbirth/", Label.CENTER); Label lbAbout4 = new Label("http://www.webwriters.de/mbirth/", Label.CENTER);
static Button btOk = new Button("OK"); Button btOk = new Button("OK");
static Dimension dmScreen = Toolkit.getDefaultToolkit().getScreenSize(); // get Screen dimensions Dimension dmScreen = Toolkit.getDefaultToolkit().getScreenSize(); // get Screen dimensions
transient Dialog diInfo = new Dialog(this, "infoPrint", false); Dialog diInfo = new Dialog(this, "infoPrint", false);
static Label lbInfo = new Label(); Label lbInfo = new Label();
transient InfoPrintThread thIPT = new InfoPrintThread(); InfoPrintThread thIPT = new InfoPrintThread();
private class MainWindowAdapter extends WindowAdapter { private class MainWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent we) { public void windowClosing(WindowEvent we) {
@ -143,15 +151,110 @@ public class JFastReader extends Frame implements ActionListener {
System.out.println("Exit button clicked. Sending event..."); System.out.println("Exit button clicked. Sending event...");
dispatchEvent(new WindowEvent(this, 201)); dispatchEvent(new WindowEvent(this, 201));
} else if (ae.getSource().equals(btLoad)) { // Load-Button } else if (ae.getSource().equals(btLoad)) { // Load-Button
System.out.println("Show load-dialog...");
FileDialog fdLoad = new FileDialog(jFastReader, "Load file", FileDialog.LOAD); FileDialog fdLoad = new FileDialog(jFastReader, "Load file", FileDialog.LOAD);
fdLoad.show(); fdLoad.show();
System.out.println("Directory: "+fdLoad.getDirectory()); if (fdLoad.getFile() != null) {
System.out.println("File: "+fdLoad.getFile()); System.out.println("Directory: "+fdLoad.getDirectory());
curFile = fdLoad.getDirectory() + fdLoad.getFile(); System.out.println("File: "+fdLoad.getFile());
curFile = new File(fdLoad.getDirectory() + fdLoad.getFile());
seeker = new MySeeker();
curWord = 1;
maxWord = 1;
getWord(1);
showWord(curWord);
} else {
infoPrint("Cancelled.");
}
} else if (ae.getSource().equals(btBack)) {
System.out.println("<<< BACK");
if (curWord > 1) {
showWord(--curWord);
}
} else if (ae.getSource().equals(btForw)) {
System.out.println(">>> FORW");
if (curWord < maxWord) {
showWord(++curWord);
}
} }
// TODO: more events // TODO: more events
} }
private String[] splitString(String str, String delim) {
str = str.trim();
int i = 1;
int lastIndex = 0;
int idx;
// System.out.println("sS: Input string is >"+str+"<, delimiter >"+delim+"<");
// System.out.println("sS: Counting words...");
while ((idx=str.indexOf(delim, lastIndex)) >= 0) {
i++;
lastIndex = idx+1;
}
// System.out.println("sS: Counted "+i+" words.");
String[] result = new String[i];
lastIndex = 0;
i = 0;
while ((idx=str.indexOf(delim, lastIndex)) >= 0) {
result[i] = str.substring(lastIndex, idx);
// System.out.println("sS: Word #"+i+" >"+result[i]+"<");
lastIndex = idx+1;
i++;
}
result[i] = str.substring(lastIndex);
// System.out.println("sS: Word #"+i+" >"+result[i]+"<");
return result;
}
public String getWord(int w) {
String result = "";
if (curFile != null && curFile.exists() && curFile.isFile()) {
try {
RandomAccessFile f = new RandomAccessFile(curFile, "r");
String tmp;
long lastPos = 0;
int wordcount = 1;
if (seeker.len <= 0) {
// get wordcounts
infoPrint("Indexing...");
while ((tmp = f.readLine()) != null) {
// System.out.println("Read line: >" + tmp + "<");
String[] words = splitString(tmp, " ");
// System.out.println("Setting index for word #"+wordcount+" to pos. "+lastPos);
seeker.add(wordcount, lastPos);
wordcount += words.length;
// System.out.println("New wordcount is now "+wordcount);
lastPos = f.getFilePointer();
}
maxWord = --wordcount;
System.out.println("Words at all: "+maxWord);
}
long[] seekpos = seeker.getSeekForWord(w);
f.seek(seekpos[1]);
String myLine = f.readLine();
int indexl = 0;
while (seekpos[0]<w && myLine.indexOf(" ", indexl)>0) {
System.out.println("Need word: "+w+"; cur: "+seekpos[0]);
indexl = myLine.indexOf(" ", indexl+1)+1;
seekpos[0]++;
}
System.out.println("Need word: "+w+"; got: "+seekpos[0]);
int indexr = myLine.indexOf(" ", indexl+1);
if (indexr <= 0) { indexr = myLine.length(); }
result = myLine.substring(indexl, indexr);
f.close();
} catch (IOException ioe) {
System.out.println("IOException while reading file: "+ioe.toString());
infoPrint("File IO error");
}
}
return result;
}
private void showWord(int w) {
String wrd = getWord(w);
System.out.println("Should show word #"+w+" >"+wrd+"<");
cvFR.setWord(wrd);
}
public JFastReader() { // Constructor public JFastReader() { // Constructor
addWindowListener(new MainWindowAdapter()); addWindowListener(new MainWindowAdapter());
@ -165,17 +268,18 @@ public class JFastReader extends Frame implements ActionListener {
btAbout.addActionListener(this); btAbout.addActionListener(this);
btLoad.addActionListener(this); btLoad.addActionListener(this);
btQuit.addActionListener(this); btQuit.addActionListener(this);
btBack.addActionListener(this);
btForw.addActionListener(this);
pnMain.setFont(ftPlain8); pnMain.setFont(ftPlain8);
btGo.setFont(ftPlain8);
btAbout.setFont(ftPlain8);
btText.setFont(ftPlain8);
pnTop.add(lbGo); pnTopLeft.add(lbGo);
pnTop.add(tfGo); pnTopLeft.add(tfGo);
pnTop.add(btGo); pnTopLeft.add(btGo);
pnTop.add(btAbout); pnTopRight.add(btAbout);
pnTop.add(btText); pnTopRight.add(btText);
pnTop.add(pnTopLeft, BorderLayout.WEST);
pnTop.add(pnTopRight, BorderLayout.EAST);
pnSpeed.add(lbSpeedTag, BorderLayout.NORTH); pnSpeed.add(lbSpeedTag, BorderLayout.NORTH);
pnSpeed.add(cvSpeed, BorderLayout.CENTER); pnSpeed.add(cvSpeed, BorderLayout.CENTER);
@ -254,75 +358,46 @@ public class JFastReader extends Frame implements ActionListener {
diAbout.setLocation(ptWindow.x+(dmWindow.width-dmAboutBox.width)/2, ptWindow.y+dmWindow.height-dmAboutBox.height); diAbout.setLocation(ptWindow.x+(dmWindow.width-dmAboutBox.width)/2, ptWindow.y+dmWindow.height-dmAboutBox.height);
} }
private static class MyCanvas extends Canvas { private class MySeeker {
static Color bgColor = new Color(0,0,128); int[] poses = new int[0];
static Color fgColor = new Color(255,255,0); long[] seeker = new long[0];
static String word = ""; int len = 0;
static int maxFontSize = 100;
public void paint(Graphics g) { public void add(int word, long pos) {
g.setColor(bgColor); int[] newp = new int[len+1];
g.fillRect(0,0,getSize().width-1,getSize().height-2); long[] news = new long[len+1];
g.setColor(Color.black); for (int i=0;i<len;i++) {
g.drawRect(0,0,getSize().width-1,getSize().height-2); newp[i] = poses[i];
if (word.length()>0) { news[i] = seeker[i];
g.setColor(fgColor);
// do auto-downsizing of font
int fs = maxFontSize;
Font f;
FontMetrics fm;
do {
f = new Font("Dialog", Font.BOLD, fs);
fm = g.getFontMetrics(f);
fs -= 2;
} while ((fm.stringWidth(word)>getSize().width-2) || (fm.getHeight()>getSize().height-2));
g.setFont(f);
g.drawString(word, getSize().width/2-fm.stringWidth(word)/2, getSize().height/2-fm.getHeight()/2+fm.getAscent());
} }
newp[len] = word;
news[len] = pos;
poses = newp;
seeker = news;
len++;
} }
public void setWord(String txt) { public long[] getSeekForWord(int word) {
word = txt; // System.out.println("gSFW: Seek word #"+word);
repaint(); long[] result = new long[2];
} if (len>0) {
} int i=0;
while (i<len && poses[i]<=word) {
private static class MyProgBar extends Canvas { // System.out.println("gSFW: "+i+" Curwordindex: "+poses[i]+" / Seek: "+seeker[i]);
static int minVal = 0; i++;
static int maxVal = 100; }
static int curVal = 50; if (i>0) { i--; }
static Color fgColor = new Color(0,0,128); // System.out.println("gSFW: Nearest word is #"+poses[i]+" at pos. "+seeker[i]);
static Color bgColor = Color.lightGray; result[0] = poses[i];
static Color txColor = Color.white; result[1] = seeker[i];
} else {
public void paint(Graphics g) { System.out.println("gSFW: No data.");
double percentage = (double)(curVal-minVal)/(double)(maxVal-minVal); result[0] = 0;
String percString = Double.toString(percentage*100) + "%"; result[1] = 0;
g.setFont(ftPlain10); }
FontMetrics fm = getFontMetrics(g.getFont()); return result;
g.setColor(bgColor);
g.fillRect(0,0,getSize().width-1,getSize().height-1);
g.setColor(fgColor);
g.fillRect(0,0,(int)((double)(getSize().width-1)/(maxVal-minVal)*(curVal-minVal)),getSize().height-1);
g.setXORMode(txColor);
g.drawString(percString, getSize().width/2-fm.stringWidth(percString)/2, getSize().height/2-fm.getHeight()/2+fm.getAscent());
g.setPaintMode();
g.setColor(Color.black);
g.drawRect(0,0,getSize().width-1,getSize().height-1);
} }
public void setMinValue(int v) {
minVal = v;
}
public void setMaxValue(int v) {
maxVal = v;
}
public void setPos(int p) {
curVal = p;
repaint();
}
} }
// waits 3 seconds and then hides the diInfo-Dialog // waits 3 seconds and then hides the diInfo-Dialog