Skip to content

Added gui elements, css edits, and exception handling (Ying Wang, Joh… #14

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 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>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# lab_browser
A simple GUI example: a web browser
John Dai (rd122), Ying Wang (yw103)
10 changes: 10 additions & 0 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class BrowserException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public String myMessage;
public BrowserException(String message){
myMessage=message;
}
}
56 changes: 39 additions & 17 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 @@ -21,7 +22,8 @@ public class BrowserModel {
private int myCurrentIndex;
private List<URL> myHistory;
private Map<String, URL> myFavorites;

private ResourceBundle myErrorResources;
public static final String DEFAULT_RESOURCE_PACKAGE = "resources/";

/**
* Creates an empty model.
Expand All @@ -32,43 +34,54 @@ public BrowserModel () {
myCurrentIndex = -1;
myHistory = new ArrayList<>();
myFavorites = new HashMap<>();
myErrorResources = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + "Error");
}

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

}

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

throw new BrowserException(myErrorResources.getString("noPreviousURL"));
}

/**
* 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++;
try{
myCurrentURL = completeURL(url);
if (hasNext()) {
myHistory = myHistory.subList(0, myCurrentIndex + 1);
}
myHistory.add(myCurrentURL);
myCurrentIndex++;
return myCurrentURL;
}
catch (BrowserException e){
System.out.println("SDfds");

System.out.printf("Could not resolve URL %s", url);
return null; // need to change later
}
return myCurrentURL;
}

/**
Expand Down Expand Up @@ -114,30 +127,39 @@ public void addFavorite (String name) {

/**
* Returns URL from favorites associated with given name, null if none set.
* @throws BrowserException
*/
public URL getFavorite (String name) {
public URL getFavorite (String name) throws BrowserException {
if (name != null && !name.equals("") && myFavorites.containsKey(name)) {
return myFavorites.get(name);
}
return null;
throw new BrowserException(myErrorResources.getString("noFavoriteFound"));
}


// deal with a potentially incomplete URL
private URL completeURL (String possible) {
private URL completeURL (String possible) throws BrowserException {
try {
System.out.println("00");

// try it as is
return new URL(possible);
} catch (MalformedURLException e) {
System.out.println("aa");

try {
// try it as a relative link
// BUGBUG: need to generalize this :(
return new URL(myCurrentURL.toString() + "/" + possible);
} catch (MalformedURLException ee) {
System.out.println("bb");

try {
// e.g., let user leave off initial protocol
return new URL(PROTOCOL_PREFIX + possible);
} catch (MalformedURLException eee) {
return null;
System.out.println("cc");
throw new BrowserException(myErrorResources.getString("incomplete"));
}
}
}
Expand Down
28 changes: 25 additions & 3 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class BrowserView {
private ComboBox<String> myFavorites;
// get strings from resource file
private ResourceBundle myResources;
private ResourceBundle myErrorResources;
// the data
private BrowserModel myModel;

Expand All @@ -75,6 +76,7 @@ public BrowserView (BrowserModel model, String language) {
myModel = model;
// use resources for labels
myResources = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + language);
myErrorResources = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + "Error");
BorderPane root = new BorderPane();
// must be first since other panels may refer to page
root.setCenter(makePageDisplay());
Expand All @@ -84,7 +86,7 @@ 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);
}

/**
Expand Down Expand Up @@ -126,12 +128,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){

}
}

// move to the previous URL in the history
private void back () {
update(myModel.back());
try{
update(myModel.back());
}
catch(BrowserException e){

}
}

// change current URL to the home page, if set
Expand All @@ -141,7 +153,12 @@ private void home () {

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

}
}

// update just the view to display given URL
Expand Down Expand Up @@ -221,6 +238,11 @@ public void handle (ActionEvent event) {
// make buttons for setting favorites/home URLs
private Node makePreferencesPanel () {
HBox result = new HBox();
myFavorites = new ComboBox<String>();
myFavorites.setPromptText(myResources.getString("FavoriteFirstItem"));
myFavorites.valueProperty().addListener((o,s1,s2) -> showFavorite(s2));
result.getChildren().add(makeButton("AddFavoriteCommand",event->addFavorite()));
result.getChildren().add(myFavorites);
result.getChildren().add(makeButton("SetHomeCommand", event -> {
myModel.setHome();
enableButtons();
Expand Down
4 changes: 4 additions & 0 deletions src/resources/Error.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
noNextURL=no next URL
noPreviousURL=no previous URL
noFavoriteFound=No favorite found
incomplete=Incomplete URL cannot be resolved
4 changes: 2 additions & 2 deletions src/resources/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-fx-font-size: 14pt;
-fx-font-family: "Courier New";
-fx-base: rgb(132, 145, 47);
-fx-background: rgb(225, 228, 203);
-fx-background: rgb(2, 228, 255);
}

.button {
Expand All @@ -28,7 +28,7 @@
}

.label {
-fx-font-size: 11pt;
-fx-font-size: 22pt;
-fx-font-family: "Segoe UI Semibold";
-fx-text-fill: #006464;
-fx-opacity: 0.6;
Expand Down