| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
package tsukuba_bunko.peko.resource; |
| 10 |
|
|
| 11 |
|
import java.awt.Color; |
| 12 |
|
|
| 13 |
|
import java.util.Map; |
| 14 |
|
|
| 15 |
|
import java.lang.ref.WeakReference; |
| 16 |
|
|
| 17 |
|
import tsukuba_bunko.peko.Logger; |
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
public class ColorManager { |
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
0 |
private static ColorManager _instance = null; |
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
0 |
private Map _cache = null; |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
private ColorManager() |
| 42 |
|
{ |
| 43 |
0 |
super(); |
| 44 |
0 |
} |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
private void initialize() |
| 51 |
|
{ |
| 52 |
0 |
_cache = new java.util.HashMap( 89 ); |
| 53 |
0 |
_cache.put( "black", new WeakReference(Color.black) ); |
| 54 |
0 |
_cache.put( "white", new WeakReference(Color.white) ); |
| 55 |
0 |
_cache.put( "red", new WeakReference(Color.red) ); |
| 56 |
0 |
_cache.put( "blue", new WeakReference(Color.blue) ); |
| 57 |
0 |
_cache.put( "green", new WeakReference(Color.green) ); |
| 58 |
0 |
_cache.put( "yellow", new WeakReference(Color.yellow) ); |
| 59 |
0 |
_cache.put( "gray", new WeakReference(Color.gray) ); |
| 60 |
0 |
_cache.put( "darkGray", new WeakReference(Color.darkGray) ); |
| 61 |
0 |
_cache.put( "lightGray", new WeakReference(Color.lightGray) ); |
| 62 |
0 |
_cache.put( "cyan", new WeakReference(Color.cyan) ); |
| 63 |
0 |
_cache.put( "magenta", new WeakReference(Color.magenta) ); |
| 64 |
0 |
_cache.put( "orange", new WeakReference(Color.orange) ); |
| 65 |
0 |
_cache.put( "pink", new WeakReference(Color.pink) ); |
| 66 |
0 |
_cache.put( null, new WeakReference(Color.black) ); |
| 67 |
0 |
} |
| 68 |
|
|
| 69 |
|
public Color getColor( String name ) |
| 70 |
|
{ |
| 71 |
0 |
Color color = null; |
| 72 |
|
|
| 73 |
0 |
Object cache = _cache.get( name ); |
| 74 |
0 |
if( cache != null ) { |
| 75 |
0 |
color = (Color)((WeakReference)cache).get(); |
| 76 |
|
} |
| 77 |
|
|
| 78 |
0 |
if( color == null ) { |
| 79 |
0 |
if( name.startsWith("#") ) { |
| 80 |
|
try { |
| 81 |
0 |
color = new Color( Integer.parseInt(name.substring(1), 16) ); |
| 82 |
0 |
WeakReference ref = new WeakReference( color ); |
| 83 |
0 |
_cache.put( name, ref ); |
| 84 |
|
} |
| 85 |
0 |
catch( Exception e ) { |
| 86 |
0 |
Logger.error( "[resource] invalid color value specified. :" + name, e ); |
| 87 |
0 |
Logger.debug( "[resource] using default color. (black)" ); |
| 88 |
0 |
color = Color.black; |
| 89 |
0 |
} |
| 90 |
0 |
} |
| 91 |
|
else { |
| 92 |
0 |
Logger.error( "[resource] invalid color name specified. :" + name ); |
| 93 |
0 |
Logger.debug( "[resource] using default color. (black)" ); |
| 94 |
0 |
color = Color.black; |
| 95 |
|
} |
| 96 |
|
} |
| 97 |
0 |
return color; |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
public static ColorManager getInstance() |
| 106 |
|
{ |
| 107 |
0 |
if( _instance == null ) { |
| 108 |
0 |
synchronized( ColorManager.class ) { |
| 109 |
0 |
if( _instance == null ) { |
| 110 |
0 |
_instance = new ColorManager(); |
| 111 |
0 |
_instance.initialize(); |
| 112 |
|
} |
| 113 |
0 |
} |
| 114 |
|
} |
| 115 |
0 |
return _instance; |
| 116 |
|
} |
| 117 |
|
} |