import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class TextEditButtons extends Frame implements ActionListener
{
int wrdCnt = 0;
long inSize;
long outSize;
char inAry[];
char outAry[];
String inStr = new String();
String outStr = new String();
String shrtstWrd;
String lngstWrd;
StringTokenizer st1;
StringTokenizer st2;
StringTokenizer st3;
TextArea txt;
Button save;
Button quit;
Label wrdCntLab;
Label shrtstWrdLab;
Label lngstWrdLab;
File inTxt;
File outTxt;
InputStream inTxtStrm;
OutputStream outTxtStrm;
GridBagLayout gbl;
GridBagConstraints gbc;
public TextEditButtons()
{
super("Text Edit - Button version");
txt = new TextArea(20, 50);
save = new Button("Save");
quit = new Button("Quit");
wrdCntLab = new Label();
shrtstWrdLab = new Label();
lngstWrdLab = new Label();
gbc = new GridBagConstraints();
setLayout(gbl = new GridBagLayout());
/*add(save);
add(quit);
add(wrdCntLab);
add(shrtstWrdLab);
add(lngstWrdLab);*/
gbc.weightx = 0;
gbc.ipadx = 0;
gbc.anchor = gbc.FIRST_LINE_START;
gbl.setConstraints(txt, gbc);
add(txt);
gbc.weightx = 0;
gbc.ipadx = 0;
gbc.anchor = gbc.LINE_START;
gbl.setConstraints(save, gbc);
gbl.setConstraints(quit, gbc);
add(save);
add(quit);
try
{
inTxt = new File("TextEdit.java");
inTxtStrm = new FileInputStream(inTxt);
inSize = inTxt.length();
}
catch (FileNotFoundException fnfe1)
{
txt.setText("Input file not found");
}
try
{
outTxtStrm = new FileOutputStream("outputtext.txt");
}
catch (FileNotFoundException fnfe2)
{
txt.setText("Output file not found");
}
try
{
inAry = new char [(int) inSize];
for (int inAryInd = 0; inAryInd < inSize; inAryInd++)
{
inAry[inAryInd] = (char) inTxtStrm.read();
}
}
catch (IOException ioe)
{
txt.setText("I/O error");
}
for (int inAryInd = 0; inAryInd < inSize; inAryInd++)
{
inStr += inAry[inAryInd];
}
txt.setText(inStr);
addWindowListener(new GenericWindowClosing());
save.addActionListener(this);
quit.addActionListener(this);
st1 = new StringTokenizer (inStr, ",;:.?! \t\n()[]{}+-*/\\\"");
st2 = new StringTokenizer (inStr, ",;:.?! \t\n()[]{}+-*/\\\"");
st3 = new StringTokenizer (inStr, ",;:.?! \t\n()[]{}+-*/\\\"");
}
private int wordCount (StringTokenizer st1)
{
while (st1.hasMoreTokens())
{
wrdCnt++;
st1.nextToken();
}
return wrdCnt;
}
private String shortestWord (StringTokenizer st2)
{
shrtstWrd = st2.nextToken();
while (st2.hasMoreTokens())
{
String nxtWrd = st2.nextToken();
if(nxtWrd.length() < shrtstWrd.length())
{
shrtstWrd = nxtWrd;
st3.nextToken();
}
}
return shrtstWrd;
}
private String longestWord (StringTokenizer st3)
{
lngstWrd = st3.nextToken();
while (st3.hasMoreTokens())
{
String nxtWrd = st3.nextToken();
if(nxtWrd.length() > lngstWrd.length())
{
lngstWrd = nxtWrd;
st3.nextToken();
}
}
return lngstWrd;
}
public static void main (String args[])
{
int wc;
String sw;
String lw;
TextEditButtons teb = new TextEditButtons();
teb.setSize(new Dimension(400,500));
teb.setVisible(true);
wc = teb.wordCount(teb.st1);
sw = teb.shortestWord(teb.st2);
lw = teb.longestWord(teb.st3);
teb.wrdCntLab.setText("Word Count: " + wc);
teb.shrtstWrdLab.setText("Shortest word: " + sw);
teb.lngstWrdLab.setText("Longest word: " + lw);
}
public void actionPerformed(ActionEvent ae)
{
String actionStr = ae.getActionCommand();
if (actionStr.equals("Save"))
{
outStr = txt.getText();
outAry = outStr.toCharArray();
for (int outAryInd = 0; outAryInd < outStr.length(); outAryInd++)
{
try
{
outTxtStrm.write(outAry[outAryInd]);
}
catch (Exception e)
{
}
}
}
if (actionStr.equals("Quit"))
{
System.exit(0);
}
}
}