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>
52 changes: 25 additions & 27 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
/**
* Represents an exceptional situation specific to this project.
*
* @author Robert C. Duvall
*/
public class BrowserException extends RuntimeException {
// for serialization
private static final long serialVersionUID = 1L;

/**
* Create an exception based on an issue in our code.
*/
public BrowserException (String message, Object ... values) {
super(String.format(message, values));
}

/**
* Create an exception based on a caught exception with a different message.
*/
public BrowserException (Throwable cause, String message, Object ... values) {
super(String.format(message, values), cause);
}
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
}

/**
* Create an exception based on a caught exception, with no additional message.
*/
public BrowserException (Throwable exception) {
super(exception);
}
}
42 changes: 16 additions & 26 deletions src/BrowserModel.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;


/**
Expand All @@ -15,16 +14,13 @@
*/
public class BrowserModel {
// constants
public static final String DEFAULT_RESOURCES = "resources/Errors";
public static final String PROTOCOL_PREFIX = "http://";
// state
private URL myHome;
private URL myCurrentURL;
private int myCurrentIndex;
private List<URL> myHistory;
private Map<String, URL> myFavorites;
// get strings from resource file
private ResourceBundle myResources;


/**
Expand All @@ -36,55 +32,51 @@ public BrowserModel () {
myCurrentIndex = -1;
myHistory = new ArrayList<>();
myFavorites = new HashMap<>();
// use resources for errors
myResources = ResourceBundle.getBundle(DEFAULT_RESOURCES);
}

/**
* 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);
}
else {
throw new BrowserException(myResources.getString("NoNext"));
}
throw new BrowserException("no next page");
}

/**
* 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);
}
else {
throw new BrowserException(myResources.getString("NoPrevious"));
}
throw new BrowserException("no previous page");
}

/**
* 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
tmp.openStream();
// if successful, remember this URL
myCurrentURL = tmp;
if (hasNext()) {
myHistory = myHistory.subList(0, myCurrentIndex + 1);
if (myCurrentURL != null) {
if (hasNext()) {
myHistory = myHistory.subList(0, myCurrentIndex + 1);
}
myHistory.add(myCurrentURL);
myCurrentIndex++;
}
myHistory.add(myCurrentURL);
myCurrentIndex++;
return myCurrentURL;
}
catch (Exception e) {
throw new BrowserException(e, myResources.getString("NoLoad"), url);
throw new BrowserException("no URL");
}
}

Expand All @@ -104,6 +96,7 @@ public boolean hasPrevious () {

/**
* Returns URL of the current home page or null if none is set.
* @throws BrowserException
*/
public URL getHome () {
return myHome;
Expand Down Expand Up @@ -136,13 +129,11 @@ public URL getFavorite (String name) {
if (name != null && !name.equals("") && myFavorites.containsKey(name)) {
return myFavorites.get(name);
}
else {
throw new BrowserException(myResources.getString("BadFavorite"), name);
}
return null;
}

// deal with a potentially incomplete URL
private URL completeURL (String possible) throws MalformedURLException {
private URL completeURL (String possible) {
try {
// try it as is
return new URL(possible);
Expand All @@ -156,8 +147,7 @@ private URL completeURL (String possible) throws MalformedURLException {
// e.g., let user leave off initial protocol
return new URL(PROTOCOL_PREFIX + possible);
} catch (MalformedURLException eee) {
// nothing else to do, let caller report error to user
throw eee;
return null;
}
}
}
Expand Down
53 changes: 34 additions & 19 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class BrowserView {
private Button myBackButton;
private Button myNextButton;
private Button myHomeButton;
private Button setFavoriteButton;
private Button myFavoriteGoButton;
// favorites
private ComboBox<String> myFavorites;
// get strings from resource file
Expand Down Expand Up @@ -91,10 +93,11 @@ public BrowserView (BrowserModel model, String language) {
* Display given URL.
*/
public void showPage (String url) {
try {
update(myModel.go(url));
}
catch (BrowserException e) {
try{
URL valid = myModel.go(url);
update(valid);
}
catch(BrowserException e){
showError(e.getMessage());
}
}
Expand Down Expand Up @@ -125,12 +128,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,8 +154,8 @@ private void home () {
// change page to favorite choice
private void showFavorite (String favorite) {
showPage(myModel.getFavorite(favorite).toString());
// reset favorites so the same choice can be made again
// myFavorites.setValue(null);
// reset favorites ComboBox so the same choice can be made again
//myFavorites.setValue(null);
}

// update just the view to display given URL
Expand Down Expand Up @@ -184,7 +197,7 @@ private Node makePageDisplay () {
// organize user's options for controlling/giving input to model
private Node makeInputPanel () {
VBox result = new VBox();
result.getChildren().addAll(makeNavigationPanel(), makePreferencesPanel());
result.getChildren().addAll(makePreferencesPanel(), makeNavigationPanel());
return result;
}

Expand Down Expand Up @@ -224,14 +237,17 @@ public void handle (ActionEvent event) {
private Node makePreferencesPanel () {
HBox result = new HBox();
myFavorites = new ComboBox<String>();
myFavorites.setPromptText(myResources.getString("FavoriteFirstItem"));
myFavorites.valueProperty().addListener((o, s1, s2) -> showFavorite(s2));
result.getChildren().add(makeButton("AddFavoriteCommand", event -> addFavorite()));
result.getChildren().add(myFavorites);
// ADD REST OF CODE HERE
result.getChildren().add(makeButton("SetHomeCommand", event -> {
myModel.setHome();
enableButtons();
}));
setFavoriteButton = makeButton("AddFavoriteCommand", event -> addFavorite());
result.getChildren().add(setFavoriteButton);
result.getChildren().add(myFavorites);
myFavoriteGoButton = makeButton("GoCommand", event -> showFavorite(myFavorites.getValue()));
result.getChildren().add(myFavoriteGoButton);
myFavorites.setValue("My Favorites");
return result;
}

Expand Down Expand Up @@ -264,17 +280,16 @@ private TextField makeInputField (int width, EventHandler<ActionEvent> handler)
// display page
// very old style way create a callback (inner class)
private class ShowPage implements EventHandler<ActionEvent> {
@Override
public void handle (ActionEvent event) {
showPage(myURLDisplay.getText());
}
@Override
public void handle (ActionEvent event) {
showPage(myURLDisplay.getText());
}
}


// Inner class to deal with link-clicks and mouse-overs Mostly taken from
// http://blogs.kiyut.com/tonny/2013/07/30/javafx-webview-addhyperlinklistener/
private class LinkListener implements ChangeListener<State> {
public static final String HTML_LINK = "href";
public static final String EVENT_CLICK = "click";
public static final String EVENT_MOUSEOVER = "mouseover";
public static final String EVENT_MOUSEOUT = "mouseout";
Expand All @@ -283,7 +298,7 @@ private class LinkListener implements ChangeListener<State> {
public void changed (ObservableValue<? extends State> ov, State oldState, State newState) {
if (newState == Worker.State.SUCCEEDED) {
EventListener listener = event -> {
final String href = ((Element)event.getTarget()).getAttribute(HTML_LINK);
final String href = ((Element)event.getTarget()).getAttribute("href");
if (href != null) {
String domEventType = event.getType();
if (domEventType.equals(EVENT_CLICK)) {
Expand Down
Empty file modified src/Main.java
100755 → 100644
Empty file.