Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.event.HyperlinkEvent;
  3. import javax.swing.event.HyperlinkListener;
  4. import javax.swing.text.html.HTMLDocument;
  5. import javax.swing.text.html.HTMLFrameHyperlinkEvent;
  6. import java.awt.*;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.io.BufferedReader;
  10. import java.io.InputStreamReader;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13. import java.net.URLConnection;
  14. import java.util.Stack;
  15.  
  16. public class SimpleBrowser extends JFrame implements HyperlinkListener
  17. {
  18. private JTextField addressBar = new JTextField(40);
  19.  
  20. private JEditorPane webPane = new JEditorPane();
  21.  
  22. private URL currentURL = null;
  23.  
  24. private static Stack<URL> historyBack = new Stack<>();;
  25.  
  26. private static Stack<URL> historyForward = new Stack<>();;
  27.  
  28. public SimpleBrowser()
  29. {
  30. setSize(800, 600);
  31.  
  32. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33.  
  34. JPanel navigationPanel = new JPanel();
  35. navigationPanel.setLayout(new BorderLayout());
  36.  
  37. addressBar.addActionListener((ActionEvent e) -> {
  38. if (addressBar.getText().equals(""))
  39. return;
  40. search();
  41. });
  42.  
  43. navigationPanel.add(addressBar, BorderLayout.CENTER);
  44.  
  45. JButton backButton = new JButton("<");
  46. backButton.addActionListener(new ActionListener() {
  47. @Override
  48. public void actionPerformed(ActionEvent b) {
  49. back();
  50. }
  51. });
  52.  
  53. JButton forwardButton = new JButton(">");
  54. forwardButton.addActionListener(new ActionListener() {
  55. @Override
  56. public void actionPerformed(ActionEvent f) {
  57. forward();
  58. }
  59. });
  60.  
  61. navigationPanel.add(backButton, BorderLayout.NORTH);
  62. navigationPanel.add(forwardButton, BorderLayout.SOUTH);
  63.  
  64. JButton goButton = new JButton("Suche");
  65. goButton.addActionListener(new ActionListener() {
  66. @Override
  67. public void actionPerformed(ActionEvent e) {
  68. search();
  69. }
  70. });
  71.  
  72. navigationPanel.add(goButton, BorderLayout.EAST);
  73.  
  74. webPane.setContentType("text/html");
  75. webPane.setEditable(false);
  76. webPane.addHyperlinkListener(this);
  77.  
  78. getContentPane().setLayout(new BorderLayout());
  79. getContentPane().add(navigationPanel, BorderLayout.NORTH);
  80. getContentPane().add(new JScrollPane(webPane), BorderLayout.CENTER);
  81. }
  82.  
  83. public void back()
  84. {
  85. if (historyBack.empty())
  86. {
  87. System.out.println("Cannot got back. Stack is empty");
  88. return;
  89. }
  90. URL poppedURL = historyBack.pop();
  91. historyForward.push(currentURL);
  92. showPage(poppedURL);
  93. }
  94.  
  95. public void forward()
  96. {
  97. if (historyForward.empty())
  98. {
  99. System.out.println("Cannot got forward. Stack is empty");
  100. return;
  101. }
  102. URL poppedURL = historyForward.pop();
  103. historyBack.push(currentURL);
  104. showPage(poppedURL);
  105. }
  106.  
  107. public void search()
  108. {
  109. URL verifiedURL = checkURL(addressBar.getText());
  110.  
  111. if (verifiedURL != null)
  112. {
  113. showPage(verifiedURL);
  114. historyForward.clear();
  115. }
  116. else
  117. {
  118. System.out.println("Fehler in der URL");
  119. }
  120. }
  121.  
  122. public void showPage(URL url)
  123. {
  124. if (currentURL!=null)
  125. historyBack.push(currentURL);
  126. currentURL = url;
  127. String content = null;
  128. URLConnection connection;
  129.  
  130. try
  131. {
  132. connection = url.openConnection();
  133.  
  134. boolean textSite = connection.getHeaderField("Content-Type").contains("text/html") || connection.getHeaderField("Content-Type").contains("text/plain");
  135.  
  136.  
  137. webPane.setContentType(connection.getHeaderField("Content-Type"));
  138.  
  139. if (textSite)
  140. {
  141. try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream())))
  142. {
  143. String inputLine;
  144. StringBuilder stringBuilder = new StringBuilder();
  145. while ((inputLine = bufferedReader.readLine()) != null)
  146. {
  147. stringBuilder.append(inputLine);
  148. }
  149.  
  150. content = stringBuilder.toString();
  151. }
  152. }
  153. }
  154. catch (Exception e)
  155. {
  156.  
  157. }
  158.  
  159. System.out.println(content);
  160.  
  161. webPane.setText(content);
  162. }
  163.  
  164. public URL checkURL(String urlString)
  165. {
  166. if (urlString.toLowerCase().startsWith("https://"))
  167. {
  168. return null;
  169. }
  170.  
  171. if (!urlString.toLowerCase().startsWith("http://"))
  172. {
  173. urlString = "http://" + urlString;
  174. }
  175.  
  176. URL validURL;
  177.  
  178. try
  179. {
  180. validURL = new URL(urlString);
  181. }
  182. catch (MalformedURLException e)
  183. {
  184. return null;
  185. }
  186.  
  187. return validURL;
  188. }
  189.  
  190. public void hyperlinkUpdate(HyperlinkEvent e)
  191. {
  192. HyperlinkEvent.EventType eventType = e.getEventType();
  193.  
  194. if (eventType == HyperlinkEvent.EventType.ACTIVATED)
  195. {
  196. if (e instanceof HTMLFrameHyperlinkEvent)
  197. {
  198. HTMLFrameHyperlinkEvent linkEvent = (HTMLFrameHyperlinkEvent) e;
  199. HTMLDocument document = (HTMLDocument) webPane.getDocument();
  200. document.processHTMLFrameHyperlinkEvent(linkEvent);
  201. }
  202. else
  203. {
  204. showPage(e.getURL());
  205. }
  206. }
  207. }
  208.  
  209. public static void main(String[] args)
  210. {
  211. SimpleBrowser browser = new SimpleBrowser();
  212. browser.setVisible(true);
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement