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

  1. //
  2. // Mini Java Editor window
  3. //
  4.  
  5. import java.io.*;
  6. import java.awt.*;
  7.  
  8. class MJEWindow extends Frame {
  9.   TextArea area;
  10.   MJEOutput output;
  11.   String fonts[];
  12.   String styles[] = {"Plain", "Bold", "Italic"};
  13.   String sizes[] = {"8", "9", "10", "12", "14", "16", "18", "24"};
  14.   String clipboard;
  15.   String savePath = null;
  16.   String fname;
  17.   int fstyle;
  18.   int fsize;
  19.  
  20.   MenuItem saveItem = new MenuItem("Save");
  21.   MenuItem compileItem = new MenuItem("Compile");
  22.   CheckboxMenuItem fontItems[];
  23.   CheckboxMenuItem styleItems[];
  24.   CheckboxMenuItem sizeItems[];
  25.  
  26.   public static void main(String args[]) {;
  27.     MJEWindow win = new MJEWindow("Untitled");
  28.  
  29.     if (args.length > 0) {
  30.       win.setTitle(win.getFile(new File(args[0]).getName()));
  31.  
  32.       win.savePath = args[0];
  33.       win.area.setText(win.readFile(win.savePath));
  34.       win.saveItem.enable();
  35.     }
  36.   }
  37.  
  38.   MJEWindow(String title) {
  39.  
  40.   // Initialize
  41.  
  42.     area = new TextArea(35, 80);
  43.     output = new MJEOutput(this, 5);
  44.  
  45.     fonts = getToolkit().getFontList();
  46.     MenuBar menuBar = new MenuBar();
  47.  
  48.     fname = "Courier";
  49.     fstyle = Font.PLAIN;
  50.     fsize = 12;
  51.  
  52.   // Create file menu
  53.  
  54.     Menu fileMenu = new Menu("File");
  55.     fileMenu.add(new MenuItem("New"));
  56.     fileMenu.add(new MenuItem("Open..."));
  57.     fileMenu.add(saveItem);
  58.     fileMenu.add(new MenuItem("Save As..."));
  59.     fileMenu.add(new MenuItem("-"));
  60.     fileMenu.add(compileItem);
  61.     menuBar.add(fileMenu);
  62.  
  63.   // Create edit menu
  64.  
  65.     Menu editMenu = new Menu("Edit");
  66.     editMenu.add(new MenuItem("Cut"));
  67.     editMenu.add(new MenuItem("Copy"));
  68.     editMenu.add(new MenuItem("Paste"));
  69.     editMenu.add(new MenuItem("Select All"));
  70.     editMenu.add(new MenuItem("-"));
  71.     editMenu.add(new MenuItem("Go To Line..."));
  72.     editMenu.add(new MenuItem("Find..."));
  73.     menuBar.add(editMenu);
  74.  
  75.   // Create font menu
  76.  
  77.     Menu fontMenu = new Menu("Font");
  78.     fontItems = new CheckboxMenuItem[fonts.length];
  79.  
  80.     for (int i = 0; i < fonts.length; i++) {
  81.       fontItems[i] = new CheckboxMenuItem(fonts[i]);
  82.       fontMenu.add(fontItems[i]);
  83.     
  84.       if (fonts[i].equals(fname))
  85.     fontItems[i].setState(true);
  86.     }
  87.  
  88.     menuBar.add(fontMenu);
  89.  
  90.   // Create style menu
  91.  
  92.     Menu styleMenu = new Menu("Style");
  93.     styleItems = new CheckboxMenuItem[styles.length];
  94.  
  95.     for (int i = 0; i < styles.length; i++) {
  96.       styleItems[i] = new CheckboxMenuItem(styles[i]);
  97.       styleMenu.add(styleItems[i]);
  98.     
  99.       if (i == fstyle) 
  100.     styleItems[i].setState(true);
  101.     }
  102.  
  103.     menuBar.add(styleMenu);
  104.     
  105.  // Create size menu
  106.  
  107.     Menu sizeMenu = new Menu("Size");
  108.     sizeItems = new CheckboxMenuItem[sizes.length];
  109.  
  110.     for (int i = 0; i < sizes.length; i++) {
  111.       sizeItems[i] = new CheckboxMenuItem(sizes[i]);    
  112.       sizeMenu.add(sizeItems[i]);
  113.  
  114.       if (sizes[i].equals(new Integer(fsize).toString()))
  115.     sizeItems[i].setState(true);
  116.     }
  117.  
  118.     menuBar.add(sizeMenu);
  119.  
  120.   // Show window
  121.  
  122.     setMenuBar(menuBar);
  123.     saveItem.disable();
  124.     compileItem.disable();
  125.  
  126.     area.setFont(new Font(fname, fstyle, fsize));
  127.     setTitle(title);
  128.     output.clear();
  129.  
  130.     add("Center", area);
  131.     add("South", output);
  132.     pack();
  133.     show();
  134.   }
  135.  
  136. // Open file
  137.  
  138.   public boolean open(String path) {
  139.     if (path != null) {
  140.       setTitle(getFile(path));
  141.       savePath = path;
  142.       area.setText(readFile(path));
  143.       saveItem.enable();
  144.       output.clear();
  145.  
  146.       if (path.endsWith(".java")) 
  147.     compileItem.enable();
  148.       else
  149.     compileItem.disable();
  150.  
  151.       return true;
  152.     }
  153.  
  154.     return false;
  155.   }
  156.  
  157.   public boolean save() {
  158.     writeFile(savePath, area.getText());
  159.     return true;
  160.   }
  161.  
  162. // Read file
  163.  
  164.   public String readFile(String fl) {
  165.     String text = new String();
  166.  
  167.     try {
  168.       FileInputStream fs = new FileInputStream(fl);
  169.       DataInputStream ds = new DataInputStream(fs);
  170.       String str = new String();
  171.  
  172.       while (str != null) {
  173.       str = ds.readLine();
  174.  
  175.     if (str != null)
  176.       text = text + str + "\n";
  177.       }
  178.     } catch (Exception err) {
  179.       System.out.println("Cannot open file.");
  180.     }
  181.  
  182.     return text;
  183.   }
  184.  
  185. // Write file
  186.  
  187.   public void writeFile(String fl, String txt) {
  188.     try {
  189.       FileOutputStream fs = new FileOutputStream(fl);
  190.       DataOutputStream ds = new DataOutputStream(fs);
  191.     String ls = System.getProperty("line.separator");
  192.  
  193.     for (int i = 0; i < txt.length(); i++) {
  194.         char ch = txt.charAt(i);
  195.  
  196.       switch (ch) {
  197.         case '\n':
  198.           ds.writeBytes(ls);
  199.         break;
  200.         
  201.         default:
  202.         ds.write(ch);
  203.        }
  204.       }
  205.     } catch (Exception err) {
  206.       System.out.println("Cannot save file.");
  207.     }
  208.   }
  209.  
  210. // Handle system event
  211.  
  212.   public boolean handleEvent(Event evt) {
  213.     if (evt.id == Event.WINDOW_DESTROY && evt.target == this)
  214.       hide();
  215.  
  216.     return super.handleEvent(evt);
  217.   }
  218.  
  219. // Handle component events
  220.  
  221.   public boolean action(Event evt, Object obj) {
  222.     String label = (String) obj;
  223.     String file = null;
  224.   
  225.   // Handle file menu
  226.  
  227.     if (label.equals("New")) {
  228.       area.setText("");
  229.       setTitle("Untitled");
  230.       saveItem.disable();
  231.       output.clear();
  232.  
  233.       return true;
  234.     }
  235.  
  236.     if (label.equals("Open...")) {
  237.       FileDialog dialog = new FileDialog(this, "Open...", FileDialog.LOAD);
  238.  
  239.       dialog.show();
  240.       file = dialog.getFile();
  241.  
  242.       if (file != null)
  243.     open(dialog.getDirectory() + file);
  244.  
  245.       return true;
  246.     }
  247.  
  248.     if (label.equals("Save")) {
  249.       writeFile(savePath, area.getText());
  250.     }
  251.  
  252.     if (label.equals("Save As...")) {
  253.       FileDialog dialog = new FileDialog(this, "Save As...", FileDialog.SAVE);
  254.       
  255.       dialog.setFile(savePath);
  256.       dialog.show();
  257.       file = dialog.getFile();
  258.  
  259.       if (file != null) {
  260.     setTitle(file);
  261.     savePath = dialog.getDirectory() + file;
  262.         saveItem.enable();
  263.     writeFile(file, area.getText());
  264.       }
  265.  
  266.       return true;
  267.     }
  268.  
  269.     if (label.equals("Compile")) {
  270.       try {
  271.     output.delItems(0, output.countItems() - 1);
  272.     System.out.println(output.countItems());
  273.         output.addItem("Compiling " + savePath + "...");        
  274.     writeFile(savePath, area.getText());
  275.  
  276.     Process ps = Runtime.getRuntime().exec("javac " + savePath);    
  277.     DataInputStream ds = new DataInputStream(ps.getInputStream());
  278.  
  279.         String str = new String();
  280.  
  281.         while (str != null) {
  282.         str = ds.readLine();
  283.  
  284.       if (str != null)    
  285.          if (str.startsWith(savePath)) {
  286.           String msg = str.substring(savePath.length() + 1);
  287.  
  288.           for (int i = 1; i < 5; i++)
  289.             if (msg.charAt(i) == ':') {
  290.           int pos = 0;
  291.  
  292.                String line = msg.substring(0, i);
  293.               String err = msg.substring(i + 2, msg.length());
  294.           String code = ds.readLine().trim();
  295.           String tmp = ds.readLine();
  296.  
  297.           for (int j = 0; j < tmp.length(); j++)
  298.             if (tmp.charAt(j) == '^')
  299.               pos = j;
  300.  
  301.               output.addItem("*** " + line + " (" + pos + "): " + code + 
  302.             " (" + err + ").");
  303.         }
  304.         }
  305.         }
  306.  
  307.         output.addItem("Ready.");
  308.       }
  309.       catch (Exception err) { }
  310.     }
  311.  
  312.   // Handle edit menu
  313.  
  314.     if (label.equals("Cut")) {
  315.       clipboard = area.getSelectedText();
  316.       
  317.     area.replaceText("", area.getSelectionStart(), 
  318.         area.getSelectionEnd());
  319.  
  320.     return true;
  321.     }
  322.  
  323.     if (label.equals("Copy")) {
  324.       clipboard = area.getSelectedText();
  325.     return true;
  326.     }
  327.  
  328.     if (label.equals("Paste")) {
  329.       int start = area.getSelectionStart();
  330.       int end = area.getSelectionEnd();
  331.  
  332.       if (start == end) 
  333.         area.insertText(clipboard, start);
  334.       else
  335.         area.replaceText(clipboard, start, end);
  336.  
  337.       return true;
  338.     }
  339.  
  340.     if (label.equals("Select All"))
  341.       area.selectAll();
  342.  
  343.     if (label.equals("Go To Line...")) {
  344.       GotoDialog dialog = new GotoDialog(this);
  345.       dialog.pack();
  346.       dialog.show();
  347.     
  348.       return true;
  349.     }
  350.  
  351.     if (label.equals("Find...")) {
  352.       FindDialog dialog = new FindDialog(this);
  353.       dialog.pack();
  354.       dialog.show();
  355.     
  356.       return true;
  357.     }
  358.  
  359.   // Handle font menu
  360.  
  361.     for (int i = 0; i < fonts.length; i++) {
  362.       if (label.equals(fonts[i])) {
  363.     for (int j = 0; j < fonts.length; j++) 
  364.       if (i != j)
  365.         fontItems[j].setState(false);
  366.  
  367.       fname = label;
  368.       area.setFont(new Font(fname, fstyle, fsize));
  369.         
  370.       return true;
  371.     }
  372.     }
  373.  
  374.   // Handle style menu
  375.  
  376.     for (int i = 0; i < styles.length; i++) {
  377.       if (label.equals(styles[i])) {
  378.      for (int j = 0; j < styles.length; j++) 
  379.       if (i != j)
  380.         styleItems[j].setState(false);
  381.         
  382.      fstyle = i;
  383.     area.setFont(new Font(fname, fstyle, fsize));
  384.       
  385.     return true;
  386.       }
  387.     }
  388.  
  389.   // Handle size menu
  390.  
  391.     for (int i = 0; i < sizes.length; i++) {
  392.       if (label.equals(sizes[i])) {
  393.     for (int j = 0; j < sizes.length; j++) 
  394.       if (i != j)
  395.         sizeItems[j].setState(false);
  396.         
  397.      fsize = new Integer(label).intValue();
  398.     area.setFont(new Font(fname, fstyle, fsize));
  399.  
  400.     return true;
  401.       }
  402.     }
  403.     
  404.     return false; 
  405.   }
  406.  
  407. // Get text area
  408.  
  409.   public TextArea getTextArea() {
  410.     return area;
  411.   }
  412.  
  413.  
  414. // Get file name from path
  415.  
  416.   public String getFile(String path) {
  417.     char sp = System.getProperty("file.separator").charAt(0);
  418.  
  419.     for (int i = path.length() - 1; i > 0; i--)
  420.       if (path.charAt(i) == sp)
  421.     return path.substring(i + 1, path.length());
  422.  
  423.     return path;  
  424.   }
  425.  
  426. // Go to line
  427.  
  428.   public void gotoLine(int line) {
  429.     gotoLine(line, 0);
  430.   }
  431.  
  432.   public void gotoLine(int line, int col) {
  433.     int i = 1, pos = 0;
  434.     String str = area.getText();
  435.  
  436.     while (i < line) {
  437.       if (str.charAt(pos) == '\n')
  438.     i++;
  439.  
  440.       pos++;
  441.     }
  442.  
  443.     area.requestFocus();
  444.     area.select(pos + col, pos + col);
  445.   }      
  446. }
  447.  
  448. //
  449. // Output area
  450. //
  451.  
  452. class MJEOutput extends List {
  453.   MJEWindow win;
  454.  
  455.   public MJEOutput(MJEWindow w, int rows) {
  456.     super(rows, false);
  457.     win = w;
  458.   }
  459.  
  460.   public void clear() {
  461.     delItems(0, countItems() - 1);
  462.     addItem("Ready.");
  463.   }
  464.  
  465.   public boolean action(Event evt, Object obj) {
  466.     String str = (String) obj;
  467.  
  468.     if (str.charAt(0) == '*') { 
  469.       for (int i = 5; i < 9; i++)
  470.         if (str.charAt(i) == ' ') {
  471.       int line = new Integer(str.substring(4, i)).intValue();
  472.       int col = 0;
  473.  
  474.       for (int j = i + 2; j < i + 5; j++)
  475.         if (str.charAt(j) == ')') 
  476.           col = new Integer(str.substring(i + 2, j)).intValue();
  477.  
  478.       win.gotoLine(line, col);
  479.       break;
  480.         }
  481.     }
  482.  
  483.     return super.action(evt, obj);
  484.   }
  485. }
  486.