Source of FileOrganizer.java


  1: import javafx.application.Application;
  2: import javafx.scene.Scene;
  3: import javafx.stage.Stage;
  4: import javafx.scene.layout.FlowPane;
  5: import javafx.scene.control.TextField;
  6: import javafx.scene.control.Label;
  7: import javafx.scene.control.Button;
  8: import javafx.event.ActionEvent;
  9: import javafx.event.EventHandler;
 10: import java.io.File;
 11: import java.io.FileNotFoundException;
 12: import java.util.Scanner;
 13: 
 14: public class FileOrganizer extends Application
 15: {
 16:    private TextField fileNameField;
 17:    private TextField firstLineField;
 18: 
 19:    public static void main(String[] args)
 20:    {
 21:       launch(args);
 22:    }
 23: 
 24:    @Override
 25:    public void start(Stage primaryStage) throws Exception
 26:    {
 27:       final int WIDTH = 400;
 28:       final int HEIGHT = 300;
 29:       final int NUMBER_OF_PIXELS = 300;
 30: 
 31:           FlowPane root = new FlowPane();
 32: 
 33:           Button showButton = new Button("Show first line");
 34:           root.getChildren().add(showButton);
 35:           showButton.setOnAction(new EventHandler<ActionEvent>()
 36:            {
 37:                    @Override
 38:                    public void handle(ActionEvent event)
 39:                    {
 40:                            showFirstLine();
 41:                    }
 42:            }
 43:           );
 44: 
 45:           Button removeButton = new Button("Remove file");
 46:           root.getChildren().add(removeButton);
 47:           removeButton.setOnAction(new EventHandler<ActionEvent>()
 48:            {
 49:                    @Override
 50:                    public void handle(ActionEvent event)
 51:                    {
 52:                            removeFile();
 53:                    }
 54:            }
 55:           );
 56: 
 57:           Button resetButton = new Button("Reset");
 58:           root.getChildren().add(resetButton);
 59:           resetButton.setOnAction(new EventHandler<ActionEvent>()
 60:            {
 61:                    @Override
 62:                    public void handle(ActionEvent event)
 63:                    {
 64:                            resetFields();
 65:                    }
 66:            }
 67:           );
 68: 
 69:           fileNameField = new TextField("Enter file name here.");
 70:           fileNameField.setPrefWidth(NUMBER_OF_PIXELS);
 71:           root.getChildren().add(fileNameField);
 72: 
 73:           firstLineField = new TextField();
 74:           firstLineField.setPrefWidth(NUMBER_OF_PIXELS);
 75:           root.getChildren().add(firstLineField);
 76: 
 77:              Scene scene = new Scene(root, WIDTH, HEIGHT);
 78:       primaryStage.setTitle("File Organizer");
 79:       primaryStage.setScene(scene);
 80:       primaryStage.show();
 81:    }
 82: 
 83:     private void showFirstLine( )
 84:     {
 85:         Scanner fileInput = null;
 86:         String fileName = fileNameField.getText( );
 87:         File fileObject = new File(fileName);
 88: 
 89:         if (!fileObject.exists( ))
 90:             firstLineField.setText("No such file");
 91:         else if (!fileObject.canRead( ))
 92:             firstLineField.setText("That file is not readable.");
 93:         else
 94:         {
 95:             try
 96:             {
 97:                 fileInput = new Scanner(fileObject);
 98:             }
 99:             catch(FileNotFoundException e)
100:             {
101:                 firstLineField.setText("Error opening the file " +
102:                                         fileName);
103:             }
104:             String firstLine = fileInput.nextLine( );
105:             firstLineField.setText(firstLine);
106:             fileInput.close( );
107:         }
108:     }
109: 
110:     private void resetFields()
111:     {
112:         fileNameField.setText("Enter file name here.");
113:         firstLineField.setText("");
114:     }
115: 
116:     private void removeFile( )
117:     {
118:         Scanner fileInput = null;
119:         String firstLine;
120:         String fileName = fileNameField.getText( );
121:         File fileObject = new File(fileName);
122: 
123:         if (!fileObject.exists( ))
124:             firstLineField.setText("No such file");
125:         else if (!fileObject.canWrite( ))
126:             firstLineField.setText("Permission denied.");
127:         else
128:         {
129:             if (fileObject.delete( ))
130:                 firstLineField.setText("File deleted.");
131:             else
132:                 firstLineField.setText("Could not delete file.");
133:         }
134:     }
135: }