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 new file mode 100644 index 0000000..6156bf9 --- /dev/null +++ b/src/BrowserException.java @@ -0,0 +1,39 @@ + +public class BrowserException extends RuntimeException { + + String eMessage; + + public BrowserException() { + // TODO Auto-generated constructor stub + } + + public BrowserException(String message) { + super(message); + this.eMessage = 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); + this.eMessage = message; + // 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 + this.eMessage = message; + } + + public String getMessage(){ + return eMessage; + + } + +} diff --git a/src/BrowserModel.java b/src/BrowserModel.java index 9263bb3..1873c91 100755 --- a/src/BrowserModel.java +++ b/src/BrowserModel.java @@ -4,7 +4,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; - +import java.util.ResourceBundle; +import java.util.Set; /** * This represents the heart of the browser: the collections @@ -15,12 +16,15 @@ public class BrowserModel { // constants public static final String PROTOCOL_PREFIX = "http://"; + public static final String DEFAULT_ERROR_PACKAGE = "resources/ModelErrors"; // state private URL myHome; private URL myCurrentURL; private int myCurrentIndex; private List myHistory; private Map myFavorites; + // get strings from resource file + private ResourceBundle myResources; /** @@ -32,6 +36,7 @@ public BrowserModel () { myCurrentIndex = -1; myHistory = new ArrayList<>(); myFavorites = new HashMap<>(); + myResources = ResourceBundle.getBundle(DEFAULT_ERROR_PACKAGE); } /** @@ -42,7 +47,9 @@ public URL next () { myCurrentIndex++; return myHistory.get(myCurrentIndex); } - return null; + else{ + throw new BrowserException(myResources.getString("NextError")); + } } /** @@ -53,7 +60,9 @@ public URL back () { myCurrentIndex--; return myHistory.get(myCurrentIndex); } - return null; + else{ + throw new BrowserException(myResources.getString("BackError")); + } } /** @@ -76,7 +85,7 @@ public URL go (String url) { return myCurrentURL; } catch (Exception e) { - return null; + throw new BrowserException(String.format(myResources.getString("ErrorOnGo"), url)); } } @@ -120,6 +129,10 @@ public void addFavorite (String name) { myFavorites.put(name, myCurrentURL); } } + + public Set getFavoriteList(){ + return myFavorites.keySet(); + } /** * Returns URL from favorites associated with given name, null if none set. @@ -128,7 +141,9 @@ public URL getFavorite (String name) { if (name != null && !name.equals("") && myFavorites.containsKey(name)) { return myFavorites.get(name); } - return null; + else{ + throw new BrowserException(); + } } // deal with a potentially incomplete URL @@ -146,9 +161,10 @@ 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(String.format(myResources.getString("BadURL"), possible)); } } } } + } diff --git a/src/BrowserView.java b/src/BrowserView.java index f9d591d..15c7280 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; @@ -47,6 +49,7 @@ public class BrowserView { // constants public static final Dimension DEFAULT_SIZE = new Dimension(800, 600); public static final String DEFAULT_RESOURCE_PACKAGE = "resources/"; + public static final String GOOGLE_FONT = "https://fonts.googleapis.com/css?family=Permanent+Marker"; public static final String STYLESHEET = "default.css"; public static final String BLANK = " "; @@ -61,6 +64,7 @@ public class BrowserView { private Button myBackButton; private Button myNextButton; private Button myHomeButton; + private Button myAddFavoriteButton; // favorites private ComboBox myFavorites; // get strings from resource file @@ -84,19 +88,20 @@ 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); + myScene.getStylesheets().add(GOOGLE_FONT); } /** * 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()); } } @@ -224,16 +229,33 @@ public void handle (ActionEvent event) { // make buttons for setting favorites/home URLs private Node makePreferencesPanel () { HBox result = new HBox(); - myFavorites = new ComboBox(); + myFavorites = makeFavoritesBox(); // ADD REST OF CODE HERE result.getChildren().add(makeButton("SetHomeCommand", event -> { myModel.setHome(); enableButtons(); })); + myAddFavoriteButton = makeButton("AddFavoriteCommand", event -> addFavorite()); + result.getChildren().add(myAddFavoriteButton); + result.getChildren().add(myFavorites); return result; } + + private ComboBox makeFavoritesBox(){ + ComboBox box = new ComboBox(); + for(String fave: myModel.getFavoriteList()){ + box.getItems().add(fave); + } + box.setOnAction(event -> selectFavorite(event)); + return box; + } + + private void selectFavorite(ActionEvent event) { + String faveName = myFavorites.getValue();; + showFavorite(faveName); + } - // makes a button using either an image or a label + // makes a button using either an image or a label private Button makeButton (String property, EventHandler handler) { // represent all supported image suffixes final String IMAGEFILE_SUFFIXES = @@ -247,7 +269,21 @@ private Button makeButton (String property, EventHandler handler) { } else { result.setText(label); } - result.setOnAction(handler); + result.setOnAction(ex -> { + try{ + Method method = null; + try { + method = this.getClass().getMethod("addFavorite"); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + method.invoke(this); + }catch (IllegalArgumentException e) { + } catch (IllegalAccessException e) { + } catch (InvocationTargetException e) { + } + }); return result; } diff --git a/src/resources/English.properties b/src/resources/English.properties index 5cb3530..b3daf17 100755 --- a/src/resources/English.properties +++ b/src/resources/English.properties @@ -8,3 +8,4 @@ ErrorTitle=Browser Error FavoritePromptTitle=Add Favorite FavoriteFirstItem=All Favorites SetHomeCommand=Set Home +ErrorOnGo=Could not load %s diff --git a/src/resources/Gibberish.properties b/src/resources/Gibberish.properties index a5ac58d..59b738e 100755 --- a/src/resources/Gibberish.properties +++ b/src/resources/Gibberish.properties @@ -8,3 +8,4 @@ ErrorTitle=BAD FavoritePromptTitle=123456789 FavoriteFirstItem=Go Away SetHomeCommand=ET Phone Home +ErrorOnGo=asldkjfkdjfijwiejf %s diff --git a/src/resources/Image.properties b/src/resources/Image.properties index 6da9c63..ea371d5 100755 --- a/src/resources/Image.properties +++ b/src/resources/Image.properties @@ -8,3 +8,4 @@ FavoritePrompt=Enter name FavoritePromptTitle=Add Favorite FavoriteFirstItem=All Favorites SetHomeCommand=Set Home +ErrorOnGo=Could not load %s diff --git a/src/resources/ModelErrors.properties b/src/resources/ModelErrors.properties new file mode 100644 index 0000000..f8cad87 --- /dev/null +++ b/src/resources/ModelErrors.properties @@ -0,0 +1,5 @@ +ErrorOnGo=Could not load %s today! +BackError=No Previous Page! +FavoritError=No Favorites! +NextError=No Next Page! +BadURL=Could not find %s \ No newline at end of file diff --git a/src/resources/default.css b/src/resources/default.css index 0d8badb..bb6b4f9 100644 --- a/src/resources/default.css +++ b/src/resources/default.css @@ -1,40 +1,41 @@ .root { - -fx-font-size: 14pt; - -fx-font-family: "Courier New"; - -fx-base: rgb(132, 145, 47); - -fx-background: rgb(225, 228, 203); + -fx-font-size: 12pt; + -fx-font-family: "Permanent Marker"; + -fx-base: #0736A4; + -fx-background: #D32323; + -fx-text-fill:black; } .button { - -fx-text-fill: #006464; - -fx-background-color: #DFB951; - -fx-border-radius: 20; - -fx-background-radius: 20; - -fx-padding: 8; + -fx-background-color:white; + -fx-padding: 6; + -fx-border-color: #0736A4; + -fx-border-radius: 2; + -fx-text-fill:black; } -.button:hover { - -fx-background-color: #3a3a3a; +.button:hover, +.button:focus { + -fx-background-color: #0736A4;; + -fx-text-fill: white; } .combo-box-base { - -fx-text-base-color: #006464; - -fx-background-color: #DFB951; - -fx-border-radius: 20; - -fx-background-radius: 20; + -fx-background-color:white; } .combo-box-base:hover { - -fx-background-color: #3a3a3a; + -fx-background-color: #0736A4;; + -fx-text-fill: white; } .label { - -fx-font-size: 11pt; - -fx-font-family: "Segoe UI Semibold"; + -fx-font-size: 9pt; + -fx-font-family: "Permanent Marker"; -fx-text-fill: #006464; -fx-opacity: 0.6; } .text-field { - -fx-font-size: 14pt; - -fx-font-family: "Segoe UI Semibold"; + -fx-font-size: 12pt; + -fx-font-family: "Permanent Marker"; }