/** * Class Document is used to read data from, * and write data to disk files. The read-in data * is stored in a single String object. * @author David Thibodeau * @version 14-Apr-2002 modified 26-Aug-2002 */ import java.io.*; import java.util.*; public class Document { /** * readInFromFile method - This reads the specified file * * @param infilename the name of the file to be opened * @return String the text retrieved from the opened * file */ public static String readInFromFile(String infilename) { String line; // holds a line of text brought in from the file // holds the complete text file StringBuffer filedata = new StringBuffer(); try { BufferedReader in = new BufferedReader( new FileReader( infilename ) ); line = in.readLine(); // read first line of text file while ( line != null ) // continue until end of file { filedata.append( line ); // append text file data // to StringBuffer filedata.append( "\n" ); // create a new line line = in.readLine(); // read next line in text file } // end while ( line != null ) in.close(); // closes text file } // end of try block catch ( IOException iox ) // couldn't find the file { System.out.println("Problem reading " + infilename ); } return filedata.toString(); // return text file data } // end of readInFromFile(String infilename) /** * writeToFile method - creates a text file from the * String passed to it * @param outfilename the name of the file to be written * @param filedata data to be written to the file * @return void */ public static void writeToFile(String outfilename, String filedata) { try { BufferedWriter out = new BufferedWriter( new FileWriter( outfilename ) ); out.write( filedata ); out.close(); } catch ( IOException iox ) { System.out.println("Problem writing " + outfilename ); } } // end of writeColumnToFile(String outfilename) } // end Document Class //////////////////////////////////////////////////////////// /** * Class TextEditGUI creates a screen that allows the user * to manipulate text in a text area. The user can cut, copy * and paste text. Also, the user can load and save text files. * * @author David Thibodeau * @version 17-Aug-2002 */ import java.awt.* ; import java.awt.event.*; import javax.swing.*; public class TextEditGUI extends JFrame implements ActionListener { // button panel - holds buttons to cut, copy, paste, load & save JPanel buttonPanel; JButton buttonCut; JButton buttonCopy; JButton buttonPaste; JButton buttonLoad; JButton buttonSave; // text panel - holds the text area JPanel textPanel; JScrollPane textScrollPane; JTextArea textArea; // filename panel - holds text field for filename, and OK / Cancel buttons JPanel filenamePanel; JPanel filenameButtonPanel; JPanel filenameTextPanel; JLabel filenameLabel; JTextField filenameText; JButton filenameOK; JButton filenameCancel; /** * Constructor for objects of class TextEditGUI */ public TextEditGUI() { setTitle( "Text Editor Example" ); setDefaultCloseOperation( EXIT_ON_CLOSE ); // button panel Icon cutIcon = new ImageIcon( "C:\\Text Editor\\Cut.gif" ); buttonCut = new JButton( cutIcon ); buttonCut.addActionListener(this); buttonCut.setActionCommand( "Cut" ); Icon copyIcon = new ImageIcon( "C:\\Text Editor\\Copy.gif" ); buttonCopy = new JButton( copyIcon ); buttonCopy.addActionListener(this); buttonCopy.setActionCommand( "Copy" ); Icon pasteIcon = new ImageIcon( "C:\\Text Editor\\Paste.gif" ); buttonPaste = new JButton( pasteIcon ); buttonPaste.addActionListener(this); buttonPaste.setActionCommand( "Paste" ); Icon loadIcon = new ImageIcon( "C:\\Text Editor\\Load.gif" ); buttonLoad = new JButton( loadIcon ); buttonLoad.addActionListener(this); buttonLoad.setActionCommand( "Load" ); Icon saveIcon = new ImageIcon( "C:\\Text Editor\\Save.gif" ); buttonSave = new JButton( saveIcon ); buttonSave.addActionListener(this); buttonSave.setActionCommand( "Save" ); buttonPanel = new JPanel(); buttonPanel.add( buttonLoad ); buttonPanel.add( buttonSave ); buttonPanel.add( buttonCut ); buttonPanel.add( buttonCopy ); buttonPanel.add( buttonPaste ); // text panel textArea = new JTextArea( 10, 40 ); textScrollPane = new JScrollPane( textArea ); textPanel = new JPanel(); textPanel.add( textScrollPane ); // filename panel filenameLabel = new JLabel(); // will set text when needed filenameText = new JTextField(30); filenameText.setEnabled( false ); filenameOK = new JButton( "OK" ); filenameOK.addActionListener(this); filenameOK.setActionCommand( "OK" ); filenameOK.setEnabled( false ); filenameCancel = new JButton( "Cancel" ); filenameCancel.addActionListener(this); filenameCancel.setActionCommand( "Cancel" ); filenameCancel.setEnabled( false ); filenameTextPanel = new JPanel(); filenameTextPanel.add( filenameLabel ); filenameTextPanel.add( filenameText ); filenameButtonPanel = new JPanel(); filenameButtonPanel.setLayout( new BoxLayout( filenameButtonPanel, BoxLayout.X_AXIS ) ); filenameButtonPanel.add( filenameOK ); filenameButtonPanel.add( filenameCancel ); filenamePanel = new JPanel(); filenamePanel.setLayout( new BoxLayout( filenamePanel, BoxLayout.Y_AXIS ) ); filenamePanel.add( filenameTextPanel ); filenamePanel.add( filenameButtonPanel ); // content pane getContentPane().setLayout( new BoxLayout( getContentPane(), BoxLayout.Y_AXIS ) ); getContentPane().add( buttonPanel ); getContentPane().add( textPanel ); getContentPane().add( filenamePanel ); } /** * actionPerformed method. This method handles the GUI click events * for the application. In the input section, the radiobutton selection * determines whether the textfield should hold a band name or a file * name. When the Enter button is pressed, either the band name will be * added to the tree, or the specified file will be opened (if it exists) * and all lines of text will be added to the tree. When the file is read * each line will be considered a band name. * For the output section, select the output order desired, then click * the output button. The output will then be sent to the console. * @param ActionEvent supplied by Java's Swing event handler * @return void */ public void actionPerformed( ActionEvent evt ) { if ( evt.getActionCommand().equals( "Cut" ) ) { textArea.cut(); } else if( evt.getActionCommand().equals( "Copy" ) ) { textArea.copy(); } else if( evt.getActionCommand().equals( "Paste" ) ) { textArea.paste(); } else if( evt.getActionCommand().equals( "Load" ) ) { filenameLabel.setText( "Enter name of file to load:" ); filenameText.setEnabled( true ); filenameOK.setEnabled( true ); filenameCancel.setEnabled( true ); } else if( evt.getActionCommand().equals( "Save" ) ) { filenameLabel.setText( "Enter name of file to save:" ); filenameText.setEnabled( true ); filenameOK.setEnabled( true ); filenameCancel.setEnabled( true ); } else if( evt.getActionCommand().equals( "OK" ) ) { if( filenameLabel.getText() == "Enter name of file to load:" ) { if( filenameText.getText().length() < 1 ) { } else { textArea.setText( Document.readInFromFile( filenameText.getText() ) ); } } else if( filenameLabel.getText() == "Enter name of file to save:" ) { if( filenameText.getText().length() < 1 ) { } else { Document.writeToFile( filenameText.getText(), textArea.getText() ); } } filenameLabel.setText( null ); filenameText.setText( null ); filenameText.setEnabled( false ); filenameOK.setEnabled( false ); filenameCancel.setEnabled( false ); } else if( evt.getActionCommand().equals( "Cancel" ) ) { filenameLabel.setText( null ); filenameText.setText( null ); filenameText.setEnabled( false ); filenameOK.setEnabled( false ); filenameCancel.setEnabled( false ); } } // End actionPerformed( ActionEvent evt ) method /** * main method. This creates a new instance of TextEditGUI, then * instantiates a new WindowQuitter() object and lastly sets the * size and visibility of the GUI window. */ public static void main ( String[] args ) { // New GUI instance TextEditGUI textApp = new TextEditGUI(); // New WindowQuitter instance WindowQuitter wquit = new WindowQuitter(); textApp.addWindowListener( wquit ); // Set window size & make visible textApp.setSize( 500, 400 ); textApp.setVisible( true ); } // end main } // End Class TextEditGUi //////////////////////////////////////////////////////// class WindowQuitter extends WindowAdapter { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } }