Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nsl8 and ras70 #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
28 changes: 28 additions & 0 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

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
}

}
35 changes: 23 additions & 12 deletions src/BrowserModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;


/**
Expand All @@ -21,6 +22,9 @@ public class BrowserModel {
private int myCurrentIndex;
private List<URL> myHistory;
private Map<String, URL> myFavorites;
private ResourceBundle myResources;
public static final String DEFAULT_RESOURCE_PACKAGE = "resources/";



/**
Expand All @@ -32,6 +36,8 @@ public BrowserModel () {
myCurrentIndex = -1;
myHistory = new ArrayList<>();
myFavorites = new HashMap<>();
String langauge = "English"; //TODO: FIX THIS ASSUMPTION LATER!
myResources = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + langauge);
}

/**
Expand All @@ -42,18 +48,21 @@ public URL next () {
myCurrentIndex++;
return myHistory.get(myCurrentIndex);
}
return null;
else{
throw new IndexOutOfBoundsException();
}

}

/**
* Returns the first page in back history, null if back history is empty.
*/
public URL back () {
if (hasPrevious()) {
myCurrentIndex--;
return myHistory.get(myCurrentIndex);
}
return null;
if(hasPrevious()){
myCurrentIndex--;
return myHistory.get(myCurrentIndex);
}
throw new IndexOutOfBoundsException();
}

/**
Expand All @@ -76,7 +85,7 @@ public URL go (String url) {
return myCurrentURL;
}
catch (Exception e) {
return null;
throw new BrowserException(String.format(myResources.getString("LoadError"), url));
}
}

Expand Down Expand Up @@ -125,10 +134,12 @@ public void addFavorite (String name) {
* Returns URL from favorites associated with given name, null if none set.
*/
public URL getFavorite (String name) {
if (name != null && !name.equals("") && myFavorites.containsKey(name)) {
return myFavorites.get(name);
}
return null;
if(name != null && !name.equals("") && myFavorites.containsKey(name)) {
return myFavorites.get(name);
}
else{
throw new IndexOutOfBoundsException();
}
}

// deal with a potentially incomplete URL
Expand All @@ -146,7 +157,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(String.format(myResources.getString("URLError"), myCurrentURL.toString()),eee.getCause());
}
}
}
Expand Down
121 changes: 88 additions & 33 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import java.awt.Dimension;
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;
Expand All @@ -23,12 +30,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;


/**
Expand Down Expand Up @@ -61,6 +62,7 @@ public class BrowserView {
private Button myBackButton;
private Button myNextButton;
private Button myHomeButton;
private Button myFavoritesButton;
// favorites
private ComboBox<String> myFavorites;
// get strings from resource file
Expand All @@ -84,19 +86,29 @@ 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);
try{
URL valid = myModel.go(url);
update(valid);
}
else {
showError("Could not load " + url);
catch(BrowserException e) {
showError(e.getMessage());
}
}

public void showPage() {
try {
String url = myURLDisplay.getText();
URL valid = myModel.go(url);
update(valid);
} catch (BrowserException e) {
showError(e.getMessage());
}
}

Expand Down Expand Up @@ -125,25 +137,29 @@ public void showError (String message) {
}

// move to the next URL in the history
private void next () {
private void next() {
try{
update(myModel.next());
}
catch(IndexOutOfBoundsException e){
}
}

// move to the previous URL in the history
private void back () {
private void back() {
update(myModel.back());
}

// change current URL to the home page, if set
private void home () {
showPage(myModel.getHome().toString());
}

// change page to favorite choice
private void showFavorite (String favorite) {
showPage(myModel.getFavorite(favorite).toString());
// reset favorites ComboBox so the same choice can be made again
myFavorites.setValue(null);
// myFavorites.setValue(null);
}

// update just the view to display given URL
Expand Down Expand Up @@ -201,21 +217,18 @@ private Node makeNavigationPanel () {
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<ActionEvent>() {
@Override
public void handle (ActionEvent event) {
back();
}
});
myBackButton = makeButton("BackCommand", "back");
result.getChildren().add(myBackButton);
// new style way to do set up callback (lambdas)
myNextButton = makeButton("NextCommand", event -> next());
myNextButton = makeButton("NextCommand", "next");
result.getChildren().add(myNextButton);
myHomeButton = makeButton("HomeCommand", event -> home());
myHomeButton = makeButton("HomeCommand", "home");
result.getChildren().add(myHomeButton);
myFavoritesButton = makeButton("AddFavoriteCommand", "addFavorite");
result.getChildren().add(myFavoritesButton);
// if user presses button or enter in text field, load/show the URL
EventHandler<ActionEvent> showHandler = new ShowPage();
result.getChildren().add(makeButton("GoCommand", showHandler));
result.getChildren().add(makeButton("GoCommand", "showPage"));
myURLDisplay = makeInputField(40, showHandler);
result.getChildren().add(myURLDisplay);
return result;
Expand All @@ -226,15 +239,40 @@ private Node makePreferencesPanel () {
HBox result = new HBox();
myFavorites = new ComboBox<String>();
// ADD REST OF CODE HERE
result.getChildren().add(makeButton("SetHomeCommand", event -> {
myModel.setHome();
enableButtons();
}));
result.getChildren().add(myFavorites);
myFavorites.setOnAction(event -> {showFavorite(myFavorites.getSelectionModel().getSelectedItem().toString()); });



result.getChildren().add(makeButton("SetHomeCommand","makeModelAndButton"));
return result;
}

// makes a button using either an image or a label
private Button makeButton (String property, EventHandler<ActionEvent> handler) {

private void makeModelAndButton() {
myModel.setHome();
enableButtons();
}

// // makes a button using either an image or a label
// private Button makeButton (String property, EventHandler<ActionEvent> handler) {
// // represent all supported image suffixes
// final String IMAGEFILE_SUFFIXES =
// String.format(".*\\.(%s)", String.join("|", ImageIO.getReaderFileSuffixes()));
//
// Button result = new Button();
// String label = myResources.getString(property);
// if (label.matches(IMAGEFILE_SUFFIXES)) {
// result.setGraphic(new ImageView(
// new Image(getClass().getResourceAsStream(DEFAULT_RESOURCE_PACKAGE + label))));
// } else {
// result.setText(label);
// }
// result.setOnAction(handler);
// return result;
// }

// makes a button using either an image or a label
private Button makeButton (String property, String methodName, Object... args) {
// represent all supported image suffixes
final String IMAGEFILE_SUFFIXES =
String.format(".*\\.(%s)", String.join("|", ImageIO.getReaderFileSuffixes()));
Expand All @@ -247,7 +285,24 @@ private Button makeButton (String property, EventHandler<ActionEvent> handler) {
} else {
result.setText(label);
}
result.setOnAction(handler);
Class c = this.getClass();
try {
Method method = c.getDeclaredMethod(methodName);
result.setOnAction((ActionEvent e) -> {
try {
method.invoke(c, args);
}
catch (Exception exc) {
exc.printStackTrace();
}
});
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return result;
}

Expand Down
2 changes: 2 additions & 0 deletions src/resources/English.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ ErrorTitle=Browser Error
FavoritePromptTitle=Add Favorite
FavoriteFirstItem=All Favorites
SetHomeCommand=Set Home
LoadError=Could not load %s
URLError=Incomplete URL. %s is not Valid
4 changes: 2 additions & 2 deletions src/resources/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
-fx-font-size: 14pt;
-fx-font-family: "Courier New";
-fx-base: rgb(132, 145, 47);
-fx-background: rgb(225, 228, 203);
-fx-background: #FFFF00;
}

.button {
-fx-text-fill: #006464;
-fx-background-color: #DFB951;
-fx-background-color: #FAAFBE;
-fx-border-radius: 20;
-fx-background-radius: 20;
-fx-padding: 8;
Expand Down