KickJava   Java API By Example, From Geeks To Geeks.

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


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

31 class InstrumentNodeData
32     extends NodeData
33 {
34     /** A counter Instrument which exists because of configuration. */
35     private static final ImageIcon JavaDoc m_iconInstrumentCtrConf;
36     
37     /** A counter Instrument which exists because of registration. */
38     private static final ImageIcon JavaDoc m_iconInstrumentCtrReg;
39     
40     /** A counter Instrument which exists because of registration and
41      * configuration. */

42     private static final ImageIcon JavaDoc m_iconInstrumentCtrRegConf;
43     
44     /** A counter Instrument which exists because it was loaded from the state
45      * file but is no longer used. */

46     private static final ImageIcon JavaDoc m_iconInstrumentCtrOld;
47     
48     /** A value Instrument which exists because of configuration. */
49     private static final ImageIcon JavaDoc m_iconInstrumentValConf;
50     
51     /** A value Instrument which exists because of registration. */
52     private static final ImageIcon JavaDoc m_iconInstrumentValReg;
53     
54     /** A value Instrument which exists because of registration and
55      * configuration. */

56     private static final ImageIcon JavaDoc m_iconInstrumentValRegConf;
57     
58     /** A value Instrument which exists because it was loaded from the state
59      * file but is no longer used. */

60     private static final ImageIcon JavaDoc m_iconInstrumentValOld;
61     
62     private InstrumentData m_data;
63     private InstrumentManagerConnection m_connection;
64     
65     private boolean m_configured;
66     private boolean m_registered;
67     private int m_type;
68     
69     /*---------------------------------------------------------------
70      * Class Initializer
71      *-------------------------------------------------------------*/

72     static
73     {
74         // Load the images.
75
ClassLoader JavaDoc cl = InstrumentManagerTreeCellRenderer.class.getClassLoader();
76         m_iconInstrumentCtrConf =
77             new ImageIcon JavaDoc( cl.getResource( MEDIA_PATH + "instrument_ctr_conf.gif") );
78         m_iconInstrumentCtrReg =
79             new ImageIcon JavaDoc( cl.getResource( MEDIA_PATH + "instrument_ctr_reg.gif") );
80         m_iconInstrumentCtrRegConf =
81             new ImageIcon JavaDoc( cl.getResource( MEDIA_PATH + "instrument_ctr_reg_conf.gif") );
82         m_iconInstrumentCtrOld =
83             new ImageIcon JavaDoc( cl.getResource( MEDIA_PATH + "instrument_ctr_old.gif") );
84         
85         m_iconInstrumentValConf =
86             new ImageIcon JavaDoc( cl.getResource( MEDIA_PATH + "instrument_val_conf.gif") );
87         m_iconInstrumentValReg =
88             new ImageIcon JavaDoc( cl.getResource( MEDIA_PATH + "instrument_val_reg.gif") );
89         m_iconInstrumentValRegConf =
90             new ImageIcon JavaDoc( cl.getResource( MEDIA_PATH + "instrument_val_reg_conf.gif") );
91         m_iconInstrumentValOld =
92             new ImageIcon JavaDoc( cl.getResource( MEDIA_PATH + "instrument_val_old.gif") );
93     }
94     
95     /*---------------------------------------------------------------
96      * Constructors
97      *-------------------------------------------------------------*/

98     InstrumentNodeData( InstrumentData data,
99                         InstrumentManagerConnection connection )
100     {
101         m_data = data;
102         m_connection = connection;
103         
104         update();
105     }
106     
107     
108     /*---------------------------------------------------------------
109      * NodeData Methods
110      *-------------------------------------------------------------*/

111     /**
112      * Get the icon to display for the node.
113      *
114      * @return the icon to display for the node.
115      */

116     ImageIcon JavaDoc getIcon()
117     {
118         ImageIcon JavaDoc icon;
119         switch ( getType() )
120         {
121         case InstrumentData.INSTRUMENT_TYPE_COUNTER:
122             if ( isConfigured() && isRegistered() )
123             {
124                 icon = m_iconInstrumentCtrRegConf;
125             }
126             else if ( isConfigured() )
127             {
128                 icon = m_iconInstrumentCtrConf;
129             }
130             else if ( isRegistered() )
131             {
132                 icon = m_iconInstrumentCtrReg;
133             }
134             else
135             {
136                 icon = m_iconInstrumentCtrOld;
137             }
138             break;
139             
140         case InstrumentData.INSTRUMENT_TYPE_VALUE:
141             if ( isConfigured() && isRegistered() )
142             {
143                 icon = m_iconInstrumentValRegConf;
144             }
145             else if ( isConfigured() )
146             {
147                 icon = m_iconInstrumentValConf;
148             }
149             else if ( isRegistered() )
150             {
151                 icon = m_iconInstrumentValReg;
152             }
153             else
154             {
155                 icon = m_iconInstrumentValOld;
156             }
157             break;
158             
159         default:
160             throw new IllegalStateException JavaDoc( "Encountered an unknown instrument type: " +
161                 getType() );
162         }
163         
164         return icon;
165     }
166     
167     /**
168      * Return the text to use for a tool tip on this node.
169      *
170      * @return Tool Tip text. May be null, for no tool tip.
171      */

172     String JavaDoc getToolTipText()
173     {
174         String JavaDoc text;
175         switch ( getType() )
176         {
177         case InstrumentData.INSTRUMENT_TYPE_COUNTER:
178             if ( isConfigured() && isRegistered() )
179             {
180                 text = "Registered and Configured Counter Instrument";
181             }
182             else if ( isConfigured() )
183             {
184                 text = "Configured but unregistered Counter Instrument";
185             }
186             else if ( isRegistered() )
187             {
188                 text = "Registered Counter Instrument";
189             }
190             else
191             {
192                 text = "Old Counter Instrument loaded from state file";
193             }
194             break;
195             
196         case InstrumentData.INSTRUMENT_TYPE_VALUE:
197             if ( isConfigured() && isRegistered() )
198             {
199                 text = "Registered and Configured Value Instrument";
200             }
201             else if ( isConfigured() )
202             {
203                 text = "Configured but unregistered Value Instrument";
204             }
205             else if ( isRegistered() )
206             {
207                 text = "Registered Value Instrument";
208             }
209             else
210             {
211                 text = "Old Value Instrument loaded from state file";
212             }
213             break;
214             
215         default:
216             throw new IllegalStateException JavaDoc( "Encountered an unknown instrument type: " +
217                 getType() );
218         }
219         
220         return text;
221     }
222     
223     /**
224      * Returns an array of any menu items which will be displayed both
225      * in a popup menu and in the menus.
226      *
227      * @return An array of the common menu items.
228      */

229     public JMenuItem JavaDoc[] getCommonMenuItems()
230     {
231         JMenuItem JavaDoc[] menuItems = new JMenuItem JavaDoc[1];
232         
233         Action JavaDoc createSampleAction = new AbstractAction JavaDoc( "Create Sample..." )
234         {
235             public void actionPerformed( ActionEvent JavaDoc event )
236             {
237                 // Display a dialog to ask the user what new sample to create.
238
InstrumentData data = InstrumentNodeData.this.getData();
239                 m_connection.showCreateSampleDialog( data );
240             }
241         };
242         JMenuItem JavaDoc createSampleItem = new JMenuItem JavaDoc( createSampleAction );
243         createSampleItem.setMnemonic( 'C' );
244         menuItems[0] = createSampleItem;
245         
246         return menuItems;
247     }
248     
249     /*---------------------------------------------------------------
250      * Methods
251      *-------------------------------------------------------------*/

252     InstrumentData getData()
253     {
254         return m_data;
255     }
256     
257     boolean isConfigured()
258     {
259         return m_configured;
260     }
261     
262     boolean isRegistered()
263     {
264         return m_registered;
265     }
266     
267     int getType()
268     {
269         return m_type;
270     }
271     
272     boolean update()
273     {
274         boolean changed = false;
275         changed |= update( m_data.getName(), m_data.getDescription(), m_data.getStateVersion() );
276         
277         boolean newConfigured = m_data.isConfigured();
278         if ( newConfigured != m_configured )
279         {
280             changed = true;
281             m_configured = newConfigured;
282         }
283         
284         boolean newRegistered = m_data.isRegistered();
285         if ( newRegistered != m_registered )
286         {
287             changed = true;
288             m_registered = newRegistered;
289         }
290         
291         int newType = m_data.getType();
292         if ( newType != m_type )
293         {
294             changed = true;
295             m_type = newType;
296         }
297         
298         return changed;
299     }
300 }
301
Popular Tags