home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Java by Example
/
jbecd.bin
/
JBE-CD
/
Mje
/
Test2.java
< prev
next >
Wrap
Text File
|
1996-04-08
|
10KB
|
471 lines
//
// Mini Java Editor window
//
import java.io.*;
import java.awt.*;
class MJEWindow extends Frame {
TextArea area;
MJEOutput output;
String fonts[];
String styles[] = {"Plain", "Bold", "Italic"};
String sizes[] = {"8", "9", "10", "12", "14", "16", "18", "24"};
String clipboard;
String saveFile = null;
String fname;
int fstyle;
int fsize;
MenuItem saveItem = new MenuItem("Save");
MenuItem compileItem = new MenuItem("Compile");
CheckboxMenuItem fontItems[];
CheckboxMenuItem styleItems[];
CheckboxMenuItem sizeItems[];
public static void main(String args[]) {;
MJEWindow win = new MJEWindow("Untitled");
if (args.length > 0) {
win.setTitle(new File(args[0]).getName());
win.saveFile = args[0];
win.area.setText(win.readFile(win.saveFile));
win.saveItem.enable();
}
}
MJEWindow(String title) {
// Initialize
area = new TextArea(35, 80);
output = new MJEOutput(this, 5);
fonts = getToolkit().getFontList();
MenuBar menuBar = new MenuBar();
fname = "Courier";
fstyle = Font.PLAIN;
fsize = 12;
// Create file menu
Menu fileMenu = new Menu("File");
fileMenu.add(new MenuItem("New"));
fileMenu.add(new MenuItem("Open..."));
fileMenu.add(saveItem);
fileMenu.add(new MenuItem("Save As..."));
fileMenu.add(new MenuItem("-"));
fileMenu.add(compileItem);
menuBar.add(fileMenu);
// Create edit menu
Menu editMenu = new Menu("Edit");
editMenu.add(new MenuItem("Cut"));
editMenu.add(new MenuItem("Copy"));
editMenu.add(new MenuItem("Paste"));
editMenu.add(new MenuItem("Select All"));
editMenu.add(new MenuItem("-"));
editMenu.add(new MenuItem("Go To Line..."));
editMenu.add(new MenuItem("Find..."));
menuBar.add(editMenu);
// Create font menu
Menu fontMenu = new Menu("Font");
fontItems = new CheckboxMenuItem[fonts.length];
for (int i = 0; i < fonts.length; i++) {
fontItems[i] = new CheckboxMenuItem(fonts[i]);
fontMenu.add(fontItems[i]);
if (fonts[i].equals(fname))
fontItems[i].setState(true);
}
menuBar.add(fontMenu);
// Create style menu
Menu styleMenu = new Menu("Style");
styleItems = new CheckboxMenuItem[styles.length];
for (int i = 0; i < styles.length; i++) {
styleItems[i] = new CheckboxMenuItem(styles[i]);
styleMenu.add(styleItems[i]);
if (i == fstyle)
styleItems[i].setState(true);
}
menuBar.add(styleMenu);
// Create size menu
Menu sizeMenu = new Menu("Size");
sizeItems = new CheckboxMenuItem[sizes.length];
for (int i = 0; i < sizes.length; i++) {
sizeItems[i] = new CheckboxMenuItem(sizes[i]);
sizeMenu.add(sizeItems[i]);
if (sizes[i].equals(new Integer(fsize).toString()))
sizeItems[i].setState(true);
}
menuBar.add(sizeMenu);
// Show window
setMenuBar(menuBar);
saveItem.disable();
compileItem.disable();
area.setFont(new Font(fname, fstyle, fsize));
setTitle(title);
output.clear();
add("Center", area);
add("South", output);
pack();
show();
}
// Open file
public boolean open(String path) {
if (path != null) {
setTitle(path);
saveFile = path;
area.setText(readFile(path));
saveItem.enable();
output.clear();
if (path.endsWith(".java"))
compileItem.enable();
else
compileItem.disable();
return true;
}
return false;
}
public boolean save() {
writeFile(saveFile, area.getText());
return true;
}
// Read file
public String readFile(String fl) {
String text = new String();
try {
FileInputStream fs = new FileInputStream(fl);
DataInputStream ds = new DataInputStream(fs);
String str = new String();
while (str != null) {
str = ds.readLine();
if (str != null)
text = text + str + "\n";
}
} catch (Exception err) {
System.out.println("Cannot open file.");
}
return text;
}
// Write file
public void writeFile(String fl, String txt) {
try {
FileOutputStream fs = new FileOutputStream(fl);
DataOutputStream ds = new DataOutputStream(fs);
String ls = System.getProperty("line.separator");
for (int i = 0; i < txt.length(); i++) {
char ch = txt.charAt(i);
switch (ch) {
case '\n':
ds.writeBytes(ls);
break;
default:
ds.write(ch);
}
}
} catch (Exception err) {
System.out.println("Cannot save file.");
}
}
// Handle system event
public boolean handleEvent(Event evt) {
if (evt.id == Event.WINDOW_DESTROY && evt.target == this) {
save();
dispose();
}
return super.handleEvent(evt);
}
// Handle component events
public boolean action(Event evt, Object obj) {
String label = (String) obj;
String file = null;
// Handle file menu
if (label.equals("New")) {
area.setText("");
setTitle("Untitled");
saveItem.disable();
output.clear();
return true;
}
if (label.equals("Open...")) {
FileDialog dialog = new FileDialog(this, "Open...", FileDialog.LOAD);
dialog.show();
file = dialog.getFile();
if (file != null)
open(dialog.getDirectory() + file);
return true;
}
if (label.equals("Save")) {
writeFile(saveFile, area.getText());
}
if (label.equals("Save As...")) {
FileDialog dialog = new FileDialog(this, "Save As...", FileDialog.SAVE);
dialog.setFile(saveFile);
dialog.show();
file = dialog.getFile();
if (file != null) {
setTitle(file);
saveFile = file;
saveItem.enable();
writeFile(file, area.getText());
}
return true;
}
if (label.equals("Compile")) {
try {
output.delItems(0, output.countItems() - 1);
System.out.println(output.countItems());
output.addItem("Compiling " + saveFile + "...");
writeFile(saveFile, area.getText());
Process ps = Runtime.getRuntime().exec("javac " + saveFile);
DataInputStream ds = new DataInputStream(ps.getInputStream());
String str = new String();
while (str != null) {
str = ds.readLine();
if (str != null)
if (str.startsWith(saveFile)) {
String msg = str.substring(saveFile.length() + 1);
for (int i = 1; i < 5; i++)
if (msg.charAt(i) == ':') {
int pos = 0;
String line = msg.substring(0, i);
String err = msg.substring(i + 2, msg.length() - 1);
String code = ds.readLine().trim();
String tmp = ds.readLine();
for (int j = 0; j < tmp.length(); j++)
if (tmp.charAt(j) == '^')
pos = j;
output.addItem("*** " + line + " (" + pos + "): " + code +
" (" + err + ").");
}
}
}
output.addItem("Ready.");
}
catch (Exception err) { }
}
// Handle edit menu
if (label.equals("Cut")) {
clipboard = area.getSelectedText();
area.replaceText("", area.getSelectionStart(),
area.getSelectionEnd());
return true;
}
if (label.equals("Copy")) {
clipboard = area.getSelectedText();
return true;
}
if (label.equals("Paste")) {
int start = area.getSelectionStart();
int end = area.getSelectionEnd();
if (start == end)
area.insertText(clipboard, start);
else
area.replaceText(clipboard, start, end);
return true;
}
if (label.equals("Select All"))
area.selectAll();
if (label.equals("Go To Line...")) {
GotoDialog dialog = new GotoDialog(this);
dialog.pack();
dialog.show();
return true;
}
if (label.equals("Find...")) {
FindDialog dialog = new FindDialog(this);
dialog.pack();
dialog.show();
return true;
}
// Handle font menu
for (int i = 0; i < fonts.length; i++) {
if (label.equals(fonts[i])) {
for (int j = 0; j < fonts.length; j++)
if (i != j)
fontItems[j].setState(false);
fname = label;
area.setFont(new Font(fname, fstyle, fsize));
return true;
}
}
// Handle style menu
for (int i = 0; i < styles.length; i++) {
if (label.equals(styles[i])) {
for (int j = 0; j < styles.length; j++)
if (i != j)
styleItems[j].setState(false);
fstyle = i;
area.setFont(new Font(fname, fstyle, fsize));
return true;
}
}
// Handle size menu
for (int i = 0; i < sizes.length; i++) {
if (label.equals(sizes[i])) {
for (int j = 0; j < sizes.length; j++)
if (i != j)
sizeItems[j].setState(false);
fsize = new Integer(label).intValue();
area.setFont(new Font(fname, fstyle, fsize));
return true;
}
}
return false;
}
public TextArea getTextArea() {
return area;
}
public void gotoLine(int line) {
gotoLine(line, 0);
}
public void gotoLine(int line, int col) {
int i = 1, pos = 0;
String str = area.getText();
while (i < line) {
if (str.charAt(pos) == '\n')
i++;
pos++;
}
area.requestFocus();
area.select(pos + col, pos + col);
}
}
//
// Output area
//
class MJEOutput extends List {
MJEWindow win;
public MJEOutput(MJEWindow w, int rows) {
super(rows, false);
win = w;
}
public void clear() {
delItems(0, countItems() - 1);
addItem("Ready.");
}
public boolean action(Event evt, Object obj) {
String str = (String) obj;
if (str.charAt(0) == '*') {
for (int i = 5; i < 9; i++)
if (str.charAt(i) == ' ') {
int line = new Integer(str.substring(4, i)).intValue();
int col = 0;
for (int j = i + 2; j < i + 5; j++)
if (str.charAt(j) == ')')
col = new Integer(str.substring(i + 2, j)).intValue();
win.gotoLine(line, col);
break;
}
}
return super.action(evt, obj);
}
}