Source of FileOrganizer.java


  1: import javax.swing.JFrame;
  2: import javax.swing.JButton;
  3: import javax.swing.JFrame;
  4: import javax.swing.JTextField;
  5: import java.awt.Color;
  6: import java.awt.Container;
  7: import java.awt.FlowLayout;
  8: import java.awt.Graphics;
  9: import java.awt.event.ActionEvent;
 10: import java.awt.event.ActionListener;
 11: import java.io.File;
 12: import java.io.FileNotFoundException;
 13: import java.util.Scanner;

 15: public class FileOrganizer extends JFrame implements ActionListener
 16: {
 17:     public static final int WIDTH = 400;
 18:     public static final int HEIGHT = 300;
 19:     public static final int NUMBER_OF_CHAR = 30;
 20:     private JTextField fileNameField;
 21:     private JTextField firstLineField;
 22:         
 23:     public FileOrganizer( )
 24:     {
 25:         setSize(WIDTH, HEIGHT);
 26:         WindowDestroyer listener = new WindowDestroyer( );
 27:         addWindowListener(listener);
 28:                 
 29:         Container contentPane = getContentPane( );
 30:         contentPane.setLayout(new FlowLayout( ));
 31:                 
 32:         JButton showButton = new JButton("Show first line");
 33:         showButton.addActionListener(this);
 34:         contentPane.add(showButton);
 35:                 
 36:         JButton removeButton = new JButton("Remove file");
 37:         removeButton.addActionListener(this);
 38:         contentPane.add(removeButton);
 39:                 
 40:         JButton resetButton = new JButton("Reset");
 41:         resetButton.addActionListener(this);
 42:         contentPane.add(resetButton);
 43:                 
 44:         fileNameField = new JTextField(NUMBER_OF_CHAR);
 45:         contentPane.add(fileNameField);
 46:                 
 47:         fileNameField.setText("Enter file name here.");
 48:         firstLineField = new JTextField(NUMBER_OF_CHAR);
 49:         contentPane.add(firstLineField);
 50:     }
 51:         
 52:     public void actionPerformed(ActionEvent e)
 53:     {
 54:                 String actionCommand = e.getActionCommand( );
 55:                 if (actionCommand.equals("Show first line"))
 56:                     showFirstLine( );
 57:                 else if (actionCommand.equals("Remove file"))
 58:                     removeFile( );
 59:                 else if (actionCommand.equals("Reset"))
 60:                     resetFields();
 61:                 else
 62:                     firstLineField.setText("Unexpected error.");
 63:     }
 64:         
 65:     private void showFirstLine( )
 66:     {
 67:         Scanner fileInput = null;
 68:         String fileName = fileNameField.getText( );
 69:         File fileObject = new File(fileName);
 70:                 
 71:         if (!fileObject.exists( ))
 72:             firstLineField.setText("No such file");
 73:         else if (!fileObject.canRead( ))
 74:             firstLineField.setText("That file is not readable.");
 75:         else
 76:         {
 77:             try
 78:             {
 79:                 fileInput = new Scanner(fileObject);
 80:             }
 81:             catch(FileNotFoundException e)
 82:             {
 83:                 firstLineField.setText("Error opening the file " +
 84:                                         fileName);
 85:             }
 86:             String firstLine = fileInput.nextLine( );
 87:             firstLineField.setText(firstLine);
 88:             fileInput.close( );
 89:         }
 90:     }
 91:         
 92:     private void resetFields()
 93:     {
 94:         fileNameField.setText("Enter file name here.");
 95:         firstLineField.setText("");
 96:     }
 97:         
 98:     private void removeFile( )
 99:     {
100:         Scanner fileInput = null;
101:         String firstLine;
102:         String fileName = fileNameField.getText( );
103:         File fileObject = new File(fileName);
104:                 
105:         if (!fileObject.exists( ))
106:             firstLineField.setText("No such file");
107:         else if (!fileObject.canWrite( ))
108:             firstLineField.setText("Permission denied.");
109:         else
110:         {
111:             if (fileObject.delete( ))
112:                 firstLineField.setText("File deleted.");
113:             else
114:                 firstLineField.setText("Could not delete file.");
115:         }
116:     }
117:         
118:     public static void main(String[] args)
119:     {
120:         FileOrganizer gui = new FileOrganizer( );
121:         gui.setVisible(true);
122:     }
123: }
124: