source: josm/trunk/src/org/openstreetmap/josm/tools/PlatformHook.java@ 12279

Last change on this file since 12279 was 12279, checked in by Don-vip, 7 years ago

sonar - squid:S3878 - Arrays should not be created for varargs parameters

  • Property svn:eol-style set to native
File size: 11.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Dimension;
7import java.awt.GraphicsEnvironment;
8import java.io.BufferedReader;
9import java.io.File;
10import java.io.IOException;
11import java.io.InputStreamReader;
12import java.nio.charset.StandardCharsets;
13import java.security.KeyStore;
14import java.security.KeyStoreException;
15import java.security.NoSuchAlgorithmException;
16import java.security.cert.CertificateException;
17import java.security.cert.X509Certificate;
18import java.text.DateFormat;
19import java.util.Date;
20import java.util.List;
21
22import javax.swing.JOptionPane;
23
24import org.openstreetmap.josm.Main;
25import org.openstreetmap.josm.gui.ExtendedDialog;
26import org.openstreetmap.josm.io.CertificateAmendment.CertAmend;
27import org.openstreetmap.josm.tools.date.DateUtils;
28
29/**
30 * This interface allows platform (operating system) dependent code
31 * to be bundled into self-contained classes.
32 * @since 1023
33 */
34public interface PlatformHook {
35
36 /**
37 * The preStartupHook will be called extremly early. It is
38 * guaranteed to be called before the GUI setup has started.
39 *
40 * Reason: On OSX we need to inform the Swing libraries
41 * that we want to be integrated with the OS before we setup our GUI.
42 */
43 default void preStartupHook() {
44 // Do nothing
45 }
46
47 /**
48 * The afterPrefStartupHook will be called early, but after
49 * the preferences have been loaded and basic processing of
50 * command line arguments is finished.
51 * It is guaranteed to be called before the GUI setup has started.
52 */
53 default void afterPrefStartupHook() {
54 // Do nothing
55 }
56
57 /**
58 * The startupHook will be called early, but after the GUI
59 * setup has started.
60 *
61 * Reason: On OSX we need to register some callbacks with the
62 * OS, so we'll receive events from the system menu.
63 */
64 default void startupHook() {
65 // Do nothing
66 }
67
68 /**
69 * The openURL hook will be used to open an URL in the
70 * default web browser.
71 * @param url The URL to open
72 * @throws IOException if any I/O error occurs
73 */
74 void openUrl(String url) throws IOException;
75
76 /**
77 * The initSystemShortcuts hook will be called by the
78 * Shortcut class after the modifier groups have been read
79 * from the config, but before any shortcuts are read from
80 * it or registered from within the application.
81 *
82 * Plese note that you are not allowed to register any
83 * shortuts from this hook, but only "systemCuts"!
84 *
85 * BTW: SystemCuts should be named "system:<whatever>",
86 * and it'd be best if sou'd recycle the names already used
87 * by the Windows and OSX hooks. Especially the later has
88 * really many of them.
89 *
90 * You should also register any and all shortcuts that the
91 * operation system handles itself to block JOSM from trying
92 * to use them---as that would just not work. Call setAutomatic
93 * on them to prevent the keyboard preferences from allowing the
94 * user to change them.
95 */
96 void initSystemShortcuts();
97
98 /**
99 * The makeTooltip hook will be called whenever a tooltip for
100 * a menu or button is created.
101 *
102 * Tooltips are usually not system dependent, unless the
103 * JVM is too dumb to provide correct names for all the keys.
104 *
105 * Some LAFs don't understand HTML, such as the OSX LAFs.
106 *
107 * @param name Tooltip text to display
108 * @param sc Shortcut associated (to display accelerator between parenthesis)
109 * @return Full tooltip text (name + accelerator)
110 */
111 default String makeTooltip(String name, Shortcut sc) {
112 StringBuilder result = new StringBuilder();
113 result.append("<html>").append(name);
114 if (sc != null && !sc.getKeyText().isEmpty()) {
115 result.append(" <font size='-2'>(")
116 .append(sc.getKeyText())
117 .append(")</font>");
118 }
119 return result.append("&nbsp;</html>").toString();
120 }
121
122 /**
123 * Returns the default LAF to be used on this platform to look almost as a native application.
124 * @return The default native LAF for this platform
125 */
126 String getDefaultStyle();
127
128 /**
129 * Determines if the platform allows full-screen.
130 * @return {@code true} if full screen is allowed, {@code false} otherwise
131 */
132 default boolean canFullscreen() {
133 return !GraphicsEnvironment.isHeadless() &&
134 GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().isFullScreenSupported();
135 }
136
137 /**
138 * Renames a file.
139 * @param from Source file
140 * @param to Target file
141 * @return {@code true} if the file has been renamed, {@code false} otherwise
142 */
143 default boolean rename(File from, File to) {
144 return from.renameTo(to);
145 }
146
147 /**
148 * Returns a detailed OS description (at least family + version).
149 * @return A detailed OS description.
150 * @since 5850
151 */
152 String getOSDescription();
153
154 /**
155 * Returns OS build number.
156 * @return OS build number.
157 * @since 12217
158 */
159 default String getOSBuildNumber() {
160 return "";
161 }
162
163 /**
164 * Setup system keystore to add JOSM HTTPS certificate (for remote control).
165 * @param entryAlias The entry alias to use
166 * @param trustedCert the JOSM certificate for localhost
167 * @return {@code true} if something has changed as a result of the call (certificate installation, etc.)
168 * @throws KeyStoreException in case of error
169 * @throws IOException in case of error
170 * @throws CertificateException in case of error
171 * @throws NoSuchAlgorithmException in case of error
172 * @since 7343
173 */
174 default boolean setupHttpsCertificate(String entryAlias, KeyStore.TrustedCertificateEntry trustedCert)
175 throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
176 // TODO setup HTTPS certificate on Unix and OS X systems
177 return false;
178 }
179
180 /**
181 * Returns the {@code X509Certificate} matching the given certificate amendment information.
182 * @param certAmend certificate amendment
183 * @return the {@code X509Certificate} matching the given certificate amendment information, or {@code null}
184 * @throws KeyStoreException in case of error
185 * @throws IOException in case of error
186 * @throws CertificateException in case of error
187 * @throws NoSuchAlgorithmException in case of error
188 * @since 11943
189 */
190 default X509Certificate getX509Certificate(CertAmend certAmend)
191 throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
192 return null;
193 }
194
195 /**
196 * Executes a native command and returns the first line of standard output.
197 * @param command array containing the command to call and its arguments.
198 * @return first stripped line of standard output
199 * @throws IOException if an I/O error occurs
200 * @since 12217
201 */
202 default String exec(String... command) throws IOException {
203 Process p = Runtime.getRuntime().exec(command);
204 try (BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8))) {
205 return Utils.strip(input.readLine());
206 }
207 }
208
209 /**
210 * Returns the platform-dependent default cache directory.
211 * @return the platform-dependent default cache directory
212 * @since 7829
213 */
214 File getDefaultCacheDirectory();
215
216 /**
217 * Returns the platform-dependent default preferences directory.
218 * @return the platform-dependent default preferences directory
219 * @since 7831
220 */
221 File getDefaultPrefDirectory();
222
223 /**
224 * Returns the platform-dependent default user data directory.
225 * @return the platform-dependent default user data directory
226 * @since 7834
227 */
228 File getDefaultUserDataDirectory();
229
230 /**
231 * Returns the list of platform-dependent default datum shifting directories for the PROJ.4 library.
232 * @return the list of platform-dependent default datum shifting directories for the PROJ.4 library
233 * @since 11642
234 */
235 List<File> getDefaultProj4NadshiftDirectories();
236
237 /**
238 * Determines if the JVM is OpenJDK-based.
239 * @return {@code true} if {@code java.home} contains "openjdk", {@code false} otherwise
240 * @since 12219
241 */
242 default boolean isOpenJDK() {
243 String javaHome = System.getProperty("java.home");
244 return javaHome != null && javaHome.contains("openjdk");
245 }
246
247 /**
248 * Asks user to update its version of Java.
249 * @param updVersion target update version
250 * @param url download URL
251 * @param major true for a migration towards a major version of Java (8:9), false otherwise
252 * @param eolDate the EOL/expiration date
253 * @since 12219
254 */
255 default void askUpdateJava(String updVersion, String url, String eolDate, boolean major) {
256 ExtendedDialog ed = new ExtendedDialog(
257 Main.parent,
258 tr("Outdated Java version"),
259 tr("OK"), tr("Update Java"), tr("Cancel"));
260 // Check if the dialog has not already been permanently hidden by user
261 if (!ed.toggleEnable("askUpdateJava"+updVersion).toggleCheckState()) {
262 ed.setButtonIcons("ok", "java", "cancel").setCancelButton(3);
263 ed.setMinimumSize(new Dimension(480, 300));
264 ed.setIcon(JOptionPane.WARNING_MESSAGE);
265 StringBuilder content = new StringBuilder(tr("You are running version {0} of Java.",
266 "<b>"+System.getProperty("java.version")+"</b>")).append("<br><br>");
267 if ("Sun Microsystems Inc.".equals(System.getProperty("java.vendor")) && !isOpenJDK()) {
268 content.append("<b>").append(tr("This version is no longer supported by {0} since {1} and is not recommended for use.",
269 "Oracle", eolDate)).append("</b><br><br>");
270 }
271 content.append("<b>")
272 .append(major ?
273 tr("JOSM will soon stop working with this version; we highly recommend you to update to Java {0}.", updVersion) :
274 tr("You may face critical Java bugs; we highly recommend you to update to Java {0}.", updVersion))
275 .append("</b><br><br>")
276 .append(tr("Would you like to update now ?"));
277 ed.setContent(content.toString());
278
279 if (ed.showDialog().getValue() == 2) {
280 try {
281 openUrl(url);
282 } catch (IOException e) {
283 Main.warn(e);
284 }
285 }
286 }
287 }
288
289 /**
290 * Checks if the running version of Java has expired, proposes to user to update it if needed.
291 * @since 12219
292 */
293 default void checkExpiredJava() {
294 Date expiration = Utils.getJavaExpirationDate();
295 if (expiration != null && expiration.before(new Date())) {
296 String version = Utils.getJavaLatestVersion();
297 askUpdateJava(version != null ? version : "latest",
298 Main.pref.get("java.update.url", "https://www.java.com/download"),
299 DateUtils.getDateFormat(DateFormat.MEDIUM).format(expiration), false);
300 }
301 }
302}
Note: See TracBrowser for help on using the repository browser.