Skip to content
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
26 changes: 26 additions & 0 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
62 changes: 38 additions & 24 deletions src/BrowserModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -20,24 +21,31 @@ public class BrowserModel {
private URL myCurrentURL;
private int myCurrentIndex;
private List<URL> myHistory;
private ResourceBundle errorResources;

public Map<String, URL> getMyFavorites() {
return myFavorites;
}

private Map<String, URL> 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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
}
}
Expand Down
72 changes: 50 additions & 22 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -61,8 +63,7 @@ public class BrowserView {
private Button myBackButton;
private Button myNextButton;
private Button myHomeButton;
// favorites
private ComboBox<String> myFavorites;
private ComboBox<String> favoritesCombo;
// get strings from resource file
private ResourceBundle myResources;
// the data
Expand All @@ -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());
}
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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());
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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<String> createCombo(){
ComboBox<String> 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<String>();
// 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<ActionEvent> 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();
Expand All @@ -247,7 +263,19 @@ private Button makeButton (String property, EventHandler<ActionEvent> 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;
}

Expand Down Expand Up @@ -302,5 +330,5 @@ public void changed (ObservableValue<? extends State> ov, State oldState, State
}
}
}
};
}
}
2 changes: 1 addition & 1 deletion src/resources/English.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/resources/Errors.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ErrorOnGo=Could not load %s today!
ErrorOnGetFavorite="Could not get %s favorite"
ErrorOnBack="Could not go back"