diff --git a/src/BrowserException.java b/src/BrowserException.java new file mode 100644 index 0000000..12f911e --- /dev/null +++ b/src/BrowserException.java @@ -0,0 +1,26 @@ +/** + * Created by rhondusmithwick on 2/4/16. + * + * @author Rhondu Smithwick + */ +public class BrowserException extends RuntimeException { + public BrowserException() { + } + + public BrowserException(String message, Throwable cause) { + super(message, cause); + } + + public BrowserException(String message) { + super(message); + } + + public BrowserException(Throwable cause) { + super(cause); + } + + public BrowserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + +} diff --git a/src/BrowserModel.java b/src/BrowserModel.java index 9263bb3..81ccaf1 100755 --- a/src/BrowserModel.java +++ b/src/BrowserModel.java @@ -4,12 +4,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.ResourceBundle; /** * This represents the heart of the browser: the collections * that organize all the URLs into useful structures. - * + * * @author Robert C. Duvall */ public class BrowserModel { @@ -20,24 +21,31 @@ public class BrowserModel { private URL myCurrentURL; private int myCurrentIndex; private List myHistory; + private ResourceBundle errorResources; + + public Map getMyFavorites() { + return myFavorites; + } + private Map myFavorites; /** * Creates an empty model. */ - public BrowserModel () { + public BrowserModel() { myHome = null; myCurrentURL = null; myCurrentIndex = -1; myHistory = new ArrayList<>(); myFavorites = new HashMap<>(); + errorResources = ResourceBundle.getBundle(BrowserView.DEFAULT_RESOURCE_PACKAGE + "Errors"); } /** * Returns the first page in next history, null if next history is empty. */ - public URL next () { + public URL next() { if (hasNext()) { myCurrentIndex++; return myHistory.get(myCurrentIndex); @@ -48,63 +56,63 @@ public URL next () { /** * Returns the first page in back history, null if back history is empty. */ - public URL back () { + public URL back() { if (hasPrevious()) { myCurrentIndex--; return myHistory.get(myCurrentIndex); + } else { + String error = errorResources.getString("ErrorOnBack"); + throw new BrowserException(error); } - return null; } /** * Changes current page to given URL, removing next history. */ - public URL go (String url) { + public URL go(String url) { 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 (myCurrentURL != null) { - if (hasNext()) { - myHistory = myHistory.subList(0, myCurrentIndex + 1); - } - myHistory.add(myCurrentURL); - myCurrentIndex++; + if (hasNext()) { + myHistory = myHistory.subList(0, myCurrentIndex + 1); } + myHistory.add(myCurrentURL); + myCurrentIndex++; return myCurrentURL; - } - catch (Exception e) { - return null; + } catch (Exception e) { + String error = String.format(errorResources.getString("ErrorOnGo"), url); + throw new BrowserException(error); } } /** * Returns true if there is a next URL available */ - public boolean hasNext () { + public boolean hasNext() { return myCurrentIndex < (myHistory.size() - 1); } /** * Returns true if there is a previous URL available */ - public boolean hasPrevious () { + public boolean hasPrevious() { return myCurrentIndex > 0; } /** * Returns URL of the current home page or null if none is set. */ - public URL getHome () { + public URL getHome() { return myHome; } /** * Sets current home page to the current URL being viewed. */ - public void setHome () { + public void setHome() { // just in case, might be called before a page is visited if (myCurrentURL != null) { myHome = myCurrentURL; @@ -114,7 +122,7 @@ public void setHome () { /** * Adds current URL being viewed to favorites collection with given name. */ - public void addFavorite (String name) { + public void addFavorite(String name) { // just in case, might be called before a page is visited if (name != null && !name.equals("") && myCurrentURL != null) { myFavorites.put(name, myCurrentURL); @@ -124,15 +132,20 @@ 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) { if (name != null && !name.equals("") && myFavorites.containsKey(name)) { return myFavorites.get(name); + } else { + String error = String.format(errorResources.getString("ErrorOnGetFavorite"), name); + throw new BrowserException(error); } - return null; } // deal with a potentially incomplete URL - private URL completeURL (String possible) { + private URL completeURL(String possible) { try { // try it as is return new URL(possible); @@ -146,7 +159,8 @@ private URL completeURL (String possible) { // e.g., let user leave off initial protocol return new URL(PROTOCOL_PREFIX + possible); } catch (MalformedURLException eee) { - return null; + String error = String.format(errorResources.getString("ErrorOnGo"), possible); + throw new BrowserException(error); } } } diff --git a/src/BrowserView.java b/src/BrowserView.java index f9d591d..c14b496 100644 --- a/src/BrowserView.java +++ b/src/BrowserView.java @@ -1,4 +1,6 @@ import java.awt.Dimension; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.URL; import java.util.Optional; import java.util.ResourceBundle; @@ -61,8 +63,7 @@ public class BrowserView { private Button myBackButton; private Button myNextButton; private Button myHomeButton; - // favorites - private ComboBox myFavorites; + private ComboBox favoritesCombo; // get strings from resource file private ResourceBundle myResources; // the data @@ -84,20 +85,19 @@ public BrowserView (BrowserModel model, String language) { enableButtons(); // 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. */ public void showPage (String url) { - URL valid = myModel.go(url); - if (valid != null) { - update(valid); - } - else { - showError("Could not load " + url); - } + try { + URL valid = myModel.go(url); + update(valid); + } catch (BrowserException e) { + showError(e.getMessage()); + } } /** @@ -141,9 +141,12 @@ private void home () { // change page to favorite choice private void showFavorite (String favorite) { - showPage(myModel.getFavorite(favorite).toString()); + try { + showPage(myModel.getFavorite(favorite).toString()); + } catch(Exception e) { + showError(e.getMessage()); + } // reset favorites ComboBox so the same choice can be made again - myFavorites.setValue(null); } // update just the view to display given URL @@ -163,7 +166,7 @@ private void addFavorite () { // did user make a choice? if (response.isPresent()) { myModel.addFavorite(response.get()); - myFavorites.getItems().add(response.get()); + favoritesCombo.getItems().add(response.get()); } } @@ -208,6 +211,7 @@ public void handle (ActionEvent event) { } }); result.getChildren().add(myBackButton); + myBackButton.getStyleClass().add("back"); // new style way to do set up callback (lambdas) myNextButton = makeButton("NextCommand", event -> next()); result.getChildren().add(myNextButton); @@ -218,25 +222,37 @@ public void handle (ActionEvent event) { result.getChildren().add(makeButton("GoCommand", showHandler)); myURLDisplay = makeInputField(40, showHandler); result.getChildren().add(myURLDisplay); + favoritesCombo =createCombo(); + result.getChildren().add(favoritesCombo); return result; } + public ComboBox createCombo(){ + ComboBox cb = new ComboBox<>(); + cb.setOnAction(t -> { + String selectedItem = cb.getSelectionModel().getSelectedItem(); + String url = myModel.getMyFavorites().get(selectedItem).toString(); + showPage(url); + }); + return cb; + } + // make buttons for setting favorites/home URLs private Node makePreferencesPanel () { HBox result = new HBox(); - myFavorites = new ComboBox(); // ADD REST OF CODE HERE - result.getChildren().add(makeButton("SetHomeCommand", event -> { - myModel.setHome(); - enableButtons(); - })); + + result.getChildren().add(makeButton("SetHomeCommand", myModel, "setHome")); + + result.getChildren().add(makeButton("AddFavoriteCommand",this, "addFavorites")); + enableButtons(); return result; } // makes a button using either an image or a label - private Button makeButton (String property, EventHandler handler) { + private Button makeButton (String property, Object obj, String methodName) { // represent all supported image suffixes - final String IMAGEFILE_SUFFIXES = + final String IMAGEFILE_SUFFIXES = String.format(".*\\.(%s)", String.join("|", ImageIO.getReaderFileSuffixes())); Button result = new Button(); @@ -247,7 +263,19 @@ private Button makeButton (String property, EventHandler handler) { } else { result.setText(label); } - result.setOnAction(handler); + try { + Method method = obj.getClass().getMethod(methodName); + result.setOnAction(t -> { + try { + method.invoke(obj); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + }); + } catch (Exception e) { + e.printStackTrace(); + } + return result; } @@ -302,5 +330,5 @@ public void changed (ObservableValue ov, State oldState, State } } } - }; + } } diff --git a/src/resources/English.properties b/src/resources/English.properties index 5cb3530..102c06a 100755 --- a/src/resources/English.properties +++ b/src/resources/English.properties @@ -2,7 +2,7 @@ BackCommand=Back NextCommand=Next HomeCommand=Home GoCommand=Go -AddFavoriteCommand=Add Favorites +AddFavoriteCommand=AddFavorites FavoritePrompt=Enter name ErrorTitle=Browser Error FavoritePromptTitle=Add Favorite diff --git a/src/resources/Errors.properties b/src/resources/Errors.properties new file mode 100644 index 0000000..cb3caae --- /dev/null +++ b/src/resources/Errors.properties @@ -0,0 +1,3 @@ +ErrorOnGo=Could not load %s today! +ErrorOnGetFavorite="Could not get %s favorite" +ErrorOnBack="Could not go back" \ No newline at end of file