Skip to content

Browser #18

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

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

mjo14
it15
9 changes: 9 additions & 0 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

public class BrowserException extends RuntimeException{

public BrowserException(String string) {
// TODO Auto-generated constructor stub
super(string);
}

}
28 changes: 19 additions & 9 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 @@ -15,12 +16,16 @@
public class BrowserModel {
// constants
public static final String PROTOCOL_PREFIX = "http://";
public static final String DEFAULT_RESOURCE_PACKAGE = "resources/";

// state
private URL myHome;
private URL myCurrentURL;
private int myCurrentIndex;
private List<URL> myHistory;
private Map<String, URL> myFavorites;
private ResourceBundle myResources;



/**
Expand All @@ -32,34 +37,38 @@ public BrowserModel () {
myCurrentIndex = -1;
myHistory = new ArrayList<>();
myFavorites = new HashMap<>();
myResources = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + "Errors");
}

/**
* Returns the first page in next history, null if next history is empty.
* @throws BrowserException
*/
public URL next () {
public URL next () throws BrowserException {
if (hasNext()) {
myCurrentIndex++;
return myHistory.get(myCurrentIndex);
}
return null;
throw new BrowserException(String.format(myResources.getString("NoNextFuture"), ""));
}

/**
* Returns the first page in back history, null if back history is empty.
* @throws BrowserException
*/
public URL back () {
public URL back () throws BrowserException {
if (hasPrevious()) {
myCurrentIndex--;
return myHistory.get(myCurrentIndex);
}
return null;
throw new BrowserException(String.format(myResources.getString("NoPreviousHistory"), ""));
}

/**
* Changes current page to given URL, removing next history.
* @throws BrowserException
*/
public URL go (String url) {
public URL go (String url) throws BrowserException {
myCurrentURL = completeURL(url);
if (myCurrentURL != null) {
if (hasNext()) {
Expand Down Expand Up @@ -114,16 +123,17 @@ public void addFavorite (String name) {

/**
* Returns URL from favorites associated with given name, null if none set.
* @throws BrowserException
*/
public URL getFavorite (String name) {
public URL getFavorite (String name) throws BrowserException {
if (name != null && !name.equals("") && myFavorites.containsKey(name)) {
return myFavorites.get(name);
}
return null;
throw new BrowserException(String.format(myResources.getString("NoFavorites"), ""));
}

// deal with a potentially incomplete URL
private URL completeURL (String possible) {
private URL completeURL (String possible) throws BrowserException {
try {
// try it as is
return new URL(possible);
Expand All @@ -137,7 +147,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("IncompleteURL"), possible));
}
}
}
Expand Down
35 changes: 29 additions & 6 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ 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);
}

/**
Expand Down Expand Up @@ -221,12 +221,31 @@ public void handle (ActionEvent event) {
// make buttons for setting favorites/home URLs
private Node makePreferencesPanel () {
HBox result = new HBox();
myFavorites = new ComboBox<String>();
result.getChildren().add(makeButton("SetHomeCommand", event -> {
myModel.setHome();
enableButtons();
}));
result.getChildren().add(makeButton("FavoritePromptTitle", event -> {
addFavorite();
}));

result.getChildren().add(makeComboBox( event -> {
myModel.go(myModel.getFavorite(myFavorites.getValue()).toString());
showPage(myModel.getFavorite(myFavorites.getValue()).toString());
}));

return result;
}

private ComboBox<String> makeComboBox(EventHandler<ActionEvent> handler) {
// represent all supported image suffixes

myFavorites = new ComboBox<String>();

myFavorites.setOnAction(handler);
return myFavorites;
}

// makes a button using either an image or a label
private Button makeButton (String property, EventHandler<ActionEvent> handler) {
Expand All @@ -236,11 +255,15 @@ private Button makeButton (String property, EventHandler<ActionEvent> handler) {

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);
try{
if (label.matches(IMAGEFILE_SUFFIXES)) {
result.setGraphic(new ImageView(
new Image(getClass().getResourceAsStream(DEFAULT_RESOURCE_PACKAGE + label))));
} else {
result.setText(label);
}
}catch(BrowserException e){

}
result.setOnAction(handler);
return result;
Expand Down
4 changes: 4 additions & 0 deletions src/resources/Errors.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NoFavorites=No favorite found %s
IncompleteURL=Incomplete URL %s
NoPreviousHistory=Not able to go back because ur a scrub %s
NoNextFuture=I get buckets %s
4 changes: 2 additions & 2 deletions src/resources/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
}

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