aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/BrowserBar.java
blob: 158bf3caf32a5fed6106b8389d32724c70ed14d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class BrowserBar extends JToolBar {
    private final BrowserWindow parent;

    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;

        tabMenu = new JPopupMenu("Tabs");

        tabButton = new JToggleButton("Tabs");
        tabButton.addActionListener(toggleTabMenu());
        add(tabButton);

        uriInput = new JTextField(100);
        add(uriInput);

        openUriButton = new JButton("Go");
        openUriButton.addActionListener(openTab());
        add(openUriButton);
    }

    private ActionListener openTab() {
        return actionEvent -> {
            String uri = uriInput.getText();
            parent.render(uri);
            addTab(uri);
        };
    }

    // todo: fix this pretty awful code
    public void addTab(String tab) {
        JToggleButton tabButton = new JToggleButton(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);
            }
        });

        this.tabMenu.add(tabButton);
        parent.getBrowserState().addTab(tab);
    }

    private ActionListener toggleTabMenu() {
        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);
            }
        };
    }
}