-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
283 lines (240 loc) · 11.3 KB
/
Main.java
File metadata and controls
283 lines (240 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
Author: Scott Field
Name: Main
Date: 10/10/2023
Version: 1.0
Purpose:
A class for setting up the UI and the program
main loop using JavaFX
*/
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.geometry.Pos;
import javafx.scene.layout.VBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.input.KeyCode;
import java.io.File;
public class Main extends Application {
@Override
public void start(Stage primaryStage){
int windowHeight = 900;
int windowWidth = 900;
//If those tests don't result in errors run the main program
//Set the Controller
Controller binding = new Controller();
//Load The Image using the PDFReader
//remove the /**/ in this PDFReader to see the starting pdf
PDFReader fileReader = new PDFReader(/*new File("temp/test.pdf")*/);
//Set The Image to be displayed using the PDFViewer
PDFViewer fileViewer = new PDFViewer(fileReader);
//set the buttons to be cycle between pages
Button previousPageButton = new Button("Previous");
Button nextPageButton = new Button("Next");
//set the labels and text entry to jump to a page
Label leftPageLabel = new Label("Page");
Label rightPageLabel = new Label(" /0 ");
//uncomment this line to not see the starting pdf pages
//rightPageLabel.setText("/" + fileReader.getPageCount());
TextField jumpToPageEntry = new TextField();
//Create The Formatters (Formatters cannot be applied across different variables) To Ensure That Only Numbers Can Be Entered Into The JumpToPage and ZoomLevel Field
TextFormatter<String> jumpFormatter = new TextFormatter<>(change -> {
String text = change.getText();
if (text.matches("[0-9]*")) {
return change;
}
return null;
});
TextFormatter<String> zoomFormatter = new TextFormatter<>(change -> {
String text = change.getText();
if (text.matches("[0-9]*")) {
return change;
}
return null;
});
//Add The Formatter To The Text Field
jumpToPageEntry.setTextFormatter(jumpFormatter);
//Set The Intial Value To 0
jumpToPageEntry.setText("0");
// Create the VBox to store the PDFViewer and the buttons for the test
VBox pdfBox = new VBox();
pdfBox.getChildren().addAll(fileViewer);
pdfBox.setAlignment(Pos.CENTER);
//Create the HBox to store the zoom set TextField Label and Buttons
Label zoomLabel = new Label("Zoom Percent");
TextField zoomTextEntry = new TextField();
zoomTextEntry.setTextFormatter(zoomFormatter);
Button zoomInButton = new Button("Zoom In");
Button zoomOutButton = new Button("Zoom Out");
HBox zoomDisplay = new HBox(zoomLabel,zoomTextEntry,zoomInButton,zoomOutButton);
zoomDisplay.setAlignment(Pos.CENTER_RIGHT);
// Create the HBox to store the buttons for cycling between pages
HBox buttonDisplay = new HBox(previousPageButton, nextPageButton);
buttonDisplay.setAlignment(Pos.CENTER);
// Create the HBox to store the text entry for jumping to pages
HBox jumpToPageDisplay = new HBox(leftPageLabel,jumpToPageEntry,rightPageLabel);
jumpToPageDisplay.setAlignment(Pos.CENTER);
// Create the VBox to store the HBox
VBox bottomBox = new VBox(buttonDisplay,zoomDisplay,jumpToPageDisplay);
// Create the file menu
Menu fileMenu = new Menu("File");
MenuItem openFileItem = new MenuItem("Open");
MenuItem openFileUrlItem = new MenuItem("Open From URL");
MenuItem saveAsFileItem = new MenuItem("Save As");
MenuItem closeFileItem = new MenuItem("Close");
MenuItem exitItem = new MenuItem("Exit");
// Add the items to the file menu
fileMenu.getItems().addAll(openFileItem, openFileUrlItem, saveAsFileItem, closeFileItem, exitItem);
// Add the file menu to the menu bar
MenuBar fileMenuBar = new MenuBar();
fileMenuBar.getMenus().add(fileMenu);
// create the VBox to store the menu bar
VBox menuBox = new VBox(fileMenuBar);
// Create the ScrollPane and set its content to the pdfBox
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(pdfBox);
scrollPane.setFitToWidth(true);
scrollPane.setFitToHeight(true);
// Create the BorderPane to store the menuBox, pdfBox, and bottomBox
BorderPane root = new BorderPane();
root.setTop(menuBox);
root.setCenter(scrollPane);
root.setBottom(bottomBox);
// Set the PDFViewer to fit to the width and height of the pdfBox
fileViewer.setPreserveRatio(true);
fileViewer.setFitHeight(pdfBox.getHeight()-(menuBox.getHeight()+bottomBox.getHeight()));
fileViewer.setFitWidth(windowWidth);
// Create the Scene and set it on the Stage
Scene scene = new Scene(root, windowHeight, windowWidth);
primaryStage.setScene(scene);
primaryStage.show();
//Event Listeners
//set the methods for previous page
previousPageButton.setOnAction(e ->{
//go to the previous page
binding.onPreviousPageButtonPressed(fileViewer);
//set the page tracker entry to match
jumpToPageEntry.setText(Integer.toString(fileViewer.fileReader.getCurrentPageNumber() + 1));
});
//set the methods for next page
nextPageButton.setOnAction(e ->{
//go to the next page
binding.onNextPageButtonPressed(fileViewer);
//set the page tracker entry to match
jumpToPageEntry.setText(Integer.toString(fileViewer.fileReader.getCurrentPageNumber() + 1));
});
//set the method for jump to page when the enter key is clicked and the jumpToPageEntry is in scope
jumpToPageEntry.setOnKeyPressed(e->{
//When The Enter Key Is Pressed
if (e.getCode() == KeyCode.ENTER){
int pageNumber = -1;
//Try To Get A Number From The Text Entry
try{
pageNumber += Integer.parseInt(jumpToPageEntry.getText());
}catch(NumberFormatException exception){
//Exit the loop if the number is not a valid integer number
return;
}
binding.onJumpToPage(fileViewer, pageNumber);
}
});
//set the method for opening a file from a local path
openFileItem.setOnAction(e-> {
//Attempt to read the file selected by the user
PDFReader newFileReader = binding.onOpenButtonPressed(primaryStage);
//As long as there is something new to add
if (newFileReader != null){
//set the pdf viewer to use the new file reader
fileViewer.setFileReader(newFileReader);
//adjust the labels to match
rightPageLabel.setText("/" + fileViewer.fileReader.getPageCount());
zoomTextEntry.setText(Integer.toString(fileViewer.zoomLevel));
jumpToPageEntry.setText("1");
}
});
//set the method for opening a file from a URL (Currently unfinished)
openFileUrlItem.setOnAction(e ->{
PDFReader newFileReader = binding.onOpenFromURLButtonPressed(fileReader);
//As long as there is something new to add
if (newFileReader != null){
//set the pdf viewer to use the new file reader
fileViewer.setFileReader(newFileReader);
//adjust the labels to match
rightPageLabel.setText("/" + fileViewer.fileReader.getPageCount());
zoomTextEntry.setText(Integer.toString(fileViewer.zoomLevel));
jumpToPageEntry.setText("1");
}
});
//set the method for closing the currently opened file
closeFileItem.setOnAction(e->{
//clear the currently opened file and adjust the labels to match
binding.onCloseButtonPressed(fileViewer, rightPageLabel, jumpToPageEntry);
zoomTextEntry.setText("");
});
//set the method for saving the currently opened file to a new location
saveAsFileItem.setOnAction(e->{
binding.onSaveAsButtonPressed(fileViewer.fileReader.getPDFFile());
});
//set the method for zooming in the currently opened file
zoomInButton.setOnAction(e->{
binding.onZoomInButtonPressed(fileViewer, zoomTextEntry);
});
//set the method for zooming out in the currently opened file
zoomOutButton.setOnAction(e->{
binding.onZoomOutButtonPressed(fileViewer, zoomTextEntry);
});
//set the method for setting zoom based on a given value
zoomTextEntry.setOnKeyPressed(e->{
//When The Enter Key Is Pressed
if (e.getCode() == KeyCode.ENTER){
//Zoom In By The Given Value
binding.onSetZoomPressed(fileViewer, zoomTextEntry);
}
});
//set the method for zooming to a certain level
//set the method for exiting the program
exitItem.setOnAction(e->{
//exit the program
Platform.exit();
});
}
//Main Program Loop
public static void main(String[] args) {
//Run tests on startup
testInvalidInput();
testValidInput();
//run main program
launch(args);
}
//Method to test classes on startup
public static void testInvalidInput(){
System.out.println("Invalid Input Test (Program should continue even with incorrect input and all values should be zero)");
//test class validation
PDFReader testReader = new PDFReader(new File("temp/IncorrectFileType.txt"));
PDFViewer testViewer = new PDFViewer(testReader);
//test class data
System.out.println("Current Page Height: " + testReader.getCurrentPageHeight());
System.out.println("Current Page Width: " + testReader.getCurrentPageWidth());
System.out.println("Current Page Number: " + testReader.getCurrentPageNumber());
}
public static void testValidInput(){
System.out.println("Valid Input Test (Program should continue with correct input and only Page Number should be zero)");
//test class validation
PDFReader testReader = new PDFReader(new File("temp/test.pdf"));
PDFViewer testViewer = new PDFViewer(testReader);
//test class data
System.out.println("Current Page Height: " + testReader.getCurrentPageHeight());
System.out.println("Current Page Width: " + testReader.getCurrentPageWidth());
System.out.println("Current Page Number: " + testReader.getCurrentPageNumber());
}
}