Source of FileOrganizer.java


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