aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorj-james2022-12-03 04:54:57 +0000
committerj-james2022-12-03 04:54:57 +0000
commit9ad66fd06a7eb54f440966dc4d935179c2cc150a (patch)
tree80c2c4afefc39d44d6a38e1113faec04db36cc07 /src/main
parentec8ad6118e59572f350e1650d4e1d1fd665c671d (diff)
Begin Phase 4
Diffstat (limited to 'src/main')
-rw-r--r--src/main/model/BrowserState.java40
-rw-r--r--src/main/model/layout/BlockLayout.java1
-rw-r--r--src/main/model/layout/FlexLayout.java1
-rw-r--r--src/main/model/layout/Layout.java1
-rw-r--r--src/main/model/util/Event.java66
-rw-r--r--src/main/model/util/EventLog.java60
-rw-r--r--src/main/ui/BrowserApp.java14
-rw-r--r--src/main/ui/BrowserBar.java9
-rw-r--r--src/main/ui/BrowserWindow.java40
9 files changed, 187 insertions, 45 deletions
diff --git a/src/main/model/BrowserState.java b/src/main/model/BrowserState.java
new file mode 100644
index 0000000..7f948dc
--- /dev/null
+++ b/src/main/model/BrowserState.java
@@ -0,0 +1,40 @@
+package model;
+
+import model.util.Event;
+import model.util.EventLog;
+
+import java.util.ArrayDeque;
+
+public class BrowserState {
+ private ArrayDeque<String> tabs;
+ private String currentTab;
+
+ public BrowserState(ArrayDeque<String> tabs, String currentTab) {
+ this.tabs = tabs;
+ this.currentTab = currentTab;
+ }
+
+ public ArrayDeque<String> getTabs() {
+ return this.tabs;
+ }
+
+ public String getCurrentTab() {
+ return this.currentTab;
+ }
+
+ public void setCurrentTab(String tab) {
+ this.currentTab = tab;
+ }
+
+ public void addTab(String added) {
+ if (!this.tabs.contains(added)) {
+ this.tabs.add(added);
+ }
+ EventLog.getInstance().logEvent(new Event("Added tab " + added));
+ }
+
+ public void removeTab(String removed) {
+ this.tabs.remove(removed);
+ EventLog.getInstance().logEvent(new Event("Removed tab " + removed));
+ }
+}
diff --git a/src/main/model/layout/BlockLayout.java b/src/main/model/layout/BlockLayout.java
index 8808d80..c2ec40d 100644
--- a/src/main/model/layout/BlockLayout.java
+++ b/src/main/model/layout/BlockLayout.java
@@ -15,6 +15,7 @@ public class BlockLayout extends Layout {
this.getPreviousSibling().ifPresent(
sibling -> this.setY(sibling.getY() + sibling.getHeight()));
+
this.setDimension(this.getParent().getDimension());
for (Layout child : this.getChildren()) {
diff --git a/src/main/model/layout/FlexLayout.java b/src/main/model/layout/FlexLayout.java
index 389e8d0..7b1e759 100644
--- a/src/main/model/layout/FlexLayout.java
+++ b/src/main/model/layout/FlexLayout.java
@@ -12,6 +12,5 @@ public class FlexLayout extends Layout {
// todo: we'll cheese it, and treat it like a sideways block
public void layout() {
-
}
}
diff --git a/src/main/model/layout/Layout.java b/src/main/model/layout/Layout.java
index ba9f604..f9ef70e 100644
--- a/src/main/model/layout/Layout.java
+++ b/src/main/model/layout/Layout.java
@@ -72,6 +72,7 @@ public abstract class Layout {
public static DocumentLayout constructTree(ArrayList<Node> html) {
DocumentLayout result = new DocumentLayout();
result.setChildren(constructTree(html, result));
+ result.layout();
return result;
}
diff --git a/src/main/model/util/Event.java b/src/main/model/util/Event.java
new file mode 100644
index 0000000..8a42ef8
--- /dev/null
+++ b/src/main/model/util/Event.java
@@ -0,0 +1,66 @@
+package model.util;
+
+import java.util.Calendar;
+import java.util.Date;
+
+
+/**
+ * Represents an alarm system event.
+ */
+public class Event {
+ private static final int HASH_CONSTANT = 13;
+ private Date dateLogged;
+ private String description;
+
+ /**
+ * Creates an event with the given description
+ * and the current date/time stamp.
+ * @param description a description of the event
+ */
+ public Event(String description) {
+ dateLogged = Calendar.getInstance().getTime();
+ this.description = description;
+ }
+
+ /**
+ * Gets the date of this event (includes time).
+ * @return the date of the event
+ */
+ public Date getDate() {
+ return dateLogged;
+ }
+
+ /**
+ * Gets the description of this event.
+ * @return the description of the event
+ */
+ public String getDescription() {
+ return description;
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (other == null) {
+ return false;
+ }
+
+ if (other.getClass() != this.getClass()) {
+ return false;
+ }
+
+ Event otherEvent = (Event) other;
+
+ return (this.dateLogged.equals(otherEvent.dateLogged)
+ && this.description.equals(otherEvent.description));
+ }
+
+ @Override
+ public int hashCode() {
+ return (HASH_CONSTANT * dateLogged.hashCode() + description.hashCode());
+ }
+
+ @Override
+ public String toString() {
+ return dateLogged.toString() + "\n" + description;
+ }
+}
diff --git a/src/main/model/util/EventLog.java b/src/main/model/util/EventLog.java
new file mode 100644
index 0000000..507c9b2
--- /dev/null
+++ b/src/main/model/util/EventLog.java
@@ -0,0 +1,60 @@
+package model.util;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+
+/**
+ * Represents a log of alarm system events.
+ * We use the Singleton Design Pattern to ensure that there is only
+ * one EventLog in the system and that the system has global access
+ * to the single instance of the EventLog.
+ */
+public class EventLog implements Iterable<Event> {
+ /** the only EventLog in the system (Singleton Design Pattern) */
+ private static EventLog theLog;
+ private Collection<Event> events;
+
+ /**
+ * Prevent external construction.
+ * (Singleton Design Pattern).
+ */
+ private EventLog() {
+ events = new ArrayList<Event>();
+ }
+
+ /**
+ * Gets instance of EventLog - creates it
+ * if it doesn't already exist.
+ * (Singleton Design Pattern)
+ * @return instance of EventLog
+ */
+ public static EventLog getInstance() {
+ if (theLog == null) {
+ theLog = new EventLog();
+ }
+
+ return theLog;
+ }
+
+ /**
+ * Adds an event to the event log.
+ * @param e the event to be added
+ */
+ public void logEvent(Event e) {
+ events.add(e);
+ }
+
+ /**
+ * Clears the event log and logs the event.
+ */
+ public void clear() {
+ events.clear();
+ logEvent(new Event("Event log cleared."));
+ }
+
+ @Override
+ public Iterator<Event> iterator() {
+ return events.iterator();
+ }
+}
diff --git a/src/main/ui/BrowserApp.java b/src/main/ui/BrowserApp.java
index 5e73144..c4bbf99 100644
--- a/src/main/ui/BrowserApp.java
+++ b/src/main/ui/BrowserApp.java
@@ -103,20 +103,6 @@ public class BrowserApp {
}
}
- /*
- private void mainLoopII(ArrayList<String> rawHtml, String border, HtmlParser parser) {
- while (true) {
- println("Page rendered. Input additional raw HTML if desired.");
- rawHtml.add(input.next());
- println(border);
- for (String s : rawHtml) {
- parser = new HtmlParser();
- renderHtml(parser.parseHtml(s));
- }
- println(border);
- }
- }*/
-
/**
* EFFECTS: Barebones HTML rendering. Iterates through a list of Nodes and their children and prints any text.
*/
diff --git a/src/main/ui/BrowserBar.java b/src/main/ui/BrowserBar.java
index 507c33f..ed9e314 100644
--- a/src/main/ui/BrowserBar.java
+++ b/src/main/ui/BrowserBar.java
@@ -19,9 +19,6 @@ public class BrowserBar extends JToolBar {
this.parent = parent;
tabMenu = new JPopupMenu("Tabs");
- tabMenu.add(new JMenuItem("Hello"));
- tabMenu.add(new JMenuItem("World"));
- tabMenu.add(new JMenuItem("Lorem"));
tabButton = new JToggleButton("Tabs");
tabButton.addActionListener(toggleTabMenu());
@@ -44,8 +41,6 @@ public class BrowserBar extends JToolBar {
String uri = uriInput.getText();
parent.render(uri);
addTab(uri);
- System.out.println(uri);
- System.out.println("should run");
}
};
}
@@ -66,13 +61,13 @@ public class BrowserBar extends JToolBar {
} else {
tabMenu.remove(tabButton);
tabMenu.setVisible(false);
- parent.removeTab(tab);
+ parent.getBrowserState().removeTab(tab);
}
}
});
this.tabMenu.add(tabButton);
- parent.addTab(tab);
+ parent.getBrowserState().addTab(tab);
}
// MODIFIES: this
diff --git a/src/main/ui/BrowserWindow.java b/src/main/ui/BrowserWindow.java
index 9a92186..640fb1a 100644
--- a/src/main/ui/BrowserWindow.java
+++ b/src/main/ui/BrowserWindow.java
@@ -1,6 +1,9 @@
package ui;
+import model.BrowserState;
import model.html.HtmlParser;
+import model.util.Event;
+import model.util.EventLog;
import org.json.JSONArray;
import persistance.JsonUtils;
@@ -24,14 +27,14 @@ public class BrowserWindow extends JFrame {
private BrowserCanvas canvas;
private BrowserBar browserBar;
- private ArrayDeque<String> tabs;
- private String pathString;
+ private BrowserState state;
// MODIFIES: this
// EFFECTS: creates a new BrowserWindow program for rendering pages
public BrowserWindow() {
super("apus");
- tabs = new ArrayDeque<>();
+ state = new BrowserState(new ArrayDeque<>(), "");
+
canvas = new BrowserCanvas(new ArrayList<>());
// render("data/example.html");
browserBar = new BrowserBar(this);
@@ -50,11 +53,11 @@ public class BrowserWindow extends JFrame {
// MODIFIES: this
// EFFECTS: Renders an arbitrary page
public void render(String uri) {
- pathString = uri;
+ state.setCurrentTab(uri);
remove(canvas);
- System.out.println(pathString);
+// System.out.println(state.getCurrentTab());
try {
- Path path = Paths.get(pathString);
+ Path path = Paths.get(state.getCurrentTab());
String file = new String(Files.readAllBytes(path));
HtmlParser parser = new HtmlParser();
canvas = new BrowserCanvas(parser.parseHtml(file));
@@ -73,9 +76,12 @@ public class BrowserWindow extends JFrame {
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
- if (tabs.size() > 0) {
+ if (state.getTabs().size() > 0) {
saveCurrentTabs();
}
+ for (Event event : EventLog.getInstance()) {
+ System.out.println(event);
+ }
super.windowClosing(e);
}
});
@@ -100,7 +106,7 @@ public class BrowserWindow extends JFrame {
JSONArray state = JsonUtils.readFromFile(storagePath);
for (int i = 0; i < state.length(); i++) {
this.browserBar.addTab((String) state.get(i));
- this.addTab((String) state.get(i));
+ this.state.addTab((String) state.get(i));
}
} catch (Exception e) {
System.out.println("Restoring state from disk failed with " + e.toString());
@@ -115,25 +121,13 @@ public class BrowserWindow extends JFrame {
JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE,
null, new String[]{"Yes", "No"}, "Yes");
if (answer == 0) {
- JsonUtils.writeToFile(new JSONArray(tabs), storagePath);
+ JsonUtils.writeToFile(new JSONArray(state.getTabs()), storagePath);
} else {
JsonUtils.writeToFile(new JSONArray(), storagePath);
}
}
- public ArrayDeque<String> getTabs() {
- return tabs;
- }
-
- // MODIFIES: this
- // EFFECTS: add a tab
- public void addTab(String tab) {
- this.tabs.add(tab);
- }
-
- // MODIFIES: this
- // EFFECTS: remove a tab
- public void removeTab(String tab) {
- this.tabs.remove(tab);
+ public BrowserState getBrowserState() {
+ return this.state;
}
}