@@ -29,26 +29,26 @@ public ActionsMenuBar(PaintPanel panel){
2929
3030 private void addAction (PaintAction action ) {
3131 String [] strings = action .locationString ().split ("/" );
32- JMenu insertionMenu = null ;
32+ Object insertionMenu = this ;
3333 //look for existing j menus
34- for (int i = 0 ; i < getMenuCount ();i ++) {
35- JMenu menu = getMenu (i );
36- if (menu .getText ().equals (strings [0 ])){
37- insertionMenu = menu ;
38- break ;
34+ for ( int k = 0 ; k < strings .length -1 ; k ++) {
35+ for (int i = 0 ; i < menuCount ( insertionMenu );i ++) {
36+ JMenuItem menu = obtainMenu (insertionMenu , i );
37+ if (menu .getText ().equals (strings [k ])){
38+ insertionMenu = menu ;
39+ break ;
40+ }
3941 }
42+ //create a new if not found
43+ JMenu toInsert = new JMenu (strings [k ]);
44+ insertMenu ( insertionMenu , toInsert );
45+ insertionMenu = toInsert ;
4046 }
41- //create a new if not found
42- if (insertionMenu == null ){
43- insertionMenu = new JMenu (strings [0 ]);
44- this .add (insertionMenu );
45- }
46-
4747 //assume 2 level depth
4848 //TODO Change here
4949 PaintActionMenuItem item = new PaintActionMenuItem (action );
5050 item .setEnabled (action .canPerformAction ());
51- item .setText (strings [1 ]);
51+ item .setText (strings [strings . length - 1 ]);
5252 item .addActionListener (new ActionListener () {
5353
5454 @ Override
@@ -58,8 +58,33 @@ public void actionPerformed(ActionEvent e) {
5858 }
5959 });
6060
61- insertionMenu .add (item );
62-
61+ insertMenu ( insertionMenu , item );
62+ }
63+ private int menuCount ( Object a ) {
64+ if ( a instanceof JMenu ) {
65+ return ((JMenu ) a ).getItemCount ();
66+ }
67+ if ( a instanceof JMenuBar ) {
68+ return ((JMenuBar ) a ).getMenuCount ();
69+ }
70+ return -1 ;
71+ }
72+ private JMenuItem obtainMenu ( Object a , int index ) {
73+ if ( a instanceof JMenu ) {
74+ return ((JMenu ) a ).getItem (index );
75+ }
76+ if ( a instanceof JMenuBar ) {
77+ return ((JMenuBar ) a ).getMenu (index );
78+ }
79+ return null ;
80+ }
81+ private void insertMenu ( Object a , JMenuItem toInsert ) {
82+ if ( a instanceof JMenu ) {
83+ ((JMenu ) a ).add (toInsert );
84+ }
85+ if ( a instanceof JMenuBar ) {
86+ ((JMenuBar ) a ).add (toInsert );
87+ }
6388 }
6489
6590 @ Override
0 commit comments