1 19 20 package org.apache.excalibur.instrument.client; 21 22 import java.awt.event.ActionEvent ; 23 import java.util.ArrayList ; 24 25 import javax.swing.AbstractAction ; 26 import javax.swing.Action ; 27 import javax.swing.ImageIcon ; 28 import javax.swing.JMenuItem ; 29 30 33 class InstrumentSampleNodeData 34 extends NodeData 35 { 36 private static final int ICON_TYPE_CNT = 0; 37 private static final int ICON_TYPE_MAX = 1; 38 private static final int ICON_TYPE_MEAN = 2; 39 private static final int ICON_TYPE_MIN = 3; 40 41 private static final int ICON_SUBTYPE_CONF = 0; 42 private static final int ICON_SUBTYPE_LEASE = 1; 43 private static final int ICON_SUBTYPE_MAINTAINED_LEASE = 2; 44 private static final int ICON_SUBTYPE_OLD = 3; 45 46 private static final ImageIcon [][] m_icons = new ImageIcon [4][4]; 47 48 private String m_instrumentName; 49 private InstrumentSampleData m_data; 50 private InstrumentManagerConnection m_connection; 51 52 private boolean m_configured; 53 54 55 private long m_leaseExpireTime; 56 57 58 private long m_leaseDuration; 59 60 61 private int m_type; 62 63 64 private int m_size; 65 66 67 private long m_interval; 68 69 72 static 73 { 74 ClassLoader cl = InstrumentManagerTreeCellRenderer.class.getClassLoader(); 76 77 loadTypeIcons( cl, ICON_TYPE_CNT, MEDIA_PATH + "sample_cnt_" ); 78 loadTypeIcons( cl, ICON_TYPE_MAX, MEDIA_PATH + "sample_max_" ); 79 loadTypeIcons( cl, ICON_TYPE_MEAN, MEDIA_PATH + "sample_mean_" ); 80 loadTypeIcons( cl, ICON_TYPE_MIN , MEDIA_PATH + "sample_min_" ); 81 } 82 83 private static void loadTypeIcons( ClassLoader cl, int type, String prefix ) 84 { 85 m_icons[type][ICON_SUBTYPE_CONF] = 86 new ImageIcon ( cl.getResource( prefix + "conf.gif") ); 87 88 m_icons[type][ICON_SUBTYPE_LEASE] = 89 new ImageIcon ( cl.getResource( prefix + "lease.gif") ); 90 91 m_icons[type][ICON_SUBTYPE_MAINTAINED_LEASE] = 92 new ImageIcon ( cl.getResource( prefix + "mlease.gif") ); 93 94 m_icons[type][ICON_SUBTYPE_OLD] = 95 new ImageIcon ( cl.getResource( prefix + "old.gif") ); 96 } 97 98 101 InstrumentSampleNodeData( String instrumentName, 102 InstrumentSampleData data, 103 InstrumentManagerConnection connection ) 104 { 105 m_instrumentName = instrumentName; 106 m_data = data; 107 m_connection = connection; 108 109 update(); 110 } 111 112 113 116 121 ImageIcon getIcon() 122 { 123 int iconType; 124 switch ( getType() ) 125 { 126 case InstrumentSampleData.INSTRUMENT_SAMPLE_TYPE_COUNTER: 127 iconType = ICON_TYPE_CNT; 128 break; 129 130 case InstrumentSampleData.INSTRUMENT_SAMPLE_TYPE_MAXIMUM: 131 iconType = ICON_TYPE_MAX; 132 break; 133 134 case InstrumentSampleData.INSTRUMENT_SAMPLE_TYPE_MEAN: 135 iconType = ICON_TYPE_MEAN; 136 break; 137 138 case InstrumentSampleData.INSTRUMENT_SAMPLE_TYPE_MINIMUM: 139 iconType = ICON_TYPE_MIN; 140 break; 141 142 default: 143 throw new IllegalStateException ( "Encountered an unknown instrument sample type: " + 144 getType() ); 145 } 146 147 int iconSubtype; 148 if ( isConfigured() ) 149 { 150 iconSubtype = ICON_SUBTYPE_CONF; 151 } 152 else if ( isLeased() ) 153 { 154 if ( isLeaseMaintained() ) 155 { 156 iconSubtype = ICON_SUBTYPE_MAINTAINED_LEASE; 157 } 158 else 159 { 160 iconSubtype = ICON_SUBTYPE_LEASE; 161 } 162 } 163 else 164 { 165 iconSubtype = ICON_SUBTYPE_OLD; 166 } 167 168 return m_icons[iconType][iconSubtype]; 169 } 170 171 176 String getToolTipText() 177 { 178 String text; 179 switch ( getType() ) 180 { 181 case InstrumentSampleData.INSTRUMENT_SAMPLE_TYPE_COUNTER: 182 if ( isConfigured() ) 183 { 184 text = "Configured Counter Instrument Sample"; 185 } 186 else if ( isLeased() ) 187 { 188 if ( isLeaseMaintained() ) 189 { 190 text = "Leased Counter Instrument Sample (Maintained " + 191 ( getLeaseDuration() / 1000 ) + " seconds)"; 192 } 193 else 194 { 195 text = "Leased Counter Instrument Sample (Expires in " + 196 ( getRemainingLeaseTime() / 1000 ) + " seconds)"; 197 } 198 } 199 else 200 { 201 text = "Old Counter Instrument Sample loaded from state file"; 202 } 203 break; 204 205 case InstrumentSampleData.INSTRUMENT_SAMPLE_TYPE_MAXIMUM: 206 if ( isConfigured() ) 207 { 208 text = "Configured Maximum Value Instrument Sample"; 209 } 210 else if ( isLeased() ) 211 { 212 if ( isLeaseMaintained() ) 213 { 214 text = "Leased Maximum Value Instrument Sample (Maintained " + 215 ( getLeaseDuration() / 1000 ) + " seconds)"; 216 } 217 else 218 { 219 text = "Leased Maximum Value Instrument Sample (Expires in " + 220 ( getRemainingLeaseTime() / 1000 ) + " seconds)"; 221 } 222 } 223 else 224 { 225 text = "Old Maximum Value Instrument Sample loaded from state file"; 226 } 227 break; 228 229 case InstrumentSampleData.INSTRUMENT_SAMPLE_TYPE_MEAN: 230 if ( isConfigured() ) 231 { 232 text = "Configured Mean Value Instrument Sample"; 233 } 234 else if ( isLeased() ) 235 { 236 if ( isLeaseMaintained() ) 237 { 238 text = "Leased Mean Value Instrument Sample (Maintained " + 239 ( getLeaseDuration() / 1000 ) + " seconds)"; 240 } 241 else 242 { 243 text = "Leased Mean Value Instrument Sample (Expires in " + 244 ( getRemainingLeaseTime() / 1000 ) + " seconds)"; 245 } 246 } 247 else 248 { 249 text = "Old Mean Value Instrument Sample loaded from state file"; 250 } 251 break; 252 253 case InstrumentSampleData.INSTRUMENT_SAMPLE_TYPE_MINIMUM: 254 if ( isConfigured() ) 255 { 256 text = "Configured Minimum Value Instrument Sample"; 257 } 258 else if ( isLeased() ) 259 { 260 if ( isLeaseMaintained() ) 261 { 262 text = "Leased Minimum Value Instrument Sample (Maintained " + 263 ( getLeaseDuration() / 1000 ) + " seconds)"; 264 } 265 else 266 { 267 text = "Leased Minimum Value Instrument Sample (Expires in " + 268 ( getRemainingLeaseTime() / 1000 ) + " seconds)"; 269 } 270 } 271 else 272 { 273 text = "Old Minimum Value Instrument Sample loaded from state file"; 274 } 275 break; 276 277 default: 278 throw new IllegalStateException ( "Encountered an unknown instrument sample type: " + 279 getType() ); 280 } 281 282 return text; 283 } 284 285 291 public JMenuItem [] getCommonMenuItems() 292 { 293 ArrayList menuItems = new ArrayList (); 294 295 Action viewAction = new AbstractAction ( "View" ) 296 { 297 public void actionPerformed( ActionEvent event ) 298 { 299 m_connection.viewSample( InstrumentSampleNodeData.this.getName() ); 300 } 301 }; 302 JMenuItem viewItem = new JMenuItem ( viewAction ); 303 viewItem.setMnemonic( 'V' ); 304 menuItems.add( viewItem ); 305 306 if ( isLeased() ) 307 { 308 if ( isLeaseMaintained() ) 309 { 310 Action stopMaintainingAction = new AbstractAction ( "Stop Maintaining Lease..." ) 311 { 312 public void actionPerformed( ActionEvent event ) 313 { 314 m_connection.stopMaintainingSample( 315 InstrumentSampleNodeData.this.getName() ); 316 } 317 }; 318 JMenuItem stopMaintainingItem = new JMenuItem ( stopMaintainingAction ); 319 stopMaintainingItem.setMnemonic( 'S' ); 320 menuItems.add( stopMaintainingItem ); 321 } 322 else 323 { 324 Action startMaintainingAction = new AbstractAction ( "Start Maintaining Lease..." ) 325 { 326 public void actionPerformed( ActionEvent event ) 327 { 328 long leaseDuration = 600000; 330 String description = InstrumentSampleUtils.getDefaultDescriptionForType( 331 m_type, m_interval ); 332 m_connection.startMaintainingSample( 333 m_instrumentName, m_type, m_interval, m_size, leaseDuration, 334 description ); 335 } 336 }; 337 JMenuItem startMaintainingItem = new JMenuItem ( startMaintainingAction ); 338 startMaintainingItem.setMnemonic( 'S' ); 339 menuItems.add( startMaintainingItem ); 340 } 341 } 342 343 JMenuItem [] menuItemArray = new JMenuItem [menuItems.size()]; 344 menuItems.toArray( menuItemArray ); 345 346 return menuItemArray; 347 } 348 349 352 void select() 353 { 354 m_connection.viewSample( getName() ); 355 } 356 357 360 InstrumentSampleData getData() 361 { 362 return m_data; 363 } 364 365 boolean isConfigured() 366 { 367 return m_configured; 368 } 369 370 boolean isLeased() 371 { 372 return m_leaseExpireTime > 0; 373 } 374 375 boolean isLeaseMaintained() 376 { 377 return m_leaseDuration > 0; 378 } 379 380 int getType() 381 { 382 return m_type; 383 } 384 385 int getSize() 386 { 387 return m_size; 388 } 389 390 long getInterval() 391 { 392 return m_interval; 393 } 394 395 boolean update() 396 { 397 boolean changed = false; 398 changed |= update( m_data.getName(), m_data.getDescription(), m_data.getStateVersion() ); 399 400 boolean newConfigured = m_data.isConfigured(); 401 if ( newConfigured != m_configured ) 402 { 403 changed = true; 404 m_configured = newConfigured; 405 } 406 407 long newLeaseExpireTime = m_data.getLeaseExpirationTime(); 408 if ( newLeaseExpireTime != m_leaseExpireTime ) 409 { 410 changed = true; 411 m_leaseExpireTime = newLeaseExpireTime; 412 } 413 414 int newType = m_data.getType(); 415 if ( newType != m_type ) 416 { 417 changed = true; 418 m_type = newType; 419 } 420 421 int newSize = m_data.getSize(); 422 if ( newSize != m_size ) 423 { 424 changed = true; 425 m_size = newSize; 426 } 427 428 long newInterval = m_data.getInterval(); 429 if ( newInterval != m_interval ) 430 { 431 changed = true; 432 m_interval = newInterval; 433 } 434 435 return changed; 436 } 437 438 long getRemainingLeaseTime() 439 { 440 long now = System.currentTimeMillis(); 441 return m_leaseExpireTime - now; 442 } 443 444 void setLeaseExpireTime( long leaseExpireTime ) 445 { 446 m_leaseExpireTime = leaseExpireTime; 447 } 448 449 455 void setLeaseDuration( long leaseDuration ) 456 { 457 m_leaseDuration = leaseDuration; 458 } 459 460 465 long getLeaseDuration() 466 { 467 return m_leaseDuration; 468 } 469 } 470 | Popular Tags |