diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..fb50116 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/BrowserException.java b/src/BrowserException.java index 7fef393..10d64aa 100644 --- a/src/BrowserException.java +++ b/src/BrowserException.java @@ -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); - } } diff --git a/src/BrowserModel.java b/src/BrowserModel.java old mode 100755 new mode 100644 index 41ec6e5..874e257 --- a/src/BrowserModel.java +++ b/src/BrowserModel.java @@ -4,7 +4,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.ResourceBundle; /** @@ -15,7 +14,6 @@ */ public class BrowserModel { // constants - public static final String DEFAULT_RESOURCES = "resources/Errors"; public static final String PROTOCOL_PREFIX = "http://"; // state private URL myHome; @@ -23,8 +21,6 @@ public class BrowserModel { private int myCurrentIndex; private List myHistory; private Map myFavorites; - // get strings from resource file - private ResourceBundle myResources; /** @@ -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"); } } @@ -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; @@ -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); @@ -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; } } } diff --git a/src/BrowserView.java b/src/BrowserView.java index 7d59606..c534102 100644 --- a/src/BrowserView.java +++ b/src/BrowserView.java @@ -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 myFavorites; // get strings from resource file @@ -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()); } } @@ -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 @@ -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 @@ -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; } @@ -224,14 +237,17 @@ public void handle (ActionEvent event) { private Node makePreferencesPanel () { HBox result = new HBox(); myFavorites = new ComboBox(); - 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; } @@ -264,17 +280,16 @@ private TextField makeInputField (int width, EventHandler handler) // display page // very old style way create a callback (inner class) private class ShowPage implements EventHandler { - @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 { - 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"; @@ -283,7 +298,7 @@ private class LinkListener implements ChangeListener { public void changed (ObservableValue 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)) { diff --git a/src/Main.java b/src/Main.java old mode 100755 new mode 100644