1 7 package org.ejtools.adwt.service; 8 9 import java.awt.Container ; 10 import java.beans.beancontext.BeanContextServices ; 11 import java.util.Hashtable ; 12 import java.util.Iterator ; 13 import java.util.Vector ; 14 15 import javax.swing.Action ; 16 import javax.swing.Icon ; 17 import javax.swing.JButton ; 18 import javax.swing.JToolBar ; 19 20 import org.apache.log4j.Logger; 21 import org.ejtools.adwt.action.CommandAction; 22 import org.ejtools.beans.beancontext.CustomBeanContextServiceProvider; 23 24 31 public class ToolBarServiceProvider extends CustomBeanContextServiceProvider implements ToolBarService 32 { 33 34 private Hashtable bars = new Hashtable (); 35 36 private boolean bigIcons = false; 37 38 private ToolBarService service = null; 39 40 private JToolBar toolBar = new JToolBar (); 41 42 private static Logger logger = Logger.getLogger(ToolBarServiceProvider.class); 43 44 45 46 public ToolBarServiceProvider() 47 { 48 this.service = this; 49 } 50 51 52 57 public Container getContainer() 58 { 59 return this.toolBar; 60 } 61 62 63 70 public Iterator getCurrentServiceSelectors(BeanContextServices bcs, Class serviceClass) 71 { 72 return new Vector ().iterator(); 73 } 74 75 76 85 public Object getService(BeanContextServices bcs, Object requestor, Class serviceClass, Object serviceSelector) 86 { 87 return service; 88 } 89 90 91 96 public void register(CommandAction action) 97 { 98 String menu = action.getMenu(); 99 JToolBarWrapper bar = (JToolBarWrapper) this.bars.get(menu); 100 101 if (bar == null) 102 { 103 bar = new JToolBarWrapper(); 104 bar.setFloatable(false); 105 106 this.bars.put(menu, bar); 107 108 if (this.toolBar.getComponentCount() > 0) 109 { 110 this.toolBar.addSeparator(); 111 } 112 113 this.toolBar.add(bar); 114 } 115 116 JButton button = bar.add(action); 117 118 Object o = null; 119 if (this.bigIcons) 120 { 121 o = action.getValue(CommandAction.ICON); 122 } 123 else 124 { 125 o = action.getValue(Action.SMALL_ICON); 126 } 127 if (o != null) 128 { 129 button.setIcon((Icon ) o); 130 } 131 if (action.getValue(CommandAction.TOOLTIP) != null) 132 { 133 button.setToolTipText((String ) action.getValue(CommandAction.TOOLTIP)); 134 } 135 136 toolBar.validate(); 137 logger.debug("Button registered"); 138 } 139 140 141 148 public void releaseService(BeanContextServices bcs, Object requestor, Object service) { } 149 150 151 156 public void setBigIcons(boolean bigIcons) 157 { 158 if (this.bars.size() > 0) 159 { 160 throw new IllegalStateException ("Cannot change icon size if buttons are registered"); 161 } 162 this.bigIcons = bigIcons; 163 } 164 165 166 171 public void unregister(CommandAction action) 172 { 173 String menu = action.getMenu(); 174 JToolBarWrapper bar = (JToolBarWrapper) this.bars.get(menu); 175 176 if (bar == null) 177 { 178 return; 179 } 180 181 bar.remove(action); 182 toolBar.validate(); 183 logger.debug("Button unregistered"); 184 } 185 186 187 190 protected Class [] getServiceClass() 191 { 192 return new Class []{ToolBarService.class}; 193 } 194 195 196 202 protected class JToolBarWrapper extends JToolBar 203 { 204 205 protected Hashtable buttons = new Hashtable (); 206 207 208 209 public JToolBarWrapper() 210 { 211 super(); 212 this.setBorder(null); 213 } 214 215 216 221 public JToolBarWrapper(int orientation) 222 { 223 super(orientation); 224 } 225 226 227 232 public JToolBarWrapper(String name) 233 { 234 super(name); 235 } 236 237 238 244 public JToolBarWrapper(String name, int orientation) 245 { 246 super(name, orientation); 247 } 248 249 250 256 public JButton add(Action action) 257 { 258 JButton button = super.add(action); 259 this.buttons.put(action, button); 260 return button; 261 } 262 263 264 269 public void remove(Action action) 270 { 271 JButton button = (JButton ) this.buttons.get(action); 272 this.remove(button); 273 } 274 } 275 } 276 277 | Popular Tags |