77import javax .swing .JMenuBar ;
88import javax .swing .JMenuItem ;
99
10+ import actions .AddDataInputBoxAction ;
11+ import actions .AddTextBoxAction ;
1012import actions .ConstructLineSegmentAction ;
1113import actions .GeneratePolygonSourceJava ;
14+ import actions .InputDataForDataInputBoxAction ;
1215import actions .PaintAction ;
1316import painttools .tools .SelectionToolListener ;
1417import ui .PaintPanel ;
@@ -18,31 +21,34 @@ public class ActionsMenuBar extends JMenuBar implements SelectionToolListener{
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 ;
32+ JMenu insertionMenu = null ;
2733 //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 ;
34- }
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 ;
3539 }
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+
4147 //assume 2 level depth
4248 //TODO Change here
4349 PaintActionMenuItem item = new PaintActionMenuItem (action );
4450 item .setEnabled (action .canPerformAction ());
45- item .setText (strings [strings . length - 1 ]);
51+ item .setText (strings [1 ]);
4652 item .addActionListener (new ActionListener () {
4753
4854 @ Override
@@ -52,33 +58,8 @@ public void actionPerformed(ActionEvent e) {
5258 }
5359 });
5460
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- }
61+ insertionMenu .add (item );
62+
8263 }
8364
8465 @ Override
0 commit comments