Skip to content
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>
20 changes: 20 additions & 0 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

public class BrowserException extends RuntimeException{
private static final long serialVersionUID = 1997753363232807009L;

public BrowserException(){

}
public BrowserException(String message){
super(message);
}
public BrowserException(Throwable cause){
super(cause);
}
public BrowserException(String message, Throwable cause){
super(message,cause);
}
public BrowserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace){
super(message,cause,enableSuppression,writableStackTrace);
}
}
10 changes: 7 additions & 3 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,13 +22,15 @@ public class BrowserModel {
private int myCurrentIndex;
private List<URL> myHistory;
private Map<String, URL> myFavorites;
private ResourceBundle myResources;


/**
* Creates an empty model.
*/
public BrowserModel () {
public BrowserModel (String language) {
myHome = null;
myResources = ResourceBundle.getBundle(BrowserView.DEFAULT_RESOURCE_PACKAGE + language+"Error");
myCurrentURL = null;
myCurrentIndex = -1;
myHistory = new ArrayList<>();
Expand Down Expand Up @@ -58,8 +61,9 @@ public URL back () {

/**
* Changes current page to given URL, removing next history.
* @throws BrowserException
*/
public URL go (String url) {
public URL go (String url) throws BrowserException {
try {
URL tmp = completeURL(url);
// unfortunately, completeURL may not have returned a valid URL, so test it
Expand All @@ -76,7 +80,7 @@ public URL go (String url) {
return myCurrentURL;
}
catch (Exception e) {
return null;
throw new BrowserException(String.format(myResources.getString("ErrorLoad"),url));
}
}

Expand Down
26 changes: 17 additions & 9 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import java.awt.Dimension;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
Expand Down Expand Up @@ -47,7 +48,7 @@ public class BrowserView {
// constants
public static final Dimension DEFAULT_SIZE = new Dimension(800, 600);
public static final String DEFAULT_RESOURCE_PACKAGE = "resources/";
public static final String STYLESHEET = "default.css";
public static final String STYLESHEET = "custom.css";
public static final String BLANK = " ";

// scene, needed to report back to Application
Expand All @@ -61,6 +62,7 @@ public class BrowserView {
private Button myBackButton;
private Button myNextButton;
private Button myHomeButton;
private Button myFavButton;
// favorites
private ComboBox<String> myFavorites;
// get strings from resource file
Expand All @@ -84,19 +86,19 @@ 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.
*/
public void showPage (String url) {
URL valid = myModel.go(url);
if (valid != null) {
try{
URL valid = myModel.go(url);
update(valid);
}
else {
showError("Could not load " + url);
catch(BrowserException e){
showError(e.getMessage());
}
}

Expand Down Expand Up @@ -143,7 +145,7 @@ private void home () {
private void showFavorite (String favorite) {
showPage(myModel.getFavorite(favorite).toString());
// reset favorites ComboBox so the same choice can be made again
myFavorites.setValue(null);
// myFavorites.setValue(null);
}

// update just the view to display given URL
Expand Down Expand Up @@ -213,6 +215,8 @@ public void handle (ActionEvent event) {
result.getChildren().add(myNextButton);
myHomeButton = makeButton("HomeCommand", event -> home());
result.getChildren().add(myHomeButton);
myFavButton = makeButton("AddFavoriteCommand",event-> addFavorite());
result.getChildren().add(myFavButton);
// 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 All @@ -226,6 +230,8 @@ private Node makePreferencesPanel () {
HBox result = new HBox();
myFavorites = new ComboBox<String>();
// ADD REST OF CODE HERE
result.getChildren().add(myFavorites);
myFavorites.setOnAction(event->showFavorite(myFavorites.getValue()));
result.getChildren().add(makeButton("SetHomeCommand", event -> {
myModel.setHome();
enableButtons();
Expand All @@ -234,7 +240,7 @@ private Node makePreferencesPanel () {
}

// makes a button using either an image or a label
private Button makeButton (String property, EventHandler<ActionEvent> handler) {
private Button makeButton (String property, String handler) {
// represent all supported image suffixes
final String IMAGEFILE_SUFFIXES =
String.format(".*\\.(%s)", String.join("|", ImageIO.getReaderFileSuffixes()));
Expand All @@ -247,7 +253,9 @@ private Button makeButton (String property, EventHandler<ActionEvent> handler) {
} else {
result.setText(label);
}
result.setOnAction(handler);
// result.setOnAction(handler);
Method m = this.getClass().getDeclaredMethod(handler);
result.setOnAction(e->m.invoke());
return result;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ public class Main extends Application {
// convenience constants
public static final String TITLE = "NanoBrowser";
public static final String DEFAULT_START_PAGE = "http://www.cs.duke.edu/rcd";
public static final String LANGUAGE = "English";


@Override
public void start (Stage stage) {
// create program specific components
BrowserModel model = new BrowserModel();
BrowserView display = new BrowserView(model, "English");
BrowserModel model = new BrowserModel(LANGUAGE);
BrowserView display = new BrowserView(model, LANGUAGE);
// give the window a title
stage.setTitle(TITLE);
// add our user interface components to Frame and show it
Expand Down
1 change: 1 addition & 0 deletions src/resources/English.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ GoCommand=Go
AddFavoriteCommand=Add Favorites
FavoritePrompt=Enter name
ErrorTitle=Browser Error
ErrorLoad=Could not load %s today
FavoritePromptTitle=Add Favorite
FavoriteFirstItem=All Favorites
SetHomeCommand=Set Home
2 changes: 2 additions & 0 deletions src/resources/EnglishError.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ErrorTitle=Browser Error
ErrorLoad=Could not load %s today
40 changes: 40 additions & 0 deletions src/resources/custom.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.root {
-fx-font-size: 14pt;
-fx-font-family: "Courier New";
-fx-base: red;
-fx-background: skyblue;
}

.button {
-fx-text-fill: #006464;
-fx-background-color: lightgreen;
-fx-border-radius: 20;
-fx-background-radius: 20;
-fx-padding: 8;
}

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

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

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

.text-field {
-fx-font-size: 14pt;
-fx-font-family: "Segoe UI Semibold";
}