TextEditor.java

/* Imports */ import java.awt.*; import java.awt.event.*; import java.io.*; import java.nio.*; import javax.swing.*; /** * TextEditor.java * * Simple text editor that shows how to read and write files. * * @author D.Barry **/ public class TextEditor implements Runnable{ /* Constants */ private static final String NAME = "TextEditor"; private static final int WIDTH = 500; private static final int HEIGHT = 500; /* Instance variables */ private String content; private File file; private JTextArea textArea; /** * main() * * Entry point into the program. * * @param args The arguments to the program. **/ public static void main(String[] args){ if(args.length == 1){ /* Create a new program */ TextEditor te = new TextEditor(args[0]); /* Run this program in the EDT thread */ SwingUtilities.invokeLater(te); }else{ /* Display an error message */ error(); } } /** * TextEditor() * * The text editor to be displayed on screen. * * @param path The name of the file to be loaded. **/ public TextEditor(String path){ /* Make a file object so we can start inferring some truths */ file = new File(path); /* Check if a file or folder exists */ if(file.exists()){ /* Check if we have a file or folder */ if(file.isDirectory()){ /* Display an error message */ error(); } }else{ /* Create a blank file */ createFile(file); } /* Read the file */ content = readFile(file); } /** * createFile() * * Create a new file. * * @param file The file to be created. **/ private void createFile(File file){ try{ file.createNewFile(); }catch(IOException e){ /* Do nothing */ } } /** * readFile() * * Read the files into a String object. * * @param file The file to be read. * @return The contents on the file, otherwise NULL. **/ private String readFile(File file){ String data = null; FileReader fr = null; try{ fr = new FileReader(file); /* Create a buffer to read the bytes into */ char[] buffer = new char[(int)file.length()]; /* Make the read */ fr.read(buffer); data = new String(buffer); /* Close the file for everybody else */ fr.close(); }catch(IOException e){ /* Do nothing */ }finally{ /* If we did manage to open the file, don't lock it up */ if(fr != null){ /* Try to finish what we started */ try{ fr.close(); }catch(IOException e){ /* Do nothing */ } } } return data; } /** * writeFile() * * Writes the file to the disk. * * @param file The file to be written. * @param data The data be be written to the disk. **/ private void writeFile(File file, String data){ FileOutputStream out = null; try{ out = new FileOutputStream(file); }catch(FileNotFoundException e){ /* Handle error */ writeError(); } try{ if(out != null){ out.write(data.getBytes()); out.close(); } }catch(IOException e){ /* Handle error */ writeError(); } } /** * writeError() * * Display a message box to tell the user there has been an issue, then exit * the system. **/ private void writeError(){ /* Display message */ JOptionPane.showMessageDialog( null, "Failed to write file.", "Write Error", JOptionPane.INFORMATION_MESSAGE ); /* Dirty exit */ System.exit(0); } /** * run() * * Main entry point for the GUI. **/ public void run(){ /* Create the frame that we will put everything in */ JFrame jFrame = new JFrame(NAME); /* Make sure that it is able to close */ jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); /* Set the size of the frame */ jFrame.setPreferredSize(new Dimension(WIDTH, HEIGHT)); /* Create the text area */ textArea = new JTextArea(); /* Lines can be wrapped */ textArea.setLineWrap(true); textArea.setWrapStyleWord(true); textArea.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); textArea.setText(content); /* Add a scroll bar */ jFrame.add(new JScrollPane(textArea)); /* Listen to closing event */ jFrame.addWindowListener( new WindowAdapter(){ /** * windowClosing() * * This method captures the close event on a frame. * * @param e The even to cause the close. **/ @Override public void windowClosing(WindowEvent e){ /* Auto-save file */ writeFile(file, textArea.getText()); } } ); /* Put all of the elements in the frame */ jFrame.pack(); /* Make the frame visible to all */ jFrame.setVisible(true); } /** * error() * * Displays a message that indicates the correct use of this program. **/ private static void error(){ System.out.println( "\nTextEditor <FILE>" + "\n" + "\n FILE" + "\n" + "\n Requires a valid file in order to be loaded, otherwise a" + "\n new file is created. This parameter cannot be excluded" + "\n when running the program." + "\n" ); /* A very bad practice hard quit from the program */ System.exit(0); } }