KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > menu > XJMenu


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.xjlib.appkit.menu;
33
34 import javax.swing.*;
35 import javax.swing.event.MenuEvent JavaDoc;
36 import javax.swing.event.MenuListener JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40
41 public class XJMenu extends XJMenuItem {
42
43     protected JMenu jMenu = new JMenu();
44     protected List JavaDoc<XJMenuItem> items = new ArrayList JavaDoc<XJMenuItem>();
45     protected XJMainMenuBar mainMenuBar;
46
47     public XJMenu() {
48         jMenu.addMenuListener(new XJMenuListener());
49     }
50
51     public void setMainMenuBar(XJMainMenuBar mainMenuBar) {
52         this.mainMenuBar = mainMenuBar;
53         for(int index=0; index<items.size(); index++) {
54             Object JavaDoc item = items.get(index);
55             if(item instanceof XJMenu) {
56                 XJMenu menu = (XJMenu)item;
57                 menu.setMainMenuBar(mainMenuBar);
58             }
59         }
60     }
61
62     public void setTitle(String JavaDoc title) {
63         jMenu.setText(title);
64     }
65
66     public void addSeparator() {
67         items.add(new XJMenuItemSeparator());
68         jMenu.addSeparator();
69     }
70
71     public void addItem(XJMenuItem item) {
72         item.setMenu(this);
73         items.add(item);
74         jMenu.add(item.getSwingComponent());
75     }
76
77     public void addItem(XJMenu menu) {
78         menu.setMenu(this);
79         items.add(menu);
80         jMenu.add(menu.getSwingComponent());
81     }
82
83     public void insertSeparatorBefore(int menuItemTag) {
84         XJMenuItem menuItem = getItemForTag(menuItemTag);
85         if(menuItem == null)
86             return;
87
88         insertSeparatorAtIndex(items.indexOf(menuItem));
89     }
90
91     public void insertSeparatorAfter(int menuItemTag) {
92         XJMenuItem menuItem = getItemForTag(menuItemTag);
93         if(menuItem == null)
94             return;
95
96         insertSeparatorAtIndex(items.indexOf(menuItem)+1);
97     }
98
99     public void insertSeparatorAtIndex(int index) {
100         items.add(index, new XJMenuItemSeparator());
101         jMenu.insertSeparator(index);
102     }
103
104     public void insertItemBefore(XJMenuItem item, int menuItemTag) {
105         XJMenuItem menuItem = getItemForTag(menuItemTag);
106         if(menuItem == null)
107             return;
108
109         insertItemAtIndex(item, items.indexOf(menuItem));
110     }
111
112     public void insertItemAfter(XJMenuItem item, int menuItemTag) {
113         XJMenuItem menuItem = getItemForTag(menuItemTag);
114         if(menuItem == null)
115             return;
116
117         insertItemAtIndex(item, items.indexOf(menuItem)+1);
118     }
119
120     public void insertItemAtIndex(XJMenuItem item, int index) {
121         items.add(index, item);
122         jMenu.add(item.getSwingComponent(), index);
123     }
124
125     public void removeItem(int index) {
126         jMenu.remove(index);
127     }
128
129     public XJMenuItem getItemForTag(int tag) {
130         Iterator JavaDoc<XJMenuItem> iterator = items.iterator();
131         while(iterator.hasNext()) {
132             XJMenuItem menuItem = iterator.next();
133             if(menuItem.getTag() == tag)
134                 return menuItem;
135         }
136         return null;
137     }
138
139     public XJMenuItem getItemAtIndex(int index) {
140         if(index >=0 && index < items.size())
141             return items.get(index);
142         else
143             return null;
144     }
145
146     public int getItemCount() {
147         return jMenu.getItemCount();
148     }
149
150     public Iterator JavaDoc<XJMenuItem> itemIterator() {
151         return items.iterator();
152     }
153
154     public void clear() {
155         items.clear();
156         jMenu.removeAll();
157     }
158
159     public JComponent getSwingComponent() {
160         return jMenu;
161     }
162
163     public class XJMenuListener implements MenuListener JavaDoc {
164
165         public void menuSelected(MenuEvent JavaDoc e) {
166             if(mainMenuBar != null)
167                 mainMenuBar.menuSelected(XJMenu.this);
168         }
169
170         public void menuDeselected(MenuEvent JavaDoc e) {
171         }
172
173         public void menuCanceled(MenuEvent JavaDoc e) {
174         }
175     }
176 }
177
Popular Tags