aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/BrowserBar.java
blob: 09c78a58b7cc137a40146baf95ed82cc2d06e9bb (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package ui;

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

public class BrowserBar extends JToolBar {
    private BrowserWindow parent;

    private JPopupMenu tabMenu;
    private JToggleButton tabButton;
    private JTextField uriInput;
//    private JButton saveTabsButton;
    private JButton openUriButton;

    public BrowserBar(BrowserWindow parent) {
        this.parent = parent;

        var test = new JMenuItem("Ipsum");
        tabMenu = new JPopupMenu("Tabs");
        tabMenu.add(new JMenuItem("Hello"));
        tabMenu.add(new JMenuItem("World"));
        tabMenu.add(new JMenuItem("Lorem"));
        tabMenu.add(test);
        tabMenu.remove(test);

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

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

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

    }

    // 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);
                System.out.println(uri);
                System.out.println("should run");
            }
        };
    }

    // 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.removeTab(tab);
                }
            }
        });

        this.tabMenu.add(tabButton);
        parent.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);
                }
            }
        };
    }
}