KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > instrument > client > InstrumentSampleNodeData


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.excalibur.instrument.client;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.util.ArrayList JavaDoc;
24
25 import javax.swing.AbstractAction JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import javax.swing.ImageIcon JavaDoc;
28 import javax.swing.JMenuItem JavaDoc;
29
30 /**
31  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
32  */

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 JavaDoc[][] m_icons = new ImageIcon JavaDoc[4][4];
47     
48     private String JavaDoc m_instrumentName;
49     private InstrumentSampleData m_data;
50     private InstrumentManagerConnection m_connection;
51     
52     private boolean m_configured;
53     
54     /* The time the current lease expires, or 0. */
55     private long m_leaseExpireTime;
56     
57     /** The length of the maintained lease. */
58     private long m_leaseDuration;
59     
60     /** Type of the sample. */
61     private int m_type;
62     
63     /** Number of points in the sample. */
64     private int m_size;
65     
66     /** Time interval of the sample points. */
67     private long m_interval;
68     
69     /*---------------------------------------------------------------
70      * Class Initializer
71      *-------------------------------------------------------------*/

72     static
73     {
74         // Load the icons.
75
ClassLoader JavaDoc 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 JavaDoc cl, int type, String JavaDoc prefix )
84     {
85         m_icons[type][ICON_SUBTYPE_CONF] =
86             new ImageIcon JavaDoc( cl.getResource( prefix + "conf.gif") );
87         
88         m_icons[type][ICON_SUBTYPE_LEASE] =
89             new ImageIcon JavaDoc( cl.getResource( prefix + "lease.gif") );
90         
91         m_icons[type][ICON_SUBTYPE_MAINTAINED_LEASE] =
92             new ImageIcon JavaDoc( cl.getResource( prefix + "mlease.gif") );
93         
94         m_icons[type][ICON_SUBTYPE_OLD] =
95             new ImageIcon JavaDoc( cl.getResource( prefix + "old.gif") );
96     }
97     
98     /*---------------------------------------------------------------
99      * Constructors
100      *-------------------------------------------------------------*/

101     InstrumentSampleNodeData( String JavaDoc 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     /*---------------------------------------------------------------
114      * NodeData Methods
115      *-------------------------------------------------------------*/

116     /**
117      * Get the icon to display for the node.
118      *
119      * @return the icon to display for the node.
120      */

121     ImageIcon JavaDoc 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 JavaDoc( "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     /**
172      * Return the text to use for a tool tip on this node.
173      *
174      * @return Tool Tip text. May be null, for no tool tip.
175      */

176     String JavaDoc getToolTipText()
177     {
178         String JavaDoc 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 JavaDoc( "Encountered an unknown instrument sample type: " +
279                 getType() );
280         }
281         
282         return text;
283     }
284     
285     /**
286      * Returns an array of any menu items which will be displayed both
287      * in a popup menu and in the menus.
288      *
289      * @return An array of the common menu items.
290      */

291     public JMenuItem JavaDoc[] getCommonMenuItems()
292     {
293         ArrayList JavaDoc menuItems = new ArrayList JavaDoc();
294         
295         Action JavaDoc viewAction = new AbstractAction JavaDoc( "View" )
296         {
297             public void actionPerformed( ActionEvent JavaDoc event )
298             {
299                 m_connection.viewSample( InstrumentSampleNodeData.this.getName() );
300             }
301         };
302         JMenuItem JavaDoc viewItem = new JMenuItem JavaDoc( viewAction );
303         viewItem.setMnemonic( 'V' );
304         menuItems.add( viewItem );
305         
306         if ( isLeased() )
307         {
308             if ( isLeaseMaintained() )
309             {
310                 Action JavaDoc stopMaintainingAction = new AbstractAction JavaDoc( "Stop Maintaining Lease..." )
311                 {
312                     public void actionPerformed( ActionEvent JavaDoc event )
313                     {
314                         m_connection.stopMaintainingSample(
315                             InstrumentSampleNodeData.this.getName() );
316                     }
317                 };
318                 JMenuItem JavaDoc stopMaintainingItem = new JMenuItem JavaDoc( stopMaintainingAction );
319                 stopMaintainingItem.setMnemonic( 'S' );
320                 menuItems.add( stopMaintainingItem );
321             }
322             else
323             {
324                 Action JavaDoc startMaintainingAction = new AbstractAction JavaDoc( "Start Maintaining Lease..." )
325                 {
326                     public void actionPerformed( ActionEvent JavaDoc event )
327                     {
328                         // Need to show a dialog here.
329
long leaseDuration = 600000;
330                         String JavaDoc 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 JavaDoc startMaintainingItem = new JMenuItem JavaDoc( startMaintainingAction );
338                 startMaintainingItem.setMnemonic( 'S' );
339                 menuItems.add( startMaintainingItem );
340             }
341         }
342         
343         JMenuItem JavaDoc[] menuItemArray = new JMenuItem JavaDoc[menuItems.size()];
344         menuItems.toArray( menuItemArray );
345         
346         return menuItemArray;
347     }
348     
349     /**
350      * Called when the node is selected.
351      */

352     void select()
353     {
354         m_connection.viewSample( getName() );
355     }
356     
357     /*---------------------------------------------------------------
358      * Methods
359      *-------------------------------------------------------------*/

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     /**
450      * Sets the maintained lease time. If 0 or less means that the the lease
451      * is not maintained.
452      *
453      * @param leaseDuration The maintained lease time.
454      */

455     void setLeaseDuration( long leaseDuration )
456     {
457         m_leaseDuration = leaseDuration;
458     }
459
460     /**
461      * Returns the length of the maintained lease.
462      *
463      * @return The length of the maintained lease.
464      */

465     long getLeaseDuration()
466     {
467         return m_leaseDuration;
468     }
469 }
470
Popular Tags