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

Austin Liu (abl17) and Sung-Hoon Kim (sk342) Recitation Pull Request #6

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
7 changes: 7 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?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="con" path="org.eclipse.fx.ide.jdt.core.JAVAFX_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# lab_browser
Names: Austin Liu (abl17), Sung-Hoon Kim (sk342)

A simple GUI example: a web browser
8 changes: 8 additions & 0 deletions build.fxbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="ASCII"?>
<anttasks:AntTask xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:anttasks="http://org.eclipse.fx.ide.jdt/1.0" buildDirectory="${project}/build">
<deploy>
<application name="lab_browser"/>
<info/>
</deploy>
<signjar/>
</anttasks:AntTask>
15 changes: 15 additions & 0 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

public class BrowserException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;

public BrowserException() {
super();
}

public BrowserException(String s) {
super(s);
}
}
267 changes: 138 additions & 129 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 @@ -13,133 +14,141 @@
* @author Robert C. Duvall
*/
public class BrowserModel {
// constants
public static final String PROTOCOL_PREFIX = "http://";
// state
private URL myHome;
private URL myCurrentURL;
private int myCurrentIndex;
private List<URL> myHistory;
private Map<String, URL> myFavorites;


/**
* Creates an empty model.
*/
public BrowserModel () {
myHome = null;
myCurrentURL = null;
myCurrentIndex = -1;
myHistory = new ArrayList<>();
myFavorites = new HashMap<>();
}

/**
* Returns the first page in next history, null if next history is empty.
*/
public URL next () {
if (hasNext()) {
myCurrentIndex++;
return myHistory.get(myCurrentIndex);
}
return null;
}

/**
* 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;
}

/**
* Changes current page to given URL, removing next history.
*/
public URL go (String url) {
myCurrentURL = completeURL(url);
if (myCurrentURL != null) {
if (hasNext()) {
myHistory = myHistory.subList(0, myCurrentIndex + 1);
}
myHistory.add(myCurrentURL);
myCurrentIndex++;
}
return myCurrentURL;
}

/**
* Returns true if there is a next URL available
*/
public boolean hasNext () {
return myCurrentIndex < (myHistory.size() - 1);
}

/**
* Returns true if there is a previous URL available
*/
public boolean hasPrevious () {
return myCurrentIndex > 0;
}

/**
* Returns URL of the current home page or null if none is set.
*/
public URL getHome () {
return myHome;
}

/**
* Sets current home page to the current URL being viewed.
*/
public void setHome () {
// just in case, might be called before a page is visited
if (myCurrentURL != null) {
myHome = myCurrentURL;
}
}

/**
* Adds current URL being viewed to favorites collection with given 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);
}
}

/**
* 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;
}

// deal with a potentially incomplete URL
private URL completeURL (String possible) {
try {
// try it as is
return new URL(possible);
} catch (MalformedURLException e) {
try {
// try it as a relative link
// BUGBUG: need to generalize this :(
return new URL(myCurrentURL.toString() + "/" + possible);
} catch (MalformedURLException ee) {
try {
// e.g., let user leave off initial protocol
return new URL(PROTOCOL_PREFIX + possible);
} catch (MalformedURLException eee) {
return null;
}
}
}
}
// constants
public static final String PROTOCOL_PREFIX = "http://";
// state
private URL myHome;
private URL myCurrentURL;
private int myCurrentIndex;
private List<URL> myHistory;
private Map<String, URL> myFavorites;

private ResourceBundle myResource;


/**
* Creates an empty model.
*/
public BrowserModel () {
myHome = null;
myCurrentURL = null;
myCurrentIndex = -1;
myHistory = new ArrayList<>();
myFavorites = new HashMap<>();

myResource = ResourceBundle.getBundle(BrowserView.DEFAULT_RESOURCE_PACKAGE + "ErrorsEnglish");
}

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

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

/**
* Changes current page to given URL, removing next history.
*/
public URL go (String url) {
try {
myCurrentURL = completeURL(url);
if (hasNext()) {
myHistory = myHistory.subList(0, myCurrentIndex + 1);
}
myHistory.add(myCurrentURL);
myCurrentIndex++;
return myCurrentURL;
}
catch(BrowserException e) {
throw new BrowserException(myResource.getString("InvalidURL"));
}
}


/**
* Returns true if there is a next URL available
*/
public boolean hasNext () {
return myCurrentIndex < (myHistory.size() - 1);
}

/**
* Returns true if there is a previous URL available
*/
public boolean hasPrevious () {
return myCurrentIndex > 0;
}

/**
* Returns URL of the current home page or null if none is set.
*/
public URL getHome () {
return myHome;
}

/**
* Sets current home page to the current URL being viewed.
*/
public void setHome () {
// just in case, might be called before a page is visited
if (myCurrentURL != null) {
myHome = myCurrentURL;
}
}

/**
* Adds current URL being viewed to favorites collection with given 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);
}
}

/**
* 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);
}
throw new BrowserException(myResource.getString("NoFavoriteFound"));
}

// deal with a potentially incomplete URL
private URL completeURL (String possible) {
try {
// try it as is
return new URL(possible);
} catch (MalformedURLException e) {
try {
// try it as a relative link
// BUGBUG: need to generalize this :(
return new URL(myCurrentURL.toString() + "/" + possible);
} catch (MalformedURLException ee) {
try {
// e.g., let user leave off initial protocol
return new URL(PROTOCOL_PREFIX + possible);
} catch (MalformedURLException eee) {
throw new BrowserException("MalformedURL");
}
}
}
}
}
32 changes: 22 additions & 10 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@
*/
public class BrowserView {
// constants
public static final Dimension DEFAULT_SIZE = new Dimension(800, 600);
public static final Dimension DEFAULT_SIZE = new Dimension(1000, 600);
public static final String DEFAULT_RESOURCE_PACKAGE = "resources/";
public static final String STYLESHEET = "default.css";
// public static final String STYLESHEET = "default.css";
public static final String STYLESHEET = "recitation.css";
public static final String BLANK = " ";

// scene, needed to report back to Application
Expand All @@ -61,7 +62,9 @@ public class BrowserView {
private Button myBackButton;
private Button myNextButton;
private Button myHomeButton;

// favorites
private Button myFavoriteButton;
private ComboBox<String> myFavorites;
// get strings from resource file
private ResourceBundle myResources;
Expand All @@ -84,20 +87,20 @@ 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) {
update(valid);
}
else {
showError("Could not load " + url);
}
try{
URL valid = myModel.go(url);
update(valid);
}
catch(BrowserException e) {
showError(String.format("Could not load %s. Check your spelling", url));
}
}

/**
Expand Down Expand Up @@ -210,6 +213,7 @@ public void handle (ActionEvent event) {
result.getChildren().add(myNextButton);
myHomeButton = makeButton("HomeCommand", event -> home());
result.getChildren().add(myHomeButton);

// if user presses button or enter in text field, load/show the URL
EventHandler<ActionEvent> showHandler = new ShowPage();
result.getChildren().add(makeButton("GoCommand", showHandler));
Expand All @@ -225,6 +229,14 @@ private Node makePreferencesPanel () {
myModel.setHome();
enableButtons();
}));

//Add Recitation assignment:
myFavoriteButton = makeButton("AddFavoriteCommand", event -> addFavorite());
result.getChildren().add(myFavoriteButton);
myFavorites = new ComboBox<String>();
myFavorites.setPromptText(myResources.getString("FavoriteFirstItem"));
myFavorites.setOnAction(e -> showFavorite(myFavorites.getSelectionModel().getSelectedItem()));
result.getChildren().add(myFavorites);
return result;
}

Expand Down
5 changes: 5 additions & 0 deletions src/resources/ErrorsEnglish.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
NoNextHistory=No next history!
NoBackHistory=No back history!
NoFavoriteFound=No favorite found!
MalformedURL=Malformed URL!
InvalidURL=Invalid URL!
Loading