77import javax .swing .JMenuBar ;
88import javax .swing .JMenuItem ;
99
10+ import painttools .tools .SelectionToolListener ;
11+ import ui .PaintPanel ;
1012import actions .AddDataInputBoxAction ;
1113import actions .AddTextBoxAction ;
1214import actions .ConstructLineSegmentAction ;
1315import actions .GeneratePolygonSourceJava ;
1416import actions .InputDataForDataInputBoxAction ;
1517import actions .PaintAction ;
16- import painttools .tools .SelectionToolListener ;
17- import ui .PaintPanel ;
1818
1919public class ActionsMenuBar extends JMenuBar implements SelectionToolListener {
2020
@@ -30,25 +30,49 @@ public ActionsMenuBar(PaintPanel panel){
3030 private void addAction (PaintAction action ) {
3131 String [] strings = action .locationString ().split ("/" );
3232 JMenu insertionMenu = null ;
33- //look for existing j menus
34- for ( int i = 0 ; i < getMenuCount ();i ++) {
33+ // look for existing i menus, determine where to insert
34+ for ( int i = 0 ; i < getMenuCount (); i ++) {
3535 JMenu menu = getMenu (i );
3636 if (menu .getText ().equals (strings [0 ])){
3737 insertionMenu = menu ;
3838 break ;
39- }
39+ }
4040 }
41- //create a new if not found
42- if (insertionMenu == null ){
43- insertionMenu = new JMenu (strings [0 ]);
44- this .add (insertionMenu );
41+ // if not found, create a new menu
42+ if ( insertionMenu == null ) {
43+ JMenu newMenu = new JMenu (strings [0 ]);
44+ add (newMenu );
45+ insertionMenu = newMenu ;
4546 }
4647
48+ // do the similar steps above for k level
49+ for ( int k = 1 ; k < strings .length -1 ; k ++) {
50+ boolean menuFound = false ;
51+ for (int i = 0 ; i < insertionMenu .getItemCount ();i ++) {
52+
53+ // only check JMenu, exclude PaintActionMenuItem
54+ if (insertionMenu .getItem (i ) instanceof JMenu ) {
55+ JMenu menu = (JMenu )insertionMenu .getItem (i );
56+ if (menu .getText ().equals (strings [k ])){
57+ insertionMenu = menu ;
58+ menuFound = true ;
59+ break ;
60+ }
61+ }
62+ }
63+ // if not found, create a new menu
64+
65+ if ( !menuFound ) {
66+ JMenu newMenu = new JMenu (strings [k ]);
67+ insertionMenu .add (newMenu );
68+ insertionMenu = newMenu ;
69+ }
70+ }
4771 //assume 2 level depth
4872 //TODO Change here
4973 PaintActionMenuItem item = new PaintActionMenuItem (action );
5074 item .setEnabled (action .canPerformAction ());
51- item .setText (strings [1 ]);
75+ item .setText (strings [strings . length - 1 ]);
5276 item .addActionListener (new ActionListener () {
5377
5478 @ Override
@@ -57,25 +81,25 @@ public void actionPerformed(ActionEvent e) {
5781
5882 }
5983 });
60-
6184 insertionMenu .add (item );
62-
6385 }
6486
6587 @ Override
6688 public void selectionChanged () {
6789 for (int i = 0 ; i < getMenuCount (); i ++) {
6890 JMenu menu = getMenu (i );
69- for (int j = 0 ; j < menu .getItemCount (); j ++){
70- PaintActionMenuItem item = (PaintActionMenuItem ) menu .getItem (j );
71- item .setEnabled (item .getAssociatedAction ().canPerformAction ());
72- }
73-
74- }
91+ recursiveUpdate (menu );
92+ }
93+ }
7594
95+ private void recursiveUpdate (JMenu jitem ) {
96+ for ( int i = 0 ; i < jitem .getItemCount (); i ++ ) {
97+ JMenuItem item = jitem .getItem (i );
98+ if ( item instanceof PaintActionMenuItem )
99+ item .setEnabled (((PaintActionMenuItem )item ).getAssociatedAction ().canPerformAction ());
100+ else if ( item instanceof JMenu )
101+ recursiveUpdate ( (JMenu )item );
102+ }
76103 }
77-
78104
79-
80-
81105}
0 commit comments