Follow up question to: Utilizing OOP when creating a web browser
After I received help on the question above, I've created 3 classes for my Browser
. Browser
is my "View"-class that handles the GUI, Model is self-explaining and History
contains the lists and their functionality.
First of all, is this an OK solution from a MVC-perspective? And also, my History
class uses static counters because that was the best solution I could think of considering I want them to be the same no matter where they're used, but is there a better way to use counters without having them static? I've been told that using static
should be avoided while in the noob stages of programming.
Browser
:
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Browser{
public JTextField addressBar;
public JEditorPane display;
private JButton button;
private JPanel panel;
private String URL = new String();
private JButton backward;
private JButton forward;
private JFrame frame;
private History history;
public Browser(Model controller, History history) {
this.history = history;
frame = new JFrame("My Browser");
panel = new JPanel();
addressBar = new JTextField("Enter a URL");
addressBar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
URL = e.getActionCommand().toString();
controller.newURL(URL);
}
});
display = new JEditorPane();
display.setEditable(false);
display.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
URL = e.getURL().toString();
controller.newURL(URL);
}
}
});
addButton("Close").addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() != null) {
controller.closeAction();
}
}
});
forward = addButton("Forward");
forward.setEnabled(false);
forward.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() != null) {
controller.forwardAction();
}
}
});
backward = addButton("Back");
backward.setEnabled(false);
backward.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() != null) {
controller.backwardAction();
}
}
});
addButton("History").addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() != null) {
controller.historyAction();
}
}
});
setframe();
}
private void setframe() {
frame.add(addressBar, BorderLayout.NORTH);
frame.add(panel, BorderLayout.SOUTH);
frame.add(new JScrollPane(display), BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setVisible(true);
}
private JButton addButton(String name) {
button = new JButton(name);
panel.add(button);
return button;
}
public void initBackward() {
if (History.i > 0) {
backward.setEnabled(true);
} else {
backward.setEnabled(false);
}
}
public void initForward() {
if (History.i < history.list.size() - 1) {
forward.setEnabled(true);
} else {
forward.setEnabled(false);
}
}
}
Model
:
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class Model{
private Browser browser;
private History history;
public Model() {
history = new History();
browser = new Browser(this, history);
}
public void loadURL(String URL) {
try {
browser.display.setPage(URL);
browser.addressBar.setText(URL);
} catch (Exception e) {
history.removefromList(history.list);
history.removefromList(history.previousPages);
JOptionPane.showMessageDialog(null, "fel länk");
}
}
public void closeAction() {
System.exit(0);
}
public void forwardAction() {
History.i++;
browser.initBackward();
browser.initForward();
loadURL(history.list.get(History.i));
}
public void backwardAction() {
History.i--;
browser.initForward();
browser.initBackward();
loadURL(history.list.get(History.i));
}
public void historyAction() {
String html = new String();
for (String link : history.previousPages) {
html = html + "<a href=\"" + link + "\">" + link + "</a>\n";
}
html = "<html><body" + html + "</body></html>";
JEditorPane ep = new JEditorPane("text/html", html);
ep.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
loadURL(e.getURL().toString());
}
}
});
ep.setEditable(false);
JOptionPane.showMessageDialog(null, ep);
}
public void newURL(String URL) {
History.i++;
History.j++;
history.checklist();
loadURL(URL);
history.addtoList(history.previousPages, URL);
history.addtoList(history.list, URL);
browser.initBackward();
browser.initForward();
}
}
History
:
import java.util.ArrayList;
import java.util.List;
public class History {
public static int i = -1;
public static int j = -1;
public List<String> list = new ArrayList<>();
public List<String> previousPages = new ArrayList<>();
public void checklist() {
if (i != j) {
list.subList(i, list.size()).clear();
j = i;
}
}
public void addtoList(List<String> list, String URL) {
list.add(URL);
}
public void removefromList(List<String> list) {
list.remove(list.size()-1);
}
}
MyFrame
:
public class MyFrame {
public static void main(String[] args) {
new Model();
}
}