Skip to content

Recitation changes #9

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 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
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# lab_browser
A simple GUI example: a web browser
Names: Matt Elgart (mje21), Nick Groszewski (nrg12)
14 changes: 10 additions & 4 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(String.format("NextError", myCurrentURL));
}

/**
Expand All @@ -53,7 +53,7 @@ public URL back () {
myCurrentIndex--;
return myHistory.get(myCurrentIndex);
}
return null;
throw new BrowserException(String.format("BackError", myCurrentURL));
}

/**
Expand Down Expand Up @@ -119,7 +119,7 @@ public URL getFavorite (String name) {
if (name != null && !name.equals("") && myFavorites.containsKey(name)) {
return myFavorites.get(name);
}
return null;
throw new BrowserException(String.format("FavoriteError", myCurrentURL));
}

// deal with a potentially incomplete URL
Expand All @@ -137,9 +137,15 @@ 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("IncorrectURL", myCurrentURL));
}
}
}
}

public class BrowserException extends RuntimeException {
public BrowserException(String message) {
super(message);
}
}
}
53 changes: 44 additions & 9 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
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 BrowserModel.BrowserException;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
Expand All @@ -23,12 +33,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 +65,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 Down Expand Up @@ -92,14 +97,20 @@ public BrowserView (BrowserModel model, String language) {
*/
public void showPage (String url) {
URL valid = myModel.go(url);
if (url != null) {
try {
update(valid);
}
else {
showError("Could not load " + url);
catch (BrowserException e) {
showError(String.format("Could not load %s check your spelling", url));
}
}

public class BrowserException extends RuntimeException {
public BrowserException(String message) {
super(message);
}
}

/**
* Returns scene for this view so it can be added to stage.
*/
Expand Down Expand Up @@ -169,6 +180,7 @@ private void enableButtons () {
myBackButton.setDisable(! myModel.hasPrevious());
myNextButton.setDisable(! myModel.hasNext());
myHomeButton.setDisable(myModel.getHome() == null);
myFavorites.setDisable(false);
}

// convenience method to create HTML page display
Expand Down Expand Up @@ -225,6 +237,10 @@ private Node makePreferencesPanel () {
myModel.setHome();
enableButtons();
}));
myFavoritesButton = makeButton("AddFavoriteCommand", event -> addFavorite());
result.getChildren().add(myFavoritesButton);
myFavorites = makeComboBox("FavoriteFirstItem", event -> showFavorite(myFavorites.getValue()));
result.getChildren().add(myFavorites);
return result;
}

Expand All @@ -245,6 +261,25 @@ private Button makeButton (String property, EventHandler<ActionEvent> handler) {
result.setOnAction(handler);
return result;
}

// makes a comboBox
private ComboBox<String> makeComboBox (String property, EventHandler<ActionEvent> handler) {
// represent all supported image suffixes
final String IMAGEFILE_SUFFIXES =
String.format(".*\\.(%s)", String.join("|", ImageIO.getReaderFileSuffixes()));

ComboBox<String> result = new ComboBox<String>();
String label = myResources.getString(property);
/*if (label.matches(IMAGEFILE_SUFFIXES)) {
result.setGraphic(new ImageView(
new Image(getClass().getResourceAsStream(DEFAULT_RESOURCE_PACKAGE + label))));
} else {*/
//result.setAccessibleText(label);
result.setPromptText(label);
//}
result.setOnAction(handler);
return result;
}

// make text field for input
private TextField makeInputField (int width, EventHandler<ActionEvent> handler) {
Expand Down
4 changes: 4 additions & 0 deletions src/resources/ErrorsEnglish.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BackError=Could not load %s, no back found
NextError=Could not load %s, no next found
FavoriteError=Could not load %s, no favorite found
IncorrectURL=Could not load %s, incorrect URL