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..271688d
--- /dev/null
+++ b/src/BrowserException.java
@@ -0,0 +1,29 @@
+
+
+public class BrowserException extends RuntimeException {
+
+ 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
+ }
+
+}
diff --git a/src/BrowserModel.java b/src/BrowserModel.java
index 9263bb3..55a6922 100755
--- a/src/BrowserModel.java
+++ b/src/BrowserModel.java
@@ -42,7 +42,7 @@ public URL next () {
myCurrentIndex++;
return myHistory.get(myCurrentIndex);
}
- return null;
+ throw new BrowserException();
}
/**
@@ -53,7 +53,7 @@ public URL back () {
myCurrentIndex--;
return myHistory.get(myCurrentIndex);
}
- return null;
+ throw new BrowserException();
}
/**
@@ -76,7 +76,7 @@ public URL go (String url) {
return myCurrentURL;
}
catch (Exception e) {
- return null;
+ throw new BrowserException(url);
}
}
@@ -128,7 +128,7 @@ public URL getFavorite (String name) {
if (name != null && !name.equals("") && myFavorites.containsKey(name)) {
return myFavorites.get(name);
}
- return null;
+ throw new BrowserException(name);
}
// deal with a potentially incomplete URL
@@ -146,7 +146,7 @@ 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(possible);
}
}
}
diff --git a/src/BrowserView.java b/src/BrowserView.java
index f9d591d..c497452 100644
--- a/src/BrowserView.java
+++ b/src/BrowserView.java
@@ -1,7 +1,18 @@
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;
+
+import javax.imageio.ImageIO;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.events.EventListener;
+import org.w3c.dom.events.EventTarget;
+
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
@@ -23,12 +34,6 @@
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
-import javax.imageio.ImageIO;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.NodeList;
-import org.w3c.dom.events.EventListener;
-import org.w3c.dom.events.EventTarget;
/**
@@ -61,6 +66,7 @@ public class BrowserView {
private Button myBackButton;
private Button myNextButton;
private Button myHomeButton;
+ private Button myFavoritesButton;
// favorites
private ComboBox myFavorites;
// get strings from resource file
@@ -70,8 +76,13 @@ public class BrowserView {
/**
* Create a view of the given model of a web browser.
+ * @throws SecurityException
+ * @throws NoSuchMethodException
+ * @throws InvocationTargetException
+ * @throws IllegalArgumentException
+ * @throws IllegalAccessException
*/
- public BrowserView (BrowserModel model, String language) {
+ public BrowserView (BrowserModel model, String language) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
myModel = model;
// use resources for labels
myResources = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + language);
@@ -84,19 +95,18 @@ 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,7 +151,8 @@ private void home () {
// change page to favorite choice
private void showFavorite (String favorite) {
- showPage(myModel.getFavorite(favorite).toString());
+ String url = myModel.getFavorite(favorite).toString();
+ showPage(url);
// reset favorites ComboBox so the same choice can be made again
myFavorites.setValue(null);
}
@@ -183,7 +194,7 @@ private Node makePageDisplay () {
}
// organize user's options for controlling/giving input to model
- private Node makeInputPanel () {
+ private Node makeInputPanel () throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
VBox result = new VBox();
result.getChildren().addAll(makeNavigationPanel(), makePreferencesPanel());
return result;
@@ -197,44 +208,46 @@ private Node makeInformationPanel () {
}
// make user-entered URL/text field and back/next buttons
- private Node makeNavigationPanel () {
+ private Node makeNavigationPanel () throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
HBox result = new HBox();
// create buttons, with their associated actions
// old style way to do set up callback (anonymous class)
- myBackButton = makeButton("BackCommand", new EventHandler() {
- @Override
- public void handle (ActionEvent event) {
- back();
- }
- });
+ myBackButton = makeButton("BackCommand", "methodName");
result.getChildren().add(myBackButton);
// new style way to do set up callback (lambdas)
- myNextButton = makeButton("NextCommand", event -> next());
+ myNextButton = makeButton("NextCommand", "methodName");
result.getChildren().add(myNextButton);
- myHomeButton = makeButton("HomeCommand", event -> home());
+ myHomeButton = makeButton("HomeCommand", "methodName");
result.getChildren().add(myHomeButton);
// if user presses button or enter in text field, load/show the URL
EventHandler showHandler = new ShowPage();
- result.getChildren().add(makeButton("GoCommand", showHandler));
+ result.getChildren().add(makeButton("GoCommand", "methodName"));
myURLDisplay = makeInputField(40, showHandler);
result.getChildren().add(myURLDisplay);
return result;
}
// make buttons for setting favorites/home URLs
- private Node makePreferencesPanel () {
+ private Node makePreferencesPanel () throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
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", "methodName"));
+
+ myFavoritesButton = makeButton("AddFavoriteCommand", "methodName");
+ result.getChildren().add(myFavoritesButton);
+ result.getChildren().add(myFavorites);
+
+ myFavorites.setOnAction((event) -> {
+ String selectedPerson = myFavorites.getSelectionModel().getSelectedItem();
+ showFavorite(selectedPerson);
+ });
+
return result;
}
// makes a button using either an image or a label
- private Button makeButton (String property, EventHandler handler) {
+ private Button makeButton (String property, String method) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
// represent all supported image suffixes
final String IMAGEFILE_SUFFIXES =
String.format(".*\\.(%s)", String.join("|", ImageIO.getReaderFileSuffixes()));
@@ -247,7 +260,14 @@ private Button makeButton (String property, EventHandler handler) {
} else {
result.setText(label);
}
- result.setOnAction(handler);
+ Method myMethod = getClass().getMethod(method, getClass());
+ result.setOnAction(e -> {try {
+ myMethod.invoke(this, this);
+ } catch (Exception e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }});
+
return result;
}
diff --git a/src/Main.java b/src/Main.java
index c258d02..8c5f7ff 100755
--- a/src/Main.java
+++ b/src/Main.java
@@ -1,3 +1,5 @@
+import java.lang.reflect.InvocationTargetException;
+
import javafx.application.Application;
import javafx.stage.Stage;
@@ -14,7 +16,7 @@ public class Main extends Application {
@Override
- public void start (Stage stage) {
+ public void start (Stage stage) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
// create program specific components
BrowserModel model = new BrowserModel();
BrowserView display = new BrowserView(model, "English");
diff --git a/src/resources/English.properties b/src/resources/English.properties
index 5cb3530..0499e19 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
+ErrorMessage = Could not load
\ No newline at end of file