Debian Codegraph @

netx/net/sourceforge/jnlp/runtime/html/browser/HtmlBrowserPanel.java

raw
1 /*
2 Copyright (C) 2012 Red Hat, Inc.
3
4 This file is part of IcedTea.
5
6 IcedTea is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 2.
9
10 IcedTea is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with IcedTea; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301 USA.
19
20 Linking this library statically or dynamically with other modules is
21 making a combined work based on this library. Thus, the terms and
22 conditions of the GNU General Public License cover the whole
23 combination.
24
25 As a special exception, the copyright holders of this library give you
26 permission to link this library with independent modules to produce an
27 executable, regardless of the license terms of these independent
28 modules, and to copy and distribute the resulting executable under
29 terms of your choice, provided that you also meet, for each linked
30 independent module, the terms and conditions of the license of that
31 module. An independent module is a module which is not derived from
32 or based on this library. If you modify this library, you may extend
33 this exception to your version of the library, but you are not
34 obligated to do so. If you do not wish to do so, delete this
35 exception statement from your version.
36 */
37 package net.sourceforge.jnlp.runtime.html.browser;
38
39 import java.awt.BorderLayout;
40 import java.awt.GridLayout;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.io.IOException;
44 import java.net.URL;
45 import java.util.ArrayList;
46 import java.util.List;
47 import javax.swing.JButton;
48 import javax.swing.JCheckBox;
49 import javax.swing.JComboBox;
50 import javax.swing.JEditorPane;
51 import javax.swing.JFrame;
52 import javax.swing.JMenuItem;
53 import javax.swing.JOptionPane;
54 import javax.swing.JPanel;
55 import javax.swing.JPopupMenu;
56 import javax.swing.JScrollPane;
57 import javax.swing.JTextField;
58 import javax.swing.JToggleButton;
59 import javax.swing.event.HyperlinkEvent;
60 import javax.swing.event.HyperlinkListener;
61 import javax.swing.text.html.HTMLDocument;
62 import net.sourceforge.jnlp.runtime.JNLPRuntime;
63 import net.sourceforge.jnlp.runtime.Translator;
64 import net.sourceforge.jnlp.util.UrlUtils;
65 import net.sourceforge.jnlp.util.logging.OutputController;
66 import java.nio.charset.Charset;
67 import java.nio.charset.StandardCharsets;
68
69 /**
70 * this calss intentioanlly NOT cache any content, but always load data. Its
71 * original usecase was to to implement http proxy logging. And there reloads
72 * really metters.
73 *
74 */
75 public class HtmlBrowserPanel extends JPanel {
76
77 private void fireDocumentChanged(String current) {
78 for (DocumentChangededListener documentChangededListener : documentChangededListeners) {
79 documentChangededListener.documentChangeded(current);
80 }
81 }
82
83 private void fireAddressChanged(String url) {
84 for (DocumentChangededListener documentChangededListener : documentChangededListeners) {
85 documentChangededListener.addressChangeded(url);
86 }
87 }
88
89 private void fireAddressChanged(URL url) {
90 for (DocumentChangededListener documentChangededListener : documentChangededListeners) {
91 documentChangededListener.addressChangeded(url);
92 }
93 }
94
95 public void addDocumentChangedListener(DocumentChangededListener i) {
96 documentChangededListeners.add(i);
97 }
98 List<DocumentChangededListener> documentChangededListeners = new ArrayList<>();
99
100 public URL getUrl() {
101 if (current == null) {
102 return null;
103 }
104 return current.url;
105
106 }
107
108 private static class State {
109
110 private static String removeMeta(String string) {
111 return string.replaceAll("(?i)<\\s*meta.*?>", ""); //any meta chars confuse 3.2 html jeditorpane heavily
112 }
113
114 final URL url;
115 final String source;
116 final String all;
117
118 private State(URL url, String all, String html) {
119 this.url = url;
120 this.all = all;
121 this.source = html;
122 }
123
124 private String getSource() {
125 if (all == null || all.trim().isEmpty()) {
126 return source;
127 }
128 return all;
129 }
130
131 private String getHtml() {
132 if (source == null || source.trim().isEmpty()) {
133 return removeMeta(all);
134 } else {
135 return removeMeta(source);
136 }
137 }
138 }
139
140 private class History {
141
142 private final List<URL> visited = new ArrayList();
143 private final List<URL> backed = new ArrayList();
144
145 private URL back() {
146 if (visited.isEmpty()) {
147 return null;
148 }
149 URL q = visited.get(0);
150 visited.remove(0);
151 put(current, backed);
152 URL u = loadCatched(q); //override current
153 createTooltips();
154 return q;
155 }
156
157 private URL fwd() {
158 if (backed.isEmpty()) {
159 return null;
160 }
161 URL q = backed.get(0);
162 backed.remove(0);
163 put(current, visited);
164 URL u = loadCatched(q); //override current
165 createTooltips();
166 return q;
167 }
168
169 private void put(URL url, List<URL> where) {
170 if (url != null) {
171 if (where.isEmpty()) {
172 where.add(0, url);
173 } else if (!where.get(0).equals(url)) {
174 where.add(0, url);
175 }
176 }
177
178 }
179
180 private void put(State current, List<URL> where) {
181 if (current != null) {
182 put(current.url, where);
183 }
184 }
185
186 private void visit(String text) {
187 put(current, visited);
188 URL u = loadCatched(text); //override current
189 createTooltips();
190 }
191
192 private void visit(URL url) {
193 put(current, visited);
194 URL u = loadCatched(url); //override current
195 createTooltips();
196 }
197
198 private void createTooltips() {
199 createTooltip(visited, backButton, Translator.R("BUTback"));
200 createTooltip(backed, fwdButton, Translator.R("BUTforward"));
201 }
202
203 private void createTooltip(List<URL> visited, JButton b, String title) {
204 StringBuilder sb = new StringBuilder("<html><h3>" + title + "</h3><ol>");
205 JPopupMenu p = new JPopupMenu();
206 for (final URL url : visited) {
207 sb.append("<li>").append(url.toExternalForm()).append("</li>");
208 JMenuItem jim = new JMenuItem(url.toExternalForm());
209 jim.addActionListener(new ActionListener() {
210
211 @Override
212 public void actionPerformed(ActionEvent e) {
213 visit(url);
214 }
215 });
216 p.add(jim);
217 }
218 b.removeAll();;
219 b.setComponentPopupMenu(p);
220 sb.append("</ol></html>");
221 b.setToolTipText(sb.toString());
222
223 }
224
225 }
226
227 private final JPanel customUrl = new JPanel(new BorderLayout());
228 private final JPanel mainButtons = new JPanel(new GridLayout(1, 0));
229 private final JPanel tools = new JPanel(new BorderLayout());
230 private final JTextField goTo = new JTextField();
231 private final JButton gotoButton = new JButton(Translator.R("BrowserGoTo"));
232 private final JButton backButton = new JButton("<<<");
233 private final JButton reloadButton = new JButton(Translator.R("BUTreload"));
234 private final JButton fwdButton = new JButton(">>>");
235 private final JToggleButton viewSourceButton = new JToggleButton(Translator.R("BrowserSource"));
236 private final JCheckBox socketCheckbox = new JCheckBox(Translator.R("BrowserSocket"));
237 private final JComboBox<Charset> encodingBox = new JComboBox<>(new Charset[]{
238 null,
239 Charset.forName("US-ASCII"),
240 Charset.forName("UTF-8"),
241 Charset.forName("ISO-8859-1"),
242 Charset.forName("UTF-16"),
243 Charset.forName("UTF-16BE"),
244 Charset.forName("UTF-16LE")
245
246 });
247
248 private static final String TEXTPLAIN = "text/plain";
249 private static final String TEXTHTML = "text/html";
250
251 //because of various relaodings, those are always recreated
252 private JEditorPane currentHtml;
253 private JScrollPane currentScrollHtml;
254
255 private State current;
256 private boolean source = false;
257 private boolean useSocket = false;
258 private final History history = new History();
259
260 public HtmlBrowserPanel(final URL url) {
261 this(url, false);
262 }
263
264 public HtmlBrowserPanel(final String url) {
265 this(url, false);
266 }
267
268 public HtmlBrowserPanel(final URL url, boolean socket) {
269 super(new BorderLayout());
270 setUseSocket(socket);
271 crateGui();
272 URL u = loadCatched(url);
273 }
274
275 public HtmlBrowserPanel(final String url, boolean socket) {
276 super(new BorderLayout());
277 setUseSocket(socket);
278 crateGui();
279 URL u = loadCatched(url);
280 }
281
282 private URL loadCatched(String url) {
283 try {
284 URL u = new URL(url);
285 load(u);
286 return u;
287 } catch (Exception ex) {
288 OutputController.getLogger().log(ex);
289 if (!JNLPRuntime.isHeadless()) {
290 JOptionPane.showMessageDialog(null, ex);
291 }
292 return null;
293 }
294 }
295
296 private URL loadCatched(URL url) {
297 try {
298 load(url);
299 return url;
300 } catch (Exception ex) {
301 OutputController.getLogger().log(ex);
302 if (!JNLPRuntime.isHeadless()) {
303 JOptionPane.showMessageDialog(null, ex);
304 }
305 return null;
306 }
307 }
308
309 private void load(URL url) throws IOException {
310 goTo.setText(url.toExternalForm());
311 fireAddressChanged(url);
312 fireAddressChanged(url.toExternalForm());
313 //url conenction is checking response code.It can be used as 511 is unimplemented
314 String[] result;
315 if (isUseSocket()) {
316 OutputController.getLogger().log("Using socket connection");
317 Charset ch = (Charset)(encodingBox.getSelectedItem());
318 if (ch == null) {
319 result = UrlUtils.loadUrlWithInvalidHeader(url);
320 } else {
321 result = UrlUtils.loadUrlWithInvalidHeader(url, ch);
322 }
323 } else {
324 OutputController.getLogger().log("Using URLconnection");
325 String s;
326 Charset ch = (Charset)(encodingBox.getSelectedItem());
327 if (ch == null) {
328 s = UrlUtils.loadUrl(url);
329 } else {
330 s = UrlUtils.loadUrl(url, ch);
331 }
332 result = new String[]{s, s, s};
333 }
334 OutputController.getLogger().log(result[0]);
335 if (result[2].trim().isEmpty()) {
336 result[2] = result[1];
337 }
338 current = new State(url, result[0], result[2]);
339 if (source) {
340 currentHtml = new JEditorPane(TEXTPLAIN, current.getSource());
341 if (encodingBox.getSelectedItem()!=null){
342 currentHtml.getDocument().putProperty("IgnoreCharsetDirective", Boolean.TRUE);
343 }
344 } else {
345 currentHtml = new JEditorPane(TEXTHTML, current.getHtml());
346 if (encodingBox.getSelectedItem()!=null){
347 currentHtml.getDocument().putProperty("IgnoreCharsetDirective", Boolean.TRUE);
348 }
349 ((HTMLDocument) currentHtml.getDocument()).setBase(current.url);
350 }
351 fireDocumentChanged(getCurrentSource());
352 currentHtml.setEditable(false);//oherwise hyperlinks dont work
353 currentHtml.addHyperlinkListener(new HyperlinkListener() {
354 @Override
355 public void hyperlinkUpdate(HyperlinkEvent e) {
356 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
357 URL u = e.getURL();
358 history.visit(u);
359 }
360 }
361 });
362 if (currentScrollHtml != null) {
363 this.remove(currentScrollHtml);
364 }
365 currentScrollHtml = new JScrollPane(currentHtml);
366 this.add(currentScrollHtml);
367
368 this.validate();
369 }
370
371 private void crateGui() {
372
373 mainButtons.add(backButton);
374 backButton.setToolTipText(Translator.R("BUTback"));
375 mainButtons.add(reloadButton);
376 mainButtons.add(fwdButton);
377 fwdButton.setToolTipText(Translator.R("BUTforward"));
378 mainButtons.add(viewSourceButton);
379 mainButtons.add(socketCheckbox);
380 socketCheckbox.setSelected(isUseSocket());
381 socketCheckbox.setToolTipText(Translator.R("BrowserSocketHelp"));
382 customUrl.add(gotoButton, BorderLayout.WEST);
383 customUrl.add(goTo);
384 customUrl.add(encodingBox, BorderLayout.EAST);
385 tools.add(customUrl, BorderLayout.SOUTH);
386 tools.add(mainButtons, BorderLayout.NORTH);
387 gotoButton.addActionListener(new ActionListener() {
388 @Override
389 public void actionPerformed(ActionEvent e) {
390 gotoUrl(goTo.getText());
391 }
392 });
393 reloadButton.addActionListener(new ActionListener() {
394 @Override
395 public void actionPerformed(ActionEvent e) {
396 URL u = loadCatched(goTo.getText());
397 }
398 });
399 goTo.addActionListener(new ActionListener() {
400 @Override
401 public void actionPerformed(ActionEvent e) {
402 gotoUrl(goTo.getText());
403 }
404 });
405 backButton.addActionListener(new ActionListener() {
406 @Override
407 public void actionPerformed(ActionEvent e) {
408 URL u = history.back();
409
410 }
411 });
412 fwdButton.addActionListener(new ActionListener() {
413 @Override
414 public void actionPerformed(ActionEvent e) {
415 URL u = history.fwd();
416 }
417 });
418 socketCheckbox.addActionListener(new ActionListener() {
419 @Override
420 public void actionPerformed(ActionEvent e) {
421 setUseSocket(socketCheckbox.isSelected());
422 }
423 });
424 viewSourceButton.addActionListener(new ActionListener() {
425 @Override
426 public void actionPerformed(ActionEvent e) {
427 source = !source;
428 if (current == null || currentHtml == null) {
429 return;
430 }
431 showBySource();
432 }
433
434 });
435
436 this.add(tools, BorderLayout.NORTH);
437 }
438
439 private void showBySource() {
440 if (source) {
441 currentHtml.setContentType(TEXTPLAIN);
442 currentHtml.setText(current.getSource());
443 } else {
444 currentHtml.setContentType(TEXTHTML);
445 currentHtml.setText(current.getHtml());
446 ((HTMLDocument) currentHtml.getDocument()).setBase(current.url);
447 }
448 }
449
450 private void setUseSocket(boolean b) {
451 useSocket = b;
452 if (socketCheckbox != null) {
453 socketCheckbox.setSelected(b);
454 }
455 }
456
457 public boolean isUseSocket() {
458 return useSocket;
459 }
460
461 public static void warn() {
462 OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, Translator.R("BrowserWarningLine1"));
463 OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, Translator.R("BrowserWarningLine2"));
464 }
465
466 public static void showStandAloneWindow(String url, boolean socket) {
467 warn();
468 JFrame f = new JFrame();
469 f.add(new HtmlBrowserPanel(url, socket));
470 f.pack();
471 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
472 f.setVisible(true);
473 }
474
475 public String getCurrentSource() {
476 if (current == null) {
477 return "";
478 }
479 return current.getSource();
480 }
481
482 public void gotoUrl(String text) {
483 history.visit(text);
484 }
485
486 }