home *** CD-ROM | disk | FTP | other *** search
/ Java by Example / jbecd.bin / JBE-CD / Mje / MJE.java < prev    next >
Encoding:
Java Source  |  1996-04-08  |  6.4 KB  |  321 lines

  1. //
  2. // Mini Java Editor
  3. //
  4. // By Lim Thye Chean
  5. //
  6.  
  7. import java.io.*;
  8. import java.awt.*;
  9. import java.util.*;
  10.  
  11. class MJE extends Frame {
  12.   static final String about = new String("Mini Java Editor v0.8 - " +
  13.     "by Lim Thye Chean ");
  14.  
  15.   String project = null;
  16.   Vector paths = new Vector();
  17.   Hashtable windows = new Hashtable();
  18.   MJEFiles list = new MJEFiles(this, 10);
  19.   Label status = new Label(about);
  20.   MenuItem saveItem = new MenuItem("Save Project");
  21.  
  22.   public static void main(String args[]) {;
  23.     MJE ed = new MJE("Mini Java Editor");
  24.  
  25.     if (args.length > 0) {
  26.       ed.setTitle(ed.getFile(new File(args[0]).getName()));
  27.  
  28.       ed.project = args[0];
  29.       ed.read(ed.project);
  30.       ed.saveItem.enable();
  31.     }
  32.   }
  33.  
  34.   MJE(String title) {
  35.  
  36.   // Initialize
  37.  
  38.     MenuBar menuBar = new MenuBar();
  39.  
  40.   // Create file menu
  41.  
  42.     Menu fileMenu = new Menu("File");
  43.     fileMenu.add(new MenuItem("New Project"));
  44.     fileMenu.add(new MenuItem("Open Project..."));
  45.     fileMenu.add(saveItem);
  46.     fileMenu.add(new MenuItem("Save Project As..."));
  47.     fileMenu.add(new MenuItem("-"));
  48.     fileMenu.add(new MenuItem("Exit"));
  49.     menuBar.add(fileMenu);
  50.  
  51.   // Create edit menu
  52.  
  53.     Menu editMenu = new Menu("Edit");
  54.     editMenu.add(new MenuItem("Add File..."));
  55.     editMenu.add(new MenuItem("Delete File"));
  56.     menuBar.add(editMenu);
  57.  
  58.   // Create help menu
  59.  
  60.     Menu helpMenu = new Menu("Help");
  61.     helpMenu.add(new MenuItem("About Mini Java Editor..."));
  62.     menuBar.add(helpMenu);
  63.     menuBar.setHelpMenu(helpMenu);
  64.  
  65.   // Show editor
  66.  
  67.     setMenuBar(menuBar);
  68.     saveItem.disable();
  69.  
  70.     setTitle(title);
  71.  
  72.     add("Center", list);
  73.     add("South", status);
  74.     pack();
  75.     show();
  76.   }
  77.  
  78. // Save all files
  79.  
  80.   public void save() {
  81.     Enumeration en = windows.keys();
  82.  
  83.     while (en.hasMoreElements()) {
  84.       String path = (String) en.nextElement();
  85.  
  86.       status.setText("Saving " + path + "...");
  87.       ((MJEWindow) windows.get(path)).save();
  88.       status.setText("");
  89.     }
  90.   }
  91.  
  92. // Read file
  93.  
  94.   public boolean read(String path) {
  95.     try {
  96.       FileInputStream fs = new FileInputStream(path);
  97.       DataInputStream ds = new DataInputStream(fs);
  98.       String str = new String();
  99.  
  100.       while (str != null) {
  101.       str = ds.readLine();
  102.  
  103.     if (str != null)
  104.       paths.addElement(str);
  105.       }
  106.     } catch (Exception err) {
  107.       status.setText("Cannot open file.");
  108.       return false;
  109.     }
  110.  
  111.     return true;
  112.   }
  113.  
  114. // Write file
  115.  
  116.   public void write(String path) {
  117.     try {
  118.       FileOutputStream fs = new FileOutputStream(path);
  119.       PrintStream ds = new PrintStream(fs);
  120.  
  121.       for (int i = 0; i < paths.size(); i++)
  122.     ds.println((String) paths.elementAt(i));
  123.     } catch (Exception err) {
  124.       status.setText("Cannot save file.");
  125.     }
  126.   }
  127.  
  128. // Exit editor
  129.  
  130.   public void exit() {
  131.     save();
  132.     System.exit(0);
  133.   }
  134.  
  135. // Handle system event
  136.  
  137.   public boolean handleEvent(Event evt) {
  138.     if (evt.id == Event.WINDOW_DESTROY && evt.target == this)
  139.       exit();
  140.  
  141.     return super.handleEvent(evt);
  142.   }
  143.  
  144. // Handle component events
  145.  
  146.   public boolean action(Event evt, Object obj) {
  147.     String label = (String) obj;
  148.   
  149.   // Handle file menu
  150.  
  151.     if (label.equals("New Project")) {
  152.       project = null;
  153.       saveItem.disable();
  154.       list.clear();
  155.       setTitle("Mini Java Editor");
  156.       return true;
  157.     }
  158.  
  159.     if (label.equals("Open Project...")) {
  160.       FileDialog dialog = new FileDialog(this, "Open Project...", 
  161.     FileDialog.LOAD);
  162.  
  163.       dialog.show();
  164.       String file = dialog.getFile();
  165.  
  166.       if (file != null) {
  167.     if (file.endsWith(".project")) {
  168.       project = dialog.getDirectory() + file;
  169.  
  170.       if (read(project)) {
  171.         setTitle(getFile(project) + " - Mini Java Editor");
  172.         saveItem.enable();
  173.         list.refresh();
  174.         status.setText("");
  175.       }
  176.     }
  177.       }
  178.  
  179.       return true;
  180.     }
  181.  
  182.     if (label.equals("Save Project")) {
  183.       if (project != null) {
  184.     save();
  185.         write(project);
  186.       }
  187.     }
  188.  
  189.     if (label.equals("Save Project As...")) {
  190.       FileDialog dialog = new FileDialog(this, "Save As...", FileDialog.SAVE);
  191.       
  192.       dialog.setFile("Untitled.project");
  193.       dialog.show();
  194.       String file = dialog.getFile();
  195.  
  196.       if (file != null) {
  197.     project = dialog.getDirectory() + file;
  198.     setTitle(getFile(project) + " - Mini Java Editor");
  199.         saveItem.enable();
  200.     save();
  201.     write(project);
  202.       }
  203.  
  204.       return true;
  205.     }
  206.  
  207.     if (label.equals("Exit"))
  208.       exit();
  209.  
  210.   // Handle edit menu
  211.  
  212.     if (label.equals("Add File...")) {
  213.       FileDialog dialog = new FileDialog(this, "Add File...", 
  214.     FileDialog.LOAD);
  215.  
  216.       dialog.show();
  217.       String file = dialog.getFile();
  218.  
  219.       if (file != null) {
  220.     paths.addElement(dialog.getDirectory() + file);
  221.     list.refresh();
  222.       }
  223.     }
  224.  
  225.     if (label.equals("Delete File")) {
  226.       for (int i = 0; i < paths.size(); i++) {
  227.     String path = (String) paths.elementAt(i);
  228.  
  229.     if (path.endsWith(list.getSelectedItem())) {
  230.       paths.removeElementAt(i);
  231.  
  232.       if (windows.containsKey(path)) {
  233.         MJEWindow win = (MJEWindow) windows.get(path);
  234.  
  235.         win.save();
  236.         win.dispose();
  237.       }
  238.     }
  239.       }
  240.  
  241.       list.refresh();
  242.     }
  243.  
  244.   // Handle help menu
  245.  
  246.     if (label.equals("About Mini Java Editor...")) {
  247.       InfoDialog dialog = new InfoDialog(this, label, MJE.about); 
  248.  
  249.       dialog.pack();
  250.       dialog.show();
  251.     
  252.       return true;
  253.     }
  254.     
  255.     return false; 
  256.   }
  257.  
  258. // Get file name from path
  259.  
  260.   public String getFile(String path) {
  261.     char sp = System.getProperty("file.separator").charAt(0);
  262.  
  263.     for (int i = path.length() - 1; i > 0; i--)
  264.       if (path.charAt(i) == sp)
  265.     return path.substring(i + 1, path.length());
  266.  
  267.     return path;  
  268.   }
  269. }
  270.  
  271. // 
  272. // File list area
  273. //
  274.  
  275. class MJEFiles extends List {
  276.   MJE editor;
  277.  
  278.   public MJEFiles(MJE ed, int rows) {
  279.     super(rows, false);
  280.     editor = ed;
  281.   }
  282.  
  283.   public void clear() {
  284.     delItems(0, countItems() - 1);
  285.   }
  286.  
  287.   public void refresh() {
  288.     clear();
  289.  
  290.     for (int i = 0; i < editor.paths.size(); i++)
  291.       addItem(editor.getFile((String) editor.paths.elementAt(i)));
  292.   }
  293.  
  294.   public boolean action(Event evt, Object obj) {
  295.     Hashtable wins = editor.windows;
  296.     String str = (String) obj;
  297.     String path = "";
  298.  
  299.     for (int i = 0; i < editor.paths.size(); i++) {
  300.       String epath = (String) editor.paths.elementAt(i);
  301.  
  302.       if (epath.endsWith(str)) {
  303.     path = epath;
  304.  
  305.     if (wins.containsKey(path))
  306.       ((MJEWindow) wins.get(path)).show();
  307.     else {
  308.       MJEWindow win = new MJEWindow(editor.getFile(path));
  309.       win.open(path);
  310.       wins.put(path, win);
  311.     }
  312.  
  313.     System.out.println(path);
  314.     break;
  315.       }      
  316.     }
  317.  
  318.     return true;
  319.   }
  320. }
  321.