Skip to content

Lab exercise written by Mike Ma & Ying Qi #33

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 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
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

Author: Mike Ma, Ying Qi
9 changes: 9 additions & 0 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


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

public BrowserException(String message) {
super(message);
}
}
6 changes: 3 additions & 3 deletions src/BrowserModel.java
Original file line number Diff line number Diff line change
@@ -59,7 +59,7 @@ public URL back () {
/**
* Changes current page to given URL, removing next history.
*/
public URL go (String url) {
public URL go (String url) throws BrowserException{
myCurrentURL = completeURL(url);
if (myCurrentURL != null) {
if (hasNext()) {
@@ -123,7 +123,7 @@ public URL getFavorite (String name) {
}

// 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);
@@ -137,7 +137,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("message");
}
}
}
29 changes: 21 additions & 8 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;

import javax.imageio.ImageIO;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -91,13 +92,16 @@ public BrowserView (BrowserModel model, String language) {
* Display given URL.
*/
public void showPage (String url) {
URL valid = myModel.go(url);
if (url != null) {
update(valid);
}
else {
showError("Could not load " + url);
}
URL valid;
try {
update(myModel.go(url));
} catch (BrowserException e) {
try {
throw new BrowserException("Could not load %s check your spelling");
} catch (BrowserException e1) {
e1.printStackTrace();
}
}
}

/**
@@ -219,12 +223,21 @@ public void handle (ActionEvent event) {
}

// make buttons for setting favorites/home URLs
private Node makePreferencesPanel () {
private HBox makePreferencesPanel () {
HBox result = new HBox();
result.getChildren().add(makeButton("SetHomeCommand", event -> {
myModel.setHome();
enableButtons();
}));
result.getChildren().add(makeButton("AddFavoriteCommand", event -> {
addFavorite();
enableButtons();
}));
myFavorites = new ComboBox<>();
myFavorites.setPromptText(myResources.getString("FavoriteFirstItem"));
myFavorites.valueProperty().addListener((observable, oldValue, newValue) -> {
showFavorite(newValue);});
result.getChildren().add(myFavorites);
return result;
}