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

Recitation - Lab Browser (as466 & em186) #8

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
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
Arthur Schweitzer(as466)
Emanuele Macchi (em186)
8 changes: 8 additions & 0 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

public class BrowserException extends Exception{

public BrowserException(String text)
{
super(text);
}
}
249 changes: 129 additions & 120 deletions src/BrowserModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,142 +4,151 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.ResourceBundle;

/**
* This represents the heart of the browser: the collections
* that organize all the URLs into useful structures.
* This represents the heart of the browser: the collections that organize all
* the URLs into useful structures.
*
* @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 myErrorResources;

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

/**
* 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.
*
* @throws BrowserException
*/
public URL next() throws BrowserException {
if (hasNext()) {
myCurrentIndex++;
return myHistory.get(myCurrentIndex);
}
throw new BrowserException("No next page to go to!");
}

/**
* 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.
*
* @throws BrowserException
*/
public URL back() throws BrowserException {
if (hasPrevious()) {
myCurrentIndex--;
return myHistory.get(myCurrentIndex);
}
throw new BrowserException("No page to go back to!");
}

/**
* 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.
*
* @throws BrowserException
*/
public URL go(String url) throws BrowserException {
myCurrentURL = completeURL(url);
if (myCurrentURL != null) {
if (hasNext()) {
myHistory = myHistory.subList(0, myCurrentIndex + 1);
myHistory.add(myCurrentURL);
myCurrentIndex++;
}
}
return myCurrentURL;

/**
* 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 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 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;
}
/**
* 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;
}
}
/**
* 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);
}
}
/**
* 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;
}
/**
* Returns URL from favorites associated with given name, null if none set.
*
* @throws BrowserException
*/
public URL getFavorite(String name) throws BrowserException {
if (name != null && !name.equals("") && myFavorites.containsKey(name)) {
return myFavorites.get(name);
}
throw new BrowserException("No favorites found");
}

// 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;
}
}
}
}
// 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("Not a valid URL!");
}
}
}
}
}
Loading