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

NetIDs of members: ejl29, au15 #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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

Names: Abhishek Upadhyaya Ghimire and Elsie Ling
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 msg){
super(msg);
}
}
234 changes: 120 additions & 114 deletions src/BrowserModel.java
Original file line number Diff line number Diff line change
@@ -4,142 +4,148 @@
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 () {
public URL next () throws BrowserException {
if (hasNext()) {
myCurrentIndex++;
return myHistory.get(myCurrentIndex);
}
return null;
throw new BrowserException(myErrorResources.getString("HistoryException"));
}

/**
* 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 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(myErrorResources.getString("BackException"));
}

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

/**
* Returns URL of the current home page or null if none is set.
*/
public URL getHome () {
return myHome;
}
/**
* Returns true if there is a next URL available
*/
public boolean hasNext() {
return myCurrentIndex < (myHistory.size() - 1);
}

/**
* 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 true if there is a previous URL available
*/
public boolean hasPrevious() {
return myCurrentIndex > 0;
}

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

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

// 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;
}
}
}
}
/**
* 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.
* @throws BrowserException
*/
public URL getFavorite(String name) throws BrowserException {
if (name != null && !name.equals("") && myFavorites.containsKey(name)) {
return myFavorites.get(name);
}
throw new BrowserException(myErrorResources.getString("FavoriteException"));
}

// 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(myErrorResources.getString("HistoryException"));
}
}
}
}
}
58 changes: 50 additions & 8 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import java.awt.Dimension;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
@@ -91,7 +93,13 @@ public BrowserView (BrowserModel model, String language) {
* Display given URL.
*/
public void showPage (String url) {
URL valid = myModel.go(url);
URL valid = null;
try {
valid = myModel.go(url);
} catch (BrowserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (url != null) {
update(valid);
}
@@ -126,12 +134,22 @@ public void showError (String message) {

// move to the next URL in the history
private void next () {
update(myModel.next());
try {
update(myModel.next());
} catch (BrowserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// move to the previous URL in the history
private void back () {
update(myModel.back());
try {
update(myModel.back());
} catch (BrowserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// change current URL to the home page, if set
@@ -141,7 +159,13 @@ private void home () {

// change page to favorite choice
private void showFavorite (String favorite) {
showPage(myModel.getFavorite(favorite).toString());
try {
showPage(myModel.getFavorite(favorite).toString());
} catch (BrowserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

// update just the view to display given URL
@@ -182,7 +206,10 @@ private Node makePageDisplay () {
// organize user's options for controlling/giving input to model
private Node makeInputPanel () {
VBox result = new VBox();
result.getChildren().addAll(makeNavigationPanel(), makePreferencesPanel());
result.getChildren().addAll(makeNavigationPanel());
for (Node n: makePreferencesPanel()) {
result.getChildren().addAll(n);
}
return result;
}

@@ -219,14 +246,29 @@ public void handle (ActionEvent event) {
}

// make buttons for setting favorites/home URLs
private Node makePreferencesPanel () {
HBox result = new HBox();
result.getChildren().add(makeButton("SetHomeCommand", event -> {
private List<Node> makePreferencesPanel () {
HBox home = new HBox();
HBox favorite = new HBox();
List<Node> result = new ArrayList<Node>();
EventHandler<ActionEvent> showHandler = new ShowPage();

home.getChildren().add(makeButton("SetHomeCommand", event -> {
myModel.setHome();
enableButtons();
}));
favorite.getChildren().add(makeButton("AddFavoriteCommand", event -> {
addFavorite();
enableButtons();

}));

result.add(home);
result.add(favorite);
//result.add(myFavorites);
return result;
}



// makes a button using either an image or a label
private Button makeButton (String property, EventHandler<ActionEvent> handler) {
Loading