📄 typemanager.java
字号:
Class[] interfaces = object.getClass().getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
wrapper = wrappers.get(interfaces[i]);
if (wrapper != null)
break;
}
}
if (wrapper == null)
throw new StylesheetException("no registered Styleable wrapper " +
"for " + object.getClass());
try {
Constructor c = getObjectConstructor(wrapper);
result = (Styleable) c.newInstance(object);
styleables.put(object, result);
}
catch (NoSuchMethodException e) {
throw new StylesheetException("Styleable wrapper " + wrapper +
" does not have a public constructor which takes " +
"Object");
}
catch (InstantiationException e) {
throw new StylesheetException(e);
}
catch (IllegalAccessException e) {
throw new StylesheetException(e);
}
catch (InvocationTargetException e) {
throw new StylesheetException(e);
}
}
return result;
}
}
/**
* Returns the <code>StyleSupport</code> instance which {@link
* DefaultStyleable} should use for the specified object. Additional
* <code>StyleSupport</code> classes may be registered with {@link
* #registerStyleSupport}.
*
*@param object the object which needs a <code>StyleSupport</code> instance
*@return the registered <code>StyleSupport</code>
*@throws StylesheetException if an error occurs creating the
* <code>StyleSupport</code>
*@see #registerStyleSupport
*/
public static StyleSupport getStyleSupport(Object object)
throws StylesheetException {
Class cls = object.getClass();
StyleSupport result = styleSupportInstances.get(cls);
if (result == null) {
Class<? extends StyleSupport> styleSupportClass =
styleSupportClasses.get(cls);
if (styleSupportClass == null) {
Class[] interfaces = object.getClass().getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
styleSupportClass = styleSupportClasses.get(interfaces[i]);
if (styleSupportClass != null)
break;
}
}
if (styleSupportClass == null)
throw new StylesheetException("no registered StyleSupport for " + cls);
try {
Constructor c = getClassConstructor(styleSupportClass);
result = (StyleSupport) c.newInstance(cls);
styleSupportInstances.put(object.getClass(), result);
}
catch (NoSuchMethodException e) {
throw new StylesheetException("Style support " + styleSupportClass +
" does not have a public constructor which takes " +
"Class");
}
catch (InstantiationException e) {
throw new StylesheetException(e);
}
catch (IllegalAccessException e) {
throw new StylesheetException(e);
}
catch (InvocationTargetException e) {
throw new StylesheetException(e);
}
}
return result;
}
private static Constructor getObjectConstructor(Class cls)
throws NoSuchMethodException {
Constructor result = objectConstructors.get(cls);
if (result == null) {
result = cls.getConstructor(Object.class);
objectConstructors.put(cls, result);
}
return result;
}
private static Constructor getClassConstructor(Class cls)
throws NoSuchMethodException {
Constructor result = classConstructors.get(cls);
if (result == null) {
result = cls.getConstructor(Class.class);
classConstructors.put(cls, result);
}
return result;
}
private static void registerPrimitiveConverter(Class type) {
registerTypeConverter(type, new PrimitiveConverter(type));
}
/**
* Registers a new <code>TypeConverter</code>, which is used to convert from
* the strings found
* in CSS files to the destination type. The <code>TypeConverter</code>
* will be called whenever the specified class or any of its descendents
* (which do not have a more specific <code>TypeConverter</code> registered)
* is required.
* <p>
* This call requires the <code>setGlobalStylesheet</code> AWTPermission.
*
*@param type the class to register
*@param converter the <code>TypeConverter</code> which provides support for
* the class
*@throws SecurityException if the required permission is not available
*/
public static void registerTypeConverter(Class type,
TypeConverter converter) {
if (initialized) {
SecurityManager security = System.getSecurityManager();
if (security != null)
security.checkPermission(
new AWTPermission("setGlobalStylesheet"));
}
converters.put(type, converter);
}
/**
* Registers a new <code>TypeConverter</code> class, which is used to
* convert from the strings found in CSS files to the destination type.
* Unlike {@link #registerTypeConverter}, which registers a specific
* <code>TypeConverter</code> instance, this call registers a class which is
* then instantiated for each specific subclass of <code>type</code> that
* needs conversion. The <code>converter</code> class must have a
* constructor which takes a <code>Class</code>.
* <p>
* This call requires the <code>setGlobalStylesheet</code> AWTPermission.
*
*@param type the class to register
*@param converter the <code>TypeConverter</code> class which provides
* conversion
*@throws SecurityException if the required permission is not available
*/
public static void registerTypeConverterClass(Class type,
Class<? extends TypeConverter> converter) {
if (initialized) {
SecurityManager security = System.getSecurityManager();
if (security != null)
security.checkPermission(
new AWTPermission("setGlobalStylesheet"));
}
converters.put(type, converter);
}
/**
* Returns the <code>TypeConverter</code> to use when converting strings
* into the specified type. Additional <code>TypeConverters</code> are
* registered with the {@link #registerTypeConverter} method.
*
*@param type the class into which to convert the string
*@return the TypeConverter to use for the conversion, or <code>null</code>
* if none is registered
*@see #registerTypeConverter
*/
public static TypeConverter getTypeConverter(Class type) {
Object result = cachedConverters.get(type);
if (result == null) {
result = converters.get(type);
if (result instanceof Class) {
try {
Constructor c = ((Class) result).getConstructor(
Class.class);
result = c.newInstance(type);
cachedConverters.put(type, (TypeConverter) result);
}
catch (Exception e) {
throw new StylesheetException(e);
}
}
}
return (TypeConverter) result;
}
/**
* Converts a <code>String</code> into the specified type. The
* <code>TypeConverter</code> registered to the required class is used to
* perform the conversion. Primitive types will be wrapped; for example if
* you request <code>int.class</code> the return type will actually be an
* <code>Integer</code>.
*
*@param string the string to convert
*@param type the type into which to convert the string
*@return the converted object
*@see #getTypeConverter
*@see #registerTypeConverter
*/
public static Object convertFromString(String string, Class type) {
TypeConverter converter = getTypeConverter(type);
if (converter == null)
throw new IllegalArgumentException("unsupported type: " + type);
return converter.convertFromString(string);
}
/**
* Splits a comma-separated string into individual arguments. Commas
* appearing inside of parentheses are ignored, so the string "1, 2,
* foo(3, 4)" would be split into ["1", "2", "foo(3, 4)"].
*/
static String[] parseArgs(String args) {
if (args.length() == 0)
return new String[0];
List<String> result = new ArrayList<String>();
StringBuilder token = new StringBuilder();
int depth = 0;
for (int i = 0; i < args.length(); i++) {
char c = args.charAt(i);
switch (c) {
case '(': depth++; token.append(c); break;
case ')': depth--; token.append(c); break;
case ',': if (depth == 0) {
result.add(token.toString().trim());
token.setLength(0);
break;
} // fall through
default: token.append(c);
}
}
result.add(token.toString().trim());
return result.toArray(new String[result.size()]);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -