WebNFS Developer's Guide

Appendix C The DemoEditor Sample Program

The DemoEditor Sample Program

The DemoEditor demonstrates a use of the XFileChooser object. It brings up a window with a File Menu and Option Menu. The File Menu allows you to open, save, and close a file. The XFileChooser object comes up when you do an Open or a Save. Figure 5-6 shows what the DemoEditor program looks like; "The DemoEditor Sample Program" explains this program at length.

To compile the DemoEditor demo on Solaris, type as follows:


% javac -classpath ../../xfilechooser.jar DemoEditor.java

To run the compiled bytecode:


% java -classpath ../../xfilechooser.jar:. DemoEditor

If compiling or running on Windows, use backslashes in the path, and semicolon as separator, e.g.:


% java -classpath ..\..\xfilechooser.jar;. DemoEditor

Note -

You must be using the java in JDK1.2, thus set your path accordingly. If you are using a Java2 release prior to JDK 1.2.1 you may need the JFileChooser patch to fix bug 4169763. The XFileChooser release notes describe how to use the the Xbootclasspath to include the patch.



Example C-1 The DemoEditor Sample Program

/*
 * @(#)DemoEditor.java	1.2 99/04/18
 *
 * Copyright 1999 by Sun Microsystems, Inc.,
 * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Sun Microsystems, Inc. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Sun.
 */

/*
 * DemoEditor.java
 * A text editor which demonstrates how to use the XFileChooser.
 * The Editor can save and restore a text file full of simple styles
 * and have some simple options (e.g. cut, paste, copy)
 */

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
import com.sun.xfile.*;
import com.sun.xfilechooser.*;

public class DemoEditor extends JFrame implements ActionListener {

    JTextPane jtp = new JTextPane();
    JScrollPane jsp = new JScrollPane(jtp);
    ColoredAttributeSet stdoutSet;
    JMenuItem saveItem, openItem, exitItem, closeItem;
    JMenuItem cutItem, pasteItem, copyItem;
    
    public DemoEditor() {
	super("DemoEditor");

	addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
		System.exit(0);
	    }});

	setSize(600,500);

	jtp.setCaretColor(Color.magenta);
	
	JMenuBar jmb = new JMenuBar();
	JMenu fileMenu = new JMenu("File");
	saveItem = new JMenuItem("Save As");
	saveItem.addActionListener(this);
	openItem = new JMenuItem("Open");
	openItem.addActionListener(this);
	exitItem = new JMenuItem("Exit");
	exitItem.addActionListener(this);
	closeItem = new JMenuItem("Close");
	closeItem.addActionListener(this);


	fileMenu.add(openItem);
	fileMenu.add(saveItem);
	fileMenu.add(closeItem);
	fileMenu.add(exitItem);
	
	OptionListener optL = new OptionListener();
	JMenu optMenu = new JMenu("Options");
	cutItem = new JMenuItem("Cut");
	cutItem.addActionListener(optL);
	pasteItem = new JMenuItem("Paste");
	pasteItem.addActionListener(optL);
	copyItem = new JMenuItem("Copy");
	copyItem.addActionListener(optL);

	optMenu.add(cutItem);
	optMenu.add(pasteItem);
	optMenu.add(copyItem);
	
	jmb.add(fileMenu);
	jmb.add(optMenu);
	setJMenuBar(jmb);
	Container contentPane = getContentPane();
	contentPane.add(jsp);
	// set attribute style
	stdoutSet = new ColoredAttributeSet(Color.black);

    }

    public void init() {
	Document doc = jtp.getDocument();
    }

    public String retrieveAsString(XFileInputStream is) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(is));
	StringBuffer buffer = new StringBuffer(4096);
	String line;

	while((line = br.readLine()) != null) {
	    buffer.append(line);
	    buffer.append('\n');
	}

	br.close();
	return buffer.toString();
    }

    public void readFromFile(XFile file) {
	Document doc = jtp.getDocument();
	try {
	    XFileInputStream is = new XFileInputStream(file);
	    String ins = retrieveAsString(is);
	    doc.insertString(0, ins, stdoutSet);
	    jtp.setCaretPosition(0);
	    is.close();
	} catch (Exception e) {}
    }

    public void writeToFile(XFile file) {
	Document doc = jtp.getDocument();
	String s = null;
	
	try {
	    XFileOutputStream os = new XFileOutputStream(file);
	    s = doc.getText(0, doc.getLength());
	    os.write(s.getBytes(), 0, doc.getLength());
	    os.close();
	} catch (Exception e) {}
	
    }

    public void closeDocument() {
	Document doc = jtp.getDocument();
	try {
	    doc.remove(0, doc.getLength());
	} catch (Exception e) {}
    }
    

    public void actionPerformed(ActionEvent ae) {
	int retval;
	XFileChooser chooser = new XFileChooser();
      	if (ae.getSource() == closeItem) {
	    closeDocument();
	} else if (ae.getSource() == openItem) {
	    retval = chooser.showOpenDialog(this);
	    if(retval == XFileChooser.APPROVE_OPTION) {
		XFile theFile = chooser.getSelectedXFile();
		closeDocument();
		if(theFile != null)
		    readFromFile(theFile);
	    }
	} else if (ae.getSource() == saveItem) {
	    retval = chooser.showSaveDialog(this);
	    if(retval == XFileChooser.APPROVE_OPTION) {
		XFile theFile = chooser.getSelectedXFile();
		if(theFile != null)
		    writeToFile(theFile);
	    } 
	} else if (ae.getSource() == exitItem) {
	    System.exit(0);
	}
    }

    public static void main(String args[]) {
	DemoEditor tc = new DemoEditor();
	tc.init();
	tc.setVisible(true);
    }

    public class ColoredAttributeSet extends SimpleAttributeSet {
	public ColoredAttributeSet(Color c) {
	    super();
	    StyleConstants.setForeground(this, c);
	}
    }

    public class OptionListener implements ActionListener {
	public void actionPerformed(ActionEvent ae) {
	    Object src = ae.getSource();
	    if (src == cutItem)
		jtp.cut();
	    else if (src == copyItem)
		jtp.copy();
	    else if (src == pasteItem) 
		jtp.paste();
	}
    }

}