77import javax .swing .JMenuBar ;
88import javax .swing .JMenuItem ;
99
10+ import painttools .tools .SelectionToolListener ;
11+ import ui .PaintPanel ;
12+ import actions .AddDataInputBoxAction ;
13+ import actions .AddTextBoxAction ;
1014import actions .ConstructLineSegmentAction ;
1115import actions .GeneratePolygonSourceJava ;
16+ import actions .InputDataForDataInputBoxAction ;
1217import actions .PaintAction ;
13- import painttools .tools .SelectionToolListener ;
14- import ui .PaintPanel ;
1518
1619public class ActionsMenuBar extends JMenuBar implements SelectionToolListener {
1720
1821 public ActionsMenuBar (PaintPanel panel ){
1922 addAction (new GeneratePolygonSourceJava (panel ));
2023 addAction (new ConstructLineSegmentAction (panel ));
24+ addAction (new AddTextBoxAction (panel ));
25+ addAction (new AddDataInputBoxAction (panel ));
26+ addAction (new InputDataForDataInputBoxAction (panel ));
2127
2228 }
2329
2430 private void addAction (PaintAction action ) {
2531 String [] strings = action .locationString ().split ("/" );
26- Object insertionMenu = this ;
27- //look for existing j menus
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 ;
32+ JMenu insertionMenu = null ;
33+ // look for existing i menus, determine where to insert
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 ;
39+ }
40+ }
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 ;
46+ }
47+
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+ }
3461 }
3562 }
36- //create a new if not found
37- JMenu toInsert = new JMenu (strings [k ]);
38- insertMenu ( insertionMenu , toInsert );
39- insertionMenu = toInsert ;
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+ }
4070 }
4171 //assume 2 level depth
4272 //TODO Change here
@@ -51,50 +81,25 @@ public void actionPerformed(ActionEvent e) {
5181
5282 }
5383 });
54-
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- }
84+ insertionMenu .add (item );
8285 }
8386
8487 @ Override
8588 public void selectionChanged () {
8689 for (int i = 0 ; i < getMenuCount (); i ++) {
8790 JMenu menu = getMenu (i );
88- for (int j = 0 ; j < menu .getItemCount (); j ++){
89- PaintActionMenuItem item = (PaintActionMenuItem ) menu .getItem (j );
90- item .setEnabled (item .getAssociatedAction ().canPerformAction ());
91- }
92-
93- }
91+ recursiveUpdate (menu );
92+ }
93+ }
9494
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+ }
95103 }
96-
97-
98104
99-
100105}
0 commit comments