| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import java.awt.Color; |
| 4 |
import java.awt.Component; |
| 5 |
import java.awt.Dimension; |
| 6 |
import java.awt.Graphics; |
| 7 |
import java.awt.Insets; |
| 8 |
import java.awt.Point; |
| 9 |
import java.awt.Rectangle; |
| 10 |
import java.awt.event.MouseEvent; |
| 11 |
import javax.swing.BorderFactory; |
| 12 |
import javax.swing.Icon; |
| 13 |
import javax.swing.JComponent; |
| 14 |
import javax.swing.ListModel; |
| 15 |
import javax.swing.ToolTipManager; |
| 16 |
import javax.swing.event.ListDataEvent; |
| 17 |
import javax.swing.event.ListDataListener; |
| 18 |
|
| 19 |
/** |
| 20 |
* Icon which paints a graphical representation of a list of Transmissions. |
| 21 |
* |
| 22 |
* @author Mark Taylor |
| 23 |
* @since 21 Nov 2008 |
| 24 |
*/ |
| 25 |
public class TransmissionListIcon implements Icon { |
| 26 |
|
| 27 |
private final ListModel rxModel_; |
| 28 |
private final ListModel txModel_; |
| 29 |
private final int size_; |
| 30 |
private final int transIconWidth_; |
| 31 |
private final Icon targetIcon_; |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor. |
| 35 |
* |
| 36 |
* @param rxModel list of messages received; |
| 37 |
* all elements must be {@link Transmission} objects |
| 38 |
* @param txModel list of messages sent; |
| 39 |
* all elements must be {@link Transmission} objects |
| 40 |
* @param size height of icon in pixels; this also scales the width |
| 41 |
*/ |
| 42 |
public TransmissionListIcon( ListModel rxModel, ListModel txModel, |
| 43 |
int size ) { |
| 44 |
rxModel_ = rxModel; |
| 45 |
txModel_ = txModel; |
| 46 |
size_ = size; |
| 47 |
transIconWidth_ = (int) Math.floor( size_ * 0.866 ); // equilateral |
| 48 |
final boolean hasTx = txModel_ != null; |
| 49 |
final boolean hasRx = rxModel_ != null; |
| 50 |
targetIcon_ = new Icon() { |
| 51 |
public int getIconWidth() { |
| 52 |
return size_; |
| 53 |
} |
| 54 |
public int getIconHeight() { |
| 55 |
return size_; |
| 56 |
} |
| 57 |
public void paintIcon( Component c, Graphics g, int x, int y ) { |
| 58 |
g.drawOval( x + 1, y + 1, size_ - 2, size_ - 2 ); |
| 59 |
} |
| 60 |
}; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Returns the transmission (if any) which is painted at a given point. |
| 65 |
* |
| 66 |
* @param point screen point relative to the origin of this icon |
| 67 |
* @return transmission painted at <code>point</code> or null if there |
| 68 |
* isn't one |
| 69 |
*/ |
| 70 |
public Transmission getTransmissionAt( Point point ) { |
| 71 |
int x = point.x; |
| 72 |
int y = point.y; |
| 73 |
if ( x < 0 || x > getIconWidth() || y < 0 || y > getIconHeight() ) { |
| 74 |
return null; |
| 75 |
} |
| 76 |
int x0 = 0; |
| 77 |
|
| 78 |
int rxWidth = rxModel_.getSize() * transIconWidth_; |
| 79 |
if ( x - x0 < rxWidth ) { |
| 80 |
int ir = ( x - x0 ) / transIconWidth_; |
| 81 |
return (Transmission) rxModel_.getElementAt( ir ); |
| 82 |
} |
| 83 |
x0 += rxWidth; |
| 84 |
|
| 85 |
int targetWidth = targetIcon_.getIconWidth(); |
| 86 |
if ( x - x0 < targetWidth ) { |
| 87 |
return null; |
| 88 |
} |
| 89 |
x0 += targetWidth; |
| 90 |
|
| 91 |
int txWidth = txModel_.getSize() * transIconWidth_; |
| 92 |
if ( x - x0 < txWidth ) { |
| 93 |
int it = ( x - x0 ) / transIconWidth_; |
| 94 |
return (Transmission) txModel_.getElementAt( it ); |
| 95 |
} |
| 96 |
x0 += txWidth; |
| 97 |
|
| 98 |
assert x > x0; |
| 99 |
return null; |
| 100 |
} |
| 101 |
|
| 102 |
public int getIconWidth() { |
| 103 |
return ( rxModel_ != null ? rxModel_.getSize() * transIconWidth_ : 0 ) |
| 104 |
+ targetIcon_.getIconWidth() |
| 105 |
+ ( txModel_ != null ? txModel_.getSize() * transIconWidth_ : 0 ); |
| 106 |
} |
| 107 |
|
| 108 |
public int getIconHeight() { |
| 109 |
return size_; |
| 110 |
} |
| 111 |
|
| 112 |
public void paintIcon( Component c, Graphics g, int x, int y ) { |
| 113 |
if ( rxModel_ != null ) { |
| 114 |
for ( int i = 0; i < rxModel_.getSize(); i++ ) { |
| 115 |
Transmission trans = (Transmission) rxModel_.getElementAt( i ); |
| 116 |
Icon transIcon = getTransIcon( trans, false ); |
| 117 |
transIcon.paintIcon( c, g, x, y ); |
| 118 |
x += transIcon.getIconWidth(); |
| 119 |
} |
| 120 |
} |
| 121 |
targetIcon_.paintIcon( c, g, x, y ); |
| 122 |
x += targetIcon_.getIconWidth(); |
| 123 |
if ( txModel_ != null ) { |
| 124 |
for ( int i = 0; i < txModel_.getSize(); i++ ) { |
| 125 |
Transmission trans = (Transmission) txModel_.getElementAt( i ); |
| 126 |
Icon transIcon = getTransIcon( trans, true ); |
| 127 |
transIcon.paintIcon( c, g, x, y ); |
| 128 |
x += transIcon.getIconWidth(); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Returns an icon which can paint a particular transmission. |
| 135 |
* |
| 136 |
* @param trans transmission |
| 137 |
* @param isTx true if <code>trans</code> represents a send, |
| 138 |
* false if it represents a receive |
| 139 |
*/ |
| 140 |
private Icon getTransIcon( Transmission trans, final boolean isTx ) { |
| 141 |
return new Icon() { |
| 142 |
public int getIconHeight() { |
| 143 |
return size_; |
| 144 |
} |
| 145 |
public int getIconWidth() { |
| 146 |
return transIconWidth_; |
| 147 |
} |
| 148 |
public void paintIcon( Component c, Graphics g, int x, int y ) { |
| 149 |
int xlo = x + 1; |
| 150 |
int xhi = x + transIconWidth_ - 1; |
| 151 |
int[] xs = isTx ? new int[] { xhi, xlo, xhi, } |
| 152 |
: new int[] { xlo, xhi, xlo, }; |
| 153 |
int[] ys = new int[] { y, y + size_ / 2, y + size_ - 1 }; |
| 154 |
g.fillPolygon( xs, ys, 3 ); |
| 155 |
} |
| 156 |
}; |
| 157 |
} |
| 158 |
|
| 159 |
public JComponent createBox( int nTrans ) { |
| 160 |
return new TransmissionListBox( this, nTrans ); |
| 161 |
} |
| 162 |
|
| 163 |
private static class TransmissionListBox extends JComponent { |
| 164 |
private final TransmissionListIcon icon_; |
| 165 |
private final int nTrans_; |
| 166 |
private final boolean hasRx_; |
| 167 |
private final boolean hasTx_; |
| 168 |
private Dimension minSize_; |
| 169 |
|
| 170 |
TransmissionListBox( TransmissionListIcon icon, int nTrans ) { |
| 171 |
icon_ = icon; |
| 172 |
nTrans_ = nTrans; |
| 173 |
setOpaque( true ); |
| 174 |
setBackground( Color.WHITE ); |
| 175 |
setBorder( BorderFactory.createLineBorder( Color.BLACK ) ); |
| 176 |
ListDataListener listener = new ListDataListener() { |
| 177 |
public void contentsChanged( ListDataEvent evt ) { |
| 178 |
repaint(); |
| 179 |
} |
| 180 |
public void intervalAdded( ListDataEvent evt ) { |
| 181 |
repaint(); |
| 182 |
} |
| 183 |
public void intervalRemoved( ListDataEvent evt ) { |
| 184 |
repaint(); |
| 185 |
} |
| 186 |
}; |
| 187 |
hasRx_ = icon.rxModel_ != null; |
| 188 |
hasTx_ = icon.txModel_ != null; |
| 189 |
if ( hasRx_ ) { |
| 190 |
icon.rxModel_.addListDataListener( listener ); |
| 191 |
} |
| 192 |
if ( hasTx_ ) { |
| 193 |
icon.txModel_.addListDataListener( listener ); |
| 194 |
} |
| 195 |
setPreferredSize( getSizeForCount( nTrans ) ); |
| 196 |
setMinimumSize( getSizeForCount( Math.max( nTrans, 4 ) ) ); |
| 197 |
ToolTipManager.sharedInstance().registerComponent( this ); |
| 198 |
} |
| 199 |
|
| 200 |
public Dimension getSizeForCount( int nTrans ) { |
| 201 |
int width = icon_.targetIcon_.getIconWidth(); |
| 202 |
width += ( ( hasRx_ ? 1 : 0 ) + ( hasTx_ ? 1 : 0 ) ) |
| 203 |
* nTrans_ * icon_.transIconWidth_; |
| 204 |
int height = icon_.size_; |
| 205 |
Dimension size = new Dimension( width, height ); |
| 206 |
Insets insets = getInsets(); |
| 207 |
size.width += insets.left + insets.right; |
| 208 |
size.height += insets.top + insets.bottom; |
| 209 |
return size; |
| 210 |
} |
| 211 |
|
| 212 |
public String getToolTipText( MouseEvent evt ) { |
| 213 |
Point p = evt.getPoint(); |
| 214 |
Point iconPos = getIconPosition(); |
| 215 |
p.x -= iconPos.x; |
| 216 |
p.y -= iconPos.y; |
| 217 |
Transmission trans = icon_.getTransmissionAt( p ); |
| 218 |
if ( trans != null ) { |
| 219 |
return new StringBuffer() |
| 220 |
.append( trans.getMessage().getMType() ) |
| 221 |
.append( ": " ) |
| 222 |
.append( trans.getSender().toString() ) |
| 223 |
.append( " -> " ) |
| 224 |
.append( trans.getReceiver().toString() ) |
| 225 |
.toString(); |
| 226 |
} |
| 227 |
else { |
| 228 |
return null; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
private Point getIconPosition() { |
| 233 |
Insets insets = getInsets(); |
| 234 |
return new Point( insets.left, insets.top ); |
| 235 |
} |
| 236 |
|
| 237 |
protected void paintComponent( Graphics g ) { |
| 238 |
super.paintComponent( g ); |
| 239 |
Color color = g.getColor(); |
| 240 |
Rectangle bounds = getBounds(); |
| 241 |
Insets insets = getInsets(); |
| 242 |
if ( isOpaque() ) { |
| 243 |
g.setColor( getBackground() ); |
| 244 |
g.fillRect( insets.left, insets.top, |
| 245 |
bounds.width - insets.left - insets.right, |
| 246 |
bounds.height - insets.top - insets.bottom ); |
| 247 |
} |
| 248 |
g.setColor( color ); |
| 249 |
Point p = getIconPosition(); |
| 250 |
icon_.paintIcon( this, g, p.x, p.y ); |
| 251 |
} |
| 252 |
} |
| 253 |
} |