77 */
88package org .fife .rsta .ui ;
99
10- import java .awt .Color ;
11- import java .awt .Component ;
12- import java .awt .ComponentOrientation ;
13- import java .awt .Container ;
14- import java .lang .reflect .Method ;
10+ import java .awt .*;
11+ import java .io .IOException ;
1512import java .net .URI ;
1613import java .net .URISyntaxException ;
1714import java .util .*;
15+ import java .util .List ;
1816import javax .swing .BorderFactory ;
1917import javax .swing .JButton ;
2018import javax .swing .JComboBox ;
3533 */
3634public final class UIUtil {
3735
38- private static boolean desktopCreationAttempted ;
39- private static Object desktop ;
40- private static final Object LOCK_DESKTOP_CREATION = new Object ();
41-
4236 /**
4337 * A very common border that can be shared across many components.
4438 */
@@ -59,7 +53,7 @@ private UIUtil() {
5953 * @param uri The URI to open. If this is <code>null</code>, nothing
6054 * happens and this method returns <code>false</code>.
6155 * @return Whether the operation was successful. This will be
62- * <code>false</code> on JRE's older than 1.6 .
56+ * <code>false</code> on systems without desktop support .
6357 * @see #browse(URI)
6458 */
6559 public static boolean browse (String uri ) {
@@ -80,24 +74,20 @@ public static boolean browse(String uri) {
8074 * @param uri The URI to open. If this is <code>null</code>, nothing
8175 * happens and this method returns <code>false</code>.
8276 * @return Whether the operation was successful. This will be
83- * <code>false</code> on JRE's older than 1.6 .
77+ * <code>false</code> on systems without desktop support .
8478 * @see #browse(String)
8579 */
8680 public static boolean browse (URI uri ) {
8781
8882 boolean success = false ;
8983
90- if (uri !=null ) {
91- Object desktop = getDesktop ();
92- if (desktop != null ) {
84+ if (uri !=null && Desktop . isDesktopSupported () ) {
85+ Desktop desktop = Desktop . getDesktop ();
86+ if (desktop . isSupported ( Desktop . Action . BROWSE ) ) {
9387 try {
94- Method m = desktop .getClass ().getDeclaredMethod (
95- "browse" , URI .class );
96- m .invoke (desktop , uri );
88+ desktop .browse (uri );
9789 success = true ;
98- } catch (RuntimeException re ) {
99- throw re ; // Keep FindBugs happy
100- } catch (Exception e ) {
90+ } catch (IOException ioe ) {
10191 // Ignore, just return "false" below.
10292 }
10393 }
@@ -170,47 +160,6 @@ public static <T> List<T> getDescendantsOfType(Container comp, Class<T> clazz) {
170160 }
171161
172162
173- /**
174- * Returns the singleton <code>java.awt.Desktop</code> instance, or
175- * <code>null</code> if it is unsupported on this platform (or the JRE
176- * is older than 1.6).
177- *
178- * @return The desktop, as an {@link Object}.
179- */
180- private static Object getDesktop () {
181-
182- synchronized (LOCK_DESKTOP_CREATION ) {
183-
184- if (!desktopCreationAttempted ) {
185-
186- desktopCreationAttempted = true ;
187-
188- try {
189- Class <?> desktopClazz = Class .forName ("java.awt.Desktop" );
190- Method m = desktopClazz .
191- getDeclaredMethod ("isDesktopSupported" );
192-
193- boolean supported = (Boolean ) m .invoke (null );
194- if (supported ) {
195- m = desktopClazz .getDeclaredMethod ("getDesktop" );
196- desktop = m .invoke (null );
197- }
198-
199- } catch (RuntimeException re ) {
200- throw re ; // Keep FindBugs happy
201- } catch (Exception e ) {
202- // Ignore; keeps desktop as null.
203- }
204-
205- }
206-
207- }
208-
209- return desktop ;
210-
211- }
212-
213-
214163 /**
215164 * Returns an empty border of width 5 on all sides. Since this is a
216165 * very common border in GUI's, the border returned is a singleton.
@@ -248,13 +197,11 @@ public static Color getErrorTextForeground() {
248197 */
249198 public static int getMnemonic (ResourceBundle msg , String key ) {
250199 int mnemonic = 0 ;
251- try {
200+ if ( msg . containsKey ( key )) {
252201 Object value = msg .getObject (key );
253202 if (value instanceof String ) {
254203 mnemonic = ((String )value ).charAt (0 );
255204 }
256- } catch (MissingResourceException mre ) {
257- // Swallow. TODO: When we drop 1.4/1.5 support, use containsKey().
258205 }
259206 return mnemonic ;
260207 }
@@ -302,7 +249,7 @@ public static void makeSpringCompactGrid(Container parent, int rows,
302249 "must use SpringLayout." , cce );
303250 }
304251
305- //Align all cells in each column and make them the same width.
252+ // Align all cells in each column and make them the same width.
306253 Spring x = Spring .constant (initialX );
307254 for (int c = 0 ; c < cols ; c ++) {
308255 Spring width = Spring .constant (0 );
@@ -320,7 +267,7 @@ public static void makeSpringCompactGrid(Container parent, int rows,
320267 x = Spring .sum (x , Spring .sum (width , Spring .constant (xPad )));
321268 }
322269
323- //Align all cells in each row and make them the same height.
270+ // Align all cells in each row and make them the same height.
324271 Spring y = Spring .constant (initialY );
325272 for (int r = 0 ; r < rows ; r ++) {
326273 Spring height = Spring .constant (0 );
@@ -337,7 +284,7 @@ public static void makeSpringCompactGrid(Container parent, int rows,
337284 y = Spring .sum (y , Spring .sum (height , Spring .constant (yPad )));
338285 }
339286
340- //Set the parent's size.
287+ // Set the parent's size.
341288 SpringLayout .Constraints pCons = layout .getConstraints (parent );
342289 pCons .setConstraint (SpringLayout .SOUTH , y );
343290 pCons .setConstraint (SpringLayout .EAST , x );
0 commit comments