Skip to content

Commit

Permalink
Update file reading algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
fredjt committed Jun 19, 2020
1 parent 5dfa547 commit 05a6b9e
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 67 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<classpath>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path=""/>
<classpathentry kind="lib" path="Notepad.jar" sourcepath="C:/Users/Home PC/OneDrive - Knights - University of Central Florida/Trent School/Principles of Computer Programming/jdk1.8.0_241/demo/jfc/Notepad/src.zip"/>
<classpathentry kind="output" path=""/>
</classpath>
Binary file added Notepad.jar
Binary file not shown.
3 changes: 3 additions & 0 deletions filereader/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
/Utils.class
/package-info.class
/GUI$1.class
/TextFilePane$1.class
/TextFileUtils.class
/TextFileContentsLoader.class
16 changes: 9 additions & 7 deletions filereader/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
import javafx.stage.Stage;

public class GUI extends Application {
public static Stage newPrimaryStage;
private static Stage newPrimaryStage;
private static Scene scene;
private static MainPane pane;

public static void main(String[] args) {
launch();
}

@Override
public void start(Stage primaryStage) {
newPrimaryStage = new Stage();
MainPane pane = new MainPane();
Scene scene = new Scene(pane);
pane = new MainPane();
scene = new Scene(pane);
newPrimaryStage.setScene(scene);
newPrimaryStage.setMaximized(true);
newPrimaryStage.show();
}

public static void main(String[] args) {
launch();
}
}
20 changes: 7 additions & 13 deletions filereader/MainPane.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package filereader;

import javafx.scene.layout.GridPane;
import javafx.scene.layout.BorderPane;

public class MainPane extends GridPane {
TextFilePane textPane;
public class MainPane extends BorderPane {
private static TextFilePane textPane;
private static MainToolbar mainToolbar;

MainPane() {
MainToolbar mainToolbar = new MainToolbar();
mainToolbar = new MainToolbar();
textPane = new TextFilePane();
add(mainToolbar, 0, 0);
add(textPane, 0, 1);
}

/**
* @param textPane the textPane to set
*/
public void setTextPane(TextFilePane textPane) {
this.textPane = textPane;
setTop(mainToolbar);
setCenter(textPane);
}
}
10 changes: 5 additions & 5 deletions filereader/MainToolbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

public class MainToolbar extends HBox {
private TextField pathField;
private static Button openButton;

MainToolbar() {
Button openButton = new Button("Open");
openButton = new Button("Open");
openButton.setOnAction(new OpenFileHandler());

pathField = new TextField();
Expand All @@ -26,12 +27,11 @@ class OpenFileHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
Utils.currentFile = fileChooser.showOpenDialog(new Stage());
pathField.setText(Utils.currentFile.toString());
TextFileUtils.setCurrentFile(fileChooser.showOpenDialog(new Stage()));
pathField.setText(TextFileUtils.getCurrentFile().toString());
try {
TextFilePane.getOutputArea().setText(Utils.readFileLines(Utils.currentFile, (int) (GUI.newPrimaryStage.getHeight() / 17)));
TextFileUtils.readFileLines(TextFileUtils.getCurrentFile());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Expand Down
12 changes: 6 additions & 6 deletions filereader/TextFilePane.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package filereader;

import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Text;

public class TextFilePane extends BorderPane {
private static Text outputArea;
private static TextArea outputArea;

TextFilePane() {
outputArea = new Text();
outputArea.setFont(Utils.font);
outputArea = new TextArea();

setCenter(outputArea);
}

/**
* @return the output TextArea
*/
public static Text getOutputArea() {
public static TextArea getOutputArea() {
return outputArea;
}
}
39 changes: 39 additions & 0 deletions filereader/TextFileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package filereader;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

import javafx.scene.text.Font;

public class TextFileUtils {
private static File currentFile;

private TextFileUtils() {}

public static String readFile(File file, int length) throws IOException {
char[] contents = new char[length];
FileReader reader = new FileReader(file);
reader.read(contents);
reader.close();
return new String(contents);
}

public static void readFileLines(File file) throws IOException {
TextFilePane.getOutputArea().setText("");
Scanner reader = new Scanner(file);
while (reader.hasNext()) {
TextFilePane.getOutputArea().setText(TextFilePane.getOutputArea().getText() + reader.nextLine() + "\n");
}
reader.close();
}

public static File getCurrentFile() {
return currentFile;
}

public static void setCurrentFile(File file) {
currentFile = file;
}
}
36 changes: 0 additions & 36 deletions filereader/Utils.java

This file was deleted.

0 comments on commit 05a6b9e

Please sign in to comment.