-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
278 lines (243 loc) · 11.5 KB
/
Controller.java
File metadata and controls
278 lines (243 loc) · 11.5 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
/*
Author: Scott Field
Name: Controller
Date: 10/12/2023
Version: 1.0
Purpose:
A class for storing all of the methods to be used when
the buttons in the program are pressed
*/
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Optional;
import javafx.geometry.Insets;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.HBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.control.Alert;
public class Controller{
//Controller doesn't need any member variables because it is just storing functions for the various buttons that the UI uses
public Controller(){
}
//Function to increase the size of the pdf by a set amount
public void onZoomInButtonPressed(PDFViewer fileViewer, TextField zoomTextEntry){
//get the current size
double originalHeight = fileViewer.getFitHeight() / (fileViewer.zoomLevel / 100.0);
double originalWidth = fileViewer.getFitWidth() / (fileViewer.zoomLevel / 100.0);
//adjust the zoom level
fileViewer.zoomLevel += 10;
//get the new size (current size minus 10%)
double newHeight = originalHeight * (fileViewer.zoomLevel / 100.0);
double newWidth = originalWidth * (fileViewer.zoomLevel / 100.0);
//output the zoom level to the zoome entry widget
zoomTextEntry.setText(Integer.toString(fileViewer.zoomLevel));
//set the file Viewer New Size
fileViewer.setFitHeight(newHeight);
fileViewer.setFitWidth(newWidth);
}
//Function to decrease the size of the pdf by a set amount
public void onZoomOutButtonPressed(PDFViewer fileViewer, TextField zoomTextEntry){
//Ensure that the zoom level is not reduced to 0
if (fileViewer.zoomLevel >= 11){
//get the current size
double originalHeight = fileViewer.getFitHeight() / (fileViewer.zoomLevel / 100.0);
double originalWidth = fileViewer.getFitWidth() / (fileViewer.zoomLevel / 100.0);
//adjust the zoom level
fileViewer.zoomLevel -= 10;
//get the new size (current size minus 10%)
double newHeight = originalHeight * (fileViewer.zoomLevel / 100.0);
double newWidth = originalWidth * (fileViewer.zoomLevel / 100.0);
//output the zoom level to the zoome entry widget
zoomTextEntry.setText(Integer.toString(fileViewer.zoomLevel));
//set the file Viewer New Size
fileViewer.setFitHeight(newHeight);
fileViewer.setFitWidth(newWidth);
//Alert The User That Zoom Cannot Be Decreased Below 1% (if zoom is < 11 decreasing it by 10% will put it below 1%)
}else{
Alert alert = new Alert(AlertType.INFORMATION);
alert.setHeaderText("Zoom Level Warning");
alert.setContentText("Zoom cannot be decreased by 10% if that would place it below 1%");
alert.showAndWait();
}
}
//Function to increase or decrease the size of a pdf by a user given zoom value
public void onSetZoomPressed(PDFViewer fileViewer, TextField zoomTextEntry){
String inputString = zoomTextEntry.getText();
//if the user has input something
if (!inputString.isEmpty()) {
try {
//attempt to get the number from the input
int inputNumber = Integer.parseInt(inputString);
//as long as the number is greater than 0 calculate the zoom level
if (inputNumber > 0) {
//get the original height and width
double originalHeight = fileViewer.getFitHeight() / (fileViewer.zoomLevel / 100.0);
double originalWidth = fileViewer.getFitWidth() / (fileViewer.zoomLevel / 100.0);
//get the new height and width from that
double newHeight = originalHeight * (inputNumber / 100.0);
double newWidth = originalWidth * (inputNumber / 100.0);
//set the file viewer new size
fileViewer.zoomLevel = inputNumber;
fileViewer.setFitHeight(newHeight);
fileViewer.setFitWidth(newWidth);
}
//If there is an error exit the loop
}catch (NumberFormatException ex) {
return;
}
}
/*
This is placed here in case the user inputs 0 or "" to reset the zoom text entry to its original value
It will also set the new zoom value provided the user has entered a valid number
*/
zoomTextEntry.setText(Integer.toString(fileViewer.zoomLevel));
}
//Function to load the next page
public void onNextPageButtonPressed(PDFViewer fileViewer){
fileViewer.incrementPage();
}
//Function to load the previous page
public void onPreviousPageButtonPressed(PDFViewer fileViewer){
fileViewer.decrementPage();
}
//Function to jump to a specific page
public void onJumpToPage(PDFViewer fileViewer, int pageNumber){
fileViewer.jumpToPage(pageNumber);
}
//Function to open a new pdf file from local directory
public PDFReader onOpenButtonPressed(Stage primaryStage){
//open the file
FileChooser fileDialog = new FileChooser();
// Set the title of the file dialog
fileDialog.setTitle("Open File");
// Set the initial directory of the file dialog
fileDialog.setInitialDirectory(new File(System.getProperty("user.home")));
// Get the file the user selected
File selectedFile = fileDialog.showOpenDialog(primaryStage);
//Add the file to the PDF Viewer if it is not null
if (selectedFile != null){
PDFReader myPdfReader = new PDFReader(selectedFile);
//If the file the user selected is a valid pdf
if (myPdfReader.isValid){
return myPdfReader;
//Otherwise display an alert to the user and wait for them to close it
}else{
Alert alert = new Alert(AlertType.ERROR);
alert.setHeaderText("Invalid FileType");
alert.setContentText("The File You Selected Was Not A Valid PDF");
alert.showAndWait();
}
}
//otherwise return null
return null;
}
//Function to open a new pdf file from a web url
public PDFReader onOpenFromURLButtonPressed(PDFReader myPdfReader){
//Define The Custom Dialog Box
Dialog<String> dialog = new Dialog<>();
dialog.setTitle("Open PDF from URL");
dialog.getDialogPane().setPrefWidth(600);
// Set the button types
ButtonType openButtonType = new ButtonType("Open", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(openButtonType, ButtonType.CANCEL);
// Create the content for the dialog box
HBox content = new HBox();
content.setSpacing(10);
content.setPadding(new Insets(10, 10, 10, 10));
Label urlLabel = new Label("PDF URL:");
TextField urlTextField = new TextField();
urlTextField.setPrefWidth(450);
content.getChildren().addAll(urlLabel, urlTextField);
//add the content to the dialog box
dialog.getDialogPane().setContent(content);
// Set the result converter for the dialog box
dialog.setResultConverter(dialogButton -> {
if (dialogButton == openButtonType) {
return urlTextField.getText();
}
return null;
});
// Show the dialog box and wait for a response
Optional<String> result = dialog.showAndWait();
//If the user put anything in the dialog box
if (result.isPresent()){
String urlString = result.get();
URL url;
//Attempt to assign the URL with the provided url string
try{
url = new URL(urlString);
//If The URL did not lead to a valid pdf or was invalid
}catch(Exception e){
//Display An Error Message That The URL Was Not Valid
Alert alert = new Alert(AlertType.ERROR);
alert.setHeaderText("PDF Not Found");
alert.setContentText("The URL You Supplied Did Not Lead To A Valid PDF");
alert.showAndWait();
//if the url is invalid return null
return null;
}
myPdfReader = new PDFReader(url);
return myPdfReader;
}
//return null if the result is not present
return null;
}
//Function to save a currently opened pdf file to a local directory (and also rename it if the user changes the name)
public void onSaveAsButtonPressed(File pdfFile){
Alert alert;
if (pdfFile != null){
//Set the file save dialog
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save PDF File");
fileChooser.setInitialFileName(pdfFile.getName());
//Set the dialog to show that the file will be saved as a pdf file
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("PDF Files", "*.pdf"));
//Open the file save dialog
File selectedFile = fileChooser.showSaveDialog(null);
//If the user selects a file location
if (selectedFile != null) {
//If the selected file name doesn't end with .pdf
if (!selectedFile.getName().endsWith(".pdf")) {
//add .pdf to the end of the file
selectedFile = new File(selectedFile.getAbsolutePath() + ".pdf");
}
// Copy the pdfFile data to the selectedFile data
try {
Files.copy(pdfFile.toPath(), selectedFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
// If Their is a problem copying the data log the error and alert the user
} catch (IOException e) {
e.printStackTrace();
alert = new Alert(AlertType.ERROR);
alert.setHeaderText("File Data Save Error");
alert.setContentText("A was a problem saving the file data to the new file location, please try again later.");
alert.showAndWait();
}
}
//If no file has been loaded to the user Alert them that they need to load a file before saving one
}else{
alert = new Alert(AlertType.INFORMATION);
alert.setHeaderText("No File Loaded");
alert.setContentText("Please open a PDF File before attempting to Save it to a new location ");
alert.showAndWait();
}
}
//Function to close the currently opened pdf file
public void onCloseButtonPressed(PDFViewer fileViewer,Label rightPageLabel, TextField jumpToPageEntry){
//clear the file reader
fileViewer.fileReader.clear();
//adjust the page labels
rightPageLabel.setText("/" + fileViewer.fileReader.getPageCount());
jumpToPageEntry.setText("0");
//clear the currently open image
fileViewer.setImage(null);
}
}