Skip to content

Michael Daou (md199) and Henry Yuen (py22) #10

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 4 commits 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

Authored by: Michael Daou (md199); Henry Yuen (py22)
20 changes: 20 additions & 0 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

public class BrowserException extends RuntimeException {

/**
*
*/
private static final long serialVersionUID = 1L;

public BrowserException(){
System.out.println("ERROR");
}

public BrowserException(String message){
super(message);
}

public BrowserException(Throwable cause){
super(cause);
}
}
11 changes: 7 additions & 4 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 @@ -21,6 +22,7 @@ public class BrowserModel {
private int myCurrentIndex;
private List<URL> myHistory;
private Map<String, URL> myFavorites;
private ResourceBundle myErrors;


/**
Expand All @@ -32,6 +34,7 @@ public BrowserModel () {
myCurrentIndex = -1;
myHistory = new ArrayList<>();
myFavorites = new HashMap<>();
myErrors = ResourceBundle.getBundle("myProperties");
}

/**
Expand All @@ -42,7 +45,7 @@ public URL next () {
myCurrentIndex++;
return myHistory.get(myCurrentIndex);
}
return null;
throw new BrowserException(myErrors.getString("NullNext"));
}

/**
Expand All @@ -53,7 +56,7 @@ public URL back () {
myCurrentIndex--;
return myHistory.get(myCurrentIndex);
}
return null;
throw new BrowserException(myErrors.getString("NullBack"));
}

/**
Expand Down Expand Up @@ -119,7 +122,7 @@ public URL getFavorite (String name) {
if (name != null && !name.equals("") && myFavorites.containsKey(name)) {
return myFavorites.get(name);
}
return null;
throw new BrowserException(myErrors.getString("NullFavorite"));
}

// deal with a potentially incomplete URL
Expand All @@ -137,7 +140,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(myErrors.getString("BadURL"));
}
}
}
Expand Down
28 changes: 22 additions & 6 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import java.awt.Dimension;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.event.ActionEvent;
Expand Down Expand Up @@ -61,10 +63,13 @@ public class BrowserView {
private Button myBackButton;
private Button myNextButton;
private Button myHomeButton;
private Button myFavButton;
//private ComboBox myFavDropdown;
// favorites
private ComboBox<String> myFavorites;
private ComboBox<String> myFavorites = new ComboBox<String>();
// get strings from resource file
private ResourceBundle myResources;
private ResourceBundle myErrors;
// the data
private BrowserModel myModel;

Expand All @@ -75,15 +80,20 @@ public BrowserView (BrowserModel model, String language) {
myModel = model;
// use resources for labels
myResources = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + language);
myErrors = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + "myProperties");
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();
// create scene to hold UI
myScene = new Scene(root, DEFAULT_SIZE.width, DEFAULT_SIZE.height);
myScene.getStylesheets().add("resources/main.css");
//myScene.getStylesheets().add(DEFAULT_RESOURCE_PACKAGE + STYLESHEET);
}

Expand All @@ -92,11 +102,11 @@ public BrowserView (BrowserModel model, String language) {
*/
public void showPage (String url) {
URL valid = myModel.go(url);
if (url != null) {
try {
update(valid);
}
else {
showError("Could not load " + url);
catch(NullPointerException e) {
showError(String.format(myErrors.getString("NullPageMessage"), url));
}
}

Expand Down Expand Up @@ -160,7 +170,7 @@ private void addFavorite () {
// did user make a choice?
if (response.isPresent()) {
myModel.addFavorite(response.get());
myFavorites.getItems().add(response.get());
myFavorites.getItems().add(response.get());
}
}

Expand Down Expand Up @@ -215,6 +225,7 @@ public void handle (ActionEvent event) {
result.getChildren().add(makeButton("GoCommand", showHandler));
myURLDisplay = makeInputField(40, showHandler);
result.getChildren().add(myURLDisplay);

return result;
}

Expand All @@ -225,7 +236,12 @@ private Node makePreferencesPanel () {
myModel.setHome();
enableButtons();
}));
return result;
myFavButton = makeButton("AddFavoriteCommand", event->addFavorite());

result.getChildren().add(myFavButton);
myFavorites.setPromptText("Favorites");
result.getChildren().add(myFavorites);
return result;
}

// makes a button using either an image or a label
Expand Down
7 changes: 7 additions & 0 deletions src/resources/myProperties.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

NullURL=Your URL is broken
NullNext=Next URL is null
NullBack= Back URL is null
NullFavorite = Favorite URL is null
BadURL= Your URL is very bad.
NullPageMessage = Could not show %s. So sorry.