Skip to content

Recitation Practice #31

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

Student: Joy Patel (jhp21), Efe Aras (ea89)
12 changes: 12 additions & 0 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

public class BrowserException extends RuntimeException{

/**
*
*/
private static final long serialVersionUID = 1L;

public BrowserException(String error_message_tag){
super(error_message_tag);
}
}
261 changes: 144 additions & 117 deletions src/BrowserModel.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;


/**
Expand All @@ -13,133 +15,158 @@
* @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;
// 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 myResources;


/**
* 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;
}
/**
* Creates an empty model.
*/
public BrowserModel () {
myHome = null;
myCurrentURL = null;
myCurrentIndex = -1;
myHistory = new ArrayList<>();
myFavorites = new HashMap<>();
myResources = ResourceBundle.getBundle(BrowserView.DEFAULT_RESOURCE_PACKAGE + "ErrorMessages");
}

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

/**
* 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 the first page in back history, null if back history is empty.
*/
public URL back () {
try{
myCurrentIndex--;
return myHistory.get(myCurrentIndex);
}
catch(Exception e){
throw new BrowserException(myResources.getString("Back"));
}
}

/**
* Returns true if there is a next URL available
*/
public boolean hasNext () {
return myCurrentIndex < (myHistory.size() - 1);
}
/**
* Changes current page to given URL, removing next history.
* @throws BrowserException
* @throws
*/
public URL go (String url) throws BrowserException {
myCurrentURL = completeURL(url);
try {
myCurrentURL.openStream();
} catch (IOException e) {
// TODO Auto-generated catch block
throw new BrowserException(String.format(myResources.getString("MalformedURL"),""));
}
if (myCurrentURL != null) {
if (hasNext()) {
myHistory = myHistory.subList(0, myCurrentIndex + 1);
}
myHistory.add(myCurrentURL);
myCurrentIndex++;
}
return myCurrentURL;
}

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

/**
* Returns URL of the current home page or null if none is set.
*/
public URL getHome () {
return myHome;
}
/**
* Returns true if there is a previous URL available
*/
public boolean hasPrevious () {
return myCurrentIndex > 0;
}

/**
* 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;
}
}
/**
* Returns URL of the current home page or null if none is set.
*/
public URL getHome () {
return myHome;
}

/**
* 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);
}
}
/**
* Sets current home page to the current URL being viewed.
* @throws BrowserException
*/
public void setHome () throws BrowserException {
// just in case, might be called before a page is visited
try{
myHome = myCurrentURL;
}
catch(Exception e){
throw new BrowserException(String.format(myResources.getString("Null"),e.getMessage()));
}
}

/**
* 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;
}
/**
* Adds current URL being viewed to favorites collection with given name.
* @throws BrowserException
*/
public void addFavorite (String name) throws BrowserException {
// just in case, might be called before a page is visited
//if (name != null && !name.equals("") && myCurrentURL != null) {
try{
myFavorites.put(name, myCurrentURL);
}
catch(Exception e){
throw new BrowserException(String.format(myResources.getString("Null"), name));
}
}

// 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;
}
}
}
}
/**
* Returns URL from favorites associated with given name, null if none set.
* @throws BrowserException
*/
public URL getFavorite (String name) throws BrowserException {
try{
return myFavorites.get(name);
}
catch(Exception e){
throw new BrowserException(String.format(myResources.getString("NotInFav"), name));
}
}

// deal with a potentially incomplete URL
private URL completeURL (String possible) throws BrowserException {
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(String.format(myResources.getString("MalformedURL"),PROTOCOL_PREFIX + possible));
}
}
}
}
}
Loading