Debian Codegraph @

netx/net/sourceforge/jnlp/runtime/html/browser/LinksPanel.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.Color;
41 import java.awt.Desktop;
42 import java.awt.GridLayout;
43 import java.awt.Toolkit;
44 import java.awt.datatransfer.Clipboard;
45 import java.awt.datatransfer.StringSelection;
46 import java.awt.event.ActionEvent;
47 import java.awt.event.ActionListener;
48 import java.net.URI;
49 import java.net.URL;
50 import java.util.ArrayList;
51 import java.util.HashSet;
52 import java.util.List;
53 import java.util.Set;
54 import java.util.regex.Matcher;
55 import java.util.regex.Pattern;
56 import javax.swing.JButton;
57 import javax.swing.JLabel;
58 import javax.swing.JOptionPane;
59 import javax.swing.JPanel;
60 import net.sourceforge.jnlp.runtime.JNLPRuntime;
61 import net.sourceforge.jnlp.runtime.Translator;
62 import net.sourceforge.jnlp.util.UrlUtils;
63 import net.sourceforge.jnlp.util.logging.OutputController;
64
65 public class LinksPanel extends JPanel implements DocumentChangededListener {
66
67 private URL baseUrl;
68
69 private final Set<String> added = new HashSet<>();
70 private HtmlBrowserPanel browser;
71
72 public void setInternalBrowser(HtmlBrowserPanel browser) {
73 this.browser = browser;
74 }
75
76 private static int counter = 0;
77
78 private class RowHolder extends JPanel {
79
80 //shoudl go from text state
81 private final String url;
82
83 public RowHolder(final String urll) {
84 super(new BorderLayout());
85 counter++;
86 Color bg = this.getBackground();
87 if (counter % 2 == 1) {
88 bg = new Color(Math.max(0, bg.getRed() - 20), Math.min(255, bg.getGreen() + 20), Math.max(0, bg.getBlue() - 20));
89 }
90 this.url = urll;
91 JLabel l = new JLabel(url);
92 l.setBackground(bg);
93 this.add(l);
94 JPanel buttonsPanel = new JPanel(new GridLayout(1, 0));
95 buttonsPanel.setBackground(bg);
96 JButton b1 = new JButton(Translator.R("BrowserOpenExternal"));
97 b1.addActionListener(new ActionListener() {
98 @Override
99 public void actionPerformed(ActionEvent e) {
100 try {
101 Desktop.getDesktop().browse(new URI(url));
102 } catch (Exception ex) {
103 OutputController.getLogger().log(ex);
104 if (!JNLPRuntime.isHeadless()) {
105 JOptionPane.showMessageDialog(null, ex);
106 }
107 }
108 }
109 });
110 buttonsPanel.add(b1);
111 //if (browser != null) {
112 JButton b2 = new JButton(Translator.R("BrowserOpeninternal"));
113 b2.addActionListener(new ActionListener() {
114 @Override
115 public void actionPerformed(ActionEvent e) {
116 browser.gotoUrl(url);
117 }
118 });
119 buttonsPanel.add(b2);
120 //}
121 JButton b3 = new JButton(Translator.R("BrowserCopyUrlToClip"));
122 b3.addActionListener(new ActionListener() {
123 @Override
124 public void actionPerformed(ActionEvent e) {
125 StringSelection selection = new StringSelection(url);
126 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
127 clipboard.setContents(selection, selection);
128 }
129 });
130 buttonsPanel.add(b3);
131 this.setBackground(bg);
132 this.add(buttonsPanel, BorderLayout.EAST);
133
134 }
135
136 }
137
138 public LinksPanel() {
139 super(new GridLayout(0, 1));
140 addClearButton();
141 }
142
143 private void addClearButton() {
144
145 JButton clear = new JButton(Translator.R("BrowserClearAll"));
146 clear.addActionListener(new ActionListener() {
147 @Override
148 public void actionPerformed(ActionEvent e) {
149 LinksPanel.this.removeAll();
150 added.clear();
151 addClearButton();
152 }
153 });
154 this.add(clear);
155 }
156
157 public void setBaseUrl(URL baseUrl) {
158 this.baseUrl = baseUrl;
159 }
160
161 public static final String URL_REGEX = "\\(?\\b(https://|http://|www[.])[-A-Za-z0-9+&;@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&amp;@#/%=~_()|]";
162 public static final Pattern URL_REGEX_PATTERN = Pattern.compile(URL_REGEX);
163
164 public List<String> pullAbsoluteLinks(String text) {
165 List<String> links = new ArrayList<>();
166 Matcher m = URL_REGEX_PATTERN.matcher(text);
167 while (m.find()) {
168 String urlStr = m.group();
169 if (urlStr.startsWith("(") && urlStr.endsWith(")")) {
170 urlStr = urlStr.substring(1, urlStr.length() - 1);
171 }
172 links.add(urlStr);
173 }
174 return links;
175 }
176
177 private static final String HTML_QUOTING_REGEX = "(\"|')";
178 public static final String HREF_REGEX_START = "(?i)href\\s*=\\s*" + HTML_QUOTING_REGEX;
179 private static final String HREF_REGEX_END = HTML_QUOTING_REGEX;
180 public static final String HREF_REGEX = HREF_REGEX_START + ".*?" + HREF_REGEX_END;
181 public static final Pattern HREF_REGEX_PATERN = Pattern.compile(HREF_REGEX);
182
183 public List<String> pullHrefs(String text) {
184 List<String> links = new ArrayList<>();
185
186 Matcher m = HREF_REGEX_PATERN.matcher(text);
187 while (m.find()) {
188 String urlStr = m.group();
189 urlStr = urlStr.replaceFirst(HREF_REGEX_START, "");
190 urlStr = urlStr.substring(0, urlStr.length() - 1);
191 links.add(urlStr);
192 }
193 return links;
194 }
195
196 public void parseAndAdd(String currentSource) {
197 List<String> l = pullAbsoluteLinks(currentSource);
198 for (String string : l) {
199 addLink(string);
200 }
201 l = pullHrefs(currentSource);
202 for (String string : l) {
203 if (added.add(string)) {
204 //not added => relative url
205 addLink(absolutize(string));
206 }
207 }
208 }
209
210 private String absolutize(String string) {
211 if (baseUrl == null) {
212 return string;
213 }
214 return UrlUtils.ensureSlashTail(UrlUtils.removeFileName(baseUrl)).toExternalForm() + string;
215 }
216
217 @Override
218 public void documentChangeded(String current) {
219 parseAndAdd(current);
220 }
221
222 private void addLink(String string) {
223 OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, string);
224 if (added.add(string)) {
225 this.add(new RowHolder(string));
226 }
227 }
228
229 @Override
230 public void addressChangeded(String url) {
231 addLink(url);
232 }
233
234 @Override
235 public void addressChangeded(URL url) {
236 if (url != null) {
237 baseUrl = url;
238 addLink(url.toExternalForm());
239 }
240 }
241
242 public List<String> getAllUrls() {
243 return new ArrayList<>(added);
244 }
245
246 }