| 1 |
package org.astrogrid.samp.gui; |
| 2 |
|
| 3 |
import java.awt.BorderLayout; |
| 4 |
import java.awt.Color; |
| 5 |
import java.awt.Dimension; |
| 6 |
import java.awt.Toolkit; |
| 7 |
import java.awt.event.MouseAdapter; |
| 8 |
import java.awt.event.MouseEvent; |
| 9 |
import java.awt.event.MouseListener; |
| 10 |
import java.io.IOException; |
| 11 |
import java.net.MalformedURLException; |
| 12 |
import java.net.URL; |
| 13 |
import java.util.Iterator; |
| 14 |
import java.util.List; |
| 15 |
import java.util.Map; |
| 16 |
import java.util.logging.Logger; |
| 17 |
import javax.swing.BorderFactory; |
| 18 |
import javax.swing.Box; |
| 19 |
import javax.swing.AbstractListModel; |
| 20 |
import javax.swing.JComponent; |
| 21 |
import javax.swing.JEditorPane; |
| 22 |
import javax.swing.JLabel; |
| 23 |
import javax.swing.JList; |
| 24 |
import javax.swing.JPanel; |
| 25 |
import javax.swing.JScrollPane; |
| 26 |
import javax.swing.JSplitPane; |
| 27 |
import javax.swing.JTextField; |
| 28 |
import javax.swing.ListModel; |
| 29 |
import javax.swing.border.Border; |
| 30 |
import org.astrogrid.samp.Client; |
| 31 |
import org.astrogrid.samp.Metadata; |
| 32 |
import org.astrogrid.samp.Subscriptions; |
| 33 |
import org.astrogrid.samp.hub.HubClient; |
| 34 |
|
| 35 |
/** |
| 36 |
* Component which displays details about a {@link org.astrogrid.samp.Client}. |
| 37 |
* |
| 38 |
* @author Mark Taylor |
| 39 |
* @since 16 Jul 2008 |
| 40 |
*/ |
| 41 |
public class ClientPanel extends JPanel { |
| 42 |
|
| 43 |
private final JTextField idField_; |
| 44 |
private final JTextField profileField_; |
| 45 |
private final Box metaBox_; |
| 46 |
private final JList subsList_; |
| 47 |
private Client client_; |
| 48 |
private static final int INFO_WIDTH = 240; |
| 49 |
private final Logger logger_ = |
| 50 |
Logger.getLogger( ClientPanel.class.getName() ); |
| 51 |
|
| 52 |
/** |
| 53 |
* Constructor. |
| 54 |
* |
| 55 |
* @param hubLike true if this will be displaying clients implementing |
| 56 |
* the HubClient interface |
| 57 |
*/ |
| 58 |
public ClientPanel( boolean hubLike ) { |
| 59 |
super( new BorderLayout() ); |
| 60 |
JSplitPane splitter = new JSplitPane( JSplitPane.VERTICAL_SPLIT ); |
| 61 |
splitter.setBorder( BorderFactory.createEmptyBorder() ); |
| 62 |
splitter.setOneTouchExpandable( true ); |
| 63 |
splitter.setResizeWeight( 0.5 ); |
| 64 |
add( splitter, BorderLayout.CENTER ); |
| 65 |
|
| 66 |
// Construct and place registration subpanel. |
| 67 |
Box regBox = Box.createVerticalBox(); |
| 68 |
regBox.setBorder( createTitledBorder( "Registration" ) ); |
| 69 |
Box idLine = Box.createHorizontalBox(); |
| 70 |
idField_ = new JTextField(); |
| 71 |
idField_.setEditable( false ); |
| 72 |
idField_.setBorder( BorderFactory.createEmptyBorder() ); |
| 73 |
idLine.add( new JLabel( "Public ID: " ) ); |
| 74 |
idLine.add( idField_ ); |
| 75 |
regBox.add( idLine ); |
| 76 |
if ( hubLike ) { |
| 77 |
profileField_ = new JTextField(); |
| 78 |
profileField_.setEditable( false ); |
| 79 |
profileField_.setBorder( BorderFactory.createEmptyBorder() ); |
| 80 |
Box profileLine = Box.createHorizontalBox(); |
| 81 |
profileLine.add( new JLabel( "Profile: " ) ); |
| 82 |
profileLine.add( profileField_ ); |
| 83 |
regBox.add( profileLine ); |
| 84 |
} |
| 85 |
else { |
| 86 |
profileField_ = null; |
| 87 |
} |
| 88 |
add( regBox, BorderLayout.NORTH ); |
| 89 |
|
| 90 |
// Construct and place metadata subpanel. |
| 91 |
metaBox_ = Box.createVerticalBox(); |
| 92 |
JPanel metaPanel = new JPanel( new BorderLayout() ); |
| 93 |
metaPanel.add( metaBox_, BorderLayout.NORTH ); |
| 94 |
JScrollPane metaScroller = new JScrollPane( metaPanel ); |
| 95 |
metaScroller.setBorder( createTitledBorder( "Metadata" ) ); |
| 96 |
metaScroller.setPreferredSize( new Dimension( INFO_WIDTH, 120 ) ); |
| 97 |
splitter.setTopComponent( metaScroller ); |
| 98 |
|
| 99 |
// Construct and place subscriptions subpanel. |
| 100 |
Box subsBox = Box.createVerticalBox(); |
| 101 |
subsList_ = new JList(); |
| 102 |
JScrollPane subsScroller = new JScrollPane( subsList_ ); |
| 103 |
subsScroller.setBorder( createTitledBorder( "Subscriptions" ) ); |
| 104 |
subsScroller.setPreferredSize( new Dimension( INFO_WIDTH, 120 ) ); |
| 105 |
subsBox.add( subsScroller ); |
| 106 |
splitter.setBottomComponent( subsBox ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Updates this component to display the current state of a given client. |
| 111 |
* |
| 112 |
* @param client client, or null to clear display |
| 113 |
*/ |
| 114 |
public void setClient( Client client ) { |
| 115 |
idField_.setText( client == null ? null : client.getId() ); |
| 116 |
if ( profileField_ != null ) { |
| 117 |
profileField_.setText( client instanceof HubClient |
| 118 |
? ((HubClient) client).getProfileToken() |
| 119 |
.getProfileName() |
| 120 |
: null ); |
| 121 |
} |
| 122 |
setMetadata( client == null ? null : client.getMetadata() ); |
| 123 |
setSubscriptions( client == null ? null : client.getSubscriptions() ); |
| 124 |
client_ = client; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Returns the most recently displayed client. |
| 129 |
* |
| 130 |
* @return client |
| 131 |
*/ |
| 132 |
public Client getClient() { |
| 133 |
return client_; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Updates this component's metadata panel to display the current state |
| 138 |
* of a given metadata object. |
| 139 |
* |
| 140 |
* @param meta metadata map, or null to clear metadata display |
| 141 |
*/ |
| 142 |
public void setMetadata( Metadata meta ) { |
| 143 |
metaBox_.removeAll(); |
| 144 |
if ( meta != null ) { |
| 145 |
for ( Iterator it = meta.entrySet().iterator(); it.hasNext(); ) { |
| 146 |
Map.Entry entry = (Map.Entry) it.next(); |
| 147 |
String key = (String) entry.getKey(); |
| 148 |
Object value = entry.getValue(); |
| 149 |
Box keyBox = Box.createHorizontalBox(); |
| 150 |
keyBox.add( new JLabel( key + ":" ) ); |
| 151 |
keyBox.add( Box.createHorizontalGlue() ); |
| 152 |
metaBox_.add( keyBox ); |
| 153 |
Box valueBox = Box.createHorizontalBox(); |
| 154 |
valueBox.add( Box.createHorizontalStrut( 24 ) ); |
| 155 |
valueBox.add( createViewer( value ) ); |
| 156 |
metaBox_.add( valueBox ); |
| 157 |
} |
| 158 |
} |
| 159 |
metaBox_.add( Box.createVerticalGlue() ); |
| 160 |
metaBox_.repaint(); |
| 161 |
metaBox_.revalidate(); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Updates this component's subscriptions panel to display the current |
| 166 |
* state of a given subscriptions object. |
| 167 |
* |
| 168 |
* @param subs subscriptions map, or null to clear subscriptions display |
| 169 |
*/ |
| 170 |
public void setSubscriptions( Subscriptions subs ) { |
| 171 |
final Object[] subscriptions = subs == null |
| 172 |
? new Object[ 0 ] |
| 173 |
: subs.keySet().toArray(); |
| 174 |
subsList_.setModel( new AbstractListModel() { |
| 175 |
public int getSize() { |
| 176 |
return subscriptions.length; |
| 177 |
} |
| 178 |
public Object getElementAt( int index ) { |
| 179 |
return subscriptions[ index ]; |
| 180 |
} |
| 181 |
} ); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Attempts to open a URL in some kind of external browser. |
| 186 |
* |
| 187 |
* @param url URL to view |
| 188 |
*/ |
| 189 |
public void openURL( URL url ) throws IOException { |
| 190 |
BrowserLauncher.openURL( url.toString() ); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Returns a graphical component which displays a legal SAMP object |
| 195 |
* (SAMP map, list or string). |
| 196 |
* |
| 197 |
* @param value SAMP object |
| 198 |
* @return new component displaying <code>value</code> |
| 199 |
*/ |
| 200 |
private JComponent createViewer( Object value ) { |
| 201 |
if ( value instanceof String ) { |
| 202 |
JTextField field = new JTextField(); |
| 203 |
field.setEditable( false ); |
| 204 |
field.setText( (String) value ); |
| 205 |
field.setCaretPosition( 0 ); |
| 206 |
try { |
| 207 |
final URL url = new URL( (String) value ); |
| 208 |
field.setForeground( Color.BLUE ); |
| 209 |
field.addMouseListener( new MouseAdapter() { |
| 210 |
public void mouseClicked( MouseEvent evt ) { |
| 211 |
try { |
| 212 |
openURL( url ); |
| 213 |
} |
| 214 |
catch ( IOException e ) { |
| 215 |
Toolkit.getDefaultToolkit().beep(); |
| 216 |
logger_.warning( "Can't open URL " + url + e ); |
| 217 |
} |
| 218 |
} |
| 219 |
} ); |
| 220 |
} |
| 221 |
catch ( MalformedURLException e ) { |
| 222 |
// not a URL - fine |
| 223 |
} |
| 224 |
return field; |
| 225 |
} |
| 226 |
else if ( value instanceof List ) { |
| 227 |
return new JList( ((List) value).toArray() ); |
| 228 |
} |
| 229 |
else if ( value instanceof Map ) { |
| 230 |
JEditorPane edPane = |
| 231 |
new JEditorPane( "text/html", toHtml( value ) ); |
| 232 |
edPane.setEditable( false ); |
| 233 |
edPane.setCaretPosition( 0 ); |
| 234 |
return edPane; |
| 235 |
} |
| 236 |
else { |
| 237 |
return new JLabel( "???" ); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Returns an HTML representation of a legal SAMP object |
| 243 |
* (SAMP map, list or string). |
| 244 |
* |
| 245 |
* @param data SAMP object |
| 246 |
* @return HTML representation of <code>data</code> |
| 247 |
*/ |
| 248 |
private static String toHtml( Object data ) { |
| 249 |
StringBuffer sbuf = new StringBuffer(); |
| 250 |
if ( data instanceof Map ) { |
| 251 |
sbuf.append( "<dl>\n" ); |
| 252 |
for ( Iterator it = ((Map) data).entrySet().iterator(); |
| 253 |
it.hasNext(); ) { |
| 254 |
Map.Entry entry = (Map.Entry) it.next(); |
| 255 |
sbuf.append( "<dt>" ) |
| 256 |
.append( htmlEscape( String.valueOf( entry.getKey() ) ) ) |
| 257 |
.append( "</dt>\n" ) |
| 258 |
.append( "<dd>" ) |
| 259 |
.append( toHtml( entry.getValue() ) ) |
| 260 |
.append( "</dd>\n" ); |
| 261 |
} |
| 262 |
sbuf.append( "</dl>\n" ); |
| 263 |
} |
| 264 |
else if ( data instanceof List ) { |
| 265 |
sbuf.append( "<ul>\n" ); |
| 266 |
for ( Iterator it = ((List) data).iterator(); it.hasNext(); ) { |
| 267 |
sbuf.append( "<li>" ) |
| 268 |
.append( toHtml( it.next() ) ) |
| 269 |
.append( "</li>\n" ); |
| 270 |
} |
| 271 |
sbuf.append( "</ul>\n" ); |
| 272 |
} |
| 273 |
else if ( data instanceof String ) { |
| 274 |
sbuf.append( htmlEscape( (String) data ) ); |
| 275 |
} |
| 276 |
else { |
| 277 |
sbuf.append( "???" ); |
| 278 |
} |
| 279 |
return sbuf.toString(); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Escapes a literal string for use within HTML text. |
| 284 |
* |
| 285 |
* @param text literal string |
| 286 |
* @return escaped version of <code>text</code> safe for use within HTML |
| 287 |
*/ |
| 288 |
private static String htmlEscape( String text ) { |
| 289 |
int leng = text.length(); |
| 290 |
StringBuffer sbuf = new StringBuffer( leng ); |
| 291 |
for ( int i = 0; i < leng; i++ ) { |
| 292 |
char c = text.charAt( i ); |
| 293 |
switch ( c ) { |
| 294 |
case '<': |
| 295 |
sbuf.append( "<" ); |
| 296 |
break; |
| 297 |
case '>': |
| 298 |
sbuf.append( ">" ); |
| 299 |
break; |
| 300 |
case '&': |
| 301 |
sbuf.append( "&" ); |
| 302 |
break; |
| 303 |
default: |
| 304 |
sbuf.append( c ); |
| 305 |
} |
| 306 |
} |
| 307 |
return sbuf.toString(); |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Creates a titled border with a uniform style. |
| 312 |
* |
| 313 |
* @param title title text |
| 314 |
* @return border |
| 315 |
*/ |
| 316 |
static Border createTitledBorder( String title ) { |
| 317 |
return BorderFactory.createCompoundBorder( |
| 318 |
BorderFactory.createEmptyBorder( 5, 5, 5, 5 ), |
| 319 |
BorderFactory.createTitledBorder( |
| 320 |
BorderFactory.createLineBorder( Color.BLACK ), title ) ); |
| 321 |
} |
| 322 |
} |