77import javax .swing .JMenuBar ;
88import javax .swing .JMenuItem ;
99
10- import actions .AddDataInputBoxAction ;
11- import actions .AddTextBoxAction ;
1210import actions .ConstructLineSegmentAction ;
1311import actions .GeneratePolygonSourceJava ;
14- import actions .InputDataForDataInputBoxAction ;
1512import actions .PaintAction ;
1613import painttools .tools .SelectionToolListener ;
1714import ui .PaintPanel ;
@@ -21,34 +18,31 @@ public class ActionsMenuBar extends JMenuBar implements SelectionToolListener{
2118 public ActionsMenuBar (PaintPanel panel ){
2219 addAction (new GeneratePolygonSourceJava (panel ));
2320 addAction (new ConstructLineSegmentAction (panel ));
24- addAction (new AddTextBoxAction (panel ));
25- addAction (new AddDataInputBoxAction (panel ));
26- addAction (new InputDataForDataInputBoxAction (panel ));
2721
2822 }
2923
3024 private void addAction (PaintAction action ) {
3125 String [] strings = action .locationString ().split ("/" );
32- JMenu insertionMenu = null ;
26+ Object insertionMenu = this ;
3327 //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 ;
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+ }
3935 }
36+ //create a new if not found
37+ JMenu toInsert = new JMenu (strings [k ]);
38+ insertMenu ( insertionMenu , toInsert );
39+ insertionMenu = toInsert ;
4040 }
41- //create a new if not found
42- if (insertionMenu == null ){
43- insertionMenu = new JMenu (strings [0 ]);
44- this .add (insertionMenu );
45- }
46-
4741 //assume 2 level depth
4842 //TODO Change here
4943 PaintActionMenuItem item = new PaintActionMenuItem (action );
5044 item .setEnabled (action .canPerformAction ());
51- item .setText (strings [1 ]);
45+ item .setText (strings [strings . length - 1 ]);
5246 item .addActionListener (new ActionListener () {
5347
5448 @ Override
@@ -58,8 +52,33 @@ public void actionPerformed(ActionEvent e) {
5852 }
5953 });
6054
61- insertionMenu .add (item );
62-
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+ }
6382 }
6483
6584 @ Override
0 commit comments