| 1 |
/* |
| 2 |
* Copyright (c) 2005-2010 Laf-Widget Kirill Grouchnikov. All Rights Reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are met: |
| 6 |
* |
| 7 |
* o Redistributions of source code must retain the above copyright notice, |
| 8 |
* this list of conditions and the following disclaimer. |
| 9 |
* |
| 10 |
* o Redistributions in binary form must reproduce the above copyright notice, |
| 11 |
* this list of conditions and the following disclaimer in the documentation |
| 12 |
* and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* o Neither the name of Laf-Widget Kirill Grouchnikov nor the names of |
| 15 |
* its contributors may be used to endorse or promote products derived |
| 16 |
* from this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 20 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 21 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 22 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 25 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 27 |
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 28 |
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
package org.pushingpixels.lafwidget.ant; |
| 31 |
|
| 32 |
import java.lang.reflect.Constructor; |
| 33 |
import java.lang.reflect.Method; |
| 34 |
import java.util.HashMap; |
| 35 |
import java.util.Map; |
| 36 |
|
| 37 |
import javax.swing.plaf.basic.BasicLookAndFeel; |
| 38 |
import javax.swing.plaf.metal.MetalLookAndFeel; |
| 39 |
|
| 40 |
/** |
| 41 |
* Utility functions. |
| 42 |
* |
| 43 |
* @author Kirill Grouchnikov |
| 44 |
*/ |
| 45 |
public class Utils { |
| 46 |
/** |
| 47 |
* Maps the LAF names. |
| 48 |
*/ |
| 49 |
protected Map<String, String> lafMap; |
| 50 |
|
| 51 |
/** |
| 52 |
* Singleton. |
| 53 |
*/ |
| 54 |
protected static Utils instance = new Utils(); |
| 55 |
|
| 56 |
/** |
| 57 |
* IDs of all UI delegates. |
| 58 |
*/ |
| 59 |
public static final String[] UI_IDS = new String[] { "ButtonUI", |
| 60 |
"CheckBoxUI", "CheckBoxMenuItemUI", "ColorChooserUI", "ComboBoxUI", |
| 61 |
"DesktopIconUI", "DesktopPaneUI", "EditorPaneUI", |
| 62 |
"FormattedTextFieldUI", "InternalFrameUI", "LabelUI", "ListUI", |
| 63 |
"MenuUI", "MenuBarUI", "MenuItemUI", "OptionPaneUI", "PanelUI", |
| 64 |
"PasswordFieldUI", "PopupMenuUI", "PopupMenuSeparatorUI", |
| 65 |
"ProgressBarUI", "RadioButtonUI", "RadioButtonMenuItemUI", |
| 66 |
"RootPaneUI", "ScrollBarUI", "ScrollPaneUI", "SplitPaneUI", |
| 67 |
"SliderUI", "SeparatorUI", "SpinnerUI", "ToolBarSeparatorUI", |
| 68 |
"TabbedPaneUI", "TableUI", "TableHeaderUI", "TextAreaUI", |
| 69 |
"TextFieldUI", "TextPaneUI", "ToggleButtonUI", "ToolBarUI", |
| 70 |
"ToolTipUI", "TreeUI", "ViewportUI" }; |
| 71 |
|
| 72 |
/** |
| 73 |
* Constructor. |
| 74 |
*/ |
| 75 |
private Utils() { |
| 76 |
this.lafMap = new HashMap<String, String>(); |
| 77 |
this.lafMap.put(BasicLookAndFeel.class.getName(), |
| 78 |
"javax.swing.plaf.basic.Basic"); |
| 79 |
this.lafMap.put(MetalLookAndFeel.class.getName(), |
| 80 |
"javax.swing.plaf.metal.Metal"); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Returns instance. |
| 85 |
* |
| 86 |
* @return Instance. |
| 87 |
*/ |
| 88 |
public static Utils getUtils() { |
| 89 |
return Utils.instance; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Returns fully-qualified class name for the UI delegate based on the |
| 94 |
* specified parameters. |
| 95 |
* |
| 96 |
* @param uiKey |
| 97 |
* UI key. |
| 98 |
* @param lafClassName |
| 99 |
* Class name of the LAF. |
| 100 |
* @return Fully-qualified class name for the UI delegate. The LAF hierarchy |
| 101 |
* is searched starting from the specified class name and up. For |
| 102 |
* example, if the second parameter points to |
| 103 |
* {@link MetalLookAndFeel}, the metal delegate classname is |
| 104 |
* returned if exists; otherwise the basic delegate classname is |
| 105 |
* returned. |
| 106 |
*/ |
| 107 |
public String getUIDelegate(String uiKey, String lafClassName) { |
| 108 |
try { |
| 109 |
lafClassName = lafClassName.replace('/', '.'); |
| 110 |
return this.getUIDelegate(uiKey, Class.forName(lafClassName)); |
| 111 |
} catch (ClassNotFoundException cnfe) { |
| 112 |
throw new AugmentException( |
| 113 |
"Class '" + lafClassName + "' not found", cnfe); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Returns fully-qualified class name for the UI delegate based on the |
| 119 |
* specified parameters. |
| 120 |
* |
| 121 |
* @param uiKey |
| 122 |
* UI key. |
| 123 |
* @param origLafClazz |
| 124 |
* LAF class. |
| 125 |
* @return Fully-qualified class name for the UI delegate. The LAF hierarchy |
| 126 |
* is searched starting from the specified class and up. For |
| 127 |
* example, if the second parameter points to |
| 128 |
* {@link MetalLookAndFeel}, the metal delegate classname is |
| 129 |
* returned if exists; otherwise the basic delegate classname is |
| 130 |
* returned. |
| 131 |
*/ |
| 132 |
public String getUIDelegate(String uiKey, Class<?> origLafClazz) { |
| 133 |
Class<?> lafClazz = origLafClazz; |
| 134 |
while (lafClazz != null) { |
| 135 |
String prefix = (String) this.lafMap.get(lafClazz.getName()); |
| 136 |
if (prefix != null) { |
| 137 |
String fullClassName = prefix + uiKey; |
| 138 |
Class<?> delegateClazz = null; |
| 139 |
try { |
| 140 |
delegateClazz = Class.forName(fullClassName); |
| 141 |
} catch (ClassNotFoundException cnfe) { |
| 142 |
} |
| 143 |
if (delegateClazz != null) |
| 144 |
return fullClassName; |
| 145 |
} |
| 146 |
lafClazz = lafClazz.getSuperclass(); |
| 147 |
} |
| 148 |
throw new AugmentException("No match for '" + uiKey + "' in '" |
| 149 |
+ origLafClazz.getName() + "' hierarchy"); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Returns JNI-compliant description of the specified class (type). For |
| 154 |
* example, for <code>JButton[]</code> this function will return |
| 155 |
* <code>[Ljavax/swing/JButton;</code>. |
| 156 |
* |
| 157 |
* @param clazz |
| 158 |
* Class. |
| 159 |
* @return JNI-compliant class (type) description. |
| 160 |
*/ |
| 161 |
public static String getTypeDesc(Class<?> clazz) { |
| 162 |
if (clazz.isArray()) |
| 163 |
return "[" + Utils.getTypeDesc(clazz.getComponentType()); |
| 164 |
if (clazz == void.class) |
| 165 |
return "V"; |
| 166 |
if (clazz == boolean.class) |
| 167 |
return "Z"; |
| 168 |
if (clazz == byte.class) |
| 169 |
return "B"; |
| 170 |
if (clazz == char.class) |
| 171 |
return "C"; |
| 172 |
if (clazz == short.class) |
| 173 |
return "S"; |
| 174 |
if (clazz == int.class) |
| 175 |
return "I"; |
| 176 |
if (clazz == long.class) |
| 177 |
return "J"; |
| 178 |
if (clazz == float.class) |
| 179 |
return "F"; |
| 180 |
if (clazz == double.class) |
| 181 |
return "D"; |
| 182 |
return "L" + clazz.getName().replace('.', '/') + ";"; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Returns JNI-compliant description of the specified method. For example, |
| 187 |
* for <code>void installUI(JButton button)</code> this function will |
| 188 |
* return <code>(Ljavax/swing/JButton;)V</code>. |
| 189 |
* |
| 190 |
* @param method |
| 191 |
* Method. |
| 192 |
* @return JNI-compliant method description. |
| 193 |
*/ |
| 194 |
public static String getMethodDesc(Method method) { |
| 195 |
StringBuffer result = new StringBuffer(); |
| 196 |
result.append("("); |
| 197 |
Class<?>[] paramClasses = method.getParameterTypes(); |
| 198 |
for (int i = 0; i < paramClasses.length; i++) { |
| 199 |
Class<?> paramClass = paramClasses[i]; |
| 200 |
result.append(Utils.getTypeDesc(paramClass)); |
| 201 |
} |
| 202 |
result.append(")"); |
| 203 |
result.append(Utils.getTypeDesc(method.getReturnType())); |
| 204 |
return result.toString(); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Test app. |
| 209 |
* |
| 210 |
* @param args |
| 211 |
*/ |
| 212 |
public static void main(String[] args) { |
| 213 |
for (Map.Entry<String, String> entry : Utils.instance.lafMap.entrySet()) { |
| 214 |
//Map.Entry entry = (Map.Entry) it.next(); |
| 215 |
String lafClassName = entry.getKey(); |
| 216 |
System.out.println(lafClassName); |
| 217 |
String prefix = entry.getValue(); |
| 218 |
for (int i = 0; i < Utils.UI_IDS.length; i++) { |
| 219 |
String uiClassName = prefix + Utils.UI_IDS[i]; |
| 220 |
try { |
| 221 |
Class<?> uiClazz = Class.forName(uiClassName); |
| 222 |
System.out.println("\t" + Utils.UI_IDS[i]); |
| 223 |
Constructor<?>[] ctrs = uiClazz.getDeclaredConstructors(); |
| 224 |
for (int j = 0; j < ctrs.length; j++) { |
| 225 |
Constructor<?> ctr = ctrs[j]; |
| 226 |
Class<?>[] ctrArgs = ctr.getParameterTypes(); |
| 227 |
System.out.print("\t\t" + ctrArgs.length + " args : "); |
| 228 |
for (int k = 0; k < ctrArgs.length; k++) |
| 229 |
System.out.print(ctrArgs[k].getName() + ","); |
| 230 |
System.out.println(); |
| 231 |
} |
| 232 |
} catch (ClassNotFoundException cnfe) { |
| 233 |
} catch (Exception e) { |
| 234 |
e.printStackTrace(); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
} |