i need help to program a distributed text editor. This editor will be used concurrently by different users on different machines and having the same interface to everybody. people will have to agree on some actions while working on the text file
Following is a simple text editor
java file
help will be very much appreciated!
///////////////////////////////////////////////////////////////////////////
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class SimpleEditor extends Frame implements ActionListener {
private MenuBar menuBar;
private Menu fileMenu;
private TextArea text;
private FileDialog fileDialog;
private
String fileName;
public SimpleEditor() {
super("Simple Editor");
menuBar = new MenuBar();
setMenuBar(menuBar);
fileMenu = new Menu("File");
menuBar.add(fileMenu);
fileMenu.add(new MenuItem("New"));
fileMenu.add(new MenuItem("Open"));
fileMenu.add(new MenuItem("Save"));
fileMenu.add(new MenuItem("Save as..."));
fileMenu.add(new MenuItem("Exit"));
fileMenu.addActionListener(this);
fileDialog = new FileDialog(this);
text = new TextArea("",20,70,TextArea.SCROLLBARS_BOTH);
text.setEditable(true);
text.setFont(new Font("Courier", Font.PLAIN, 12)); // const. width font
add(text);
pack();
show();
}
// actionPerformed handles menu selections
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if(cmd.equals("Exit")) {
this.dispose();
System.exit(0);
}
else if(cmd.equals("Open")) {
fileDialog.show();
fileName = fileDialog.getDirectory()+fileDialog.getFile();
displayFile(fileName);
}
else if(cmd.equals("Save")) {
if(fileName == null) {
fileDialog.show();
fileName = fileDialog.getDirectory()+fileDialog.getFile();
}
writeFile(fileName);
}
else if(cmd.equals("Save as...")) {
fileDialog.show();
fileName = fileDialog.getDirectory()+fileDialog.getFile();
writeFile(fileName);
}
else if(cmd.equals("New")) {
text.setText("");
fileName = null;
}
}
// Method to write the text in the TextArea to a specified file
private void writeFile(String filename) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
String data = text.getText();
bw.write(data);
bw.close();
} catch(IOException ioe) {
System.out.println("Exception " + ioe);
}
}
// Method to read and display a file in the TextArea
private void displayFile(String fileName) {
String line;
text.setText(""); // Set text area to `nothing'
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
line = br.readLine();
while(line != null) { // While we have valid lines of text
text.append(line+'\n'); // Append lines to TextArea + \n
line = br.readLine(); // ... and get next line
}
br.close();
} catch(IOException ioe) {
System.out.println("Exception " + ioe);
}
}
public static void main(String args[]) {
SimpleEditor f = new SimpleEditor();
if(args.length == 1) { // If we have a filename specified
f.fileName = args[0];
f.displayFile(f.fileName); // load the file and display it
}
else
f.fileName = null;
}
}