Skip to content

sck13 mjb81 discussion #30

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="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
33 changes: 20 additions & 13 deletions src/BrowserModel.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
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 resources.BrowserException;


/**
* This represents the heart of the browser: the collections
Expand Down Expand Up @@ -36,30 +40,32 @@ public BrowserModel () {

/**
* Returns the first page in next history, null if next history is empty.
* @throws BrowserException
*/
public URL next () {
if (hasNext()) {
myCurrentIndex++;
return myHistory.get(myCurrentIndex);
}
return null;
public URL next () throws BrowserException {
if (hasNext()) {
myCurrentIndex++;
return myHistory.get(myCurrentIndex);
}
throw new BrowserException("NoNextHistory");
}

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

/**
* Changes current page to given URL, removing next history.
* @throws BrowserException
*/
public URL go (String url) {
public URL go (String url) throws BrowserException {
myCurrentURL = completeURL(url);
if (myCurrentURL != null) {
if (hasNext()) {
Expand Down Expand Up @@ -114,16 +120,17 @@ 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("No Favorites");
}

// deal with a potentially incomplete URL
private URL completeURL (String possible) {
private URL completeURL (String possible) throws BrowserException {
try {
// try it as is
return new URL(possible);
Expand All @@ -137,7 +144,7 @@ private URL completeURL (String possible) {
// e.g., let user leave off initial protocol
return new URL(PROTOCOL_PREFIX + possible);
} catch (MalformedURLException eee) {
return null;
throw new BrowserException("Incomplete");
}
}
}
Expand Down
55 changes: 41 additions & 14 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import java.awt.Dimension;

import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
Expand All @@ -23,6 +24,8 @@
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import resources.BrowserException;

import javax.imageio.ImageIO;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand Down Expand Up @@ -61,6 +64,7 @@ public class BrowserView {
private Button myBackButton;
private Button myNextButton;
private Button myHomeButton;
private Button myAddFavorites;
// favorites
private ComboBox<String> myFavorites;
// get strings from resource file
Expand All @@ -70,8 +74,9 @@ public class BrowserView {

/**
* Create a view of the given model of a web browser.
* @throws BrowserException
*/
public BrowserView (BrowserModel model, String language) {
public BrowserView (BrowserModel model, String language) throws BrowserException {
myModel = model;
// use resources for labels
myResources = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + language);
Expand All @@ -84,13 +89,14 @@ 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);
}

/**
* Display given URL.
* @throws BrowserException
*/
public void showPage (String url) {
public void showPage (String url) throws BrowserException {
URL valid = myModel.go(url);
if (url != null) {
update(valid);
Expand Down Expand Up @@ -125,22 +131,22 @@ public void showError (String message) {
}

// move to the next URL in the history
private void next () {
private void next () throws BrowserException {
update(myModel.next());
}

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

// change current URL to the home page, if set
private void home () {
private void home () throws BrowserException {
showPage(myModel.getHome().toString());
}

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

Expand Down Expand Up @@ -169,6 +175,7 @@ private void enableButtons () {
myBackButton.setDisable(! myModel.hasPrevious());
myNextButton.setDisable(! myModel.hasNext());
myHomeButton.setDisable(myModel.getHome() == null);
myAddFavorites.setDisable(false);
}

// convenience method to create HTML page display
Expand All @@ -180,7 +187,7 @@ private Node makePageDisplay () {
}

// organize user's options for controlling/giving input to model
private Node makeInputPanel () {
private Node makeInputPanel () throws BrowserException {
VBox result = new VBox();
result.getChildren().addAll(makeNavigationPanel(), makePreferencesPanel());
return result;
Expand All @@ -194,22 +201,32 @@ private Node makeInformationPanel () {
}

// make user-entered URL/text field and back/next buttons
private Node makeNavigationPanel () {
private Node makeNavigationPanel () throws BrowserException {
HBox result = new HBox();
// create buttons, with their associated actions
// old style way to do set up callback (anonymous class)
myBackButton = makeButton("BackCommand", new EventHandler<ActionEvent>() {
@Override
public void handle (ActionEvent event) {
back();
try {
back();
} catch (BrowserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
result.getChildren().add(myBackButton);
// new style way to do set up callback (lambdas)
myNextButton = makeButton("NextCommand", event -> next());
myNextButton = makeButton("NextCommand", event -> {try{next();}catch(BrowserException ex){}});
result.getChildren().add(myNextButton);
myHomeButton = makeButton("HomeCommand", event -> home());
myHomeButton = makeButton("HomeCommand", event -> {try{home();}catch(BrowserException ex){}});
result.getChildren().add(myHomeButton);
myAddFavorites = makeButton("AddFavoriteCommand", event -> addFavorite());
result.getChildren().add(myAddFavorites);
myFavorites = new ComboBox<String>();
myFavorites.setPromptText("Favorites");
result.getChildren().add(myFavorites);
// if user presses button or enter in text field, load/show the URL
EventHandler<ActionEvent> showHandler = new ShowPage();
result.getChildren().add(makeButton("GoCommand", showHandler));
Expand Down Expand Up @@ -259,7 +276,12 @@ private TextField makeInputField (int width, EventHandler<ActionEvent> handler)
private class ShowPage implements EventHandler<ActionEvent> {
@Override
public void handle (ActionEvent event) {
showPage(myURLDisplay.getText());
try {
showPage(myURLDisplay.getText());
} catch (BrowserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Expand All @@ -279,7 +301,12 @@ public void changed (ObservableValue<? extends State> ov, State oldState, State
if (href != null) {
String domEventType = event.getType();
if (domEventType.equals(EVENT_CLICK)) {
showPage(href);
try {
showPage(href);
} catch (BrowserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (domEventType.equals(EVENT_MOUSEOVER)) {
showStatus(href);
} else if (domEventType.equals(EVENT_MOUSEOUT)) {
Expand Down
3 changes: 2 additions & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import javafx.application.Application;
import javafx.stage.Stage;
import resources.BrowserException;


/**
Expand All @@ -14,7 +15,7 @@ public class Main extends Application {


@Override
public void start (Stage stage) {
public void start (Stage stage) throws BrowserException {
// create program specific components
BrowserModel model = new BrowserModel();
BrowserView display = new BrowserView(model, "English");
Expand Down
12 changes: 12 additions & 0 deletions src/resources/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package resources;

public class BrowserException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;

public BrowserException(String message) {
super(message);
}
}
1 change: 1 addition & 0 deletions src/resources/Errors.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NoURL=No URL
12 changes: 6 additions & 6 deletions src/resources/default.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.root {
-fx-font-size: 14pt;
-fx-font-family: "Courier New";
-fx-font-family: "Helvetica";
-fx-base: rgb(132, 145, 47);
-fx-background: rgb(225, 228, 203);
-fx-background: #0000FF;
}

.button {
Expand All @@ -14,17 +14,17 @@
}

.button:hover {
-fx-background-color: #3a3a3a;
-fx-background-color: #FA1205;
}

.combo-box-base {
-fx-text-base-color: #006464;
-fx-text-base-color: #000;
-fx-background-color: #DFB951;
-fx-border-radius: 20;
-fx-background-radius: 20;
-fx-background-radius: 0;
}
.combo-box-base:hover {
-fx-background-color: #3a3a3a;
-fx-background-color: #FA1205;
}

.label {
Expand Down