Gridarta Editor
HtmlPane.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.dialog.help;
21 
22 import java.awt.Component;
23 import java.awt.Cursor;
24 import java.io.File;
25 import java.io.IOException;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 import javax.swing.JEditorPane;
31 import javax.swing.JScrollPane;
32 import javax.swing.JViewport;
33 import javax.swing.SwingUtilities;
34 import javax.swing.event.HyperlinkEvent;
35 import javax.swing.event.HyperlinkEvent.EventType;
36 import javax.swing.event.HyperlinkListener;
37 import javax.swing.text.Document;
39 import org.jetbrains.annotations.NotNull;
40 import org.jetbrains.annotations.Nullable;
41 
46 class HtmlPane extends JScrollPane implements HyperlinkListener {
47 
51  @NotNull
52  private static final Logger LOG = Logger.getLogger("HtmlPane.class");
53 
57  private static final long serialVersionUID = 1L;
58 
62  @NotNull
63  private final JEditorPane html;
64 
70  HtmlPane(@NotNull final String fileName) {
71  try {
72  // first looking for the html file in extracted form
73  final File file = new File(CommonConstants.HELP_DIR, fileName);
74  if (file.exists()) {
75  // file exists in expected directory
76  final String s = "file:" + file.getAbsolutePath();
77  html = new JEditorPane(s);
78  } else {
79  // file missing, so let's look if we can get it from the jar
80  final URL url1 = ClassLoader.getSystemResource(CommonConstants.HELP_DIR.replace('\\', '/') + '/' + fileName);
81 
82  if (url1 == null) {
83  // let's try it again without first directory
84  LOG.info("trying: HelpFiles/" + fileName);
85  final URL url2 = ClassLoader.getSystemResource("HelpFiles/" + fileName);
86  if (url2 == null) {
87  LOG.info("Failed to open help file '" + fileName + "'!");
88  throw new RuntimeException("failed to open help file: " + fileName); // FIXME
89  }
90  html = new JEditorPane(url2);
91  } else {
92  html = new JEditorPane(url1);
93  }
94  }
95 
96  html.setEditable(false);
97  html.addHyperlinkListener(this);
98  final JViewport vp = getViewport();
99 
100  // under windows, the content of the panel get destroyed after scrolling
101  // this will avoid this problem!
102  // but it will make the scrolling slower
103  // so test this!
104  // FIXME: This problem should be fixed in the latest versions of the JRE.
105  getViewport().putClientProperty("EnableWindowBlit", Boolean.TRUE);
106  vp.setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
107 
108  vp.add(html);
109  setFocusable(true);
110  requestFocus();
111  setAutoscrolls(true);
112  } catch (final NullPointerException e) {
113  // failed to open the html file
114  throw new RuntimeException(e); // FIXME
115  } catch (final MalformedURLException e) {
116  LOG.log(Level.WARNING, "Malformed URL: %s", e);
117  throw new RuntimeException(e); // FIXME
118  } catch (final IOException e) {
119  LOG.log(Level.WARNING, "IOException: %s", e);
120  throw new RuntimeException(e); // FIXME
121  }
122  }
123 
130  HtmlPane(@NotNull final String type, @NotNull final String text) {
131  // open new JEditorPane
132  html = new JEditorPane(type, text);
133  html.setEditable(false);
134  html.addHyperlinkListener(this);
135  final JViewport vp = getViewport();
136 
137  // under windows, the content of the panel get destroyed after scrolling
138  // this will avoid this problem!
139  // but it will make the scrolling slower
140  // so test this!
141  // XXX The main issue probably is extending JScrollPane (Cher)
142  getViewport().putClientProperty("EnableWindowBlit", Boolean.TRUE);
143  vp.setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
144 
145  vp.add(html);
146  //vp.setView(html);
147  //vp.setViewPosition(new Point(0, 0));
148 
149  setAutoscrolls(true);
150  }
151 
156  @Override
157  public void hyperlinkUpdate(@NotNull final HyperlinkEvent e) {
158  if (e.getEventType().equals(EventType.ACTIVATED)) {
159  linkActivated(e.getURL());
160  }
161  }
162 
172  private void linkActivated(@NotNull final URL u) {
173  final Cursor cursor = html.getCursor();
174  final Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
175  html.setCursor(waitCursor);
176  SwingUtilities.invokeLater(new PageLoader(u, cursor));
177  }
178 
182  private class PageLoader implements Runnable {
183 
187  @Nullable
188  private URL url;
189 
193  @NotNull
194  private final Cursor cursor;
195 
201  private PageLoader(@NotNull final URL url, @NotNull final Cursor cursor) {
202  this.url = url;
203  this.cursor = cursor;
204  }
205 
206  @Override
207  public void run() {
208  if (url == null) {
209  // restore the original cursor
210  html.setCursor(cursor);
211 
212  // PENDING(prinz) remove this hack when
213  // automatic validation is activated.
214  final Component parent = html.getParent();
215  parent.repaint();
216  } else {
217  final Document doc = html.getDocument();
218  try {
219  html.setPage(url);
220  } catch (final IOException ioe) {
221  html.setDocument(doc);
222  getToolkit().beep();
223  // TODO not just beep but display an error message as well.
224  } finally {
225  // schedule the cursor to revert after
226  // the paint has happened.
227  url = null;
228  SwingUtilities.invokeLater(this);
229  }
230  }
231  }
232 
233  }
234 
235 }
net.sf.gridarta.utils.CommonConstants.HELP_DIR
static final String HELP_DIR
The directory that contains all (html) help files.
Definition: CommonConstants.java:33
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.dialog.help.HtmlPane.PageLoader.url
URL url
URL to load.
Definition: HtmlPane.java:188
net.sf
net.sf.gridarta.gui.dialog.help.HtmlPane.PageLoader.PageLoader
PageLoader(@NotNull final URL url, @NotNull final Cursor cursor)
Creates a new instance.
Definition: HtmlPane.java:201
net.sf.gridarta.utils.CommonConstants
Class with constants used in Gridarta and derivates.
Definition: CommonConstants.java:28
net.sf.gridarta.gui.dialog.help.HtmlPane.PageLoader.run
void run()
Definition: HtmlPane.java:207
net.sf.gridarta.gui.dialog.help.HtmlPane.hyperlinkUpdate
void hyperlinkUpdate(@NotNull final HyperlinkEvent e)
Notification of a change relative to a hyperlink.
Definition: HtmlPane.java:157
net.sf.gridarta.gui.dialog.help.HtmlPane.html
final JEditorPane html
The JEditorPane that displays the html page.
Definition: HtmlPane.java:63
net.sf.gridarta.gui.dialog.help.HtmlPane.HtmlPane
HtmlPane(@NotNull final String type, @NotNull final String text)
Constructor to load the html-file <fileName> and display it's contents in this HtmlPane.
Definition: HtmlPane.java:130
net
net.sf.gridarta.gui.dialog.help.HtmlPane
Pane for displaying HTML.
Definition: HtmlPane.java:46
net.sf.gridarta.gui.dialog.help.HtmlPane.linkActivated
void linkActivated(@NotNull final URL u)
Follows the reference in an link.
Definition: HtmlPane.java:172
net.sf.gridarta.gui.dialog.help.HtmlPane.PageLoader.cursor
final Cursor cursor
Original cursor that should be restored once the document is loaded.
Definition: HtmlPane.java:194
net.sf.gridarta.gui.dialog.help.HtmlPane.HtmlPane
HtmlPane(@NotNull final String fileName)
Constructor to load the html-file <fileName> and display it's contents in this HtmlPane.
Definition: HtmlPane.java:70
net.sf.gridarta.gui.dialog.help.HtmlPane.LOG
static final Logger LOG
The Logger for printing log messages.
Definition: HtmlPane.java:52
net.sf.gridarta.gui.dialog.help.HtmlPane.PageLoader
Synchronous page loader, loads a page and handles the cursor.
Definition: HtmlPane.java:182
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.gui.dialog.help.HtmlPane.serialVersionUID
static final long serialVersionUID
Serial Version UID.
Definition: HtmlPane.java:57