KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.event.ActionListener JavaDoc;
24
25 import javax.swing.AbstractAction JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import javax.swing.JCheckBoxMenuItem JavaDoc;
28 import javax.swing.JInternalFrame JavaDoc;
29 import javax.swing.JMenu JavaDoc;
30 import javax.swing.JMenuBar JavaDoc;
31 import javax.swing.JMenuItem JavaDoc;
32 import javax.swing.event.MenuEvent JavaDoc;
33 import javax.swing.event.MenuListener JavaDoc;
34
35 /**
36  *
37  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
38  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:23 $
39  * @since 4.1
40  */

41 public class MenuBar
42     extends JMenuBar JavaDoc
43 {
44     protected InstrumentClientFrame m_frame;
45     
46     boolean m_showUnconfigured = true;
47
48     private JMenu JavaDoc m_menuFile;
49
50     private JMenu JavaDoc m_menuInstrumentManagers;
51
52     private JMenu JavaDoc m_menuOptions;
53     private JCheckBoxMenuItem JavaDoc m_menuItemShowUnconfigured;
54
55     private JMenu JavaDoc m_menuWindow;
56
57     /*---------------------------------------------------------------
58      * Constructors
59      *-------------------------------------------------------------*/

60     MenuBar( InstrumentClientFrame frame )
61     {
62         m_frame = frame;
63
64         add( buildFileMenu() );
65         add( buildInstrumentManagerMenu() );
66         add( buildOptionsMenu() );
67         add( buildWindowMenu() );
68     }
69
70     /*---------------------------------------------------------------
71      * Methods
72      *-------------------------------------------------------------*/

73     private JMenu JavaDoc buildFileMenu()
74     {
75         m_menuFile = (JMenu JavaDoc)new LargeMenu( "File" );
76         m_menuFile.setMnemonic( 'F' );
77
78
79         // Clear
80
Action JavaDoc newAction = new AbstractAction JavaDoc( "New" )
81         {
82             public void actionPerformed( ActionEvent JavaDoc event )
83             {
84                 m_frame.fileNew();
85             }
86         };
87         JMenuItem JavaDoc newItem = new JMenuItem JavaDoc( newAction );
88         newItem.setMnemonic( 'N' );
89         m_menuFile.add( newItem );
90
91
92         // Open
93
Action JavaDoc openAction = new AbstractAction JavaDoc( "Open ..." )
94         {
95             public void actionPerformed( ActionEvent JavaDoc event )
96             {
97                 m_frame.fileOpen();
98             }
99         };
100         JMenuItem JavaDoc open = new JMenuItem JavaDoc( openAction );
101         open.setMnemonic( 'O' );
102         m_menuFile.add( open );
103
104
105         // Seperator
106
m_menuFile.addSeparator();
107
108
109         // Save
110
Action JavaDoc saveAction = new AbstractAction JavaDoc( "Save" )
111         {
112             public void actionPerformed( ActionEvent JavaDoc event )
113             {
114                 m_frame.fileSave();
115             }
116         };
117         JMenuItem JavaDoc save = new JMenuItem JavaDoc( saveAction );
118         save.setMnemonic( 'S' );
119         m_menuFile.add( save );
120
121
122         // Save As
123
Action JavaDoc saveAsAction = new AbstractAction JavaDoc( "Save As ..." )
124         {
125             public void actionPerformed( ActionEvent JavaDoc event )
126             {
127                 m_frame.fileSaveAs();
128             }
129         };
130         JMenuItem JavaDoc saveAs = new JMenuItem JavaDoc( saveAsAction );
131         saveAs.setMnemonic( 'A' );
132         m_menuFile.add( saveAs );
133
134
135         // Seperator
136
m_menuFile.addSeparator();
137
138
139         // Exit
140
Action JavaDoc exitAction = new AbstractAction JavaDoc( "Exit" )
141         {
142             public void actionPerformed( ActionEvent JavaDoc event )
143             {
144                 m_frame.fileExit();
145             }
146         };
147         JMenuItem JavaDoc exit = new JMenuItem JavaDoc( exitAction );
148         exit.setMnemonic( 'X' );
149         m_menuFile.add( exit );
150
151         return m_menuFile;
152     }
153
154     private JMenu JavaDoc buildInstrumentManagerMenu()
155     {
156         m_menuInstrumentManagers = new LargeMenu( "Instrument Managers" );
157         m_menuInstrumentManagers.setMnemonic( 'I' );
158
159         m_menuInstrumentManagers.addMenuListener( new MenuListener JavaDoc()
160         {
161             public void menuSelected( MenuEvent JavaDoc event )
162             {
163                 rebuildInstrumentManagersMenu();
164             }
165
166             public void menuDeselected( MenuEvent JavaDoc event )
167             {
168             }
169
170             public void menuCanceled( MenuEvent JavaDoc event )
171             {
172             }
173         } );
174
175         return m_menuInstrumentManagers;
176     }
177
178     private void rebuildInstrumentManagersMenu()
179     {
180         m_menuInstrumentManagers.removeAll();
181
182         // Add Connect menu item
183
Action JavaDoc connectAction = new AbstractAction JavaDoc( "Connect to Instrument Manager..." )
184         {
185             public void actionPerformed( ActionEvent JavaDoc event )
186             {
187                 // For now, skip the dialog
188
m_frame.showConnectDialog();
189             }
190         };
191
192         JMenuItem JavaDoc connectItem = new JMenuItem JavaDoc( connectAction );
193         connectItem.setMnemonic( 'C' );
194         m_menuInstrumentManagers.add( connectItem );
195
196         // Add links to the connections
197
InstrumentManagerConnection[] connections = m_frame.getConnections();
198         if ( connections.length > 0 )
199         {
200             m_menuInstrumentManagers.addSeparator();
201
202             for ( int i = 0; i < connections.length; i++ )
203             {
204                 InstrumentManagerConnection connection = connections[i];
205
206                 Action JavaDoc action = new AbstractAction JavaDoc( connection.getTitle() )
207                 {
208                     public void actionPerformed( ActionEvent JavaDoc event )
209                     {
210                     }
211                 };
212                 action.putValue( "InstrumentManagerConnection", connection );
213
214                 JMenu JavaDoc menu = new LargeMenu( action );
215
216                 // Set up a Listener to handle the selected event.
217
menu.addMenuListener( new MenuListener JavaDoc()
218                 {
219                     public void menuSelected( MenuEvent JavaDoc event )
220                     {
221                         JMenu JavaDoc menu = (JMenu JavaDoc)event.getSource();
222                         Action JavaDoc action = menu.getAction();
223
224                         rebuildInstrumentManagerMenu(
225                             menu, (InstrumentManagerConnection)action.getValue(
226                             "InstrumentManagerConnection" ) );
227                     }
228
229                     public void menuDeselected( MenuEvent JavaDoc event )
230                     {
231                     }
232
233                     public void menuCanceled( MenuEvent JavaDoc event )
234                     {
235                     }
236                 } );
237
238                 m_menuInstrumentManagers.add( menu );
239             }
240         }
241     }
242
243     private void rebuildInstrumentManagerMenu( final JMenu JavaDoc managerMenu,
244                                                final InstrumentManagerConnection connection )
245     {
246         managerMenu.removeAll();
247
248         boolean showAll = m_showUnconfigured;
249
250         // Delete
251
Action JavaDoc deleteAction = new AbstractAction JavaDoc( "Delete" )
252         {
253             public void actionPerformed( ActionEvent JavaDoc event )
254             {
255                 JMenuItem JavaDoc item = (JMenuItem JavaDoc)event.getSource();
256                 Action JavaDoc action = item.getAction();
257
258                 InstrumentManagerConnection connection =
259                     (InstrumentManagerConnection)action.getValue( "InstrumentManagerConnection" );
260
261                 connection.delete();
262             }
263         };
264         deleteAction.putValue( "InstrumentManagerConnection", connection );
265
266         JMenuItem JavaDoc deleteItem = new JMenuItem JavaDoc( deleteAction );
267         deleteItem.setMnemonic( 'I' );
268         managerMenu.add( deleteItem );
269
270         // Instrumentable menu items
271
InstrumentManagerData manager = connection.getInstrumentManager();
272
273         if ( manager != null )
274         {
275             managerMenu.addSeparator();
276
277             InstrumentableData[] instrumentables = manager.getInstrumentables();
278
279             for( int i = 0; i < instrumentables.length; i++ )
280             {
281                 InstrumentableData instrumentable = instrumentables[i];
282
283                 if( showAll || instrumentable.isConfigured() )
284                 {
285                     String JavaDoc description = instrumentable.getDescription();
286
287                     Action JavaDoc action = new AbstractAction JavaDoc( description )
288                     {
289                         public void actionPerformed( ActionEvent JavaDoc event )
290                         {
291                         }
292                     };
293                     action.putValue( "InstrumentManagerConnection", connection );
294                     action.putValue( "InstrumentableData", instrumentable );
295
296                     JMenu JavaDoc menu = new LargeMenu( action );
297
298                     // Set up a Listener to handle the selected event.
299
menu.addMenuListener( new MenuListener JavaDoc()
300                     {
301                         public void menuSelected( MenuEvent JavaDoc event )
302                         {
303                             JMenu JavaDoc menu = (JMenu JavaDoc)event.getSource();
304                             Action JavaDoc action = menu.getAction();
305
306                             rebuildInstrumentableMenu(
307                                 menu,
308                                 (InstrumentManagerConnection)action.getValue(
309                                     "InstrumentManagerConnection" ),
310                                 (InstrumentableData)action.getValue(
311                                     "InstrumentableData" ) );
312                         }
313
314                         public void menuDeselected( MenuEvent JavaDoc event )
315                         {
316                         }
317
318                         public void menuCanceled( MenuEvent JavaDoc event )
319                         {
320                         }
321                     } );
322
323                     managerMenu.add( menu );
324                 }
325             }
326         }
327     }
328
329     private void rebuildInstrumentableMenu( final JMenu JavaDoc instrumentableMenu,
330                                             final InstrumentManagerConnection connection,
331                                             final InstrumentableData instrumentable )
332     {
333         instrumentableMenu.removeAll();
334
335         boolean showAll = m_showUnconfigured;
336         
337         // Child Instrumentables
338
InstrumentableData[] children = instrumentable.getInstrumentables();
339
340         for( int i = 0; i < children.length; i++ )
341         {
342             InstrumentableData child = children[i];
343
344             if( showAll || child.isConfigured() )
345             {
346                 String JavaDoc description = child.getDescription();
347
348                 Action JavaDoc action = new AbstractAction JavaDoc( description )
349                 {
350                     public void actionPerformed( ActionEvent JavaDoc event )
351                     {
352                     }
353                 };
354                 action.putValue( "InstrumentManagerConnection", connection );
355                 action.putValue( "InstrumentableData", child );
356
357                 JMenu JavaDoc menu = new LargeMenu( action );
358
359                 // Set up a Listener to handle the selected event.
360
menu.addMenuListener( new MenuListener JavaDoc()
361                 {
362                     public void menuSelected( MenuEvent JavaDoc event )
363                     {
364                         JMenu JavaDoc menu = (JMenu JavaDoc)event.getSource();
365                         Action JavaDoc action = menu.getAction();
366
367                         rebuildInstrumentableMenu(
368                             menu,
369                             (InstrumentManagerConnection)action.getValue(
370                                 "InstrumentManagerConnection" ),
371                             (InstrumentableData)action.getValue(
372                                 "InstrumentableData" ) );
373                     }
374
375                     public void menuDeselected( MenuEvent JavaDoc event )
376                     {
377                     }
378
379                     public void menuCanceled( MenuEvent JavaDoc event )
380                     {
381                     }
382                 } );
383
384                 instrumentableMenu.add( menu );
385             }
386         }
387         
388         // Instruments
389
InstrumentData[] instruments = instrumentable.getInstruments();
390
391         for( int i = 0; i < instruments.length; i++ )
392         {
393             InstrumentData instrument = instruments[ i ];
394
395             if( showAll || instrument.isConfigured() )
396             {
397                 String JavaDoc description = instrument.getDescription();
398
399                 Action JavaDoc action = new AbstractAction JavaDoc( description )
400                 {
401                     public void actionPerformed( ActionEvent JavaDoc event )
402                     {
403                     }
404                 };
405                 action.putValue( "InstrumentManagerConnection", connection );
406                 action.putValue( "InstrumentableData", instrumentable );
407                 action.putValue( "InstrumentData", instrument );
408
409                 JMenu JavaDoc menu = new LargeMenu( action );
410
411                 // Set up a Listener to handle the selected event.
412
menu.addMenuListener( new MenuListener JavaDoc()
413                 {
414                     public void menuSelected( MenuEvent JavaDoc event )
415                     {
416                         JMenu JavaDoc menu = (JMenu JavaDoc)event.getSource();
417                         Action JavaDoc action = menu.getAction();
418
419                         rebuildInstrumentMenu(
420                             menu,
421                             (InstrumentManagerConnection)action.getValue(
422                                 "InstrumentManagerConnection" ),
423                             (InstrumentableData)action.getValue(
424                                 "InstrumentableData" ),
425                             (InstrumentData)action.getValue(
426                                 "InstrumentData" ) );
427                     }
428
429                     public void menuDeselected( MenuEvent JavaDoc event )
430                     {
431                     }
432
433                     public void menuCanceled( MenuEvent JavaDoc event )
434                     {
435                     }
436                 } );
437
438                 instrumentableMenu.add( menu );
439             }
440         }
441     }
442
443     private void rebuildInstrumentMenu( final JMenu JavaDoc instrumentMenu,
444                                         final InstrumentManagerConnection connection,
445                                         final InstrumentableData instrumentable,
446                                         final InstrumentData instrument )
447     {
448         instrumentMenu.removeAll();
449
450         boolean showAll = m_showUnconfigured;
451
452         // Create Sample
453
Action JavaDoc createAction = new AbstractAction JavaDoc( "Create Sample..." )
454         {
455             public void actionPerformed( ActionEvent JavaDoc event )
456             {
457                 connection.showCreateSampleDialog( instrument );
458             }
459         };
460         createAction.putValue( "InstrumentManagerConnection", connection );
461         createAction.putValue( "InstrumentableData", instrumentable );
462         createAction.putValue( "InstrumentData", instrument );
463
464         JMenuItem JavaDoc createItem = new JMenuItem JavaDoc( createAction );
465         createItem.setMnemonic( 'C' );
466         instrumentMenu.add( createItem );
467
468         InstrumentSampleData[] samples = instrument.getInstrumentSamples();
469
470         if ( samples.length > 0 )
471         {
472             instrumentMenu.addSeparator();
473
474             for( int i = 0; i < samples.length; i++ )
475             {
476                 final InstrumentSampleData sample = samples[ i ];
477
478                 if( showAll || sample.isConfigured() )
479                 {
480                     String JavaDoc description = sample.getDescription();
481
482                     Action JavaDoc action = new AbstractAction JavaDoc( description )
483                     {
484                         public void actionPerformed( ActionEvent JavaDoc event )
485                         {
486                             connection.viewSample( sample.getName() );
487                         }
488                     };
489                     action.putValue( "InstrumentManagerConnection", connection );
490                     action.putValue( "InstrumentSampleData", sample );
491
492                     JMenuItem JavaDoc item = new JMenuItem JavaDoc( action );
493
494                     instrumentMenu.add( item );
495                 }
496             }
497         }
498     }
499     
500     private JMenu JavaDoc buildOptionsMenu()
501     {
502         m_menuOptions = new LargeMenu( "Options" );
503         m_menuOptions.setMnemonic( 'O' );
504
505         m_menuOptions.addMenuListener( new MenuListener JavaDoc()
506         {
507             public void menuSelected( MenuEvent JavaDoc event )
508             {
509                 rebuildOptionsMenu();
510             }
511
512             public void menuDeselected( MenuEvent JavaDoc event )
513             {
514             }
515
516             public void menuCanceled( MenuEvent JavaDoc event )
517             {
518             }
519         } );
520         
521         /*
522         // Show Unconfigured Profilables option
523         m_menuItemShowUnconfigured =
524             new JCheckBoxMenuItem( "Show Unconfigured Instruments", true );
525         m_menuOptions.add( m_menuItemShowUnconfigured );
526         */

527
528         return m_menuOptions;
529     }
530
531     protected void rebuildOptionsMenu()
532     {
533         m_menuOptions.removeAll();
534         
535         final JCheckBoxMenuItem JavaDoc antialias =
536             new JCheckBoxMenuItem JavaDoc( "Antialias", m_frame.isAntialias() );
537         antialias.setMnemonic( 'a' );
538         antialias.addActionListener( new ActionListener JavaDoc()
539             {
540                 public void actionPerformed( ActionEvent JavaDoc event )
541                 {
542                     m_frame.setAntialias( antialias.getState() );
543                 }
544             } );
545         m_menuOptions.add( antialias );
546     }
547
548     private JMenu JavaDoc buildWindowMenu()
549     {
550         m_menuWindow = new LargeMenu( "Window" );
551         m_menuWindow.setMnemonic( 'W' );
552
553         m_menuWindow.addMenuListener( new MenuListener JavaDoc()
554         {
555             public void menuSelected( MenuEvent JavaDoc event )
556             {
557                 rebuildWindowMenu();
558             }
559
560             public void menuDeselected( MenuEvent JavaDoc event )
561             {
562             }
563
564             public void menuCanceled( MenuEvent JavaDoc event )
565             {
566             }
567         } );
568
569         return m_menuWindow;
570     }
571
572     protected void rebuildWindowMenu()
573     {
574         m_menuWindow.removeAll();
575
576         // Tile window menu choice
577
Action JavaDoc tileFramesAction = new AbstractAction JavaDoc( "Tile frames" )
578         {
579             public void actionPerformed( ActionEvent JavaDoc event )
580             {
581                 m_frame.tileFrames();
582             }
583         };
584
585         JMenuItem JavaDoc tileFrames = new JMenuItem JavaDoc( tileFramesAction );
586         tileFrames.setMnemonic( 't' );
587         m_menuWindow.add( tileFrames );
588
589
590         // Tile window vertically menu choice
591
Action JavaDoc tileFramesVAction = new AbstractAction JavaDoc( "Tile frames vertically" )
592         {
593             public void actionPerformed( ActionEvent JavaDoc event )
594             {
595                 m_frame.tileFramesV();
596             }
597         };
598
599         JMenuItem JavaDoc tileFramesV = new JMenuItem JavaDoc( tileFramesVAction );
600         tileFramesV.setMnemonic( 'v' );
601         m_menuWindow.add( tileFramesV );
602
603         // Tile window horizontally menu choice
604
Action JavaDoc tileFramesHAction = new AbstractAction JavaDoc( "Tile frames horizontally" )
605         {
606             public void actionPerformed( ActionEvent JavaDoc event )
607             {
608                 m_frame.tileFramesH();
609             }
610         };
611
612         JMenuItem JavaDoc tileFramesH = new JMenuItem JavaDoc( tileFramesHAction );
613         tileFramesH.setMnemonic( 'h' );
614         m_menuWindow.add( tileFramesH );
615
616         // Close All menu choice
617
Action JavaDoc closeAllAction = new AbstractAction JavaDoc( "Close All" )
618         {
619             public void actionPerformed( ActionEvent JavaDoc event )
620             {
621                 m_frame.closeAllFrames();
622             }
623         };
624
625         JMenuItem JavaDoc closeAll = new JMenuItem JavaDoc( closeAllAction );
626         closeAll.setMnemonic( 'o' );
627         m_menuWindow.add( closeAll );
628
629
630         // List up all of the visible frames.
631
JInternalFrame JavaDoc[] frames = m_frame.getDesktopPane().getAllFrames();
632
633         if( frames.length > 0 )
634         {
635             m_menuWindow.addSeparator();
636         }
637
638         for( int i = 0; i < frames.length; i++ )
639         {
640             String JavaDoc label = ( i + 1 ) + " " + frames[ i ].getTitle();
641             Action JavaDoc action = new AbstractAction JavaDoc( label )
642             {
643                 public void actionPerformed( ActionEvent JavaDoc event )
644                 {
645                     JMenuItem JavaDoc menu = (JMenuItem JavaDoc)event.getSource();
646                     Action JavaDoc action = menu.getAction();
647
648                     JInternalFrame JavaDoc frame = (JInternalFrame JavaDoc)action.getValue( "frame" );
649                     try
650                     {
651                         if( frame.isIcon() )
652                         {
653                             // Restore the frame
654
frame.setIcon( false );
655                         }
656                         frame.setSelected( true );
657                         m_frame.getDesktopPane().moveToFront( frame );
658                     }
659                     catch( java.beans.PropertyVetoException JavaDoc e )
660                     {
661                     }
662                 }
663             };
664             action.putValue( "frame", frames[ i ] );
665
666             JMenuItem JavaDoc item = new JMenuItem JavaDoc( action );
667             m_menuWindow.add( item );
668
669             if( i < 10 )
670             {
671                 item.setMnemonic( (char)( '1' + i ) );
672             }
673         }
674     }
675 }
676
Popular Tags