![JAR search and dependency download from the Maven repository](/logo.png)
org.fife.ui.autocomplete.Util Maven / Gradle / Ivy
/*
* 12/21/2008
*
* Util.java - Utility methods for the autocompletion package.
* Copyright (C) 2008 Robert Futrell
* robert_futrell at users.sourceforge.net
* http://fifesoft.com/rsyntaxtextarea
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
package org.fife.ui.autocomplete;
import java.awt.Color;
import java.lang.reflect.Method;
import java.net.URI;
/**
* Utility methods for the autocomplete framework.
*
* @author Robert Futrell
* @version 1.0
*/
class Util {
private static boolean desktopCreationAttempted;
private static Object desktop;
private static final Object LOCK_DESKTOP_CREATION = new Object();
/**
* Returns a hex string for the specified color, suitable for HTML.
*
* @param c The color.
* @return The string representation, in the form "#rrggbb
",
* or null
if c
is null
.
*/
public static String getHexString(Color c) {
if (c==null) {
return null;
}
StringBuffer sb = new StringBuffer("#");
int r = c.getRed();
if (r<16) {
sb.append('0');
}
sb.append(Integer.toHexString(r));
int g = c.getGreen();
if (g<16) {
sb.append('0');
}
sb.append(Integer.toHexString(g));
int b = c.getBlue();
if (b<16) {
sb.append('0');
}
sb.append(Integer.toHexString(b));
return sb.toString();
}
/**
* Attempts to open a web browser to the specified URI.
*
* @param uri The URI to open. If this is null
, nothing
happens and this method returns false
.
* @return Whether the operation was successful. This will be
* false
on JRE's older than 1.6.
*/
public static boolean browse(URI uri) {
boolean success = false;
if (uri!=null) {
Object desktop = getDesktop();
if (desktop!=null) {
try {
Method m = desktop.getClass().getDeclaredMethod(
"browse", new Class[] { URI.class });
m.invoke(desktop, new Object[] { uri });
success = true;
} catch (RuntimeException re) {
throw re; // Keep FindBugs happy
} catch (Exception e) {
// Ignore, just return "false" below.
}
}
}
return success;
}
/**
* Returns the singleton java.awt.Desktop
instance, or
* null
if it is unsupported on this platform (or the JRE
* is older than 1.6).
*
* @return The desktop, as an {@link Object}.
*/
private static Object getDesktop() {
synchronized (LOCK_DESKTOP_CREATION) {
if (!desktopCreationAttempted) {
desktopCreationAttempted = true;
try {
Class desktopClazz = Class.forName("java.awt.Desktop");
Method m = desktopClazz.
getDeclaredMethod("isDesktopSupported", null);
boolean supported = ((Boolean)m.invoke(null, null)).
booleanValue();
if (supported) {
m = desktopClazz.getDeclaredMethod("getDesktop", null);
desktop = m.invoke(null, null);
}
} catch (RuntimeException re) {
throw re; // Keep FindBugs happy
} catch (Exception e) {
// Ignore; keeps desktop as null.
}
}
}
return desktop;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy