diff options
author | JJ | 2022-12-27 16:36:56 +0000 |
---|---|---|
committer | JJ | 2022-12-27 16:36:56 +0000 |
commit | 69c8bcbda55c4eb8e79b89736568781440484319 (patch) | |
tree | b1db42212037747ff7109b65816839a2047be512 /src/main/ui | |
parent | c14e53775591cb4d75b486d21f4849552d5c7c8c (diff) |
Convert project to Java 19 (at long last)
Diffstat (limited to 'src/main/ui')
-rw-r--r-- | src/main/ui/BrowserApp.java | 57 | ||||
-rw-r--r-- | src/main/ui/BrowserBar.java | 75 | ||||
-rw-r--r-- | src/main/ui/BrowserCanvas.java | 29 | ||||
-rw-r--r-- | src/main/ui/BrowserWindow.java | 23 |
4 files changed, 58 insertions, 126 deletions
diff --git a/src/main/ui/BrowserApp.java b/src/main/ui/BrowserApp.java index 520cac8..36f2005 100644 --- a/src/main/ui/BrowserApp.java +++ b/src/main/ui/BrowserApp.java @@ -1,9 +1,6 @@ package ui; -import model.html.ElementNode; -import model.html.HtmlParser; -import model.html.TextNode; -import model.html.Node; +import model.html.*; import java.nio.file.*; import java.util.*; @@ -18,24 +15,15 @@ public class BrowserApp { private ArrayList<Node> parsed; private ArrayDeque<String> tabs; - /** - * EFFECTS: Renders an arbitrary HTML page and arbitrary HTML input. - */ + // Renders an arbitrary HTML page and arbitrary HTML input. public BrowserApp() { println("apus: currently a barebones html/css renderer"); this.input = new Scanner(System.in); this.tabs = new ArrayDeque<>(); - mainLoop(); - } - /** - * EFFECTS: Runs the main loop - */ - private void mainLoop() { while (true) { try { - Path path = Paths.get(pathString); - String file = new String(Files.readAllBytes(path)); + String file = Files.readString(Path.of(pathString)); HtmlParser parser = new HtmlParser(); parsed = parser.parseHtml(file); println(border); @@ -52,43 +40,38 @@ public class BrowserApp { } } - /** - * EFFECTS: Barebones HTML rendering. Iterates through a list of Nodes and their children and prints any text. - */ + // Barebones HTML rendering. Iterates through a list of Nodes and their children and prints any text. private void renderHtml(ArrayList<Node> html) { for (Node node: html) { - if (node instanceof TextNode) { - println(node.getData()); - } else { - renderHtml(((ElementNode) node).getChildren()); + switch (node) { + case ElementNode e -> { + renderHtml(e.getChildren()); + } + default -> { + println(node.getData()); + } } } } - /** - * EFFECTS: Handles user input after rendering an initial site - */ + // Handles user input after rendering an initial site private void handleInput(String input) { switch (input) { - case "newuri": + case "newuri" -> { println("please provide a path to a file (examples located in data/*):"); pathString = this.input.next(); - break; - case "newtab": + } + case "newtab" -> { this.tabs.add(pathString); println("please provide a path to a file (examples located in data/*):"); pathString = this.input.next(); - break; - case "nexttab": + } + case "nexttab" -> { this.tabs.add(pathString); pathString = this.tabs.removeFirst(); - break; - case "quit": - System.exit(0); - break; - default: - println("Sorry, I didn't quite get that. Please try again."); - break; + } + case "quit" -> System.exit(0); + default -> println("Sorry, I didn't quite get that. Please try again."); } } diff --git a/src/main/ui/BrowserBar.java b/src/main/ui/BrowserBar.java index e9db3c3..131fe19 100644 --- a/src/main/ui/BrowserBar.java +++ b/src/main/ui/BrowserBar.java @@ -2,19 +2,17 @@ package ui; import javax.swing.*; import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; +import java.awt.event.*; import java.util.*; -// BrowserBar public class BrowserBar extends JToolBar { - private BrowserWindow parent; + private final BrowserWindow parent; - private JPopupMenu tabMenu; - private JToggleButton tabButton; - private JTextField uriInput; -// private JButton saveTabsButton; - private JButton openUriButton; + private final JPopupMenu tabMenu; + private final JToggleButton tabButton; + private final JTextField uriInput; +// private final JButton saveTabsButton; + private final JButton openUriButton; public BrowserBar(BrowserWindow parent) { this.parent = parent; @@ -34,36 +32,28 @@ public class BrowserBar extends JToolBar { } - // EFFECTS: opens the content of the text field in the current tab private ActionListener openTab() { - return new ActionListener() { - @Override - public void actionPerformed(ActionEvent actionEvent) { - String uri = uriInput.getText(); - parent.render(uri); - addTab(uri); - } + return actionEvent -> { + String uri = uriInput.getText(); + parent.render(uri); + addTab(uri); }; } - // EFFECTS: adds a new tab pointing to URI in the background public void addTab(String tab) { JToggleButton tabButton = new JToggleButton(tab); - tabButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent actionEvent) { - int action = JOptionPane.showOptionDialog(null, - "Open or close this tab?", "apus", - JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, - null, new String[]{"Open", "Close"}, "Open"); - if (action == 0) { - parent.render(tab); - } else { - tabMenu.remove(tabButton); - tabMenu.setVisible(false); - parent.getBrowserState().removeTab(tab); - } + tabButton.addActionListener(actionEvent -> { + int action = JOptionPane.showOptionDialog(null, + "Open or close this tab?", "apus", + JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, + null, new String[]{"Open", "Close"}, "Open"); + if (action == 0) { + parent.render(tab); + } else { + tabMenu.remove(tabButton); + tabMenu.setVisible(false); + parent.getBrowserState().removeTab(tab); } }); @@ -71,20 +61,15 @@ public class BrowserBar extends JToolBar { parent.getBrowserState().addTab(tab); } - // MODIFIES: this - // EFFECTS: toggles the tab menu private ActionListener toggleTabMenu() { - return new ActionListener() { - @Override - public void actionPerformed(ActionEvent actionEvent) { - if (tabButton.isSelected()) { - Point location = tabButton.getLocationOnScreen(); - location.translate(0, 30); // fuck this method lol - tabMenu.setLocation(location); - tabMenu.setVisible(true); - } else { - tabMenu.setVisible(false); - } + return actionEvent -> { + if (tabButton.isSelected()) { + Point location = tabButton.getLocationOnScreen(); + location.translate(0, 30); // fuck this method lol + tabMenu.setLocation(location); + tabMenu.setVisible(true); + } else { + tabMenu.setVisible(false); } }; } diff --git a/src/main/ui/BrowserCanvas.java b/src/main/ui/BrowserCanvas.java index a58f6e1..43481ef 100644 --- a/src/main/ui/BrowserCanvas.java +++ b/src/main/ui/BrowserCanvas.java @@ -1,24 +1,17 @@ package ui; -import model.html.ElementNode; -import model.html.TextNode; -import model.html.Node; -import model.layout.DocumentLayout; -import model.layout.Layout; +import model.html.*; +import model.layout.*; import javax.swing.*; import java.awt.*; import java.util.*; public class BrowserCanvas extends JPanel { - private ArrayList<Node> html; - private DocumentLayout currentLayout; + private final DocumentLayout currentLayout; - // MODIFIES: this - // EFFECTS: constructs a BrowserCanvas object public BrowserCanvas(ArrayList<Node> html) { super(); - this.html = html; this.currentLayout = Layout.constructTree(html); printTree(this.currentLayout.getChildren()); } @@ -30,7 +23,6 @@ public class BrowserCanvas extends JPanel { } } - // EFFECTS: paints a component onto a canvas @Override public void paintComponent(Graphics g) { super.paintComponent(g); @@ -38,20 +30,6 @@ public class BrowserCanvas extends JPanel { renderHtml(this.currentLayout.getChildren(), g, location); } - // EFFECTS: naively renders our html file by printing text nodes - /*private void renderHtml(ArrayList<Node> html, Graphics g, Point location) { - for (Node node : html) { - if (node instanceof TextNode) { - if (node.getData().length() > 5) { - g.drawString(node.getData(), location.x, location.y); - location.translate(0, 20); - } - } else { - renderHtml(((ElementNode) node).getChildren(), g, location); - } - } - }*/ - private void renderHtml(ArrayList<Layout> tree, Graphics g, Point location) { for (Layout layout : tree) { // System.out.println(layout.getLocation()); @@ -67,5 +45,4 @@ public class BrowserCanvas extends JPanel { } } } - } diff --git a/src/main/ui/BrowserWindow.java b/src/main/ui/BrowserWindow.java index bd1cf7f..94bd2d8 100644 --- a/src/main/ui/BrowserWindow.java +++ b/src/main/ui/BrowserWindow.java @@ -5,11 +5,8 @@ import model.html.HtmlParser; import javax.swing.*; import java.awt.*; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayDeque; -import java.util.ArrayList; +import java.nio.file.*; +import java.util.*; // Broad JFrame usage taken from here: https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html public class BrowserWindow extends JFrame { @@ -17,43 +14,33 @@ public class BrowserWindow extends JFrame { public static final int HEIGHT = 800; private BrowserCanvas canvas; - private BrowserBar browserBar; + private final BrowserBar browserBar; + private final BrowserState state; - private BrowserState state; - - // MODIFIES: this - // EFFECTS: creates a new BrowserWindow program for rendering pages public BrowserWindow() { super("apus"); state = new BrowserState(new ArrayDeque<>(), ""); canvas = new BrowserCanvas(new ArrayList<>()); -// render("data/example.html"); browserBar = new BrowserBar(this); getContentPane().add(browserBar, BorderLayout.SOUTH); -// pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(WIDTH, HEIGHT); render("/home/apropos/Projects/website/j-james/index.html"); -// render("data/example.hctml"); // browserBar.addTab("/home/apropos/Projects/website/j-james/index.html"); setVisible(true); } - // MODIFIES: this - // EFFECTS: Renders an arbitrary page public void render(String uri) { state.setCurrentTab(uri); remove(canvas); // System.out.println(state.getCurrentTab()); try { - Path path = Paths.get(state.getCurrentTab()); - String file = new String(Files.readAllBytes(path)); + String file = Files.readString(Path.of(state.getCurrentTab())); HtmlParser parser = new HtmlParser(); canvas = new BrowserCanvas(parser.parseHtml(file)); } catch (Exception e) { System.out.println("Could not read file, rendering empty page: " + e.getMessage()); - canvas = new BrowserCanvas(new ArrayList<>()); } add(canvas); |