Skip to content
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
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
28 changes: 28 additions & 0 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

public class BrowserException extends Exception {

public BrowserException() {
// TODO Auto-generated constructor stub
}

public BrowserException(String message) {
super(message);
// TODO Auto-generated constructor stub
}

public BrowserException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}

public BrowserException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}

public BrowserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}

}
30 changes: 21 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 @@ -21,45 +22,54 @@ public class BrowserModel {
private int myCurrentIndex;
private List<URL> myHistory;
private Map<String, URL> myFavorites;
private ResourceBundle myResources;


public static final String DEFAULT_RESOURCE_PACKAGE = "resources/";

/**
* Creates an empty model.
*/
public BrowserModel () {
myHome = null;

myCurrentURL = null;
myCurrentIndex = -1;
myHistory = new ArrayList<>();
myFavorites = new HashMap<>();
myResources = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + "ErrorMessages");
}

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

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

/**
* Changes current page to given URL, removing next history.
*/
public URL go (String url) {
public URL go (String url) throws BrowserException{
try {
URL tmp = completeURL(url);
// unfortunately, completeURL may not have returned a valid URL, so test it
Expand All @@ -76,7 +86,7 @@ public URL go (String url) {
return myCurrentURL;
}
catch (Exception e) {
return null;
throw new BrowserException(String.format(myResources.getString("ErrorOnShowPage"), url));
}
}

Expand Down Expand Up @@ -124,11 +134,13 @@ public void addFavorite (String name) {
/**
* Returns URL from favorites associated with given name, null if none set.
*/
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 myFavorites.get(name);
}
else {
throw new BrowserException(String.format(myResources.getString("ErrorOnFavorite")));
}
return null;
}

// deal with a potentially incomplete URL
Expand Down
44 changes: 34 additions & 10 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class BrowserView {
private Button myNextButton;
private Button myHomeButton;
// favorites
private Button myFavoriteButton;
private ComboBox<String> myFavorites;
// get strings from resource file
private ResourceBundle myResources;
Expand Down Expand Up @@ -91,12 +92,12 @@ public BrowserView (BrowserModel model, String language) {
* Display given URL.
*/
public void showPage (String url) {
URL valid = myModel.go(url);
if (valid != null) {
try {
URL valid = myModel.go(url);
update(valid);
}
else {
showError("Could not load " + url);
catch (BrowserException e) {
showError(e.getMessage());
}
}

Expand Down Expand Up @@ -126,12 +127,22 @@ public void showError (String message) {

// move to the next URL in the history
private void next () {
update(myModel.next());
try {
update(myModel.next());
}
catch (BrowserException e) {
showError(e.getMessage());
}
}

// move to the previous URL in the history
private void back () {
update(myModel.back());
try {
update(myModel.back());
}
catch (BrowserException e) {
showError(e.getMessage());
}
}

// change current URL to the home page, if set
Expand All @@ -141,9 +152,14 @@ private void home () {

// change page to favorite choice
private void showFavorite (String favorite) {
showPage(myModel.getFavorite(favorite).toString());
// reset favorites ComboBox so the same choice can be made again
myFavorites.setValue(null);
try {
showPage(myModel.getFavorite(favorite).toString());
// reset favorites ComboBox so the same choice can be made again
myFavorites.setValue(null);
}
catch (BrowserException e) {
showError(e.getMessage());
}
}

// update just the view to display given URL
Expand Down Expand Up @@ -213,6 +229,7 @@ public void handle (ActionEvent event) {
result.getChildren().add(myNextButton);
myHomeButton = makeButton("HomeCommand", event -> home());
result.getChildren().add(myHomeButton);

// if user presses button or enter in text field, load/show the URL
EventHandler<ActionEvent> showHandler = new ShowPage();
result.getChildren().add(makeButton("GoCommand", showHandler));
Expand All @@ -225,7 +242,14 @@ public void handle (ActionEvent event) {
private Node makePreferencesPanel () {
HBox result = new HBox();
myFavorites = new ComboBox<String>();
// ADD REST OF CODE HERE
myFavorites.setValue(myResources.getString("FavoriteFirstItem"));
myFavoriteButton = makeButton("FavoritePromptTitle", event -> addFavorite());
myFavorites.valueProperty().addListener(listener -> showFavorite(myFavorites.getValue()));
result.getChildren().add(myFavoriteButton);
result.getChildren().add(myFavorites);



result.getChildren().add(makeButton("SetHomeCommand", event -> {
myModel.setHome();
enableButtons();
Expand Down
4 changes: 4 additions & 0 deletions src/resources/ErrorMessages.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ErrorOnShowPage=Could not load %s sorry dawg
ErrorOnFavorite=Not valid favorite page
ErrorOnBack=No back page exists
ErrorOnNext=No next page exists