File:  [Repository] / edoc-applet / Console.java
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Jun 17 10:38:55 2003 UTC (20 years, 10 months ago) by rogo
Branches: vend, MAIN
CVS tags: start, HEAD
First checkin.


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.*;
import java.util.Locale;
import javax.swing.event.*;
import java.io.*;

public class Console extends JDialog implements ActionListener
{

  static JTextField field = new JTextField("0.00", 6);
  JLabel label = new JLabel("Load step: ", JLabel.RIGHT);
  JButton clear = new JButton("Clear Console");
  JButton save = new JButton("Save Console");
  JCheckBox logMode = new JCheckBox("Logging on",true);
  JCheckBox debugMode = new JCheckBox("Debug on",false); 
  JPanel panel = new JPanel();
  static JTextArea console = new JTextArea();
  static JScrollPane scroll = new JScrollPane(console);
  JFileChooser saveChooser;
  JPanel content;
  JFrame frame;
  String file;
  static PrintStream print;
  boolean append = false;
  public Console(JFrame frame)
  {
    super(frame);
    this.frame = frame;
    label = new JLabel("Load step: ", JLabel.RIGHT);
    clear = new JButton("Clear Console");
    save = new JButton("Save Console");
    logMode = new JCheckBox("Logging on",true);
    debugMode =  new JCheckBox("Debug on",false);
    console = new JTextArea();
    scroll = new JScrollPane(console);
    setSize(500, 400);
    setTitle("Archimedes Console");
    console.setBackground(new Color(230, 240, 250));
    Font actFont = new Font("Courier", 0, 15);
    console.setFont(actFont);
    setLocation(new Point(500, 70));
    content = (JPanel) getContentPane();
    content.add(scroll, "Center");
    panel.add(clear);
    panel.add(save);
    panel.add(logMode);
    panel.add(debugMode);
    content.add(panel, "South");
    clear.addActionListener(this);
    save.addActionListener(this);
    debugMode.addActionListener(this);
    setSaveChooser();
    add("Archimedes Console\n");
    if(print==null)
    {
      print = new PrintStream(System.out)
      {
        public PrintStream myOut = System.out;
        public void print(String a)
        {
          myOut.print(a);
          if (logMode.isSelected())
            Console.add(a);
        }
        public void print(char a)
        {
          myOut.print(a);
          if (logMode.isSelected())
            Console.add(a+"");
        }
    
        public void println(String a)
        {
          myOut.println(a);
          if (logMode.isSelected())
            Console.add(a + "\n");
        }
        public void println(Object a)
        {
          myOut.println(a);
          if (logMode.isSelected())
            Console.add(a + "\n");
        }
        public void write(byte[] b ) throws IOException
        {
          myOut.write(b);
          if (logMode.isSelected())
          Console.add(new String(b) + "\n");
            
        }
        public void write(byte[] b,int start,int end ) 
        {
          myOut.write(b,start,end);
          if (logMode.isSelected())
          Console.add(new String(b,start,end) + "\n");
            
        }
  
  
      };
      System.setOut(print);
      System.setErr(System.out);
    }
    addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        //jife.showConsole.setState(false);
      }
    });

    //console.setEditable(false);
  }
  public void actionPerformed(ActionEvent e)
  {
    String command = e.getActionCommand();
    if (command == "Clear Console")
      clear();
    if (command == "Save Console")
    {
      saveChooser.rescanCurrentDirectory();
      saveChooser.setDialogType(JFileChooser.OPEN_DIALOG);
      saveChooser.showDialog(frame, "Save");
    }
   Upload.debug=debugMode.isSelected();
  
  
  }
  public static void clear()
  {
    console.setText("");
  }

  public static void add(String text)
  {
    console.append(text);
    scroll.getVerticalScrollBar().setValue(scroll.getVerticalScrollBar().getMaximum());
  }
  public void setSaveChooser()
  {
    // file Dialog
    saveChooser = new JFileChooser(".");
   // ExampleFileFilter filefilter = new ExampleFileFilter();
   // filefilter.addExtension("txt");
    //filefilter.setDescription("Txt File");
    //saveChooser.setFileFilter(filefilter);

    saveChooser.addActionListener(new ActionListener()
    {

      public void actionPerformed(ActionEvent e2)
      {
        //System.out.println(e2.getActionCommand());
        if (e2.getActionCommand().toString().equals("ApproveSelection"))
        {
          file = saveChooser.getSelectedFile().toString();
          //setTitle("Jife - Java Application for Structural Analysis - "+"<File: "+new File(file).getAbsolutePath()+">");
          if (file == null || file == "")
            file = "." + File.separator + "untitled.txt";
          if (file.endsWith(".txt") || file.endsWith(".TXT"));
          else
            file += ".txt";
          System.out.println("File" + file);
          try
          {

            FileOutputStream fout = new FileOutputStream(file);
            fout.write(console.getText().getBytes());
            fout.close();
          } catch (Exception e3)
          {
            System.out.println("Serial errror:" + e3);
          }

        }
      }
    });

  }

} // to class

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>