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

netid: rfm21 and lad47 #7

Open
wants to merge 1 commit 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# lab_browser
A simple GUI example: a web browser

Rob Martorano and Lucas Donaldson
netid: rfm21 and lad47


9 changes: 9 additions & 0 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Created by Rob on 9/17/15.
*/
public class BrowserException extends RuntimeException {

public BrowserException(String s) {
super(s);
}
}
20 changes: 12 additions & 8 deletions src/BrowserModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public URL next () {
myCurrentIndex++;
return myHistory.get(myCurrentIndex);
}
return null;
throw new BrowserException("couldn't go next");
}

/**
Expand All @@ -53,7 +53,7 @@ public URL back () {
myCurrentIndex--;
return myHistory.get(myCurrentIndex);
}
return null;
throw new BrowserException("couldn't go back");
}

/**
Expand Down Expand Up @@ -112,22 +112,26 @@ public void addFavorite (String name) {
}
}

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

/**
* 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;
throw new BrowserException("favorite not found");
}

// deal with a potentially incomplete URL
private URL completeURL (String possible) {
try {
// try it as is
return new URL(possible);
} catch (MalformedURLException e) {
} /*catch (MalformedURLException e) {
try {
// try it as a relative link
// BUGBUG: need to generalize this :(
Expand All @@ -136,10 +140,10 @@ private URL completeURL (String possible) {
try {
// e.g., let user leave off initial protocol
return new URL(PROTOCOL_PREFIX + possible);
} catch (MalformedURLException eee) {
return null;
}
}
}*/ catch (MalformedURLException eee) {
throw new BrowserException("bad url");
//}
//}
}
}
}
38 changes: 30 additions & 8 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import java.awt.Dimension;
import java.net.URL;
import java.util.Map;
import java.util.Optional;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
Expand Down Expand Up @@ -47,6 +48,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 STYLESHEET = "default.css";
public static final String BLANK = " ";

Expand All @@ -61,20 +63,22 @@ public class BrowserView {
private Button myBackButton;
private Button myNextButton;
private Button myHomeButton;
private Button addFavorite;
// favorites
private ComboBox<String> myFavorites;
// get strings from resource file
private ResourceBundle myResources;
private ResourceBundle errorResources;
// the data
private BrowserModel myModel;

/**
* Create a view of the given model of a web browser.
*/
public BrowserView (BrowserModel model, String language) {
myModel = model;
// use resources for labels
myResources = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + language);
errorResources = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + "Errors");
BorderPane root = new BorderPane();
// must be first since other panels may refer to page
root.setCenter(makePageDisplay());
Expand All @@ -84,20 +88,22 @@ 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 (url != null) {
URL valid;
try {
valid = myModel.go(url);
update(valid);
}
else {
showError("Could not load " + url);
catch (BrowserException e){
System.out.println(String.format(errorResources.getString("URLErrorTitle"),url));
}

}

/**
Expand Down Expand Up @@ -166,8 +172,8 @@ private void addFavorite () {

// only enable buttons when useful to user
private void enableButtons () {
myBackButton.setDisable(! myModel.hasPrevious());
myNextButton.setDisable(! myModel.hasNext());
myBackButton.setDisable(!myModel.hasPrevious());
myNextButton.setDisable(!myModel.hasNext());
myHomeButton.setDisable(myModel.getHome() == null);
}

Expand Down Expand Up @@ -225,6 +231,20 @@ private Node makePreferencesPanel () {
myModel.setHome();
enableButtons();
}));
result.getChildren().add(makeButton("AddFavoriteCommand", event -> {
addFavorite();
enableButtons();
}));
myFavorites = new ComboBox<String>();
Map<String, URL> map = myModel.getFavoriteList();
for (String key: map.keySet()){
myFavorites.getItems().add(key);
}
myFavorites.setValue("Favorite List");
myFavorites.setOnAction((event) -> {
showFavorite(myFavorites.getValue());
});
result.getChildren().add(myFavorites);
return result;
}

Expand All @@ -243,6 +263,8 @@ private Button makeButton (String property, EventHandler<ActionEvent> handler) {
result.setText(label);
}
result.setOnAction(handler);


return result;
}

Expand Down
10 changes: 10 additions & 0 deletions src/resources/Errors.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
BackCommand=Back32.gif
NextCommand=Forward32.gif
HomeCommand=Home32.gif
GoCommand=Refresh32.gif
AddFavoriteCommand=Favorite32.gif
URLErrorTitle=Could not load %s check your spelling
FavoritePrompt=Enter name
FavoritePromptTitle=Add Favorite
FavoriteFirstItem=All Favorites
SetHomeCommand=Set Home