| 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; |
| 31 |
|
| 32 |
import java.io.IOException; |
| 33 |
import java.io.InputStream; |
| 34 |
import java.net.URL; |
| 35 |
import java.util.*; |
| 36 |
|
| 37 |
import javax.swing.JComponent; |
| 38 |
|
| 39 |
/** |
| 40 |
* Repository of LAF widgets. |
| 41 |
* |
| 42 |
* @author Kirill Grouchnikov |
| 43 |
*/ |
| 44 |
public class LafWidgetRepository { |
| 45 |
/** |
| 46 |
* All registered widgets. Key is {@link Class} in the UI component |
| 47 |
* hierarchy, value is a {@link Set} of fully-qualified widget class names. |
| 48 |
*/ |
| 49 |
protected Map<Class<?>, Set<LafWidgetClassInfo>> widgets; |
| 50 |
|
| 51 |
/** |
| 52 |
* Contains fully qualified class names of widgets that should not be |
| 53 |
* installed on any components. |
| 54 |
*/ |
| 55 |
protected Set<String> widgetClassesToIgnore; |
| 56 |
|
| 57 |
/** |
| 58 |
* Currently registered LAF support. |
| 59 |
*/ |
| 60 |
protected LafWidgetSupport lafSupport; |
| 61 |
|
| 62 |
/** |
| 63 |
* Indicates whether the currently registered LAF support is custom (not |
| 64 |
* {@link LafWidgetSupport}). |
| 65 |
*/ |
| 66 |
protected boolean isCustomLafSupportSet; |
| 67 |
|
| 68 |
/** |
| 69 |
* Singleton instance. |
| 70 |
*/ |
| 71 |
protected static LafWidgetRepository repository; |
| 72 |
|
| 73 |
/** |
| 74 |
* Resource bundle for <b>Substance</b> labels. |
| 75 |
*/ |
| 76 |
private static ResourceBundle LABEL_BUNDLE = null; |
| 77 |
|
| 78 |
/** |
| 79 |
* Class loader for the {@link #LABEL_BUNDLE}. |
| 80 |
* |
| 81 |
* @since version 1.1 |
| 82 |
*/ |
| 83 |
private static ClassLoader labelBundleClassLoader; |
| 84 |
|
| 85 |
/** |
| 86 |
* Information on a single class. |
| 87 |
* |
| 88 |
* @author Kirill Grouchnikov |
| 89 |
*/ |
| 90 |
protected static class LafWidgetClassInfo { |
| 91 |
/** |
| 92 |
* Class name. |
| 93 |
*/ |
| 94 |
public String className; |
| 95 |
|
| 96 |
/** |
| 97 |
* Indicates whether the matching should be exact. |
| 98 |
*/ |
| 99 |
public boolean isExact; |
| 100 |
|
| 101 |
/** |
| 102 |
* Creates a new info object. |
| 103 |
* |
| 104 |
* @param className |
| 105 |
* Class name. |
| 106 |
* @param isExact |
| 107 |
* Indicates whether the matching should be exact. |
| 108 |
*/ |
| 109 |
public LafWidgetClassInfo(String className, boolean isExact) { |
| 110 |
this.className = className; |
| 111 |
this.isExact = isExact; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Creates a new repository. Marked private to enforce single instance. |
| 117 |
*/ |
| 118 |
private LafWidgetRepository() { |
| 119 |
this.widgets = new HashMap<Class<?>, Set<LafWidgetClassInfo>>(); |
| 120 |
this.lafSupport = new LafWidgetSupport(); |
| 121 |
this.isCustomLafSupportSet = false; |
| 122 |
this.widgetClassesToIgnore = new HashSet<String>(); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Returns the widget repository. |
| 127 |
* |
| 128 |
* @return Widget repository. |
| 129 |
*/ |
| 130 |
public static synchronized LafWidgetRepository getRepository() { |
| 131 |
if (LafWidgetRepository.repository == null) { |
| 132 |
LafWidgetRepository.repository = new LafWidgetRepository(); |
| 133 |
LafWidgetRepository.repository.populate(); |
| 134 |
} |
| 135 |
return LafWidgetRepository.repository; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Populates the repository from the specified URL. The URL should point to |
| 140 |
* a properties file, the key being the fully-qualified class name of the |
| 141 |
* widget implementation, the value being semicolon-separated |
| 142 |
* fully-qualified class names of classes in UI component hierarchy. Sample |
| 143 |
* property file: |
| 144 |
* |
| 145 |
* <pre> |
| 146 |
* org.pushingpixels.lafwidget.text.PasswordStrengthCheckerWidget = javax.swing.JPasswordField |
| 147 |
* org.pushingpixels.lafwidget.text.LockBorderWidget = javax.swing.text.JTextComponent;javax.swing.JComboBox |
| 148 |
* </pre> |
| 149 |
* |
| 150 |
* @param url |
| 151 |
* URL that points to a properties file. |
| 152 |
*/ |
| 153 |
protected void populateFrom(URL url) { |
| 154 |
Properties props = new Properties(); |
| 155 |
InputStream is = null; |
| 156 |
try { |
| 157 |
is = url.openStream(); |
| 158 |
props.load(is); |
| 159 |
|
| 160 |
Enumeration<?> names = props.propertyNames(); |
| 161 |
while (names.hasMoreElements()) { |
| 162 |
String name = (String) names.nextElement(); |
| 163 |
String value = props.getProperty(name); |
| 164 |
String[] values = value.split(";"); |
| 165 |
for (int i = 0; i < values.length; i++) { |
| 166 |
String className = values[i].trim(); |
| 167 |
boolean isExact = className.startsWith("%"); |
| 168 |
if (isExact) |
| 169 |
className = className.substring(1); |
| 170 |
try { |
| 171 |
this.registerWidget(name, Class.forName(className), |
| 172 |
isExact); |
| 173 |
} catch (ClassNotFoundException cnfe) { |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
} catch (IOException ioe) { |
| 179 |
} finally { |
| 180 |
if (is != null) { |
| 181 |
try { |
| 182 |
is.close(); |
| 183 |
} catch (IOException ioe) { |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Populates the widget repository. The classpath is scanned for all |
| 191 |
* resources that match the name <code>META-INF/lafwidget.properties</code>. |
| 192 |
* |
| 193 |
* @see #populateFrom(URL) |
| 194 |
*/ |
| 195 |
public void populate() { |
| 196 |
// the following is fix by Dag Joar and Christian Schlichtherle |
| 197 |
// for application running with -Xbootclasspath VM flag. In this case, |
| 198 |
// the using MyClass.class.getClassLoader() would return null, |
| 199 |
// but the context class loader will function properly |
| 200 |
// that classes will be properly loaded regardless of whether the lib is |
| 201 |
// added to the system class path, the extension class path and |
| 202 |
// regardless of the class loader architecture set up by some |
| 203 |
// frameworks. |
| 204 |
ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
| 205 |
|
| 206 |
try { |
| 207 |
Enumeration<?> rs = cl |
| 208 |
.getResources("META-INF/lafwidget.properties"); |
| 209 |
while (rs.hasMoreElements()) { |
| 210 |
URL rUrl = (URL) rs.nextElement(); |
| 211 |
this.populateFrom(rUrl); |
| 212 |
} |
| 213 |
} catch (IOException ioe) { |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Registers a new widget for the specified UI classes. The list should |
| 219 |
* contain {@link Class} instances. |
| 220 |
* |
| 221 |
* @param widgetClassName |
| 222 |
* Full-qualified class name for the widget. |
| 223 |
* @param supportedClasses |
| 224 |
* Classes supported by the widget. |
| 225 |
*/ |
| 226 |
public synchronized void registerWidget(String widgetClassName, |
| 227 |
List<Class<?>> supportedClasses) { |
| 228 |
for (Class<?> clazz : supportedClasses) |
| 229 |
this.registerWidget(widgetClassName, clazz, false); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Registers a new widget for the specified UI class. |
| 234 |
* |
| 235 |
* @param widgetClassName |
| 236 |
* Full-qualified class name for the widget. |
| 237 |
* @param supportedClass |
| 238 |
* Class supported by the widget. |
| 239 |
* @param isExact |
| 240 |
* if <code>true</code>, the widget will be available only for |
| 241 |
* the components of the specified class. If <code>false</code>, |
| 242 |
* the widget be available for the components of the specified |
| 243 |
* class and all its descendants (as defined in the |
| 244 |
* {@link Class#isAssignableFrom(Class)} ). |
| 245 |
*/ |
| 246 |
public synchronized void registerWidget(String widgetClassName, |
| 247 |
Class<?> supportedClass, boolean isExact) { |
| 248 |
if (JComponent.class.isAssignableFrom(supportedClass)) { |
| 249 |
if (!this.widgets.containsKey(supportedClass)) |
| 250 |
this.widgets.put(supportedClass, |
| 251 |
new HashSet<LafWidgetClassInfo>()); |
| 252 |
} |
| 253 |
// Guard against multiple registrations of the same widget. |
| 254 |
// This can happen if more than one jar on the classpath defines |
| 255 |
// the same widget. |
| 256 |
for (LafWidgetClassInfo registered : this.widgets.get(supportedClass)) { |
| 257 |
if (registered.className.equals(widgetClassName)) |
| 258 |
return; |
| 259 |
} |
| 260 |
this.widgets.get(supportedClass).add( |
| 261 |
new LafWidgetClassInfo(widgetClassName, isExact)); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Returns a set of widgets that match the specified component. The |
| 266 |
* component hierarchy is scanned bottom-up and all matching widget classes |
| 267 |
* are used to instantiate new instance of widgets. In case the |
| 268 |
* {@link #isCustomLafSupportSet} is <code>false</code>, only widgets that |
| 269 |
* return <code>false</code> in {@link LafWidget#requiresCustomLafSupport()} |
| 270 |
* are returned. |
| 271 |
* |
| 272 |
* |
| 273 |
* @param jcomp |
| 274 |
* UI component. |
| 275 |
* @return Set of widgets that match the specified component. |
| 276 |
*/ |
| 277 |
public synchronized Set<LafWidget> getMatchingWidgets(JComponent jcomp) { |
| 278 |
Set<LafWidget> result = new HashSet<LafWidget>(); |
| 279 |
Class<?> clazz = jcomp.getClass(); |
| 280 |
boolean isOriginator = true; |
| 281 |
while (clazz != null) { |
| 282 |
Set<LafWidgetClassInfo> registered = this.widgets.get(clazz); |
| 283 |
if (registered != null) { |
| 284 |
for (Iterator<LafWidgetClassInfo> it = registered.iterator(); it |
| 285 |
.hasNext();) { |
| 286 |
LafWidgetClassInfo widgetClassInfo = it.next(); |
| 287 |
if (widgetClassInfo.isExact && !isOriginator) |
| 288 |
continue; |
| 289 |
try { |
| 290 |
String widgetClassName = widgetClassInfo.className; |
| 291 |
// check if the application requested to ignore the |
| 292 |
// specific widget |
| 293 |
if (this.widgetClassesToIgnore |
| 294 |
.contains(widgetClassName)) |
| 295 |
continue; |
| 296 |
|
| 297 |
// The code below will fail if no such class exists. |
| 298 |
// This allows safely removing the relevant widget |
| 299 |
// classes making the jar size smaller (lite versions). |
| 300 |
Object widgetObj = Class.forName(widgetClassName) |
| 301 |
.newInstance(); |
| 302 |
if (widgetObj instanceof LafWidget) { |
| 303 |
LafWidget widget = (LafWidget) widgetObj; |
| 304 |
// only add widgets that do not require special LAF |
| 305 |
// support if no such support has been set. |
| 306 |
if (!widget.requiresCustomLafSupport() |
| 307 |
|| this.isCustomLafSupportSet) { |
| 308 |
widget.setComponent(jcomp); |
| 309 |
result.add(widget); |
| 310 |
} |
| 311 |
} |
| 312 |
// the exceptions are ignored - see the explanation |
| 313 |
// above. |
| 314 |
} catch (InstantiationException ie) { |
| 315 |
} catch (IllegalAccessException iae) { |
| 316 |
} catch (ClassNotFoundException cnfe) { |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
clazz = clazz.getSuperclass(); |
| 321 |
isOriginator = false; |
| 322 |
} |
| 323 |
return result; |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Sets LAF support. |
| 328 |
* |
| 329 |
* @param lafSupport |
| 330 |
* LAF support. |
| 331 |
* @throws IllegalArgumentException |
| 332 |
* If the LAF support is <code>null</code>. |
| 333 |
*/ |
| 334 |
public void setLafSupport(LafWidgetSupport lafSupport) { |
| 335 |
if (lafSupport == null) |
| 336 |
throw new IllegalArgumentException("LAF support can't be null"); |
| 337 |
this.lafSupport = lafSupport; |
| 338 |
this.isCustomLafSupportSet = (this.lafSupport.getClass() != LafWidgetSupport.class); |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Unsets custom LAF support and reverts to the base LAF support. |
| 343 |
*/ |
| 344 |
public void unsetLafSupport() { |
| 345 |
this.lafSupport = new LafWidgetSupport(); |
| 346 |
this.isCustomLafSupportSet = false; |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Returns the currently set LAF support. The result is guaranteed to be |
| 351 |
* non-<code>null</code>. |
| 352 |
* |
| 353 |
* @return Currently set non-<code>null</code> LAF support. |
| 354 |
*/ |
| 355 |
public LafWidgetSupport getLafSupport() { |
| 356 |
return this.lafSupport; |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Retrieves the current label bundle. |
| 361 |
* |
| 362 |
* @return The current label bundle. |
| 363 |
* @see #resetLabelBundle() |
| 364 |
*/ |
| 365 |
public static synchronized ResourceBundle getLabelBundle() { |
| 366 |
// fix for RFE 157 on Substance (allowing custom class loader for |
| 367 |
// resource bundles which can remove server calls |
| 368 |
// in applets) |
| 369 |
if (LafWidgetRepository.labelBundleClassLoader == null) { |
| 370 |
LafWidgetRepository.LABEL_BUNDLE = ResourceBundle.getBundle( |
| 371 |
"org.pushingpixels.lafwidget.resources.Labels", Locale |
| 372 |
.getDefault()); |
| 373 |
} else { |
| 374 |
LafWidgetRepository.LABEL_BUNDLE = ResourceBundle.getBundle( |
| 375 |
"org.pushingpixels.lafwidget.resources.Labels", Locale |
| 376 |
.getDefault(), |
| 377 |
LafWidgetRepository.labelBundleClassLoader); |
| 378 |
} |
| 379 |
return LafWidgetRepository.LABEL_BUNDLE; |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* Retrieves the label bundle for the specified locale. |
| 384 |
* |
| 385 |
* @param locale |
| 386 |
* Locale. |
| 387 |
* @return The label bundle for the specified locale. |
| 388 |
*/ |
| 389 |
public static synchronized ResourceBundle getLabelBundle(Locale locale) { |
| 390 |
// fix for RFE 157 on Substance (allowing custom class loader for |
| 391 |
// resource bundles which can remove server calls |
| 392 |
// in applets) |
| 393 |
if (LafWidgetRepository.labelBundleClassLoader == null) { |
| 394 |
return ResourceBundle.getBundle( |
| 395 |
"org.pushingpixels.lafwidget.resources.Labels", locale); |
| 396 |
} else { |
| 397 |
return ResourceBundle.getBundle( |
| 398 |
"org.pushingpixels.lafwidget.resources.Labels", locale, |
| 399 |
LafWidgetRepository.labelBundleClassLoader); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Resets the current label bundle. Useful when the application changes |
| 405 |
* Locale at runtime. |
| 406 |
* |
| 407 |
* @see #getLabelBundle() |
| 408 |
*/ |
| 409 |
public static synchronized void resetLabelBundle() { |
| 410 |
LafWidgetRepository.LABEL_BUNDLE = null; |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Sets the class loader for {@link #LABEL_BUNDLE}. |
| 415 |
* |
| 416 |
* @param labelBundleClassLoader |
| 417 |
* Class loader for {@link #LABEL_BUNDLE}. |
| 418 |
* @since version 1.1 |
| 419 |
*/ |
| 420 |
public static void setLabelBundleClassLoader( |
| 421 |
ClassLoader labelBundleClassLoader) { |
| 422 |
LafWidgetRepository.labelBundleClassLoader = labelBundleClassLoader; |
| 423 |
} |
| 424 |
|
| 425 |
/** |
| 426 |
* Marks widget with the specified class name to never be installed on any |
| 427 |
* components. |
| 428 |
* |
| 429 |
* @param widgetClassName |
| 430 |
* Fully qualified widget class name. |
| 431 |
*/ |
| 432 |
public synchronized void addToIgnoreWidgets(String widgetClassName) { |
| 433 |
this.widgetClassesToIgnore.add(widgetClassName); |
| 434 |
} |
| 435 |
} |