| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import java.awt.Component; |
| 4 |
import java.awt.Dimension; |
| 5 |
import java.awt.Graphics; |
| 6 |
import java.awt.Insets; |
| 7 |
import java.awt.Point; |
| 8 |
import java.awt.event.MouseEvent; |
| 9 |
import java.util.ArrayList; |
| 10 |
import java.util.Iterator; |
| 11 |
import java.util.List; |
| 12 |
import javax.swing.BorderFactory; |
| 13 |
import javax.swing.Icon; |
| 14 |
import javax.swing.JComponent; |
| 15 |
import javax.swing.JList; |
| 16 |
import javax.swing.ListModel; |
| 17 |
import javax.swing.event.ListDataEvent; |
| 18 |
import javax.swing.event.ListDataListener; |
| 19 |
import org.astrogrid.samp.Client; |
| 20 |
|
| 21 |
/** |
| 22 |
* ListCellRenderer which draws a representation of incoming and outgoing |
| 23 |
* messages alongside the default representation. |
| 24 |
* |
| 25 |
* @author Mark Taylor |
| 26 |
* @since 26 Nov 2008 |
| 27 |
*/ |
| 28 |
class MessageTrackerListCellRenderer extends ClientListCellRenderer { |
| 29 |
|
| 30 |
private final ClientTransmissionHolder transHolder_; |
| 31 |
private final int msgGap_; |
| 32 |
private final IconBox iconBox_; |
| 33 |
private final IconListModel iconListModel_; |
| 34 |
private final Object separator_; |
| 35 |
|
| 36 |
/** |
| 37 |
* Constructor. |
| 38 |
* |
| 39 |
* @param transHolder obtains list models containing sent/received |
| 40 |
* messages |
| 41 |
*/ |
| 42 |
public MessageTrackerListCellRenderer( |
| 43 |
ClientTransmissionHolder transHolder ) { |
| 44 |
transHolder_ = transHolder; |
| 45 |
iconListModel_ = new IconListModel(); |
| 46 |
msgGap_ = 10; |
| 47 |
separator_ = new Object(); |
| 48 |
iconBox_ = new IconBox( 16 ); |
| 49 |
iconBox_.setOpaque( false ); |
| 50 |
iconBox_.setBorder( BorderFactory.createEmptyBorder( 1, 1, 1, 1 ) ); |
| 51 |
iconBox_.setModel( iconListModel_ ); |
| 52 |
iconBox_.setRenderer( new TransmissionCellRenderer() { |
| 53 |
public String getToolTipText( IconBox iconBox, Object value, |
| 54 |
int index ) { |
| 55 |
if ( value instanceof Transmission ) { |
| 56 |
Transmission trans = (Transmission) value; |
| 57 |
String mtype = trans.getMessage().getMType(); |
| 58 |
Client client = iconListModel_.client_; |
| 59 |
if ( client == trans.getSender() ) { |
| 60 |
return mtype + " -> " + trans.getReceiver(); |
| 61 |
} |
| 62 |
else if ( client == trans.getReceiver() ) { |
| 63 |
return mtype + " <- " + trans.getSender(); |
| 64 |
} |
| 65 |
else { |
| 66 |
assert false; |
| 67 |
return null; |
| 68 |
} |
| 69 |
} |
| 70 |
else { |
| 71 |
return null; |
| 72 |
} |
| 73 |
} |
| 74 |
} ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Sets the cell renderer for transmission objects. |
| 79 |
* |
| 80 |
* @param transRend renderer |
| 81 |
*/ |
| 82 |
public void setTransmissionCellRenderer( IconBox.CellRenderer transRend ) { |
| 83 |
iconBox_.setRenderer( transRend ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Returns the cell renderer for transmission objects. |
| 88 |
* |
| 89 |
* @return renderer |
| 90 |
*/ |
| 91 |
public IconBox.CellRenderer getTransmissionCellRenderer() { |
| 92 |
return iconBox_.getRenderer(); |
| 93 |
} |
| 94 |
|
| 95 |
protected void paintComponent( Graphics g ) { |
| 96 |
super.paintComponent( g ); |
| 97 |
if ( iconListModel_.client_ != null ) { |
| 98 |
Point p = getIconBoxPosition(); |
| 99 |
Dimension boxSize = iconBox_.getPreferredSize(); |
| 100 |
if ( g.hitClip( p.x, p.y, boxSize.width, boxSize.height ) ) { |
| 101 |
g.translate( p.x, p.y ); |
| 102 |
iconBox_.paintComponent( g ); |
| 103 |
g.translate( -p.x, -p.y ); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
public Dimension getPreferredSize() { |
| 109 |
Dimension prefSize = super.getPreferredSize(); |
| 110 |
if ( iconListModel_.client_ != null ) { |
| 111 |
Dimension boxSize = iconBox_.getPreferredSize(); |
| 112 |
prefSize.width += iconBox_.getPreferredSize().width + msgGap_; |
| 113 |
} |
| 114 |
return prefSize; |
| 115 |
} |
| 116 |
|
| 117 |
public String getToolTipText( MouseEvent evt ) { |
| 118 |
Point boxPos = getIconBoxPosition(); |
| 119 |
evt.translatePoint( -boxPos.x, -boxPos.y ); |
| 120 |
return iconBox_.getToolTipText( evt ); |
| 121 |
} |
| 122 |
|
| 123 |
public Component getListCellRendererComponent( JList list, Object value, |
| 124 |
int index, boolean isSel, |
| 125 |
boolean hasFocus ) { |
| 126 |
Component c = super.getListCellRendererComponent( list, value, index, |
| 127 |
isSel, hasFocus ); |
| 128 |
if ( value instanceof Client ) { |
| 129 |
iconListModel_.setClient( (Client) value ); |
| 130 |
int size = c.getPreferredSize().height; |
| 131 |
if ( c instanceof JComponent ) { |
| 132 |
Insets cInsets = ((JComponent) c).getInsets(); |
| 133 |
size -= cInsets.top + cInsets.bottom; |
| 134 |
Insets bInsets = iconBox_.getInsets(); |
| 135 |
size -= bInsets.top + bInsets.bottom; |
| 136 |
} |
| 137 |
iconBox_.setTransverseSize( size ); |
| 138 |
} |
| 139 |
else { |
| 140 |
iconListModel_.setClient( null ); |
| 141 |
} |
| 142 |
return c; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Returns the position at which the transmission list icon should |
| 147 |
* be drawn. |
| 148 |
* |
| 149 |
* @return icon base position |
| 150 |
*/ |
| 151 |
private Point getIconBoxPosition() { |
| 152 |
Insets insets = getInsets(); |
| 153 |
return new Point( insets.left + super.getPreferredSize().width |
| 154 |
+ msgGap_, |
| 155 |
insets.top ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* ListModel which can be used in the icon box. |
| 160 |
* It contains entries for each received and sent message, as well as |
| 161 |
* one which notionally represents the application (visual sugar). |
| 162 |
* It is basically a combination of the rx and tx models. |
| 163 |
*/ |
| 164 |
private class IconListModel implements ListModel { |
| 165 |
Client client_; |
| 166 |
ListModel rxModel_; |
| 167 |
ListModel txModel_; |
| 168 |
private final ListDataListener rxListener_; |
| 169 |
private final ListDataListener txListener_; |
| 170 |
private final List listenerList_; |
| 171 |
|
| 172 |
/** |
| 173 |
* Constructor. |
| 174 |
*/ |
| 175 |
IconListModel() { |
| 176 |
listenerList_ = new ArrayList(); |
| 177 |
rxListener_ = new ListDataForwarder() { |
| 178 |
public int getOffset() { |
| 179 |
return 0; |
| 180 |
} |
| 181 |
}; |
| 182 |
txListener_ = new ListDataForwarder() { |
| 183 |
public int getOffset() { |
| 184 |
return ( rxModel_ == null ? 0 : rxModel_.getSize() ) + 1; |
| 185 |
} |
| 186 |
}; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Sets the client whose transmissions this list will represent. |
| 191 |
* May be null. |
| 192 |
* |
| 193 |
* @param client client |
| 194 |
*/ |
| 195 |
public void setClient( Client client ) { |
| 196 |
if ( rxModel_ != null ) { |
| 197 |
rxModel_.removeListDataListener( rxListener_ ); |
| 198 |
} |
| 199 |
if ( txModel_ != null ) { |
| 200 |
txModel_.removeListDataListener( txListener_ ); |
| 201 |
} |
| 202 |
client_ = client; |
| 203 |
rxModel_ = transHolder_.getRxListModel( client ); |
| 204 |
txModel_ = transHolder_.getTxListModel( client ); |
| 205 |
if ( rxModel_ != null ) { |
| 206 |
rxModel_.addListDataListener( rxListener_ ); |
| 207 |
} |
| 208 |
if ( txModel_ != null ) { |
| 209 |
txModel_.addListDataListener( txListener_ ); |
| 210 |
} |
| 211 |
fireEvent( new ListDataEvent( this, ListDataEvent.CONTENTS_CHANGED, |
| 212 |
-1, -1 ) ); |
| 213 |
} |
| 214 |
|
| 215 |
public int getSize() { |
| 216 |
return ( rxModel_ == null ? 0 : rxModel_.getSize() ) |
| 217 |
+ 1 |
| 218 |
+ ( txModel_ == null ? 0 : txModel_.getSize() ); |
| 219 |
} |
| 220 |
|
| 221 |
public Object getElementAt( int index ) { |
| 222 |
int rxSize = rxModel_ == null ? 0 : rxModel_.getSize(); |
| 223 |
if ( index < rxSize ) { |
| 224 |
return rxModel_.getElementAt( index ); |
| 225 |
} |
| 226 |
index -= rxSize; |
| 227 |
|
| 228 |
if ( index < 1 ) { |
| 229 |
return separator_; |
| 230 |
} |
| 231 |
index -= 1; |
| 232 |
|
| 233 |
int txSize = txModel_ == null ? 0 : txModel_.getSize(); |
| 234 |
if ( index < txSize ) { |
| 235 |
return txModel_.getElementAt( index ); |
| 236 |
} |
| 237 |
index -= txSize; |
| 238 |
|
| 239 |
throw new IllegalArgumentException(); |
| 240 |
} |
| 241 |
|
| 242 |
public void addListDataListener( ListDataListener listener ) { |
| 243 |
listenerList_.add( listener ); |
| 244 |
} |
| 245 |
|
| 246 |
public void removeListDataListener( ListDataListener listener ) { |
| 247 |
listenerList_.remove( listener ); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Passes an event on to registered ListDataListeners. |
| 252 |
*/ |
| 253 |
private void fireEvent( ListDataEvent evt ) { |
| 254 |
for ( Iterator it = listenerList_.iterator(); it.hasNext(); ) { |
| 255 |
ListDataListener listener = (ListDataListener) it.next(); |
| 256 |
switch ( evt.getType() ) { |
| 257 |
case ListDataEvent.INTERVAL_ADDED: |
| 258 |
listener.intervalAdded( evt ); |
| 259 |
break; |
| 260 |
case ListDataEvent.INTERVAL_REMOVED: |
| 261 |
listener.intervalRemoved( evt ); |
| 262 |
break; |
| 263 |
case ListDataEvent.CONTENTS_CHANGED: |
| 264 |
listener.contentsChanged( evt ); |
| 265 |
break; |
| 266 |
default: |
| 267 |
assert false; |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Listener implementation which can listen to constituent (rx and tx) |
| 274 |
* models and forward events from them to listeners to this model. |
| 275 |
*/ |
| 276 |
private abstract class ListDataForwarder implements ListDataListener { |
| 277 |
|
| 278 |
/** |
| 279 |
* Returns the offset into the IconBoxModel at which the |
| 280 |
* model this listener is listening to starts. |
| 281 |
* |
| 282 |
* @return model element offset |
| 283 |
*/ |
| 284 |
abstract int getOffset(); |
| 285 |
|
| 286 |
public void intervalAdded( ListDataEvent evt ) { |
| 287 |
forwardEvent( evt ); |
| 288 |
} |
| 289 |
|
| 290 |
public void intervalRemoved( ListDataEvent evt ) { |
| 291 |
forwardEvent( evt ); |
| 292 |
} |
| 293 |
|
| 294 |
public void contentsChanged( ListDataEvent evt ) { |
| 295 |
forwardEvent( evt ); |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Takes an event received by this listener, adjusts its |
| 300 |
* indexes appropriately, and forwards it to listeners to this |
| 301 |
* model. |
| 302 |
* |
| 303 |
* @param evt event to forward |
| 304 |
*/ |
| 305 |
private void forwardEvent( ListDataEvent evt ) { |
| 306 |
Object src = evt.getSource(); |
| 307 |
int i0 = evt.getIndex0(); |
| 308 |
int i1 = evt.getIndex1(); |
| 309 |
if ( 0 <= i0 && i0 <= i1 ) { |
| 310 |
int offset = getOffset(); |
| 311 |
fireEvent( new ListDataEvent( evt.getSource(), |
| 312 |
evt.getType(), |
| 313 |
evt.getIndex0() + offset, |
| 314 |
evt.getIndex1() + offset ) ); |
| 315 |
} |
| 316 |
else { |
| 317 |
fireEvent( evt ); |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
} |