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

web browser lab: ted yavzkurt tdy and tyler webner tlw37 #27

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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# lab_browser
A simple GUI example: a web browser
Browser Lab with Tyler Webner tlw37 and Ted Yavuzkurt ted
14 changes: 14 additions & 0 deletions src/BrowserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

public class BrowserException extends Exception {

/**
*
*/

private static final long serialVersionUID = 8821473464124893494L;

public BrowserException(String message){
super(message);
}

}
21 changes: 16 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 errorResources;



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

/**
Expand Down Expand Up @@ -95,31 +100,37 @@ public URL getHome () {
/**
* Sets current home page to the current URL being viewed.
*/
public void setHome () {
public void setHome() throws BrowserException {
// just in case, might be called before a page is visited
if (myCurrentURL != null) {
myHome = myCurrentURL;
}
else{
throw new BrowserException(errorResources.getString("NullHomeError"));
}
}

/**
* Adds current URL being viewed to favorites collection with given name.
*/
public void addFavorite (String name) {
public void addFavorite (String name) throws BrowserException {
// just in case, might be called before a page is visited
if (name != null && !name.equals("") && myCurrentURL != null) {
myFavorites.put(name, myCurrentURL);
}
else{
throw new BrowserException(String.format("can't add that URL %s", name));
}
}

/**
* Returns URL from favorites associated with given name, null if none set.
*/
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(String.format("Invalid URL (%s) entered.", name));
}

// deal with a potentially incomplete URL
Expand Down
37 changes: 32 additions & 5 deletions src/BrowserView.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.web.WebView;
import javax.imageio.ImageIO;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -65,6 +66,7 @@ public class BrowserView {
private ComboBox<String> myFavorites;
// get strings from resource file
private ResourceBundle myResources;

// the data
private BrowserModel myModel;

Expand All @@ -84,7 +86,8 @@ 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.setFill(Color.BLACK);
myScene.getStylesheets().add(DEFAULT_RESOURCE_PACKAGE + STYLESHEET);
}

/**
Expand Down Expand Up @@ -141,7 +144,12 @@ private void home () {

// change page to favorite choice
private void showFavorite (String favorite) {
showPage(myModel.getFavorite(favorite).toString());
try{
showPage(myModel.getFavorite(favorite).toString());
}
catch(BrowserException e){
showError(e.getMessage());
}
}

// update just the view to display given URL
Expand All @@ -159,7 +167,11 @@ private void addFavorite () {
Optional<String> response = input.showAndWait();
// did user make a choice?
if (response.isPresent()) {
myModel.addFavorite(response.get());
try {
myModel.addFavorite(response.get());
} catch (BrowserException e) {
showError(e.getMessage());
}
myFavorites.getItems().add(response.get());
}
}
Expand Down Expand Up @@ -221,13 +233,24 @@ public void handle (ActionEvent event) {
// make buttons for setting favorites/home URLs
private Node makePreferencesPanel () {
HBox result = new HBox();

myFavorites = new ComboBox<String>();
myFavorites.setPromptText(myResources.getString("FavoriteFirstItem"));
myFavorites.valueProperty().addListener((o, s1, s2)->showFavorite(s2));

result.getChildren().add(makeButton("AddFavoriteCommand", event -> addFavorite()));
result.getChildren().add(myFavorites);
result.getChildren().add(makeButton("SetHomeCommand", event -> {
myModel.setHome();
try {
myModel.setHome();
} catch (Exception e) {
showError(e.getMessage());
}
enableButtons();
}));
return result;
}

// makes a button using either an image or a label
private Button makeButton (String property, EventHandler<ActionEvent> handler) {
// represent all supported image suffixes
Expand All @@ -246,6 +269,10 @@ private Button makeButton (String property, EventHandler<ActionEvent> handler) {
return result;
}

private void showFavorites(){
myFavorites.show();
}

// make text field for input
private TextField makeInputField (int width, EventHandler<ActionEvent> handler) {
TextField result = new TextField();
Expand Down
2 changes: 1 addition & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("English");
BrowserView display = new BrowserView(model, "English");
// give the window a title
stage.setTitle(TITLE);
Expand Down
2 changes: 1 addition & 1 deletion src/resources/English.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ FavoritePrompt=Enter name
ErrorTitle=Browser Error
FavoritePromptTitle=Add Favorite
FavoriteFirstItem=All Favorites
SetHomeCommand=Set Home
SetHomeCommand=Set Home
4 changes: 4 additions & 0 deletions src/resources/Englisherrors.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NoFavoritesError=
InvalidURLError=Invalid URL (%s) entered.
NoFavoriteEnteredError
NullHomeError=Null URL bro.
20 changes: 12 additions & 8 deletions src/resources/default.css
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
.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: rgb(0, 255, 255);
-fx-background: rgb(225, 255, 0);
}

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

.body{
-fx-background-color: #000000;
}

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

.combo-box-base {
-fx-text-base-color: #006464;
-fx-background-color: #DFB951;
-fx-text-base-color: #27BE12;
-fx-background-color: #991BD9;
-fx-border-radius: 20;
-fx-background-radius: 20;
}
Expand All @@ -29,7 +33,7 @@

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