1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.util;
|
---|
3 |
|
---|
4 | import java.awt.BasicStroke;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
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.Image;
|
---|
14 | import java.awt.Stroke;
|
---|
15 | import java.awt.Toolkit;
|
---|
16 | import java.awt.Window;
|
---|
17 | import java.awt.event.ActionListener;
|
---|
18 | import java.awt.event.HierarchyEvent;
|
---|
19 | import java.awt.event.HierarchyListener;
|
---|
20 | import java.awt.image.FilteredImageSource;
|
---|
21 | import java.lang.reflect.InvocationTargetException;
|
---|
22 | import java.util.Arrays;
|
---|
23 | import java.util.List;
|
---|
24 |
|
---|
25 | import javax.swing.GrayFilter;
|
---|
26 | import javax.swing.Icon;
|
---|
27 | import javax.swing.ImageIcon;
|
---|
28 | import javax.swing.JOptionPane;
|
---|
29 | import javax.swing.SwingUtilities;
|
---|
30 | import javax.swing.Timer;
|
---|
31 |
|
---|
32 | import org.openstreetmap.josm.Main;
|
---|
33 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
34 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * basic gui utils
|
---|
38 | */
|
---|
39 | public class GuiHelper {
|
---|
40 | /**
|
---|
41 | * disable / enable a component and all its child components
|
---|
42 | */
|
---|
43 | public static void setEnabledRec(Container root, boolean enabled) {
|
---|
44 | root.setEnabled(enabled);
|
---|
45 | Component[] children = root.getComponents();
|
---|
46 | for (Component child : children) {
|
---|
47 | if(child instanceof Container) {
|
---|
48 | setEnabledRec((Container) child, enabled);
|
---|
49 | } else {
|
---|
50 | child.setEnabled(enabled);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public static void executeByMainWorkerInEDT(final Runnable task) {
|
---|
56 | Main.worker.submit(new Runnable() {
|
---|
57 | @Override
|
---|
58 | public void run() {
|
---|
59 | runInEDTAndWait(task);
|
---|
60 | }
|
---|
61 | });
|
---|
62 | }
|
---|
63 |
|
---|
64 | public static void runInEDT(Runnable task) {
|
---|
65 | if (SwingUtilities.isEventDispatchThread()) {
|
---|
66 | task.run();
|
---|
67 | } else {
|
---|
68 | SwingUtilities.invokeLater(task);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static void runInEDTAndWait(Runnable task) {
|
---|
73 | if (SwingUtilities.isEventDispatchThread()) {
|
---|
74 | task.run();
|
---|
75 | } else {
|
---|
76 | try {
|
---|
77 | SwingUtilities.invokeAndWait(task);
|
---|
78 | } catch (InterruptedException e) {
|
---|
79 | e.printStackTrace();
|
---|
80 | } catch (InvocationTargetException e) {
|
---|
81 | e.printStackTrace();
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * returns true if the user wants to cancel, false if they
|
---|
88 | * want to continue
|
---|
89 | */
|
---|
90 | public static final boolean warnUser(String title, String content, ImageIcon baseActionIcon, String continueToolTip) {
|
---|
91 | ExtendedDialog dlg = new ExtendedDialog(Main.parent,
|
---|
92 | title, new String[] {tr("Cancel"), tr("Continue")});
|
---|
93 | dlg.setContent(content);
|
---|
94 | dlg.setButtonIcons(new Icon[] {
|
---|
95 | ImageProvider.get("cancel"),
|
---|
96 | ImageProvider.overlay(
|
---|
97 | ImageProvider.get("upload"),
|
---|
98 | new ImageIcon(ImageProvider.get("warning-small").getImage().getScaledInstance(10 , 10, Image.SCALE_SMOOTH)),
|
---|
99 | ImageProvider.OverlayPosition.SOUTHEAST)});
|
---|
100 | dlg.setToolTipTexts(new String[] {
|
---|
101 | tr("Cancel"),
|
---|
102 | continueToolTip});
|
---|
103 | dlg.setIcon(JOptionPane.WARNING_MESSAGE);
|
---|
104 | dlg.setCancelButton(1);
|
---|
105 | return dlg.showDialog().getValue() != 2;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Replies the disabled (grayed) version of the specified image.
|
---|
110 | * @param image The image to disable
|
---|
111 | * @return The disabled (grayed) version of the specified image, brightened by 20%.
|
---|
112 | * @since 5484
|
---|
113 | */
|
---|
114 | public static final Image getDisabledImage(Image image) {
|
---|
115 | return Toolkit.getDefaultToolkit().createImage(
|
---|
116 | new FilteredImageSource(image.getSource(), new GrayFilter(true, 20)));
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Replies the disabled (grayed) version of the specified icon.
|
---|
121 | * @param icon The icon to disable
|
---|
122 | * @return The disabled (grayed) version of the specified icon, brightened by 20%.
|
---|
123 | * @since 5484
|
---|
124 | */
|
---|
125 | public static final ImageIcon getDisabledIcon(ImageIcon icon) {
|
---|
126 | return new ImageIcon(getDisabledImage(icon.getImage()));
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Attaches a {@code HierarchyListener} to the specified {@code Component} that
|
---|
131 | * will set its parent dialog resizeable. Use it before a call to JOptionPane#showXXXXDialog
|
---|
132 | * to make it resizeable.
|
---|
133 | * @param pane The component that will be displayed
|
---|
134 | * @param minDimension The minimum dimension that will be set for the dialog. Ignored if null
|
---|
135 | * @return {@code pane}
|
---|
136 | * @since 5493
|
---|
137 | */
|
---|
138 | public static final Component prepareResizeableOptionPane(final Component pane, final Dimension minDimension) {
|
---|
139 | if (pane != null) {
|
---|
140 | pane.addHierarchyListener(new HierarchyListener() {
|
---|
141 | @Override
|
---|
142 | public void hierarchyChanged(HierarchyEvent e) {
|
---|
143 | Window window = SwingUtilities.getWindowAncestor(pane);
|
---|
144 | if (window instanceof Dialog) {
|
---|
145 | Dialog dialog = (Dialog)window;
|
---|
146 | if (!dialog.isResizable()) {
|
---|
147 | dialog.setResizable(true);
|
---|
148 | if (minDimension != null) {
|
---|
149 | dialog.setMinimumSize(minDimension);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 | });
|
---|
155 | }
|
---|
156 | return pane;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Schedules a new Timer to be run in the future (once or several times).
|
---|
161 | * @param initialDelay milliseconds for the initial and between-event delay if repeatable
|
---|
162 | * @param actionListener an initial listener; can be null
|
---|
163 | * @param repeats specify false to make the timer stop after sending its first action event
|
---|
164 | * @return The (started) timer.
|
---|
165 | * @since 5735
|
---|
166 | */
|
---|
167 | public static final Timer scheduleTimer(int initialDelay, ActionListener actionListener, boolean repeats) {
|
---|
168 | Timer timer = new Timer(initialDelay, actionListener);
|
---|
169 | timer.setRepeats(repeats);
|
---|
170 | timer.start();
|
---|
171 | return timer;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Return s new BasicStroke object with given thickness and style
|
---|
176 | * @param code = 3.5 -> thickness=3.5px; 3.5 10 5 -> thickness=3.5px, dashed: 10px filled + 5px empty
|
---|
177 | * @return stroke for drawing
|
---|
178 | */
|
---|
179 | public static Stroke getCustomizedStroke(String code) {
|
---|
180 | String[] s = code.trim().split("[^\\.0-9]+");
|
---|
181 |
|
---|
182 | if (s.length==0) return new BasicStroke();
|
---|
183 | float w;
|
---|
184 | try {
|
---|
185 | w = Float.parseFloat(s[0]);
|
---|
186 | } catch (NumberFormatException ex) {
|
---|
187 | w = 1.0f;
|
---|
188 | }
|
---|
189 | if (s.length>1) {
|
---|
190 | float dash[]= new float[s.length-1];
|
---|
191 | boolean error = false;
|
---|
192 | float sumAbs = 0;
|
---|
193 | try {
|
---|
194 | for (int i=0; i<s.length-1; i++) {
|
---|
195 | dash[i] = Float.parseFloat(s[i+1]);
|
---|
196 | sumAbs += Math.abs(dash[i]);
|
---|
197 | }
|
---|
198 | } catch (NumberFormatException ex) {
|
---|
199 | System.err.println("Error in stroke preference format: "+code);
|
---|
200 | dash = new float[]{5.0f};
|
---|
201 | }
|
---|
202 | if (sumAbs < 1e-1) {
|
---|
203 | System.err.println("Error in stroke dash fomat (all zeros): "+code);
|
---|
204 | return new BasicStroke(w);
|
---|
205 | }
|
---|
206 | // dashed stroke
|
---|
207 | return new BasicStroke(w, BasicStroke.CAP_BUTT,
|
---|
208 | BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
|
---|
209 | } else {
|
---|
210 | if (w>1) {
|
---|
211 | // thick stroke
|
---|
212 | return new BasicStroke(w, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
|
---|
213 | } else {
|
---|
214 | // thin stroke
|
---|
215 | return new BasicStroke(w);
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Gets the font used to display JOSM title in about dialog and splash screen.
|
---|
222 | * @return By order or priority, the first font available in local fonts:
|
---|
223 | * 1. Helvetica Bold 20
|
---|
224 | * 2. Calibri Bold 23
|
---|
225 | * 3. Arial Bold 20
|
---|
226 | * 4. SansSerif Bold 20
|
---|
227 | * @since 5797
|
---|
228 | */
|
---|
229 | public static Font getTitleFont() {
|
---|
230 | List<String> fonts = Arrays.asList(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
|
---|
231 | // Helvetica is the preferred choice but is not available by default on Windows
|
---|
232 | // (http://www.microsoft.com/typography/fonts/product.aspx?pid=161)
|
---|
233 | if (fonts.contains("Helvetica")) {
|
---|
234 | return new Font("Helvetica", Font.BOLD, 20);
|
---|
235 | // Calibri is the default Windows font since Windows Vista but is not available on older versions of Windows, where Arial is preferred
|
---|
236 | } else if (fonts.contains("Calibri")) {
|
---|
237 | return new Font("Calibri", Font.BOLD, 23);
|
---|
238 | } else if (fonts.contains("Arial")) {
|
---|
239 | return new Font("Arial", Font.BOLD, 20);
|
---|
240 | // No luck, nothing found, fallback to one of the 5 fonts provided with Java (Serif, SansSerif, Monospaced, Dialog, and DialogInput)
|
---|
241 | } else {
|
---|
242 | return new Font("SansSerif", Font.BOLD, 20);
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|