1 7 package org.ejtools.adwt.service; 8 9 import java.awt.Container ; 10 import java.beans.beancontext.BeanContextServices ; 11 import java.util.Iterator ; 12 import java.util.ResourceBundle ; 13 import java.util.Vector ; 14 15 import javax.swing.JMenu ; 16 import javax.swing.JMenuBar ; 17 import javax.swing.JMenuItem ; 18 19 import org.ejtools.adwt.AwtToolkit; 20 import org.ejtools.adwt.action.CommandAction; 21 import org.ejtools.beans.beancontext.CustomBeanContextServiceProvider; 22 23 30 public class MenuBarServiceProvider extends CustomBeanContextServiceProvider implements MenuBarService 31 { 32 33 protected Vector listeners = new Vector (); 34 35 protected JMenuBar menuBar = new JMenuBar (); 36 37 protected Vector requestors = new Vector (); 38 39 protected MenuBarService service = null; 40 41 42 43 public MenuBarServiceProvider() 44 { 45 this.service = this; 46 } 47 48 49 54 public void addMenuBarListener(MenuBarService.Listener mbsl) 55 { 56 this.listeners.add(mbsl); 57 } 58 59 60 65 public Container getContainer() 66 { 67 return this.menuBar; 68 } 69 70 71 78 public Iterator getCurrentServiceSelectors(BeanContextServices bcs, Class serviceClass) 79 { 80 return new Vector ().iterator(); 81 } 82 83 84 93 public Object getService(BeanContextServices bcs, Object requestor, Class serviceClass, Object serviceSelector) 94 { 95 if (!requestors.contains(requestor)) 97 { 98 requestors.add(requestor); 99 } 100 return service; 101 } 102 103 104 109 public void register(CommandAction action) 110 { 111 String menuName = action.getMenu(); 112 ResourceBundle res = action.getResourceBundle(); 113 JMenu parent = findMenu(menuName); 114 115 if (parent == null) 116 { 117 parent = addMenu(res, menuName); 118 } 119 120 addMenuItem(action, parent); 121 } 122 123 124 131 public void releaseService(BeanContextServices bcs, Object requestor, Object service) { } 132 133 134 139 public void removeMenuBarListener(MenuBarService.Listener mbsl) 140 { 141 this.listeners.remove(mbsl); 142 } 143 144 145 150 public void unregister(CommandAction action) 151 { 152 String menuName = action.getMenu(); 153 JMenu parent = findMenu(menuName); 154 155 if (parent == null) 156 { 157 return; 158 } 159 160 removeMenuItem(action, parent); 161 } 162 163 164 171 protected JMenu addMenu(ResourceBundle res, String menuName) 172 { 173 JMenu menu = new JMenu (res.getString(menuName)); 174 menu.setFont(AwtToolkit.FONT); 175 menu.setName(menuName); 176 try 177 { 178 menu.setMnemonic(Integer.parseInt(res.getString(menuName + ".mnemonic"))); 179 } 180 catch (Exception e) 181 { 182 } 183 this.menuBar.add(menu); 184 185 this.update(); 186 187 return menu; 188 } 189 190 191 197 protected void addMenuItem(CommandAction action, JMenu menu) 198 { 199 JMenuItem item = action.getMenuItem(); 200 201 if (action.getMenuLayout() == MenuBarService.SEPARATOR_BEFORE) 202 { 203 if (menu.getItemCount() > 0) 204 { 205 menu.addSeparator(); 206 } 207 } 208 209 JMenuItem addedItem = menu.add(item); 210 addedItem.setFont(AwtToolkit.FONT); 211 212 if (action.getValue(CommandAction.TOOLTIP) != null) 213 { 214 addedItem.setToolTipText((String ) action.getValue(CommandAction.TOOLTIP)); 215 } 216 217 if (action.getMenuLayout() == MenuBarService.SEPARATOR_AFTER) 218 { 219 menu.addSeparator(); 220 } 221 } 222 223 224 230 protected JMenu findMenu(String menuName) 231 { 232 for (int i = 0; i < menuBar.getMenuCount(); i++) 233 { 234 JMenu menu = menuBar.getMenu(i); 235 if (menu.getName().equals(menuName)) 236 { 237 return menu; 238 } 239 } 240 241 return null; 242 } 243 244 245 248 protected Class [] getServiceClass() 249 { 250 return new Class []{MenuBarService.class}; 251 } 252 253 254 260 protected void removeMenuItem(CommandAction action, JMenu menu) 261 { 262 JMenuItem item = action.getMenuItem(); 263 264 menu.remove(item); 265 } 266 267 268 269 protected void update() 270 { 271 for (int i = 0; i < this.listeners.size(); i++) 272 { 273 MenuBarService.Listener o = (MenuBarService.Listener) this.listeners.elementAt(i); 274 o.update(this.menuBar); 275 } 276 } 277 } 278 279 | Popular Tags |