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

Refactoring lab (as577, srt15) #38

Open
wants to merge 2 commits 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>
23 changes: 23 additions & 0 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

public class BrowserException extends RuntimeException {

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);
}

}
17 changes: 12 additions & 5 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 @@ -15,23 +16,27 @@
public class BrowserModel {
// constants
public static final String PROTOCOL_PREFIX = "http://";
public static final String DEFAULT_RESOURCE_PACKAGE = "resources/";
// 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 () {
public BrowserModel (String language) {
myHome = null;
myCurrentURL = null;
myCurrentIndex = -1;
myHistory = new ArrayList<>();
myFavorites = new HashMap<>();

myErrorResources = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + language + "Errors");
}

/**
Expand Down Expand Up @@ -76,7 +81,9 @@ public URL go (String url) {
return myCurrentURL;
}
catch (Exception e) {
return null;
// HORRIBLE
// e.printStackTrace();
throw new BrowserException(String.format(myErrorResources.getString("ErrorLoad"), url));
}
}

Expand Down Expand Up @@ -128,7 +135,7 @@ public URL getFavorite (String name) {
if (name != null && !name.equals("") && myFavorites.containsKey(name)) {
return myFavorites.get(name);
}
return null;
throw new BrowserException();
}

// deal with a potentially incomplete URL
Expand All @@ -146,7 +153,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();
}
}
}
Expand Down
73 changes: 56 additions & 17 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 @@ -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,20 +86,20 @@ 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 @@ -201,18 +203,15 @@ private Node makeNavigationPanel () {
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();
}
});
myBackButton = makeButton("BackCommand", "back");
result.getChildren().add(myBackButton);
// new style way to do set up callback (lambdas)
myNextButton = makeButton("NextCommand", event -> next());
myNextButton = makeButton("NextCommand", "next");
result.getChildren().add(myNextButton);
myHomeButton = makeButton("HomeCommand", event -> home());
myHomeButton = makeButton("HomeCommand", "home");
result.getChildren().add(myHomeButton);
myFavButton = makeButton("AddFavoriteCommand", "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 +225,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 +235,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 method) {
// represent all supported image suffixes
final String IMAGEFILE_SUFFIXES =
String.format(".*\\.(%s)", String.join("|", ImageIO.getReaderFileSuffixes()));
Expand All @@ -247,7 +248,45 @@ private Button makeButton (String property, EventHandler<ActionEvent> handler) {
} else {
result.setText(label);
}
result.setOnAction(handler);

try {
Object obj = this.getClass().newInstance();
Class<?> noparams[] = {};

Method methodToCall = this.getClass().getDeclaredMethod(method, noparams);

result.setOnAction(e -> {
try {
methodToCall.invoke(obj, noparams);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
});

return result;
} catch(Exception e) {
throw new BrowserException();
}
}

// makes a button using either an image or a label
private Button makeButton (String property, EventHandler<ActionEvent> method) {
// represent all supported image suffixes
final String IMAGEFILE_SUFFIXES =
String.format(".*\\.(%s)", String.join("|", ImageIO.getReaderFileSuffixes()));

Button result = new Button();
String label = myResources.getString(property);
if (label.matches(IMAGEFILE_SUFFIXES)) {
result.setGraphic(new ImageView(
new Image(getClass().getResourceAsStream(DEFAULT_RESOURCE_PACKAGE + label))));
} else {
result.setText(label);
}

result.setOnAction(method);

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 right now.
FavoritePromptTitle=Add Favorite
FavoriteFirstItem=All Favorites
SetHomeCommand=Set Home
1 change: 1 addition & 0 deletions src/resources/EnglishErrors.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ErrorLoad=Could not load %s right now.
21 changes: 12 additions & 9 deletions src/resources/default.css
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
.root {
-fx-font-size: 14pt;
-fx-font-family: "Courier New";
-fx-base: rgb(132, 145, 47);
-fx-background: rgb(225, 228, 203);
-fx-font-family: "Wingdings";
-fx-base: blue;
-fx-background-image: url("/resources/Favorite32.gif");
-fx-background-repeat: repeat;
-fx-background-size: 50 50;
-fx-background-position: center center;
}

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

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

.combo-box-base {
-fx-text-base-color: #006464;
-fx-background-color: #DFB951;
-fx-text-base-color: #000000;
-fx-background-color: #FFFFFF;
-fx-border-radius: 20;
-fx-background-radius: 20;
}
Expand All @@ -37,4 +40,4 @@
.text-field {
-fx-font-size: 14pt;
-fx-font-family: "Segoe UI Semibold";
}
}