Advertisement
Guest User

Web 2.0

a guest
Jan 26th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.41 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.tree.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.awt.geom.*;
  6. import javax.swing.event.*;
  7. import java.io.*;
  8. import java.awt.GridLayout;
  9. import java.net.*;
  10. import java.util.*;
  11.  
  12. /**
  13.  * A web browser.
  14.  * @author Max Bergmark
  15.  * @author Christel Sundberg
  16.  *
  17.  */
  18.  
  19. public class Web extends JFrame implements ActionListener{
  20.     private GridBagConstraints c;
  21.     private JTextField adress;
  22.     private JButton xbutton;
  23.     private JButton fbutton;
  24.     private JButton pbutton;
  25.     private JButton gobutton;
  26.     private JButton historybutton;
  27.     private JEditorPane pane;
  28.     private ArrayList<String> navigation;
  29.     private ArrayList<String> history;
  30.     private JFrame framen;
  31.     private int navNumber;
  32.     /**
  33.      * Creates the interface and adds the ActionListeners
  34.      */
  35.     public Web(){
  36.         framen = new JFrame("Webb 2.0");
  37.         navigation = new ArrayList<String>();
  38.         history = new ArrayList<String>();
  39.        
  40.  
  41.         framen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42.         framen.setPreferredSize(new Dimension(300, 300));
  43.         framen.setLayout(new GridBagLayout());
  44.  
  45.         c = new GridBagConstraints();
  46.         c.insets = new Insets(2, 2, 2, 2); 
  47.         c.fill = GridBagConstraints.HORIZONTAL;
  48.         c.gridx = 3;
  49.         c.gridy = 0;
  50.         c.weightx = .9;
  51.         c.weighty = 0;
  52.         c.anchor = GridBagConstraints.NORTH;
  53.         adress = new JTextField();
  54.         framen.add(adress, c);
  55.  
  56.        
  57.         xbutton = new JButton("x");
  58.         xbutton.setMargin(new Insets(0, 0, 0, 0));
  59.         xbutton.setPreferredSize(new Dimension(40, 20));
  60.         c.gridx = 5;
  61.         c.weightx = 0;
  62.         c.fill = GridBagConstraints.NONE;
  63.         xbutton.addActionListener(this);
  64.         framen.add(xbutton, c);
  65.  
  66.         gobutton = new JButton("GO");
  67.         gobutton.setMargin(new Insets(0, 0, 0, 0));
  68.         gobutton.setPreferredSize(new Dimension(20, 20));
  69.         c.gridx = 4;
  70.         c.weightx = 0;
  71.         c.fill = GridBagConstraints.NONE;
  72.         gobutton.addActionListener(this);
  73.         framen.add(gobutton, c);
  74.  
  75.         fbutton = new JButton(">");
  76.         fbutton.setMargin(new Insets(0, 0, 0, 0));
  77.         fbutton.setPreferredSize(new Dimension(20, 20));
  78.         c.gridx = 1;
  79.         c.weightx = 0;
  80.         c.fill = GridBagConstraints.NONE;
  81.         fbutton.addActionListener(this);
  82.         framen.add(fbutton, c);
  83.  
  84.         pbutton = new JButton("<");
  85.         pbutton.setMargin(new Insets(0, 0, 0, 0));
  86.         pbutton.setPreferredSize(new Dimension(20, 20));
  87.         c.gridx = 0;
  88.         c.weightx = 0;
  89.         c.fill = GridBagConstraints.NONE;
  90.         pbutton.addActionListener(this);
  91.         framen.add(pbutton, c);
  92.  
  93.         historybutton = new JButton("History");
  94.         historybutton.setMargin(new Insets(0, 0, 0, 0));
  95.         historybutton.setPreferredSize(new Dimension(20, 20));
  96.         c.gridx = 2;
  97.         c.weightx = 0;
  98.         c.fill = GridBagConstraints.NONE;
  99.         historybutton.addActionListener(this);
  100.         framen.add(historybutton, c);
  101.  
  102.         pane = new JEditorPane();
  103.         pane.setContentType("text/html");
  104.         pane.setEditable(false);
  105.         pane.addHyperlinkListener(new HyperlinkListener() {
  106.             public void hyperlinkUpdate(HyperlinkEvent e) {
  107.                 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
  108.                     if (Desktop.isDesktopSupported()) {
  109.                         try {
  110.                             pane.setPage(e.getURL());
  111.                             addHistory(e.getURL().toString());
  112.                             adress.setText(e.getURL().toString());
  113.                             navigate(0);
  114.                         }
  115.                         catch (IOException e1) {
  116.                             System.out.println("hyperlink error");
  117.                             errorFunc();
  118.                         }
  119.  
  120.                     }
  121.                 }
  122.             }
  123.         });
  124.         c.gridx = 0;
  125.         c.gridy = 1;
  126.         c.gridwidth = 6;
  127.         c.weightx = 0;
  128.         c.weighty = 1;
  129.         c.fill = GridBagConstraints.BOTH;
  130.         JScrollPane scroll = new JScrollPane(pane);
  131.         framen.add(scroll, c);
  132.         try {
  133.             pane.setPage("http://www.google.com/");
  134.             addHistory("http://www.google.com/");
  135.             adress.setText("http://www.google.com/");
  136.             navNumber = 0;
  137.             navigate(0);
  138.  
  139.         }
  140.         catch (Exception e) {
  141.             System.out.println("allt gick snett");
  142.             errorFunc();
  143.         }  
  144.        
  145.         framen.pack(); //framen sätts in i fönstret
  146.         framen.getContentPane().setBackground(new Color(255, 215, 0));
  147.         framen.setVisible(true);
  148.     }
  149.     /**
  150.      * Displays the error message.
  151.      */
  152.     public void errorFunc() {
  153.         JOptionPane.showMessageDialog(framen, "FATAL ERROR");
  154.     }
  155.     /**
  156.      * Navigates the temporary history tree.
  157.      * @param i The number (-1, 0, or 1) of pages to be navigated. Used to determine the direction of navigation.
  158.      */
  159.     public void navigate(int i) {
  160.         //System.out.println(navigation + " " + navNumber + " " + navigation.size());
  161.         navNumber += i;
  162.         pbutton.setBackground(Color.WHITE);
  163.         fbutton.setBackground(Color.WHITE);
  164.         if (navNumber == 0) {
  165.             pbutton.setBackground(Color.RED);
  166.         }
  167.         if (navNumber == navigation.size()-1) {
  168.             fbutton.setBackground(Color.RED);
  169.         }
  170.         if (i != 0) {
  171.             try {
  172.                
  173.                 System.out.println(navigation.get(navNumber));
  174.                 pane.setPage(navigation.get(navNumber));
  175.                 adress.setText(navigation.get(navNumber));
  176.             }
  177.             catch (Exception e) {
  178.                 System.out.println("navigation error");
  179.                 errorFunc();
  180.             }
  181.         }
  182.     }
  183.  
  184.     /**
  185.      * Runs when a button is clicked. Updates the page and exits the window.
  186.      * @param e The ActionEvent that triggered the function call
  187.      */
  188.     public void actionPerformed( ActionEvent e ) {
  189.         String cmd = e.getActionCommand();
  190.         //System.out.println("." + cmd + ".");
  191.         if ( cmd.equals("x") ) {
  192.             framen.dispose();
  193.         }
  194.         if (cmd.equals("GO")) {
  195.             String text = adress.getText();
  196.             try {
  197.                 pane.setPage(text);
  198.                 addHistory(text);
  199.             }
  200.             catch (Exception exep) {
  201.                 System.out.println("allt gick snett här");
  202.                 errorFunc();
  203.             }  
  204.         }
  205.         if ( cmd.equals("<") ) {
  206.             if (navNumber > 0) {
  207.                 navigate(-1);
  208.             }
  209.            
  210.         }
  211.         if ( cmd.equals(">") ) {
  212.             if (navNumber < navigation.size()-1) {
  213.                 navigate(1);
  214.             }
  215.         }
  216.         if ( cmd.equals("History") ) {
  217.             openHistory();
  218.         }
  219.     }
  220.     /**
  221.      * Opens the browser history for the session.
  222.      */
  223.     private void openHistory() {
  224.         File f = new File("Web.html");
  225.         try {
  226.             pane.setPage(f.toURI().toURL());
  227.         }
  228.         catch (Exception e) {
  229.             System.out.println("history error");
  230.             errorFunc();
  231.         }
  232.     }
  233.     /**
  234.      * Generates the HTML code for the history.
  235.      * @return returns the string.
  236.      */
  237.     private String generateHTML() {
  238.         String test = "<html>\n<body>";
  239.         for (String s : history) { //loopar över history, plockar ut s varje iteration
  240.             test += "\n" + "<a href = \"" + s + "\">" + s + "</a><br>";
  241.         }
  242.         test += "\n</body>\n</html>";
  243.         return test;
  244.  
  245.     }
  246.     /**
  247.      * Adds the new website to the history.
  248.      * @param sida The URL String
  249.      */
  250.     private void addHistory(String sida){
  251.         File f = new File("history.html");
  252.         history.add(sida);
  253.         String historyhtml = generateHTML();
  254.         try (BufferedWriter writer = new BufferedWriter(new FileWriter(f))) {
  255.             writer.write(historyhtml);
  256.         }
  257.         catch (Exception error) {
  258.             return;
  259.         }
  260.         //if (sida.equals(navigation.get(navigation.size()-1))) {
  261.         if (navNumber < navigation.size()-1) {
  262.             System.out.println("deleting");
  263.             for (int i = navNumber+1; i < navigation.size(); i++) {
  264.                 //System.out.println(i + " " + (navNumber+1));
  265.                 navigation.remove(navNumber+1);
  266.             }
  267.         }
  268.  
  269.         navNumber++;
  270.         navigation.add(sida);
  271.         navigate(0);
  272.         //}
  273.         //System.out.println(history);
  274.     }
  275.     /**
  276.      * Runs the program
  277.      * @param args Command-line arguments not used
  278.      */
  279.     public static void main(String[] args) {
  280.         Web nywebb = new Web();
  281.  
  282.     }
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement