Java Assignment
Java Assignment
Java Assignment
Saving Settings
Write a function called saveSettings, which will save several pieces of
information to a file. Use an ObjectOutput stream to save an
ArrayList<String> representing all of your saved URL bookmarks in a file. It
should be written as a java object which can then be read back in using an
ObjectInput stream.
In a second file, save the height, width, screenX, and screenY coordinates of
your browser as a text file. The file format should be name/value pairs like
lab 4, only on different lines:
screenX= topLeftX
screenY= topLeftY
height= Stage Height
width= Stage Width
downloadDirectory= where to save downloaded files
homepage=default home page
You must also create a function called readSettings, which should check if
the files you wrote in saveSettings exist, and if they do, open the files and
read in the values that you saved.
If the settings text file exists, then read in one line of text at a time, and scan
the name in front of the = sign. Set the appropriate values for
width/height, and screenX/screenY position, download directory, and home
page.
Add an onCloseRequest event handler to primaryStage that calls
saveSettings whenever you close your window, and also change the Quit
MenuItem event handler to also call saveSettings when the user clicks it.
Download Manager
The WebEngine has a location property which stores the URL of the page you
are viewing. Whenever you click on a link, the engine tries to load the new
address, but the browser doesnt know how to display many different file
types: .exe, .PDF, .ZIP, .DOC, .DOCX, .XLS, .XLSX, .ISO, .IMG, .DMG, .TAR,
.TGZ, .JAR. I have given you code that adds a ChangeListener to the
engine.locationProperty() which gets called every time the engine tries to
load a new document. Look at the changeListener, and code that tests if the
newLocation ends with any of the above file endings. If it does, then you
should download that file to your browsers default Download directory,
which you have to add as part of this assignment.
1. I have given you the starting code for a DownloadBar class, which
extends HBox. Since DownloadBar is an HBox, you can add GUI Objects
which will represent the information needed for the downloading of a
file. Create a constructor for the DownloadBar which takes a String,
which should be the URL of the new location for the engine. You must
check if a file with that same name exists in your download directory
and if it does then add (1), or (2) at the end until you find a
filename that doesnt exist, like a normal web browser does. You can
not overwrite an existing file with your download.
2. Add a Text object as a private instance variable to DownloadBar, which
should display the file name that is being downloaded. This file name
should be all the characters after the last / of the URL. For instance,
in: http://www.adobe.com/files/report.pdf the file name is
report.pdf.
3. Add a ProgressBar as a private instance variable to DownloadBar,
which should display the progress of the download. The ProgressBar
class has a setProgress() function which takes a float from 0 1.0,
which represents the progress from 0% 100%.
4. Add a Button as private instance variable to DownloadBar, which
should be a Cancel button for canceling the download. When the
user clicks on the Cancel button, there should be a dialog box asking
the user if they really want to quit. If they confirm the dialog, then it
should stop downloading the file, and delete the file that was created.
The DownloadBar should then be removed from the VBox where it was
added.
Figure 1 A Download window showing 3 downloads in progress, and a cancel dialog from trying to
cancel one of them
The Task class is similar to a Transition animation, in that you first set all
of the data for the class but it doesnt start to run until you tell it to. This
is done by creating a Thread object for your Task object, and starting the
thread:
DownloadTask aFileDownload = new DownloadTask ( );
new Thread( downloadTask ) .start();
Figure 4Downloading Multiple Linux ISOs. This shows 3 DownloadTasks running at the same time
This is a good test site to try various downloads running at the same time,
and trying to download files when it already exists on your hard drive.
2. Clicking on Homepage should show a new dialog box asking the user to
enter a new URL as the default Homepage to display when the browser
starts. This URL is what should be saved in the settings text file.
3. Clicking on the Downloads item should show a new dialog box asking the
user to enter the name of the subdirectory where to save downloaded
files. Your program should create this new directory if it does not exist, or
not create the directory if it already exists. You must check if the user has
write permissions to this directory and if not, warn the user that their
selection is not valid.
4. Add keyboard shortcuts for every menuItem. This is done using the
setAccelerator function of menuItems. For example, this sets the
combination (CTRL + A) as the shortcut for about:
about.setAccelerator(new KeyCodeCombination(KeyCode.A,
KeyCombination.CONTROL_DOWN));
Quit should have (CTRL+Q), About should have (CTRL+A), Help for java
class should have (CTRL+H), History should be (CTRL+Y)
5. Add a key pressed listener to the WebView object so that typing (Ctrl +
left arrow key) makes the history go back 1 page, and typing (Ctrl + right
arrow key) makes the history go forward 1 page. The KeyEvent has an
isControlDown() function to help you.
6. Add Tooltip descriptions to each of the buttons: Back, Forward, Add
Bookmark. The JavaFX documentation for the Tooltip class for examples
on how to do this.
Bonus marks
These next items are not required for the assignment, however you can get
bonus marks towards your final grade for implementing any of the following
features:
a) Inject javascript code: The WebEngine has an executeScript(String
javascript) function that lets you execute javascript code on your page.
Set the engines setOnAlert( ) function as a callback to show a JavaFX
dialog box that displays the Alert text as the main text of the dialog,
and then show it. Add a Javascript Menu to the interface that has an
Execute code menu item. Clicking on the MenuItem prompts the user
for a line of javascript code, which the browser will then execute. For
example, typing the text: alert(window.location) should call the
engines setOnAlert callback, which should show an Alert window with
the engines current URL. Typing the text: history.back() should make
Getting started
I have posted the start of the project as an Eclipse project. It is the file on
Blackboard: CST8284_Assignment3_YourName.zip . I have provided a
function: getWindow(). This checks if the downloadWindow variable is null.
If it is, it recreates the window and shows it. This means that after calling
getDownloadWindow(), you are guaranteed that the downloadWindow is not
null, and is visible. You can then add DownloadBar objects to the VBox.
Here is a tutorial on how to save URL to a download directory:
http://www.codejava.net/java-se/networking/use-httpurlconnection-todownload-file-from-an-http-url
This should be the contents of your call() function, but you should update the
code so that it updates the progress bar as it reads from the stream. Also,
the function in the tutorial throws an IOException if anything goes wrong.
The call() function must not throw any exceptions, therefore you will have to
add all of the catch() blocks that are needed and react accordingly. If the
internet connection is disconnected during a download, then the task should
end as a failed, instead of instead of succeeded, or canceled.
Here is a website that shows how to implement a simple Task object that
counts from 1 to 50 and updates the ProgressBar:
http://java-buddy.blogspot.ca/2013/08/javafx-example-task.html
Also, look at the documentation for ProgressBar, and Task in JavaFX:
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ProgressBar.
html
https://docs.oracle.com/javafx/2/api/javafx/concurrent/Task.html
Marks:
Saving
The
The
The
Settings: (7 marks)
bookmarks are saved to a file using ObjectOutputStream. +1
window size and position is saved to file +4
Homepage and Download directory are saved +2
There are new menu items for Download directory and Homepage that let
the user set the values with an input dialog +2
The Back, Forward and Add Bookmark buttons have tooltip windows. +2
Menu Items have accelerator shortcuts +2
The browser history goes forward and back by pressing Ctrl + front /back
arrow +2