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

lab feb4th #30

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

Mario Oliver, Collin Duffy
mao26, cpd20
28 changes: 28 additions & 0 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

public class BrowserException extends RuntimeException {

public BrowserException() {
// TODO Auto-generated constructor stub
}

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

public BrowserException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}

public BrowserException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}

public BrowserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}

}
23 changes: 17 additions & 6 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,14 @@
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,8 +35,9 @@ public BrowserModel () {
myCurrentIndex = -1;
myHistory = new ArrayList<>();
myFavorites = new HashMap<>();
myResources = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + "Exception");
}

/**
* Returns the first page in next history, null if next history is empty.
*/
Expand All @@ -42,7 +46,7 @@ public URL next () {
myCurrentIndex++;
return myHistory.get(myCurrentIndex);
}
return null;
throw new BrowserException();
}

/**
Expand All @@ -53,13 +57,13 @@ public URL back () {
myCurrentIndex--;
return myHistory.get(myCurrentIndex);
}
return null;
throw new BrowserException();
}

/**
* Changes current page to given URL, removing next history.
*/
public URL go (String url) {
public URL go (String url) throws BrowserException{
try {
URL tmp = completeURL(url);
// unfortunately, completeURL may not have returned a valid URL, so test it
Expand All @@ -76,7 +80,7 @@ public URL go (String url) {
return myCurrentURL;
}
catch (Exception e) {
return null;
throw new BrowserException(String.format(myResources.getString("ErrorOnGo"), url));
}
}

Expand Down Expand Up @@ -121,14 +125,18 @@ public void addFavorite (String name) {
}
}

public ArrayList<String> getFavNames(){
return new ArrayList<String>( myFavorites.keySet());
}

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

// deal with a potentially incomplete URL
Expand All @@ -150,5 +158,8 @@ private URL completeURL (String possible) {
}
}
}
catch (Exception e) {
throw new BrowserException(String.format(myResources.getString("ErrorOnGo")));
}
}
}
Loading