home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-04-08 | 6.4 KB | 321 lines |
- //
- // Mini Java Editor
- //
- // By Lim Thye Chean
- //
-
- import java.io.*;
- import java.awt.*;
- import java.util.*;
-
- class MJE extends Frame {
- static final String about = new String("Mini Java Editor v0.8 - " +
- "by Lim Thye Chean ");
-
- String project = null;
- Vector paths = new Vector();
- Hashtable windows = new Hashtable();
- MJEFiles list = new MJEFiles(this, 10);
- Label status = new Label(about);
- MenuItem saveItem = new MenuItem("Save Project");
-
- public static void main(String args[]) {;
- MJE ed = new MJE("Mini Java Editor");
-
- if (args.length > 0) {
- ed.setTitle(ed.getFile(new File(args[0]).getName()));
-
- ed.project = args[0];
- ed.read(ed.project);
- ed.saveItem.enable();
- }
- }
-
- MJE(String title) {
-
- // Initialize
-
- MenuBar menuBar = new MenuBar();
-
- // Create file menu
-
- Menu fileMenu = new Menu("File");
- fileMenu.add(new MenuItem("New Project"));
- fileMenu.add(new MenuItem("Open Project..."));
- fileMenu.add(saveItem);
- fileMenu.add(new MenuItem("Save Project As..."));
- fileMenu.add(new MenuItem("-"));
- fileMenu.add(new MenuItem("Exit"));
- menuBar.add(fileMenu);
-
- // Create edit menu
-
- Menu editMenu = new Menu("Edit");
- editMenu.add(new MenuItem("Add File..."));
- editMenu.add(new MenuItem("Delete File"));
- menuBar.add(editMenu);
-
- // Create help menu
-
- Menu helpMenu = new Menu("Help");
- helpMenu.add(new MenuItem("About Mini Java Editor..."));
- menuBar.add(helpMenu);
- menuBar.setHelpMenu(helpMenu);
-
- // Show editor
-
- setMenuBar(menuBar);
- saveItem.disable();
-
- setTitle(title);
-
- add("Center", list);
- add("South", status);
- pack();
- show();
- }
-
- // Save all files
-
- public void save() {
- Enumeration en = windows.keys();
-
- while (en.hasMoreElements()) {
- String path = (String) en.nextElement();
-
- status.setText("Saving " + path + "...");
- ((MJEWindow) windows.get(path)).save();
- status.setText("");
- }
- }
-
- // Read file
-
- public boolean read(String path) {
- try {
- FileInputStream fs = new FileInputStream(path);
- DataInputStream ds = new DataInputStream(fs);
- String str = new String();
-
- while (str != null) {
- str = ds.readLine();
-
- if (str != null)
- paths.addElement(str);
- }
- } catch (Exception err) {
- status.setText("Cannot open file.");
- return false;
- }
-
- return true;
- }
-
- // Write file
-
- public void write(String path) {
- try {
- FileOutputStream fs = new FileOutputStream(path);
- PrintStream ds = new PrintStream(fs);
-
- for (int i = 0; i < paths.size(); i++)
- ds.println((String) paths.elementAt(i));
- } catch (Exception err) {
- status.setText("Cannot save file.");
- }
- }
-
- // Exit editor
-
- public void exit() {
- save();
- System.exit(0);
- }
-
- // Handle system event
-
- public boolean handleEvent(Event evt) {
- if (evt.id == Event.WINDOW_DESTROY && evt.target == this)
- exit();
-
- return super.handleEvent(evt);
- }
-
- // Handle component events
-
- public boolean action(Event evt, Object obj) {
- String label = (String) obj;
-
- // Handle file menu
-
- if (label.equals("New Project")) {
- project = null;
- saveItem.disable();
- list.clear();
- setTitle("Mini Java Editor");
- return true;
- }
-
- if (label.equals("Open Project...")) {
- FileDialog dialog = new FileDialog(this, "Open Project...",
- FileDialog.LOAD);
-
- dialog.show();
- String file = dialog.getFile();
-
- if (file != null) {
- if (file.endsWith(".project")) {
- project = dialog.getDirectory() + file;
-
- if (read(project)) {
- setTitle(getFile(project) + " - Mini Java Editor");
- saveItem.enable();
- list.refresh();
- status.setText("");
- }
- }
- }
-
- return true;
- }
-
- if (label.equals("Save Project")) {
- if (project != null) {
- save();
- write(project);
- }
- }
-
- if (label.equals("Save Project As...")) {
- FileDialog dialog = new FileDialog(this, "Save As...", FileDialog.SAVE);
-
- dialog.setFile("Untitled.project");
- dialog.show();
- String file = dialog.getFile();
-
- if (file != null) {
- project = dialog.getDirectory() + file;
- setTitle(getFile(project) + " - Mini Java Editor");
- saveItem.enable();
- save();
- write(project);
- }
-
- return true;
- }
-
- if (label.equals("Exit"))
- exit();
-
- // Handle edit menu
-
- if (label.equals("Add File...")) {
- FileDialog dialog = new FileDialog(this, "Add File...",
- FileDialog.LOAD);
-
- dialog.show();
- String file = dialog.getFile();
-
- if (file != null) {
- paths.addElement(dialog.getDirectory() + file);
- list.refresh();
- }
- }
-
- if (label.equals("Delete File")) {
- for (int i = 0; i < paths.size(); i++) {
- String path = (String) paths.elementAt(i);
-
- if (path.endsWith(list.getSelectedItem())) {
- paths.removeElementAt(i);
-
- if (windows.containsKey(path)) {
- MJEWindow win = (MJEWindow) windows.get(path);
-
- win.save();
- win.dispose();
- }
- }
- }
-
- list.refresh();
- }
-
- // Handle help menu
-
- if (label.equals("About Mini Java Editor...")) {
- InfoDialog dialog = new InfoDialog(this, label, MJE.about);
-
- dialog.pack();
- dialog.show();
-
- return true;
- }
-
- return false;
- }
-
- // Get file name from path
-
- public String getFile(String path) {
- char sp = System.getProperty("file.separator").charAt(0);
-
- for (int i = path.length() - 1; i > 0; i--)
- if (path.charAt(i) == sp)
- return path.substring(i + 1, path.length());
-
- return path;
- }
- }
-
- //
- // File list area
- //
-
- class MJEFiles extends List {
- MJE editor;
-
- public MJEFiles(MJE ed, int rows) {
- super(rows, false);
- editor = ed;
- }
-
- public void clear() {
- delItems(0, countItems() - 1);
- }
-
- public void refresh() {
- clear();
-
- for (int i = 0; i < editor.paths.size(); i++)
- addItem(editor.getFile((String) editor.paths.elementAt(i)));
- }
-
- public boolean action(Event evt, Object obj) {
- Hashtable wins = editor.windows;
- String str = (String) obj;
- String path = "";
-
- for (int i = 0; i < editor.paths.size(); i++) {
- String epath = (String) editor.paths.elementAt(i);
-
- if (epath.endsWith(str)) {
- path = epath;
-
- if (wins.containsKey(path))
- ((MJEWindow) wins.get(path)).show();
- else {
- MJEWindow win = new MJEWindow(editor.getFile(path));
- win.open(path);
- wins.put(path, win);
- }
-
- System.out.println(path);
- break;
- }
- }
-
- return true;
- }
- }
-