D:\NewZebra\JavaApplications\DesignPatternsProject\src\behavior_patterns\command_pattern_nested_menu\NestedMenu_CoreCommandsPerformer.java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package behavior_patterns.command_pattern_nested_menu;
21
22
23
24 @author
25
26
27
28 import java.text.*;
29 import java.util.*;
30 import java.awt.event.*;
31 import java.awt.*;
32 import javax.swing.*;
33
34
35 public class NestedMenu_CoreCommandsPerformer extends JFrame implements CommandsPerformer {
36
37 CommandPanel aCommandPanel = null;
38 JMenuBar mainJMenuBar;
39 Vector menuVector = null;
40
41
42
43 public NestedMenu_CoreCommandsPerformer() {
44
45 setSize(800, 600);
46 setTitle(" Menu Command Pattern JFrame");
47 setLayout(null);
48
49 instantiateCommandPanel();
50 queuingCommands();
51 runCommands();
52
53 setVisible(true);
54 }
55
56
57
58 public void instantiateCommandPanel(){
59 aCommandPanel = new CommandPanel();
60 aCommandPanel.setBounds(0,0,800,600);
61
62 aCommandPanel.setBackground(Color.lightGray);
63 aCommandPanel.setLayout(null);
64 add(aCommandPanel);
65 }
66
67
68
69 public void queuingCommands(){
70 int menuIndex = 0;
71 int itemIndex = 0;
72 CommandObject commandObjectHandle = null;
73
74 menuVector = new Vector();
75 for(menuIndex = 0; menuIndex < aCommandPanel.mainMenuListingArray.length; menuIndex++)
76 {
77 Vector menuItemsVector = new Vector();
78 int localMenuIndex = (menuIndex + 1) * 100;
79 switch(localMenuIndex)
80 {
81 case CommandPanel.MENU1_INDEX:
82 for(itemIndex = 0; itemIndex < aCommandPanel.menuOneListingArray.length; itemIndex++)
83 {
84 if(aCommandPanel.menuOneListingArray[itemIndex][aCommandPanel.NESTING_OPTION_INDEX].equalsIgnoreCase(aCommandPanel.SKIP_OPTION))
85 continue;
86 commandObjectHandle = new CommandObject();
87 commandObjectHandle.commandName = aCommandPanel.menuOneListingArray[itemIndex][aCommandPanel.NAME_INDEX];
88 commandObjectHandle.commandOption = aCommandPanel.menuOneListingArray[itemIndex][aCommandPanel.NESTING_OPTION_INDEX];
89 int localItemIndex = localMenuIndex + (itemIndex + 1) * 10;
90 switch(localItemIndex)
91 {
92 case CommandPanel.MENU1_ITEM1_INDEX:
93 if(aCommandPanel.menuOneListingArray[itemIndex][aCommandPanel.NESTING_OPTION_INDEX].equalsIgnoreCase(aCommandPanel.NESTED_OPTION))
94 {
95 commandObjectHandle.itemCommand = new DummyObject();
96 addNestMenuItem(localItemIndex, commandObjectHandle);
97 }
98 else
99 commandObjectHandle.itemCommand = new One_Item_1();
100 break;
101 case CommandPanel.MENU1_ITEM2_INDEX:
102 commandObjectHandle.itemCommand = new One_Item_2();
103 break;
104 case CommandPanel.MENU1_ITEM3_INDEX:
105 default:
106 commandObjectHandle.itemCommand = new One_Item_3();
107 }
108 menuItemsVector.add(commandObjectHandle);
109 }
110 break;
111 case CommandPanel.MENU2_INDEX:
112 for(itemIndex = 0; itemIndex < aCommandPanel.menuTwoListingArray.length; itemIndex++)
113 {
114 if(aCommandPanel.menuTwoListingArray[itemIndex][aCommandPanel.NESTING_OPTION_INDEX].equalsIgnoreCase(aCommandPanel.SKIP_OPTION))
115 continue;
116 commandObjectHandle = new CommandObject();
117 commandObjectHandle.commandName = aCommandPanel.menuTwoListingArray[itemIndex][aCommandPanel.NAME_INDEX];
118 commandObjectHandle.commandOption = aCommandPanel.menuTwoListingArray[itemIndex][aCommandPanel.NESTING_OPTION_INDEX];
119 int localItemIndex = localMenuIndex + (itemIndex + 1) * 10;
120 switch(localItemIndex)
121 {
122 case CommandPanel.MENU2_ITEM1_INDEX:
123 if(aCommandPanel.menuTwoListingArray[itemIndex][aCommandPanel.NESTING_OPTION_INDEX].equalsIgnoreCase(aCommandPanel.NESTED_OPTION))
124 {
125 commandObjectHandle.itemCommand = new DummyObject();
126 addNestMenuItem(localItemIndex, commandObjectHandle);
127 }
128 else
129 commandObjectHandle.itemCommand = new Two_Item_1();
130 break;
131 case CommandPanel.MENU2_ITEM2_INDEX:
132 commandObjectHandle.itemCommand = new Two_Item_2();
133 break;
134 case CommandPanel.MENU2_ITEM3_INDEX:
135 default:
136 commandObjectHandle.itemCommand = new Two_Item_3();
137 }
138 menuItemsVector.add(commandObjectHandle);
139 }
140 break;
141 case CommandPanel.MENU3_INDEX:
142 default:
143 for(itemIndex = 0; itemIndex < aCommandPanel.menuThreeListingArray.length; itemIndex++)
144 {
145 if(aCommandPanel.menuThreeListingArray[itemIndex][aCommandPanel.NESTING_OPTION_INDEX].equalsIgnoreCase(aCommandPanel.SKIP_OPTION))
146 continue;
147 commandObjectHandle = new CommandObject();
148 commandObjectHandle.commandName = aCommandPanel.menuThreeListingArray[itemIndex][aCommandPanel.NAME_INDEX];
149 commandObjectHandle.commandOption = aCommandPanel.menuThreeListingArray[itemIndex][aCommandPanel.NESTING_OPTION_INDEX];
150 int localItemIndex = localMenuIndex + (itemIndex + 1) * 10;
151 switch(localItemIndex)
152 {
153 case CommandPanel.MENU3_ITEM1_INDEX:
154 if(aCommandPanel.menuThreeListingArray[itemIndex][aCommandPanel.NESTING_OPTION_INDEX].equalsIgnoreCase(aCommandPanel.NESTED_OPTION))
155 {
156 commandObjectHandle.itemCommand = new DummyObject();
157 addNestMenuItem(localItemIndex, commandObjectHandle);
158 }
159 else
160 commandObjectHandle.itemCommand = new Three_Item_1();
161 break;
162 case CommandPanel.MENU3_ITEM2_INDEX:
163 commandObjectHandle.itemCommand = new Three_Item_2();
164 break;
165 case CommandPanel.MENU3_ITEM3_INDEX:
166 default:
167 commandObjectHandle.itemCommand = new Three_Item_3();
168 }
169 menuItemsVector.add(commandObjectHandle);
170 }
171 }
172 menuVector.add(menuItemsVector);
173 }
174 }
175
176
177
178 public void runCommands(){
179 addMenuBar();
180 }
181
182
183
184 void addMenuBar()
185 {
186 int count;
187 int numberOfFunctions;
188
189 mainJMenuBar = new JMenuBar();
190 for(count = 0; count < aCommandPanel.mainMenuListingArray.length; count++)
191 {
192 JMenu aJMenu = new JMenu(aCommandPanel.mainMenuListingArray[count]);
193 addJMenuItem(aJMenu, count);
194 mainJMenuBar.add(aJMenu);
195 }
196 this.setJMenuBar(mainJMenuBar);
197 }
198
199
200
201 void addJMenuItem(JMenu passedMenu, int menuNumber)
202 {
203 String [] itemArray;
204 int count;
205
206 Vector menuItemsVector = (Vector)menuVector.elementAt(menuNumber);
207 for(int index = 0; index < menuItemsVector.size(); index++)
208 {
209 CommandObject tempCommandObject = (CommandObject) menuItemsVector.elementAt(index);
210 if(tempCommandObject.nestedCommandVector != null)
211 {
212 JMenu aJMenu = new JMenu(tempCommandObject.commandName);
213 for(int nestedIndex = 0; nestedIndex < tempCommandObject.nestedCommandVector.size(); nestedIndex++)
214 {
215 CommandObject nestedCommandObject = (CommandObject) tempCommandObject.nestedCommandVector.elementAt(nestedIndex);
216 JMenuItem item = new JMenuItem(nestedCommandObject.commandName);
217 final Command finalCommand = nestedCommandObject.itemCommand;
218
219 item.addActionListener(new ActionListener()
220 {
221 public void actionPerformed(ActionEvent PassedActionEvent)
222 {
223 finalCommand.execute(aCommandPanel);
224 }
225 });
226 if(nestedCommandObject.commandOption.equalsIgnoreCase(aCommandPanel.DISABLE_OPTION))
227 item.setEnabled(false);
228 aJMenu.add(item);
229 }
230 passedMenu.add(aJMenu);
231 }
232 else
233 {
234 JMenuItem item = new JMenuItem(tempCommandObject.commandName);
235 final Command finalCommand = tempCommandObject.itemCommand;
236
237 item.addActionListener(new ActionListener()
238 {
239 public void actionPerformed(ActionEvent PassedActionEvent)
240 {
241 finalCommand.execute(aCommandPanel);
242 }
243 });
244 if(tempCommandObject.commandOption.equalsIgnoreCase(aCommandPanel.DISABLE_OPTION))
245 item.setEnabled(false);
246 passedMenu.add(item);
247 }
248 }
249 }
250
251
252
253 public void addNestMenuItem(int passedItemIndex, CommandObject passedCommandObjectHandle){
254
255 passedCommandObjectHandle.nestedCommandVector = new Vector();
256 int localSubItemIndex;
257
258 switch(passedItemIndex)
259 {
260 case CommandPanel.MENU1_ITEM1_INDEX:
261 case CommandPanel.MENU2_ITEM1_INDEX:
262 case CommandPanel.MENU3_ITEM1_INDEX:
263 default:
264 for(localSubItemIndex = 0; localSubItemIndex < aCommandPanel.menuTwoSubItem_1_ListingArray.length; localSubItemIndex++)
265 {
266 if(aCommandPanel.menuTwoSubItem_1_ListingArray[localSubItemIndex][aCommandPanel.NESTING_OPTION_INDEX].equalsIgnoreCase(aCommandPanel.SKIP_OPTION))
267 continue;
268 CommandObject commandObjectHandle = new CommandObject();
269 commandObjectHandle.commandName = aCommandPanel.menuTwoSubItem_1_ListingArray[localSubItemIndex][aCommandPanel.NAME_INDEX];
270 commandObjectHandle.commandOption = aCommandPanel.menuTwoSubItem_1_ListingArray[localSubItemIndex][aCommandPanel.NESTING_OPTION_INDEX];
271 int localNestedItemIndex = passedItemIndex + localSubItemIndex + 1;
272 switch(localNestedItemIndex)
273 {
274 case CommandPanel.MENU2_ITEM1_SUB1__INDEX:
275 commandObjectHandle.itemCommand = new Two_Item_1_1();
276 break;
277 case CommandPanel.MENU2_ITEM1_SUB2__INDEX:
278 commandObjectHandle.itemCommand = new Two_Item_1_2();
279 break;
280 case CommandPanel.MENU2_ITEM1_SUB3__INDEX:
281 default:
282 commandObjectHandle.itemCommand = new Two_Item_1_3();
283 }
284 passedCommandObjectHandle.nestedCommandVector.add(commandObjectHandle);
285 }
286 }
287 }
288
289
290
291
292 public static void main (String[] args)
293 {
294 NestedMenu_CoreCommandsPerformer nestedMenu_CoreCommandsPerformerHandle = new NestedMenu_CoreCommandsPerformer();
295 }
296
297 }
298
299