package livework;
/* (c) Alex McLean 2006
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*/
import java.awt.*;
import java.io.*;
import javax.swing.*;
import jedit.*;
/**
* The user interface for a livecoder using this application.
A frame with two components, one being a text editor (of the excellent JEditTextArea class) and the second a textarea any compiler errors (it actually captures everything sent to standard out).
Whenever the livecoder presses ALT-X, the sourcecode is saved, causing the LiveProxy to try to recompile and (if successful) reload it.
* @author Alex McLean - ma503am@gold.ac.uk
* @see LiveProxy
* @see JEditTextArea
* @see ErrTextArea
*/
public class Editor extends JFrame {
/** File containing sourcecode that is being edited */
private File sourcecode;
/** The component for editing the sourcecode */
private JEditTextArea textarea;
/** The textarea showing compiler output */
private ErrTextArea errTextArea;
/** I'm not going to serialize this class, but this avoids a compiler warning */
private static final long serialVersionUID = 1;
/**
* Constructor for this class
* @param sourcecode the file to edit
*/
public Editor(File sourcecode) {
super();
this.sourcecode = sourcecode;
GridBagLayout layout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
setLayout(layout);
c.gridx = 0;
c.gridy = 0;
c.weighty = 0.6f;
add(initSourcecodeComponent(), c);
c.gridx = 0;
c.gridy = 1;
c.weighty = 0.4f;
add(initErrorComponent(), c);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* initialises the pane showing errors
* @return a pane containing the ErrTextarea object
*/
private JComponent initErrorComponent() {
errTextArea = new ErrTextArea();
JScrollPane errorPane = new JScrollPane(errTextArea);
return(errorPane);
}
/**
* initialises the pane containing the text editor
* @return a pane containing the JEditTextArea object with the sourcecode loaded
*/
private Component initSourcecodeComponent () {
textarea = new JEditTextArea();
String source = "";
try {
FileReader file = new FileReader(sourcecode.getAbsoluteFile());
char[] cbuf = new char[1024];
int bytes;
while ((bytes = file.read(cbuf, 0, 1024)) != -1) {
source = source + String.valueOf(cbuf, 0, bytes);
}
}
catch (FileNotFoundException e) {
Util.error("File error", "Couldn't open '" + sourcecode.getAbsoluteFile() + "'" , e);
}
catch (IOException e) {
Util.error("File error", "Couldn't open '" + sourcecode.getAbsoluteFile() + "'" , e);
}
textarea.setTokenMarker(new JavaTokenMarker());
Font f = new Font("Monospaced", 0, 12);
textarea.setFont(f);
textarea.setInputHandler(new EditorInputHandler(this));
textarea.setText(source);
// jump to the first line
textarea.setCaretPosition(0);
return(textarea);
}
/**
* saves the edited file
*/
void save() {
try {
// clear the error text area ready for any error messages
errTextArea.setText("reloading...\n");
FileWriter file = new FileWriter(sourcecode.getAbsoluteFile());
file.write(textarea.getText());
file.close();
}
catch (IOException e) {
Util.error("File error", "Couldn't write to '" + sourcecode.getAbsoluteFile() + "'" , e);
}
}
}