| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import java.awt.Component; |
| 4 |
import java.awt.Graphics; |
| 5 |
import java.awt.Font; |
| 6 |
import java.awt.Graphics2D; |
| 7 |
import java.awt.RenderingHints; |
| 8 |
import java.util.HashMap; |
| 9 |
import java.util.Map; |
| 10 |
import javax.swing.DefaultListCellRenderer; |
| 11 |
import javax.swing.Icon; |
| 12 |
import javax.swing.JLabel; |
| 13 |
import javax.swing.JList; |
| 14 |
import org.astrogrid.samp.Client; |
| 15 |
import org.astrogrid.samp.Metadata; |
| 16 |
|
| 17 |
/** |
| 18 |
* List Cell Renderer for use with {@link org.astrogrid.samp.Client} objects. |
| 19 |
* |
| 20 |
* @author Mark Taylor |
| 21 |
* @since 16 Jul 2008 |
| 22 |
*/ |
| 23 |
class ClientListCellRenderer extends DefaultListCellRenderer { |
| 24 |
|
| 25 |
private Font[] labelFonts_; |
| 26 |
private IconStore iconStore_; |
| 27 |
private final Map addHints_; |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor. |
| 31 |
*/ |
| 32 |
public ClientListCellRenderer() { |
| 33 |
iconStore_ = new IconStore( IconStore.createEmptyIcon( 16 ) ); |
| 34 |
addHints_ = new HashMap(); |
| 35 |
addHints_.put( RenderingHints.KEY_RENDERING, |
| 36 |
RenderingHints.VALUE_RENDER_QUALITY ); |
| 37 |
addHints_.put( RenderingHints.KEY_INTERPOLATION, |
| 38 |
RenderingHints.VALUE_INTERPOLATION_BICUBIC ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Attempts to return a human-readable text label for the given client. |
| 43 |
* |
| 44 |
* @param client to find label for |
| 45 |
* @return human-readable label for client if available; if nothing |
| 46 |
* better than the public ID can be found, null is returned |
| 47 |
*/ |
| 48 |
protected String getLabel( Client client ) { |
| 49 |
Metadata meta = client.getMetadata(); |
| 50 |
return meta != null ? meta.getName() |
| 51 |
: null; |
| 52 |
} |
| 53 |
|
| 54 |
protected void paintComponent( Graphics g ) { |
| 55 |
|
| 56 |
// Improve the rendering as much as possible, since we are typically |
| 57 |
// rendering some very small graphics that can look ugly if |
| 58 |
// the resampling puts pixels out of place. |
| 59 |
Graphics2D g2 = (Graphics2D) g; |
| 60 |
RenderingHints oldHints = g2.getRenderingHints(); |
| 61 |
g2.addRenderingHints( addHints_ ); |
| 62 |
super.paintComponent( g ); |
| 63 |
g2.setRenderingHints( oldHints ); |
| 64 |
} |
| 65 |
|
| 66 |
public Component getListCellRendererComponent( JList list, Object value, |
| 67 |
int index, boolean isSel, |
| 68 |
boolean hasFocus ) { |
| 69 |
Component c = super.getListCellRendererComponent( list, value, index, |
| 70 |
isSel, hasFocus ); |
| 71 |
if ( c instanceof JLabel && value instanceof Client ) { |
| 72 |
JLabel jl = (JLabel) c; |
| 73 |
Client client = (Client) value; |
| 74 |
String label = getLabel( client ); |
| 75 |
String text = label == null ? client.getId() : label; |
| 76 |
Font font = getLabelFont( label == null ); |
| 77 |
int size; |
| 78 |
try { |
| 79 |
size = (int) |
| 80 |
Math.ceil( font.getMaxCharBounds( ((Graphics2D) |
| 81 |
list.getGraphics()) |
| 82 |
.getFontRenderContext() ) |
| 83 |
.getHeight() ); |
| 84 |
} |
| 85 |
catch ( NullPointerException e ) { |
| 86 |
size = 16; |
| 87 |
} |
| 88 |
jl.setText( text ); |
| 89 |
jl.setFont( font ); |
| 90 |
jl.setIcon( reshapeIcon( iconStore_.getIcon( client ), size ) ); |
| 91 |
} |
| 92 |
return c; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns the font used by this label, or a variant. |
| 97 |
* |
| 98 |
* @param special true if the font is to look a bit different |
| 99 |
* @return font |
| 100 |
*/ |
| 101 |
private Font getLabelFont( boolean special ) { |
| 102 |
if ( labelFonts_ == null ) { |
| 103 |
Font normalFont = getFont().deriveFont( Font.BOLD ); |
| 104 |
Font aliasFont = getFont().deriveFont( Font.PLAIN ); |
| 105 |
labelFonts_ = new Font[] { normalFont, aliasFont }; |
| 106 |
} |
| 107 |
return labelFonts_[ special ? 1 : 0 ]; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Modifies an icon so that it has a fixed shape and positioning. |
| 112 |
* |
| 113 |
* @param icon input icon |
| 114 |
* @param height fixed icon height |
| 115 |
* @return reshaped icon |
| 116 |
*/ |
| 117 |
static Icon reshapeIcon( Icon icon, final int height ) { |
| 118 |
double aspect = 2.0; |
| 119 |
final int width = (int) Math.ceil( aspect * height ); |
| 120 |
final Icon sIcon = IconStore.scaleIcon( icon, height, aspect, true ); |
| 121 |
final int xoff = ( width - sIcon.getIconWidth() ) / 2; |
| 122 |
return new Icon() { |
| 123 |
public int getIconWidth() { |
| 124 |
return width; |
| 125 |
} |
| 126 |
public int getIconHeight() { |
| 127 |
return height; |
| 128 |
} |
| 129 |
public void paintIcon( Component c, Graphics g, int x, int y ) { |
| 130 |
sIcon.paintIcon( c, g, x + xoff, y ); |
| 131 |
} |
| 132 |
}; |
| 133 |
} |
| 134 |
} |