1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.util;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BasicStroke;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Container;
|
---|
9 | import java.awt.Dialog;
|
---|
10 | import java.awt.Dimension;
|
---|
11 | import java.awt.Font;
|
---|
12 | import java.awt.GraphicsEnvironment;
|
---|
13 | import java.awt.GridBagLayout;
|
---|
14 | import java.awt.Image;
|
---|
15 | import java.awt.Stroke;
|
---|
16 | import java.awt.Toolkit;
|
---|
17 | import java.awt.Window;
|
---|
18 | import java.awt.event.ActionListener;
|
---|
19 | import java.awt.event.HierarchyEvent;
|
---|
20 | import java.awt.event.HierarchyListener;
|
---|
21 | import java.awt.event.KeyEvent;
|
---|
22 | import java.awt.image.FilteredImageSource;
|
---|
23 | import java.lang.reflect.InvocationTargetException;
|
---|
24 | import java.util.Arrays;
|
---|
25 | import java.util.Enumeration;
|
---|
26 | import java.util.List;
|
---|
27 | import java.util.concurrent.Callable;
|
---|
28 | import java.util.concurrent.ExecutionException;
|
---|
29 | import java.util.concurrent.FutureTask;
|
---|
30 |
|
---|
31 | import javax.swing.GrayFilter;
|
---|
32 | import javax.swing.Icon;
|
---|
33 | import javax.swing.ImageIcon;
|
---|
34 | import javax.swing.JComponent;
|
---|
35 | import javax.swing.JLabel;
|
---|
36 | import javax.swing.JOptionPane;
|
---|
37 | import javax.swing.JPanel;
|
---|
38 | import javax.swing.JScrollPane;
|
---|
39 | import javax.swing.SwingUtilities;
|
---|
40 | import javax.swing.Timer;
|
---|
41 | import javax.swing.UIManager;
|
---|
42 | import javax.swing.plaf.FontUIResource;
|
---|
43 |
|
---|
44 | import org.openstreetmap.josm.Main;
|
---|
45 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
46 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
47 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
48 | import org.openstreetmap.josm.tools.GBC;
|
---|
49 | import org.openstreetmap.josm.tools.ImageOverlay;
|
---|
50 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
51 | import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
|
---|
52 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * basic gui utils
|
---|
56 | */
|
---|
57 | public final class GuiHelper {
|
---|
58 |
|
---|
59 | private GuiHelper() {
|
---|
60 | // Hide default constructor for utils classes
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * disable / enable a component and all its child components
|
---|
65 | */
|
---|
66 | public static void setEnabledRec(Container root, boolean enabled) {
|
---|
67 | root.setEnabled(enabled);
|
---|
68 | Component[] children = root.getComponents();
|
---|
69 | for (Component child : children) {
|
---|
70 | if (child instanceof Container) {
|
---|
71 | setEnabledRec((Container) child, enabled);
|
---|
72 | } else {
|
---|
73 | child.setEnabled(enabled);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public static void executeByMainWorkerInEDT(final Runnable task) {
|
---|
79 | Main.worker.submit(new Runnable() {
|
---|
80 | @Override
|
---|
81 | public void run() {
|
---|
82 | runInEDTAndWait(task);
|
---|
83 | }
|
---|
84 | });
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Executes asynchronously a runnable in
|
---|
89 | * <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html">Event Dispatch Thread</a>.
|
---|
90 | * @param task The runnable to execute
|
---|
91 | * @see SwingUtilities#invokeLater
|
---|
92 | */
|
---|
93 | public static void runInEDT(Runnable task) {
|
---|
94 | if (SwingUtilities.isEventDispatchThread()) {
|
---|
95 | task.run();
|
---|
96 | } else {
|
---|
97 | SwingUtilities.invokeLater(task);
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Executes synchronously a runnable in
|
---|
103 | * <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html">Event Dispatch Thread</a>.
|
---|
104 | * @param task The runnable to execute
|
---|
105 | * @see SwingUtilities#invokeAndWait
|
---|
106 | */
|
---|
107 | public static void runInEDTAndWait(Runnable task) {
|
---|
108 | if (SwingUtilities.isEventDispatchThread()) {
|
---|
109 | task.run();
|
---|
110 | } else {
|
---|
111 | try {
|
---|
112 | SwingUtilities.invokeAndWait(task);
|
---|
113 | } catch (InterruptedException | InvocationTargetException e) {
|
---|
114 | Main.error(e);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Executes synchronously a callable in
|
---|
121 | * <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html">Event Dispatch Thread</a>
|
---|
122 | * and return a value.
|
---|
123 | * @param callable The callable to execute
|
---|
124 | * @return The computed result
|
---|
125 | * @since 7204
|
---|
126 | */
|
---|
127 | public static <V> V runInEDTAndWaitAndReturn(Callable<V> callable) {
|
---|
128 | if (SwingUtilities.isEventDispatchThread()) {
|
---|
129 | try {
|
---|
130 | return callable.call();
|
---|
131 | } catch (Exception e) {
|
---|
132 | Main.error(e);
|
---|
133 | return null;
|
---|
134 | }
|
---|
135 | } else {
|
---|
136 | FutureTask<V> task = new FutureTask<V>(callable);
|
---|
137 | SwingUtilities.invokeLater(task);
|
---|
138 | try {
|
---|
139 | return task.get();
|
---|
140 | } catch (InterruptedException | ExecutionException e) {
|
---|
141 | Main.error(e);
|
---|
142 | return null;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Warns user about a dangerous action requiring confirmation.
|
---|
149 | * @param title Title of dialog
|
---|
150 | * @param content Content of dialog
|
---|
151 | * @param baseActionIcon Unused? FIXME why is this parameter unused?
|
---|
152 | * @param continueToolTip Tooltip to display for "continue" button
|
---|
153 | * @return true if the user wants to cancel, false if they want to continue
|
---|
154 | */
|
---|
155 | public static boolean warnUser(String title, String content, ImageIcon baseActionIcon, String continueToolTip) {
|
---|
156 | ExtendedDialog dlg = new ExtendedDialog(Main.parent,
|
---|
157 | title, new String[] {tr("Cancel"), tr("Continue")});
|
---|
158 | dlg.setContent(content);
|
---|
159 | dlg.setButtonIcons(new Icon[] {
|
---|
160 | new ImageProvider("cancel").setMaxSize(ImageSizes.LARGEICON).get(),
|
---|
161 | new ImageProvider("upload").setMaxSize(ImageSizes.LARGEICON).addOverlay(
|
---|
162 | new ImageOverlay(new ImageProvider("warning-small"), 0.5, 0.5, 1.0, 1.0)).get()});
|
---|
163 | dlg.setToolTipTexts(new String[] {
|
---|
164 | tr("Cancel"),
|
---|
165 | continueToolTip});
|
---|
166 | dlg.setIcon(JOptionPane.WARNING_MESSAGE);
|
---|
167 | dlg.setCancelButton(1);
|
---|
168 | return dlg.showDialog().getValue() != 2;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Notifies user about an error received from an external source as an HTML page.
|
---|
173 | * @param parent Parent component
|
---|
174 | * @param title Title of dialog
|
---|
175 | * @param message Message displayed at the top of the dialog
|
---|
176 | * @param html HTML content to display (real error message)
|
---|
177 | * @since 7312
|
---|
178 | */
|
---|
179 | public static void notifyUserHtmlError(Component parent, String title, String message, String html) {
|
---|
180 | JPanel p = new JPanel(new GridBagLayout());
|
---|
181 | p.add(new JLabel(message), GBC.eol());
|
---|
182 | p.add(new JLabel(tr("Received error page:")), GBC.eol());
|
---|
183 | JScrollPane sp = embedInVerticalScrollPane(new HtmlPanel(html));
|
---|
184 | sp.setPreferredSize(new Dimension(640, 240));
|
---|
185 | p.add(sp, GBC.eol().fill(GBC.BOTH));
|
---|
186 |
|
---|
187 | ExtendedDialog ed = new ExtendedDialog(parent, title, new String[] {tr("OK")});
|
---|
188 | ed.setButtonIcons(new String[] {"ok.png"});
|
---|
189 | ed.setContent(p);
|
---|
190 | ed.showDialog();
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Replies the disabled (grayed) version of the specified image.
|
---|
195 | * @param image The image to disable
|
---|
196 | * @return The disabled (grayed) version of the specified image, brightened by 20%.
|
---|
197 | * @since 5484
|
---|
198 | */
|
---|
199 | public static Image getDisabledImage(Image image) {
|
---|
200 | return Toolkit.getDefaultToolkit().createImage(
|
---|
201 | new FilteredImageSource(image.getSource(), new GrayFilter(true, 20)));
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Replies the disabled (grayed) version of the specified icon.
|
---|
206 | * @param icon The icon to disable
|
---|
207 | * @return The disabled (grayed) version of the specified icon, brightened by 20%.
|
---|
208 | * @since 5484
|
---|
209 | */
|
---|
210 | public static ImageIcon getDisabledIcon(ImageIcon icon) {
|
---|
211 | return new ImageIcon(getDisabledImage(icon.getImage()));
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Attaches a {@code HierarchyListener} to the specified {@code Component} that
|
---|
216 | * will set its parent dialog resizeable. Use it before a call to JOptionPane#showXXXXDialog
|
---|
217 | * to make it resizeable.
|
---|
218 | * @param pane The component that will be displayed
|
---|
219 | * @param minDimension The minimum dimension that will be set for the dialog. Ignored if null
|
---|
220 | * @return {@code pane}
|
---|
221 | * @since 5493
|
---|
222 | */
|
---|
223 | public static Component prepareResizeableOptionPane(final Component pane, final Dimension minDimension) {
|
---|
224 | if (pane != null) {
|
---|
225 | pane.addHierarchyListener(new HierarchyListener() {
|
---|
226 | @Override
|
---|
227 | public void hierarchyChanged(HierarchyEvent e) {
|
---|
228 | Window window = SwingUtilities.getWindowAncestor(pane);
|
---|
229 | if (window instanceof Dialog) {
|
---|
230 | Dialog dialog = (Dialog) window;
|
---|
231 | if (!dialog.isResizable()) {
|
---|
232 | dialog.setResizable(true);
|
---|
233 | if (minDimension != null) {
|
---|
234 | dialog.setMinimumSize(minDimension);
|
---|
235 | }
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 | });
|
---|
240 | }
|
---|
241 | return pane;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Schedules a new Timer to be run in the future (once or several times).
|
---|
246 | * @param initialDelay milliseconds for the initial and between-event delay if repeatable
|
---|
247 | * @param actionListener an initial listener; can be null
|
---|
248 | * @param repeats specify false to make the timer stop after sending its first action event
|
---|
249 | * @return The (started) timer.
|
---|
250 | * @since 5735
|
---|
251 | */
|
---|
252 | public static Timer scheduleTimer(int initialDelay, ActionListener actionListener, boolean repeats) {
|
---|
253 | Timer timer = new Timer(initialDelay, actionListener);
|
---|
254 | timer.setRepeats(repeats);
|
---|
255 | timer.start();
|
---|
256 | return timer;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * Return s new BasicStroke object with given thickness and style
|
---|
261 | * @param code = 3.5 -> thickness=3.5px; 3.5 10 5 -> thickness=3.5px, dashed: 10px filled + 5px empty
|
---|
262 | * @return stroke for drawing
|
---|
263 | */
|
---|
264 | public static Stroke getCustomizedStroke(String code) {
|
---|
265 | String[] s = code.trim().split("[^\\.0-9]+");
|
---|
266 |
|
---|
267 | if (s.length == 0) return new BasicStroke();
|
---|
268 | float w;
|
---|
269 | try {
|
---|
270 | w = Float.parseFloat(s[0]);
|
---|
271 | } catch (NumberFormatException ex) {
|
---|
272 | w = 1.0f;
|
---|
273 | }
|
---|
274 | if (s.length > 1) {
|
---|
275 | float[] dash = new float[s.length-1];
|
---|
276 | float sumAbs = 0;
|
---|
277 | try {
|
---|
278 | for (int i = 0; i < s.length-1; i++) {
|
---|
279 | dash[i] = Float.parseFloat(s[i+1]);
|
---|
280 | sumAbs += Math.abs(dash[i]);
|
---|
281 | }
|
---|
282 | } catch (NumberFormatException ex) {
|
---|
283 | Main.error("Error in stroke preference format: "+code);
|
---|
284 | dash = new float[]{5.0f};
|
---|
285 | }
|
---|
286 | if (sumAbs < 1e-1) {
|
---|
287 | Main.error("Error in stroke dash fomat (all zeros): "+code);
|
---|
288 | return new BasicStroke(w);
|
---|
289 | }
|
---|
290 | // dashed stroke
|
---|
291 | return new BasicStroke(w, BasicStroke.CAP_BUTT,
|
---|
292 | BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
|
---|
293 | } else {
|
---|
294 | if (w > 1) {
|
---|
295 | // thick stroke
|
---|
296 | return new BasicStroke(w, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
|
---|
297 | } else {
|
---|
298 | // thin stroke
|
---|
299 | return new BasicStroke(w);
|
---|
300 | }
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Gets the font used to display monospaced text in a component, if possible.
|
---|
306 | * @param component The component
|
---|
307 | * @return the font used to display monospaced text in a component, if possible
|
---|
308 | * @since 7896
|
---|
309 | */
|
---|
310 | public static Font getMonospacedFont(JComponent component) {
|
---|
311 | // Special font for Khmer script
|
---|
312 | if ("km".equals(LanguageInfo.getJOSMLocaleCode())) {
|
---|
313 | return component.getFont();
|
---|
314 | } else {
|
---|
315 | return new Font("Monospaced", component.getFont().getStyle(), component.getFont().getSize());
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * Gets the font used to display JOSM title in about dialog and splash screen.
|
---|
321 | * @since 5797
|
---|
322 | */
|
---|
323 | public static Font getTitleFont() {
|
---|
324 | return new Font("SansSerif", Font.BOLD, 23);
|
---|
325 | }
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Embeds the given component into a new vertical-only scrollable {@code JScrollPane}.
|
---|
329 | * @param panel The component to embed
|
---|
330 | * @return the vertical scrollable {@code JScrollPane}
|
---|
331 | * @since 6666
|
---|
332 | */
|
---|
333 | public static JScrollPane embedInVerticalScrollPane(Component panel) {
|
---|
334 | return new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
|
---|
335 | }
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * Returns extended modifier key used as the appropriate accelerator key for menu shortcuts.
|
---|
339 | * It is advised everywhere to use {@link Toolkit#getMenuShortcutKeyMask()} to get the cross-platform modifier, but:
|
---|
340 | * <ul>
|
---|
341 | * <li>it returns KeyEvent.CTRL_MASK instead of KeyEvent.CTRL_DOWN_MASK. We used the extended
|
---|
342 | * modifier for years, and Oracle recommends to use it instead, so it's best to keep it</li>
|
---|
343 | * <li>the method throws a HeadlessException ! So we would need to handle it for unit tests anyway</li>
|
---|
344 | * </ul>
|
---|
345 | * @return extended modifier key used as the appropriate accelerator key for menu shortcuts
|
---|
346 | * @since 7539
|
---|
347 | */
|
---|
348 | public static int getMenuShortcutKeyMaskEx() {
|
---|
349 | return Main.isPlatformOsx() ? KeyEvent.META_DOWN_MASK : KeyEvent.CTRL_DOWN_MASK;
|
---|
350 | }
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * Sets a global font for all UI, replacing default font of current look and feel.
|
---|
354 | * @param name Font name. It is up to the caller to make sure the font exists
|
---|
355 | * @throws IllegalArgumentException if name is null
|
---|
356 | * @since 7896
|
---|
357 | */
|
---|
358 | public static void setUIFont(String name) {
|
---|
359 | CheckParameterUtil.ensureParameterNotNull(name, "name");
|
---|
360 | Main.info("Setting "+name+" as the default UI font");
|
---|
361 | Enumeration<?> keys = UIManager.getDefaults().keys();
|
---|
362 | while (keys.hasMoreElements()) {
|
---|
363 | Object key = keys.nextElement();
|
---|
364 | Object value = UIManager.get(key);
|
---|
365 | if (value != null && value instanceof FontUIResource) {
|
---|
366 | FontUIResource fui = (FontUIResource) value;
|
---|
367 | UIManager.put(key, new FontUIResource(name, fui.getStyle(), fui.getSize()));
|
---|
368 | }
|
---|
369 | }
|
---|
370 | }
|
---|
371 | }
|
---|