diff options
author | JJ | 2022-12-27 05:38:32 +0000 |
---|---|---|
committer | JJ | 2022-12-27 15:30:20 +0000 |
commit | c14e53775591cb4d75b486d21f4849552d5c7c8c (patch) | |
tree | fc9d67b66107f4965f3173183ec846e4d8e090c4 /src/main/ui | |
parent | 0e693fd4814572712a4192cb4f176e71f7a344f7 (diff) |
Remove garbage serialization/logging code
Diffstat (limited to 'src/main/ui')
-rw-r--r-- | src/main/ui/BrowserApp.java | 110 | ||||
-rw-r--r-- | src/main/ui/BrowserWindow.java | 67 |
2 files changed, 0 insertions, 177 deletions
diff --git a/src/main/ui/BrowserApp.java b/src/main/ui/BrowserApp.java index c4bbf99..520cac8 100644 --- a/src/main/ui/BrowserApp.java +++ b/src/main/ui/BrowserApp.java @@ -4,11 +4,7 @@ import model.html.ElementNode; import model.html.HtmlParser; import model.html.TextNode; import model.html.Node; -import org.json.JSONArray; -import org.json.JSONObject; -import persistance.JsonUtils; -import java.io.File; import java.nio.file.*; import java.util.*; @@ -18,7 +14,6 @@ import java.util.*; public class BrowserApp { private Scanner input; private static final String border = "==============================================="; - private static final String storagePath = "data/apus.cache"; private String pathString; private ArrayList<Node> parsed; private ArrayDeque<String> tabs; @@ -30,39 +25,10 @@ public class BrowserApp { println("apus: currently a barebones html/css renderer"); this.input = new Scanner(System.in); this.tabs = new ArrayDeque<>(); - - askToRestoreTabs(); mainLoop(); } /** - * EFFECTS: Asks the user if they'd like to restore previously closed tabs. - */ - private void askToRestoreTabs() { - if (new File(storagePath).length() > 2) { - println("Would you like to restore your previously closed tabs? (Y/N)"); - String answer; - while (true) { - answer = this.input.next(); - if (answer.equalsIgnoreCase("y")) { - restoreClosedTabs(); - break; - } else if (answer.equalsIgnoreCase("n")) { - JsonUtils.writeToFile(new JSONArray(), storagePath); - println("please provide a path to a file (examples located in data/*):"); - pathString = this.input.next(); - break; - } else { - println("Sorry, I didn't quite get that. Please try again."); - } - } - } else { - println("please provide a path to a file (examples located in data/*):"); - pathString = this.input.next(); - } - } - - /** * EFFECTS: Runs the main loop */ private void mainLoop() { @@ -87,23 +53,6 @@ public class BrowserApp { } /** - * EFFECTS: restores previous closed tabs from a cache file. - */ - private void restoreClosedTabs() { - try { - JSONArray state = JsonUtils.readFromFile(storagePath); - for (int i = 0; i < state.length(); i++) { - println(state.get(i).getClass().getName()); - tabs.add((String) state.get(i)); - } - pathString = tabs.removeLast(); - } catch (Exception e) { - println("Restoring state from disk failed with " + e.toString()); - System.exit(0); - } - } - - /** * EFFECTS: Barebones HTML rendering. Iterates through a list of Nodes and their children and prints any text. */ private void renderHtml(ArrayList<Node> html) { @@ -135,7 +84,6 @@ public class BrowserApp { pathString = this.tabs.removeFirst(); break; case "quit": - handleQuit(); System.exit(0); break; default: @@ -144,64 +92,6 @@ public class BrowserApp { } } - /** - * Helper function for the quit() case. - * EFFECTS: Asks a user whether they'd like to save their tabs, and exists the program. - */ - private void handleQuit() { - println("Would you like to save your currently opened tabs to disk? (Y/N)"); - String answer; - while (true) { - answer = this.input.next(); - if (answer.equalsIgnoreCase("y")) { - this.tabs.add(pathString); - JsonUtils.writeToFile(new JSONArray(tabs), storagePath); - break; - } else if (answer.equalsIgnoreCase("n")) { - JsonUtils.writeToFile(new JSONArray(), storagePath); - break; - } else { - println("Sorry, I didn't quite get that. Please try again."); - } - } - } - - /** - * EFFECTS: writes the current program configuration to the disk - */ - private void writeToDisk() { - ArrayList<ArrayList<JSONObject>> jsonArray = new ArrayList<>(); - for (String p : tabs) { - ArrayList<JSONObject> jsonArrayII = new ArrayList<>(); - try { - Path path = Paths.get(pathString); - String file = new String(Files.readAllBytes(path)); - HtmlParser parser = new HtmlParser(); - for (Node n : parser.parseHtml(file)) { - jsonArrayII.add(n.serialize()); - } - } catch (Exception e) { - System.out.printf("Failed to write to disk with %s", e); - } - jsonArray.add(jsonArrayII); - } - JsonUtils.writeToFile(new JSONArray(jsonArray), storagePath); - } - - /** - * EFFECTS: restores program state from a last written to state - */ - private void restoreFromDisk(JSONArray state) { - for (int i = 0; i < state.length(); i++) { - Object tab = state.get(i); - if (tab instanceof JSONArray) { - for (int j = 0; j < ((JSONArray) tab).length(); j++) { - tabs.add(((JSONArray) tab).toString()); - } - } - } - } - private void print(String toPrint) { System.out.print(toPrint); } diff --git a/src/main/ui/BrowserWindow.java b/src/main/ui/BrowserWindow.java index 2375fc1..bd1cf7f 100644 --- a/src/main/ui/BrowserWindow.java +++ b/src/main/ui/BrowserWindow.java @@ -2,16 +2,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; import javax.swing.*; import java.awt.*; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.io.File; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -22,7 +15,6 @@ import java.util.ArrayList; public class BrowserWindow extends JFrame { public static final int WIDTH = 1200; public static final int HEIGHT = 800; - private static final String storagePath = "data/apus.cache"; private BrowserCanvas canvas; private BrowserBar browserBar; @@ -46,9 +38,6 @@ public class BrowserWindow extends JFrame { // render("data/example.hctml"); // browserBar.addTab("/home/apropos/Projects/website/j-james/index.html"); setVisible(true); - setClosingBehavior(); - - initializeBrowser(); } // MODIFIES: this @@ -72,62 +61,6 @@ public class BrowserWindow extends JFrame { setVisible(true); } - // EFFECTS: Prompts the user to save their tabs before quitting - private void setClosingBehavior() { - this.addWindowListener(new WindowAdapter() { - @Override - public void windowClosing(WindowEvent e) { - if (state.getTabs().size() > 0) { - saveCurrentTabs(); - } - for (Event event : EventLog.getInstance()) { - System.out.println(event); - } - super.windowClosing(e); - } - }); - } - - // EFFECTS: sets up the browser upon launching - private void initializeBrowser() { - if (new File(storagePath).length() > 2) { - restorePreviousTabs(); - } - } - - // MODIFIES: this - // EFFECTS: prompts the user to restore their previous tabs - private void restorePreviousTabs() { - int answer = JOptionPane.showOptionDialog( - this, "Would you like to restore your previous tabs?", "apus", - JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, - null, new String[]{"Yes", "No"}, "Yes"); - if (answer == 0) { - try { - JSONArray state = JsonUtils.readFromFile(storagePath); - for (int i = 0; i < state.length(); i++) { - this.browserBar.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()); - } - } - } - - // EFFECTS: prompts the user to save their current tabs before closing - private void saveCurrentTabs() { - int answer = JOptionPane.showOptionDialog( - this, "Would you like to save your current tabs?", "apus", - JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, - null, new String[]{"Yes", "No"}, "Yes"); - if (answer == 0) { - JsonUtils.writeToFile(new JSONArray(state.getTabs()), storagePath); - } else { - JsonUtils.writeToFile(new JSONArray(), storagePath); - } - } - public BrowserState getBrowserState() { return this.state; } |