@@ -23,26 +23,26 @@ public ActionsMenuBar(PaintPanel panel){
2323
2424 private void addAction (PaintAction action ) {
2525 String [] strings = action .locationString ().split ("/" );
26- JMenu insertionMenu = null ;
26+ Object insertionMenu = this ;
2727 //look for existing j menus
28- for (int i = 0 ; i < getMenuCount ();i ++) {
29- JMenu menu = getMenu (i );
30- if (menu .getText ().equals (strings [0 ])){
31- insertionMenu = menu ;
32- break ;
28+ for ( int k = 0 ; k < strings .length -1 ; k ++) {
29+ for (int i = 0 ; i < menuCount ( insertionMenu );i ++) {
30+ JMenuItem menu = obtainMenu (insertionMenu , i );
31+ if (menu .getText ().equals (strings [k ])){
32+ insertionMenu = menu ;
33+ break ;
34+ }
3335 }
36+ //create a new if not found
37+ JMenu toInsert = new JMenu (strings [k ]);
38+ insertMenu ( insertionMenu , toInsert );
39+ insertionMenu = toInsert ;
3440 }
35- //create a new if not found
36- if (insertionMenu == null ){
37- insertionMenu = new JMenu (strings [0 ]);
38- this .add (insertionMenu );
39- }
40-
4141 //assume 2 level depth
4242 //TODO Change here
4343 PaintActionMenuItem item = new PaintActionMenuItem (action );
4444 item .setEnabled (action .canPerformAction ());
45- item .setText (strings [1 ]);
45+ item .setText (strings [strings . length - 1 ]);
4646 item .addActionListener (new ActionListener () {
4747
4848 @ Override
@@ -52,8 +52,33 @@ public void actionPerformed(ActionEvent e) {
5252 }
5353 });
5454
55- insertionMenu .add (item );
56-
55+ insertMenu ( insertionMenu , item );
56+ }
57+ private int menuCount ( Object a ) {
58+ if ( a instanceof JMenu ) {
59+ return ((JMenu ) a ).getItemCount ();
60+ }
61+ if ( a instanceof JMenuBar ) {
62+ return ((JMenuBar ) a ).getMenuCount ();
63+ }
64+ return -1 ;
65+ }
66+ private JMenuItem obtainMenu ( Object a , int index ) {
67+ if ( a instanceof JMenu ) {
68+ return ((JMenu ) a ).getItem (index );
69+ }
70+ if ( a instanceof JMenuBar ) {
71+ return ((JMenuBar ) a ).getMenu (index );
72+ }
73+ return null ;
74+ }
75+ private void insertMenu ( Object a , JMenuItem toInsert ) {
76+ if ( a instanceof JMenu ) {
77+ ((JMenu ) a ).add (toInsert );
78+ }
79+ if ( a instanceof JMenuBar ) {
80+ ((JMenuBar ) a ).add (toInsert );
81+ }
5782 }
5883
5984 @ Override
0 commit comments