| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import java.awt.Color; |
| 4 |
import java.awt.Component; |
| 5 |
import java.awt.Graphics; |
| 6 |
import java.awt.Graphics2D; |
| 7 |
import java.awt.geom.AffineTransform; |
| 8 |
import java.io.IOException; |
| 9 |
import java.net.MalformedURLException; |
| 10 |
import java.net.URL; |
| 11 |
import java.util.HashMap; |
| 12 |
import java.util.Map; |
| 13 |
import java.util.logging.Logger; |
| 14 |
import javax.swing.Icon; |
| 15 |
import javax.swing.ImageIcon; |
| 16 |
import org.astrogrid.samp.Client; |
| 17 |
import org.astrogrid.samp.Metadata; |
| 18 |
|
| 19 |
/** |
| 20 |
* Manages client icons. Images are cached where appropriate. |
| 21 |
* A size may be supplied so that all icons returned by this object's methods |
| 22 |
* are of a given standard size. |
| 23 |
* Also provides some icon utility methods. |
| 24 |
* |
| 25 |
* @author Mark Taylor |
| 26 |
* @since 17 Nov 2008 |
| 27 |
*/ |
| 28 |
public class IconStore { |
| 29 |
|
| 30 |
private final Icon defaultIcon_; |
| 31 |
|
| 32 |
private static final Map urlIconMap_ = new HashMap(); |
| 33 |
private static final Logger logger_ = |
| 34 |
Logger.getLogger( IconStore.class.getName() ); |
| 35 |
|
| 36 |
/** |
| 37 |
* Constructor. |
| 38 |
* |
| 39 |
* @param defaultIcon icon returned if no client icon is available |
| 40 |
*/ |
| 41 |
public IconStore( Icon defaultIcon ) { |
| 42 |
defaultIcon_ = defaultIcon; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Returns the icon supplied by the graphic file at a given URL. |
| 47 |
* Icons are cached, so that repeated invocations with the same url |
| 48 |
* are not expensive. |
| 49 |
* |
| 50 |
* @param url URL of image |
| 51 |
* @return image icon, resized if appropriate |
| 52 |
*/ |
| 53 |
public Icon getIcon( String url ) { |
| 54 |
if ( ! urlIconMap_.containsKey( url ) ) { |
| 55 |
try { |
| 56 |
Icon icon = readIcon( url, 5 ); |
| 57 |
synchronized ( urlIconMap_ ) { |
| 58 |
urlIconMap_.put( url, icon ); |
| 59 |
} |
| 60 |
} |
| 61 |
catch ( IOException e ) { |
| 62 |
logger_.warning( "Icon not found \"" + url + "\" " + e ); |
| 63 |
synchronized ( urlIconMap_ ) { |
| 64 |
urlIconMap_.put( url, defaultIcon_ ); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
Icon icon = (Icon) urlIconMap_.get( url ); |
| 69 |
if ( icon.getIconWidth() < 0 ) { |
| 70 |
icon = defaultIcon_; |
| 71 |
} |
| 72 |
return icon; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Returns the icon associated with a given client. |
| 77 |
* This is either the icon described in its metadata or the default icon |
| 78 |
* if there isn't one. |
| 79 |
* |
| 80 |
* @param client client whose icon is required |
| 81 |
* @return associated icon, resized if appropriate |
| 82 |
*/ |
| 83 |
public Icon getIcon( Client client ) { |
| 84 |
Metadata meta = client.getMetadata(); |
| 85 |
if ( meta != null ) { |
| 86 |
Object url = meta.get( Metadata.ICONURL_KEY ); |
| 87 |
if ( url instanceof String ) { |
| 88 |
return getIcon( (String) url ); |
| 89 |
} |
| 90 |
} |
| 91 |
return defaultIcon_; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Returns an icon with no content but a given size. |
| 96 |
* |
| 97 |
* @param size edge size in pixels |
| 98 |
* @return emtpy square icon |
| 99 |
*/ |
| 100 |
public static Icon createEmptyIcon( final int size ) { |
| 101 |
return new Icon() { |
| 102 |
public int getIconWidth() { |
| 103 |
return size; |
| 104 |
} |
| 105 |
public int getIconHeight() { |
| 106 |
return size; |
| 107 |
} |
| 108 |
public void paintIcon( Component c, Graphics g, int x, int y ) { |
| 109 |
} |
| 110 |
}; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Returns an icon which indicates a shape but doesn't look like much. |
| 115 |
* Currently it's a kind of open square. |
| 116 |
* |
| 117 |
* @param size dimension in pixels |
| 118 |
* @return minimal icon |
| 119 |
*/ |
| 120 |
public static Icon createMinimalIcon( final int size ) { |
| 121 |
return new Icon() { |
| 122 |
int gap = 2; |
| 123 |
int size = 24; |
| 124 |
public int getIconWidth() { |
| 125 |
return size; |
| 126 |
} |
| 127 |
public int getIconHeight() { |
| 128 |
return size; |
| 129 |
} |
| 130 |
public void paintIcon( Component c, Graphics g, int x, int y ) { |
| 131 |
Color color = g.getColor(); |
| 132 |
int nl = 5; |
| 133 |
for ( int i = 0; i < nl; i++ ) { |
| 134 |
int lo = gap + i; |
| 135 |
int dim = size - 2 * ( gap + i ); |
| 136 |
if ( dim <= 0 ) { |
| 137 |
break; |
| 138 |
} |
| 139 |
int glevel = 255 * ( i + 1 ) / ( nl + 1 ); |
| 140 |
g.setColor( new Color( glevel, glevel, glevel ) ); |
| 141 |
g.drawRect( x + lo, y + lo, dim, dim ); |
| 142 |
} |
| 143 |
g.setColor( color ); |
| 144 |
} |
| 145 |
}; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Constructs an icon given a file name in the images directory. |
| 150 |
* |
| 151 |
* @param fileName file name omitting directory |
| 152 |
* @return icon |
| 153 |
*/ |
| 154 |
static Icon createResourceIcon( String fileName ) { |
| 155 |
String relLoc = "images/" + fileName; |
| 156 |
URL resource = Client.class.getResource( relLoc ); |
| 157 |
if ( resource != null ) { |
| 158 |
return new ImageIcon( resource ); |
| 159 |
} |
| 160 |
else { |
| 161 |
logger_.warning( "Failed to load icon " + relLoc ); |
| 162 |
return new Icon() { |
| 163 |
public int getIconWidth() { |
| 164 |
return 24; |
| 165 |
} |
| 166 |
public int getIconHeight() { |
| 167 |
return 24; |
| 168 |
} |
| 169 |
public void paintIcon( Component c, Graphics g, int x, int y ) { |
| 170 |
} |
| 171 |
}; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Return an icon based on an existing one, but drawn to an exact size. |
| 177 |
* |
| 178 |
* @param icon original icon, or null for blank |
| 179 |
* @param size number of horizontal and vertical pixels in output |
| 180 |
* @return resized version of <code>icon</code> |
| 181 |
*/ |
| 182 |
public static Icon sizeIcon( Icon icon, final int size ) { |
| 183 |
if ( icon == null ) { |
| 184 |
return new Icon() { |
| 185 |
public int getIconWidth() { |
| 186 |
return size; |
| 187 |
} |
| 188 |
public int getIconHeight() { |
| 189 |
return size; |
| 190 |
} |
| 191 |
public void paintIcon( Component c, Graphics g, int x, int y ) { |
| 192 |
} |
| 193 |
}; |
| 194 |
} |
| 195 |
else if ( icon.getIconWidth() == size && |
| 196 |
icon.getIconHeight() == size ) { |
| 197 |
return icon; |
| 198 |
} |
| 199 |
else { |
| 200 |
return new SizedIcon( icon, size ); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Icon implementation which is rescaled to so that one dimension |
| 206 |
* (either width or height) has a fixed value. |
| 207 |
* |
| 208 |
* @param icon input icon |
| 209 |
* @param fixDim the fixed dimension in pixels |
| 210 |
* @param maxAspect maximum aspect ratio (>= 1) |
| 211 |
* @param fixVertical true to fix height, false to fix width |
| 212 |
*/ |
| 213 |
public static Icon scaleIcon( final Icon icon, final int fixDim, |
| 214 |
double maxAspect, boolean fixVertical ) { |
| 215 |
final int w = icon.getIconWidth(); |
| 216 |
final int h = icon.getIconHeight(); |
| 217 |
if ( ( fixVertical ? h : w ) == fixDim && |
| 218 |
( fixVertical ? h / (double) w |
| 219 |
: w / (double) h ) <= maxAspect ) { |
| 220 |
return icon; |
| 221 |
} |
| 222 |
double factor = fixDim / (double) ( fixVertical ? h : w ); |
| 223 |
if ( factor > 1.0 && factor < 2.0 ) { |
| 224 |
factor = 1.0; |
| 225 |
} |
| 226 |
double aspect = factor * ( fixVertical ? h : w ) / fixDim; |
| 227 |
if ( aspect > maxAspect ) { |
| 228 |
factor *= maxAspect / aspect; |
| 229 |
} |
| 230 |
final int width = fixVertical ? (int) Math.ceil( factor * w ) : fixDim; |
| 231 |
final int height = fixVertical ? fixDim : (int) Math.ceil( factor * h ); |
| 232 |
final double fact = factor; |
| 233 |
return new Icon() { |
| 234 |
public int getIconWidth() { |
| 235 |
return width; |
| 236 |
} |
| 237 |
public int getIconHeight() { |
| 238 |
return height; |
| 239 |
} |
| 240 |
public void paintIcon( Component c, Graphics g, int x, int y ) { |
| 241 |
if ( fact == 1.0 ) { |
| 242 |
icon.paintIcon( c, g, x + ( width - w ) / 2, |
| 243 |
y + ( height - h ) / 2 ); |
| 244 |
} |
| 245 |
else { |
| 246 |
Graphics2D g2 = (Graphics2D) g; |
| 247 |
AffineTransform trans = g2.getTransform(); |
| 248 |
g2.translate( x + ( width - w * fact ) / 2, |
| 249 |
y + ( height - h * fact ) / 2 ); |
| 250 |
g2.scale( fact, fact ); |
| 251 |
icon.paintIcon( c, g2, 0, 0 ); |
| 252 |
g2.setTransform( trans ); |
| 253 |
} |
| 254 |
} |
| 255 |
}; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Reads an icon from a URL, with a maximum wait time. |
| 260 |
* If the timeout is exceeded, an exception will be thrown. |
| 261 |
* |
| 262 |
* @param url icon URL |
| 263 |
* @param waitSecs maximum time in seconds to wait |
| 264 |
* @return icon from url |
| 265 |
* @throws IOException if timeout has been exceeded |
| 266 |
*/ |
| 267 |
private static Icon readIcon( String url, int waitSecs ) |
| 268 |
throws IOException { |
| 269 |
final URL urlLoc = new URL( url ); |
| 270 |
final Icon[] icons = new Icon[ 1 ]; |
| 271 |
Thread loader = new Thread( "IconLoader " + url ) { |
| 272 |
public void run() { |
| 273 |
icons[ 0 ] = new ImageIcon( urlLoc ); |
| 274 |
} |
| 275 |
}; |
| 276 |
loader.start(); |
| 277 |
try { |
| 278 |
loader.join( waitSecs * 1000 ); |
| 279 |
Icon icon = icons[ 0 ]; |
| 280 |
if ( icon != null ) { |
| 281 |
return icon; |
| 282 |
} |
| 283 |
else { |
| 284 |
throw new IOException( "Icon load timeout (" |
| 285 |
+ waitSecs + "s)" ); |
| 286 |
} |
| 287 |
} |
| 288 |
catch ( InterruptedException e ) { |
| 289 |
throw (IOException) new IOException( "Load interrupted" ) |
| 290 |
.initCause( e ); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Icon implementation which looks like an existing one, but is resized |
| 296 |
* down if necessary. |
| 297 |
*/ |
| 298 |
private static class SizedIcon implements Icon { |
| 299 |
private final Icon icon_; |
| 300 |
private final int size_; |
| 301 |
private final double factor_; |
| 302 |
|
| 303 |
/** |
| 304 |
* Constructor. |
| 305 |
* |
| 306 |
* @param icon original icon |
| 307 |
* @param size number of horizontal and vertical pixels in this icon |
| 308 |
*/ |
| 309 |
public SizedIcon( Icon icon, int size ) { |
| 310 |
icon_ = icon; |
| 311 |
size_ = size; |
| 312 |
factor_ = |
| 313 |
Math.min( 1.0, |
| 314 |
Math.min( size / (double) icon.getIconWidth(), |
| 315 |
size / (double) icon.getIconHeight() ) ); |
| 316 |
} |
| 317 |
|
| 318 |
public int getIconWidth() { |
| 319 |
return size_; |
| 320 |
} |
| 321 |
|
| 322 |
public int getIconHeight() { |
| 323 |
return size_; |
| 324 |
} |
| 325 |
|
| 326 |
public void paintIcon( Component c, Graphics g, int x, int y ) { |
| 327 |
int iw = icon_.getIconWidth(); |
| 328 |
int ih = icon_.getIconHeight(); |
| 329 |
if ( factor_ == 1.0 ) { |
| 330 |
icon_.paintIcon( c, g, x + ( size_ - iw ) / 2, |
| 331 |
y + ( size_ - ih ) / 2 ); |
| 332 |
} |
| 333 |
else { |
| 334 |
Graphics2D g2 = (Graphics2D) g; |
| 335 |
AffineTransform trans = g2.getTransform(); |
| 336 |
g2.translate( x + ( size_ - iw * factor_ ) / 2, |
| 337 |
y + ( size_ - ih * factor_ ) / 2 ); |
| 338 |
g2.scale( factor_, factor_ ); |
| 339 |
icon_.paintIcon( c, g2, 0, 0 ); |
| 340 |
g2.setTransform( trans ); |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
} |