001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.BorderLayout; 007import java.awt.EventQueue; 008import java.awt.Graphics; 009import java.awt.Point; 010import java.awt.event.MouseMotionListener; 011import java.awt.event.MouseEvent; 012import java.awt.event.MouseListener; 013import java.io.IOException; 014import java.nio.charset.StandardCharsets; 015import java.util.regex.Matcher; 016import java.util.regex.Pattern; 017 018import javax.swing.JComponent; 019import javax.swing.JPanel; 020import javax.swing.JScrollPane; 021import javax.swing.SwingUtilities; 022import javax.swing.Timer; 023import javax.swing.border.EmptyBorder; 024import javax.swing.event.HyperlinkEvent; 025import javax.swing.event.HyperlinkListener; 026import javax.swing.event.MouseInputListener; 027 028import org.openstreetmap.josm.actions.DownloadAction; 029import org.openstreetmap.josm.actions.DownloadPrimitiveAction; 030import org.openstreetmap.josm.actions.HistoryInfoAction; 031import org.openstreetmap.josm.data.Version; 032import org.openstreetmap.josm.gui.animation.AnimationExtension; 033import org.openstreetmap.josm.gui.animation.AnimationExtensionManager; 034import org.openstreetmap.josm.gui.datatransfer.OpenTransferHandler; 035import org.openstreetmap.josm.gui.dialogs.MenuItemSearchDialog; 036import org.openstreetmap.josm.gui.preferences.server.ProxyPreference; 037import org.openstreetmap.josm.gui.preferences.server.ProxyPreferenceListener; 038import org.openstreetmap.josm.gui.widgets.JosmEditorPane; 039import org.openstreetmap.josm.io.CacheCustomContent; 040import org.openstreetmap.josm.io.NetworkManager; 041import org.openstreetmap.josm.io.OnlineResource; 042import org.openstreetmap.josm.spi.preferences.Config; 043import org.openstreetmap.josm.tools.ImageProvider; 044import org.openstreetmap.josm.tools.ImageProvider.ImageSizes; 045import org.openstreetmap.josm.tools.LanguageInfo; 046import org.openstreetmap.josm.tools.Logging; 047import org.openstreetmap.josm.tools.OpenBrowser; 048import org.openstreetmap.josm.tools.Utils; 049import org.openstreetmap.josm.tools.WikiReader; 050 051/** 052 * Panel that fills the main part of the program window when JOSM has just started. 053 * 054 * It downloads and displays the so called <em>message of the day</em>, which 055 * contains news about recent major changes, warning in case of outdated versions, etc. 056 */ 057public final class GettingStarted extends JPanel implements ProxyPreferenceListener, MouseInputListener { 058 059 private final LinkGeneral lg; 060 private String content = ""; 061 private boolean contentInitialized; 062 private final Timer timer = new Timer(50, e -> repaint()); 063 064 private static final String STYLE = "<style type=\"text/css\">\n" 065 + "body {font-family: sans-serif; font-weight: bold; }\n" 066 + "h1 {text-align: center; }\n" 067 + "a {color: " + JosmEditorPane.getLinkColor() + "; }\n" 068 + ".icon {font-size: 0; }\n" 069 + "</style>\n"; 070 071 /** 072 * A subclass of {@link JosmEditorPane} which replaces links with something clickable (uses {@link OpenBrowser}) 073 * @since 623 074 */ 075 public static class LinkGeneral extends JosmEditorPane implements HyperlinkListener { 076 077 /** 078 * Constructs a new {@code LinkGeneral} with the given HTML text 079 * @param text The text to display 080 */ 081 public LinkGeneral(String text) { 082 setContentType("text/html"); 083 setText(text); 084 setEditable(false); 085 setOpaque(false); 086 addHyperlinkListener(this); 087 adaptForNimbus(this); 088 } 089 090 @Override 091 public void hyperlinkUpdate(HyperlinkEvent e) { 092 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 093 OpenBrowser.displayUrl(e.getDescription()); 094 } 095 } 096 } 097 098 /** 099 * Grabs current MOTD from cache or webpage and parses it. 100 */ 101 static class MotdContent extends CacheCustomContent<IOException> { 102 MotdContent() { 103 super("motd.html", CacheCustomContent.INTERVAL_DAILY); 104 } 105 106 private final int myVersion = Version.getInstance().getVersion(); 107 private final String myJava = Utils.getSystemProperty("java.version"); 108 private final String myLang = LanguageInfo.getWikiLanguagePrefix(); 109 110 /** 111 * This function gets executed whenever the cached files need updating 112 * @see org.openstreetmap.josm.io.CacheCustomContent#updateData() 113 */ 114 @Override 115 protected byte[] updateData() throws IOException { 116 String motd = new WikiReader().readLang("StartupPage"); 117 // Save this to prefs in case JOSM is updated so MOTD can be refreshed 118 Config.getPref().putInt("cache.motd.html.version", myVersion); 119 Config.getPref().put("cache.motd.html.java", myJava); 120 Config.getPref().put("cache.motd.html.lang", myLang); 121 return motd.getBytes(StandardCharsets.UTF_8); 122 } 123 124 @Override 125 protected boolean isOffline() { 126 return NetworkManager.isOffline(OnlineResource.JOSM_WEBSITE); 127 } 128 129 /** 130 * Additionally check if JOSM has been updated and refresh MOTD 131 */ 132 @Override 133 protected boolean isCacheValid() { 134 // We assume a default of myVersion because it only kicks in in two cases: 135 // 1. Not yet written - but so isn't the interval variable, so it gets updated anyway 136 // 2. Cannot be written (e.g. while developing). Obviously we don't want to update 137 // every time because of something we can't read. 138 return (Config.getPref().getInt("cache.motd.html.version", -999) == myVersion) 139 && Config.getPref().get("cache.motd.html.java").equals(myJava) 140 && Config.getPref().get("cache.motd.html.lang").equals(myLang); 141 } 142 } 143 144 /** 145 * Initializes getting the MOTD as well as enabling the FileDrop Listener. Displays a message 146 * while the MOTD is downloading. 147 */ 148 public GettingStarted() { 149 super(new BorderLayout()); 150 lg = new LinkGeneral("<html>" + STYLE + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor") 151 + "</h1><h2 align=\"center\">" + tr("Downloading \"Message of the day\"") + "</h2></html>"); 152 // clear the build-in command ctrl+shift+O, ctrl+space, ctrl+H because it is used as shortcut in JOSM 153 lg.getInputMap(JComponent.WHEN_FOCUSED).put(DownloadAction.SHORTCUT.getKeyStroke(), "none"); 154 lg.getInputMap(JComponent.WHEN_FOCUSED).put(DownloadPrimitiveAction.SHORTCUT.getKeyStroke(), "none"); 155 lg.getInputMap(JComponent.WHEN_FOCUSED).put(MenuItemSearchDialog.Action.SHORTCUT.getKeyStroke(), "none"); 156 lg.getInputMap(JComponent.WHEN_FOCUSED).put(HistoryInfoAction.SHORTCUT.getKeyStroke(), "none"); 157 // Don't use the text handler drag and drop behavior. See #13196/#20528. 158 lg.setDropTarget(null); 159 160 JScrollPane scroller = new JScrollPane(lg); 161 scroller.setViewportBorder(new EmptyBorder(10, 100, 10, 100)); 162 add(scroller, BorderLayout.CENTER); 163 164 scroller.addMouseMotionListener(this); 165 scroller.addMouseListener(this); 166 lg.addMouseMotionListener(this); 167 lg.addMouseListener(this); 168 169 getMOTD(); 170 171 setTransferHandler(new OpenTransferHandler()); 172 } 173 174 @Override 175 public void addNotify() { 176 if (AnimationExtensionManager.isExtensionEnabled()) { 177 timer.start(); 178 } 179 super.addNotify(); 180 } 181 182 @Override 183 public void removeNotify() { 184 if (timer.isRunning()) { 185 timer.stop(); 186 } 187 super.removeNotify(); 188 } 189 190 @Override 191 public void mouseMoved(MouseEvent e) { 192 final AnimationExtension extension = AnimationExtensionManager.getExtension(); 193 if (extension instanceof MouseMotionListener) { 194 ((MouseMotionListener) extension).mouseMoved(e); 195 } 196 } 197 198 @Override 199 public void mouseDragged(MouseEvent e) { 200 // Ignored 201 } 202 203 @Override 204 public void mousePressed(MouseEvent e) { 205 final AnimationExtension extension = AnimationExtensionManager.getExtension(); 206 if (extension instanceof MouseListener) { 207 ((MouseListener) extension).mousePressed(e); 208 } 209 } 210 211 @Override 212 public void mouseEntered(MouseEvent e) { 213 // Ignored 214 } 215 216 @Override 217 public void mouseExited(MouseEvent e) { 218 // Ignored 219 } 220 221 @Override 222 public void mouseReleased(MouseEvent e) { 223 final AnimationExtension extension = AnimationExtensionManager.getExtension(); 224 if (extension instanceof MouseListener) { 225 ((MouseListener) extension).mouseReleased(e); 226 } 227 } 228 229 @Override 230 public void mouseClicked(MouseEvent e) { 231 // Ignored 232 } 233 234 @Override 235 public void paint(Graphics g) { 236 super.paint(g); 237 if (isShowing()) { 238 Point p = new Point(0, 0); 239 SwingUtilities.convertPointToScreen(p, this); 240 AnimationExtensionManager.getExtension().adjustForSize(getWidth(), getHeight(), p.x, p.y); 241 AnimationExtensionManager.getExtension().animate(); 242 AnimationExtensionManager.getExtension().paint(g); 243 } 244 } 245 246 private void getMOTD() { 247 // Asynchronously get MOTD to speed-up JOSM startup 248 Thread t = new Thread((Runnable) () -> { 249 if (!contentInitialized && Config.getPref().getBoolean("help.displaymotd", true)) { 250 try { 251 content = new MotdContent().updateIfRequiredString(); 252 contentInitialized = true; 253 ProxyPreference.removeProxyPreferenceListener(this); 254 } catch (IOException ex) { 255 Logging.log(Logging.LEVEL_WARN, tr("Failed to read MOTD. Exception was: {0}", ex.toString()), ex); 256 content = "<html>" + STYLE + "<h1>" + "JOSM - " + tr("Java OpenStreetMap Editor") 257 + "</h1>\n<h2 align=\"center\">(" + tr("Message of the day not available") + ")</h2></html>"; 258 // In case of MOTD not loaded because of proxy error, listen to preference changes to retry after update 259 ProxyPreference.addProxyPreferenceListener(this); 260 } 261 } 262 263 if (content != null) { 264 EventQueue.invokeLater(() -> lg.setText(fixImageLinks(content))); 265 } 266 }, "MOTD-Loader"); 267 t.setDaemon(true); 268 t.start(); 269 } 270 271 static String fixImageLinks(String s) { 272 Matcher m = Pattern.compile("src=\"/browser/trunk/resources/images/(.*?)\\.(png|svg)\\?format=raw\"").matcher(s); 273 StringBuffer sb = new StringBuffer(); 274 while (m.find()) { 275 String im = m.group(1); 276 String u = new ImageProvider(im).setMaxSize(ImageSizes.HTMLINLINE).getDataURL(); 277 if (u != null) { 278 m.appendReplacement(sb, Matcher.quoteReplacement("src=\"" + u + '\"')); 279 } 280 } 281 m.appendTail(sb); 282 return sb.toString(); 283 } 284 285 @Override 286 public void proxyPreferenceChanged() { 287 getMOTD(); 288 } 289}