Skip to content

Worked in class with Dennis Xu (dx14) #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# lab_browser
A simple GUI example: a web browser

Worked on by William Yang (wzy) and Dennis Xu (dx14)
7 changes: 7 additions & 0 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

public class BrowserException extends Exception {

public BrowserException() { super(); }

public BrowserException(String message) { super(message); }
}
29 changes: 20 additions & 9 deletions src/BrowserModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;


/**
Expand All @@ -22,6 +23,8 @@ public class BrowserModel {
private List<URL> myHistory;
private Map<String, URL> myFavorites;

public static final String DEFAULT_RESOURCE_PACKAGE = "resources/";
private ResourceBundle myResources;

/**
* Creates an empty model.
Expand All @@ -32,34 +35,41 @@ public BrowserModel () {
myCurrentIndex = -1;
myHistory = new ArrayList<>();
myFavorites = new HashMap<>();

myResources = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + "ModelErrors.properties");

}

/**
* Returns the first page in next history, null if next history is empty.
* @throws BrowserException
*/
public URL next () {
public URL next () throws BrowserException {
if (hasNext()) {
myCurrentIndex++;
return myHistory.get(myCurrentIndex);
}
return null;
// return null;
throw new BrowserException(myResources.getString("NextError"));
}

/**
* Returns the first page in back history, null if back history is empty.
* @throws BrowserException
*/
public URL back () {
public URL back () throws BrowserException {
if (hasPrevious()) {
myCurrentIndex--;
return myHistory.get(myCurrentIndex);
}
return null;
throw new BrowserException(myResources.getString("BackError"));
}

/**
* Changes current page to given URL, removing next history.
* @throws BrowserException
*/
public URL go (String url) {
public URL go (String url) throws BrowserException {
myCurrentURL = completeURL(url);
if (myCurrentURL != null) {
if (hasNext()) {
Expand Down Expand Up @@ -114,16 +124,17 @@ public void addFavorite (String name) {

/**
* Returns URL from favorites associated with given name, null if none set.
* @throws BrowserException
*/
public URL getFavorite (String name) {
public URL getFavorite (String name) throws BrowserException {
if (name != null && !name.equals("") && myFavorites.containsKey(name)) {
return myFavorites.get(name);
}
return null;
throw new BrowserException(myResources.getString("FavoriteError"));
}

// deal with a potentially incomplete URL
private URL completeURL (String possible) {
private URL completeURL (String possible) throws BrowserException {
try {
// try it as is
return new URL(possible);
Expand All @@ -137,7 +148,7 @@ private URL completeURL (String possible) {
// e.g., let user leave off initial protocol
return new URL(PROTOCOL_PREFIX + possible);
} catch (MalformedURLException eee) {
return null;
throw new BrowserException(String.format("%s is a bad URL", possible));
}
}
}
Expand Down
46 changes: 33 additions & 13 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
Expand All @@ -23,7 +24,9 @@
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;

import javax.imageio.ImageIO;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
Expand Down Expand Up @@ -75,28 +78,31 @@ public BrowserView (BrowserModel model, String language) {
myModel = model;
// use resources for labels
myResources = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + language);
myFavorites = new ComboBox<String>();
BorderPane root = new BorderPane();
// must be first since other panels may refer to page
root.setCenter(makePageDisplay());
root.setTop(makeInputPanel());
root.setBottom(makeInformationPanel());
// control the navigation
enableButtons();
// Initialize ComboBox
// create scene to hold UI
myScene = new Scene(root, DEFAULT_SIZE.width, DEFAULT_SIZE.height);
//myScene.getStylesheets().add(DEFAULT_RESOURCE_PACKAGE + STYLESHEET);
myScene.getStylesheets().add(DEFAULT_RESOURCE_PACKAGE + STYLESHEET);
}

/**
* Display given URL.
* @throws BrowserException
*/
public void showPage (String url) {
public void showPage (String url) throws BrowserException {
URL valid = myModel.go(url);
if (url != null) {
try {
update(valid);
}
else {
showError("Could not load " + url);
catch (BrowserException e) {
System.out.println(String.format("Could not load %s", url));
}
}

Expand Down Expand Up @@ -125,22 +131,22 @@ public void showError (String message) {
}

// move to the next URL in the history
private void next () {
private void next () throws BrowserException {
update(myModel.next());
}

// move to the previous URL in the history
private void back () {
private void back () throws BrowserException {
update(myModel.back());
}

// change current URL to the home page, if set
private void home () {
private void home () throws BrowserException {
showPage(myModel.getHome().toString());
}

// change page to favorite choice
private void showFavorite (String favorite) {
private void showFavorite (String favorite) throws BrowserException {
showPage(myModel.getFavorite(favorite).toString());
}

Expand Down Expand Up @@ -180,7 +186,7 @@ private Node makePageDisplay () {
}

// organize user's options for controlling/giving input to model
private Node makeInputPanel () {
private Node makeInputPanel () throws BrowserException {
VBox result = new VBox();
result.getChildren().addAll(makeNavigationPanel(), makePreferencesPanel());
return result;
Expand All @@ -194,14 +200,19 @@ private Node makeInformationPanel () {
}

// make user-entered URL/text field and back/next buttons
private Node makeNavigationPanel () {
private Node makeNavigationPanel () throws BrowserException {
HBox result = new HBox();
// create buttons, with their associated actions
// old style way to do set up callback (anonymous class)
myBackButton = makeButton("BackCommand", new EventHandler<ActionEvent>() {
@Override
public void handle (ActionEvent event) {
back();
try {
back();
} catch (BrowserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
result.getChildren().add(myBackButton);
Expand All @@ -221,6 +232,10 @@ public void handle (ActionEvent event) {
// make buttons for setting favorites/home URLs
private Node makePreferencesPanel () {
HBox result = new HBox();
result.getChildren().add(makeButton("AddFavoriteCommand", event -> {
addFavorite();
}));
result.getChildren().add(myFavorites);
result.getChildren().add(makeButton("SetHomeCommand", event -> {
myModel.setHome();
enableButtons();
Expand Down Expand Up @@ -259,7 +274,12 @@ private TextField makeInputField (int width, EventHandler<ActionEvent> handler)
private class ShowPage implements EventHandler<ActionEvent> {
@Override
public void handle (ActionEvent event) {
showPage(myURLDisplay.getText());
try {
showPage(myURLDisplay.getText());
} catch (BrowserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/resources/ModelErrors.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NextError=No Next Page
BackError=No Previous Page
FavoriteError=No Favorites
10 changes: 5 additions & 5 deletions src/resources/default.css
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
.root {
-fx-font-size: 14pt;
-fx-font-family: "Courier New";
-fx-font-family: "Impact";
-fx-base: rgb(132, 145, 47);
-fx-background: rgb(225, 228, 203);
-fx-background: "GREEN";
}

.button {
-fx-text-fill: #006464;
-fx-background-color: #DFB951;
-fx-border-radius: 20;
-fx-background-radius: 20;
-fx-background-color: "RED";
-fx-border-radius: 100;
-fx-background-radius: 1px;
-fx-padding: 8;
}

Expand Down