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

Pair programming with Alex Race #32

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

/**
*
* @author Sally Al
*
*/
public class BrowserException extends RuntimeException {

/**
*
*/
private static final long serialVersionUID = 1L;

/**
*
*/
public BrowserException() {
// TODO Auto-generated constructor stub
}

/**
* @param arg0
*/
public BrowserException(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}

/**
* @param arg0
*/
public BrowserException(Throwable arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}

/**
* @param arg0
* @param arg1
*/
public BrowserException(String arg0, Throwable arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}

/**
* @param arg0
* @param arg1
* @param arg2
* @param arg3
*/
public BrowserException(String arg0, Throwable arg1, boolean arg2, boolean arg3) {
super(arg0, arg1, arg2, arg3);
// TODO Auto-generated constructor stub
}

}
40 changes: 30 additions & 10 deletions src/BrowserModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;


/**
* This represents the heart of the browser: the collections
* that organize all the URLs into useful structures.
*
*
* @author Robert C. Duvall
*/
public class BrowserModel {
Expand All @@ -21,39 +22,52 @@ public class BrowserModel {
private int myCurrentIndex;
private List<URL> myHistory;
private Map<String, URL> myFavorites;
private ResourceBundle myResources;
private static final String DEFAULT_RESOURCE_PACKAGE = "resources/";




/**
* Creates an empty model.
*/
public BrowserModel () {
public BrowserModel (String language) {
myHome = null;
myCurrentURL = null;
myCurrentIndex = -1;
myHistory = new ArrayList<>();
myFavorites = new HashMap<>();
myResources = ResourceBundle.getBundle(DEFAULT_RESOURCE_PACKAGE + language );
}

/**
* Returns the first page in next history, null if next history is empty.
*/
public URL next () {
if (hasNext()) {

try {
myCurrentIndex++;
return myHistory.get(myCurrentIndex);
return myHistory.get(myCurrentIndex);
} catch (BrowserException e){
String label = myResources.getString("EmptyHistory");
throw new BrowserException(label);
}
return null;
//return myHistory.get(myCurrentIndex);

// return null;
}

/**
* Returns the first page in back history, null if back history is empty.
*/
public URL back () {
if (hasPrevious()) {
try{
myCurrentIndex--;
return myHistory.get(myCurrentIndex);
}catch (BrowserException e){
String label = myResources.getString("NoBack");
throw new BrowserException(label);
}
return null;
}

/**
Expand Down Expand Up @@ -107,19 +121,24 @@ public void setHome () {
*/
public void addFavorite (String name) {
// just in case, might be called before a page is visited
if (name != null && !name.equals("") && myCurrentURL != null) {
try{
myFavorites.put(name, myCurrentURL);
}catch(BrowserException e){
String label = myResources.getString("NoVisit");
throw new BrowserException(label);
}
}

/**
* Returns URL from favorites associated with given name, null if none set.
*/
public URL getFavorite (String name) {
if (name != null && !name.equals("") && myFavorites.containsKey(name)) {
try {
return myFavorites.get(name);
}catch(BrowserException e){
String label = myResources.getString("NoFave");
throw new BrowserException(label);
}
return null;
}

// deal with a potentially incomplete URL
Expand All @@ -143,3 +162,4 @@ private URL completeURL (String possible) {
}
}
}

67 changes: 47 additions & 20 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;

import javax.imageio.ImageIO;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
Expand All @@ -23,20 +32,14 @@
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;
import org.w3c.dom.NodeList;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;


/**
* A class used to display the viewer for a simple HTML browser.
*
*
* See this tutorial for help on how to use the variety of components:
* http://download.oracle.com/otndocs/products/javafx/2/samples/Ensemble/
*
*
* @author Owen Astrachan
* @author Marcin Dobosz
* @author Yuzhang Han
Expand All @@ -61,6 +64,7 @@ public class BrowserView {
private Button myBackButton;
private Button myNextButton;
private Button myHomeButton;
private Button myFavoirateButton;
// favorites
private ComboBox<String> myFavorites;
// get strings from resource file
Expand All @@ -84,7 +88,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 @@ -169,6 +173,7 @@ private void enableButtons () {
myBackButton.setDisable(! myModel.hasPrevious());
myNextButton.setDisable(! myModel.hasNext());
myHomeButton.setDisable(myModel.getHome() == null);

}

// convenience method to create HTML page display
Expand Down Expand Up @@ -199,22 +204,44 @@ private Node makeNavigationPanel () {
// 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();
}
@Override
public void handle (ActionEvent event) {
back();
}
});
result.getChildren().add(myBackButton);
// new style way to do set up callback (lambdas)
myNextButton = makeButton("NextCommand", event -> next());
result.getChildren().add(myNextButton);
myHomeButton = makeButton("HomeCommand", event -> home());
result.getChildren().add(myHomeButton);


/* for(String string: myFavorites.getItems() ){
showFavorite(string);
}*/


// if user presses button or enter in text field, load/show the URL
EventHandler<ActionEvent> showHandler = new ShowPage();
result.getChildren().add(makeButton("GoCommand", showHandler));
myURLDisplay = makeInputField(40, showHandler);
result.getChildren().add(myURLDisplay);


myHomeButton = makeButton("HomeCommand", event -> home());
result.getChildren().add(myHomeButton);
myFavoirateButton = makeButton("AddFavoriteCommand", event -> addFavorite());
result.getChildren().add(myFavoirateButton);
myFavorites= new ComboBox<String>();
myFavorites.valueProperty().addListener(new ChangeListener<String>() {
@Override public void changed(ObservableValue<? extends String> observable,
String oldValue,
String newValue){
showFavorite(newValue);
}


});
result.getChildren().add(myFavorites);
return result;
}

Expand All @@ -231,7 +258,7 @@ private Node makePreferencesPanel () {
// makes a button using either an image or a label
private Button makeButton (String property, EventHandler<ActionEvent> handler) {
// represent all supported image suffixes
final String IMAGEFILE_SUFFIXES =
final String IMAGEFILE_SUFFIXES =
String.format(".*\\.(%s)", String.join("|", ImageIO.getReaderFileSuffixes()));

Button result = new Button();
Expand All @@ -257,10 +284,10 @@ private TextField makeInputField (int width, EventHandler<ActionEvent> handler)
// display page
// very old style way create a callback (inner class)
private class ShowPage implements EventHandler<ActionEvent> {
@Override
public void handle (ActionEvent event) {
showPage(myURLDisplay.getText());
}
@Override
public void handle (ActionEvent event) {
showPage(myURLDisplay.getText());
}
}


Expand Down
4 changes: 2 additions & 2 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* A simple HTML browser.
*
*
* @author Robert C. Duvall
*/
public class Main extends Application {
Expand All @@ -16,7 +16,7 @@ public class Main extends Application {
@Override
public void start (Stage stage) {
// create program specific components
BrowserModel model = new BrowserModel();
BrowserModel model = new BrowserModel("errors");
BrowserView display = new BrowserView(model, "English");
// give the window a title
stage.setTitle(TITLE);
Expand Down
12 changes: 6 additions & 6 deletions src/resources/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
-fx-font-size: 14pt;
-fx-font-family: "Courier New";
-fx-base: rgb(132, 145, 47);
-fx-background: rgb(225, 228, 203);
-fx-background: rgb(100, 208, 150);
}

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

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

.combo-box-base {
-fx-text-base-color: #006464;
-fx-text-base-color: #001111;
-fx-background-color: #DFB951;
-fx-border-radius: 20;
-fx-border-radius: 65;
-fx-background-radius: 20;
}
.combo-box-base:hover {
Expand Down
4 changes: 4 additions & 0 deletions src/resources/errors.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
EmptyHistory=History is empty
NoBack=No back history
NoFave=Favorite does not exist
NoVisit=Page not yet visited