1 7 package org.ejtools.adwt.service; 8 9 import java.beans.beancontext.BeanContextServices ; 10 import java.net.URL ; 11 import java.util.Iterator ; 12 import java.util.Vector ; 13 14 import javax.swing.Action ; 15 16 import org.apache.log4j.Logger; 17 import org.ejtools.adwt.action.Command; 18 import org.ejtools.adwt.action.CommandAction; 19 import org.ejtools.adwt.action.file.FileHistoryAction; 20 import org.ejtools.adwt.action.file.FileHistoryActionTop; 21 import org.ejtools.beans.beancontext.CustomBeanContextServiceProvider; 22 import org.ejtools.util.KeyLimitedStack; 23 24 31 public class HistoryServiceProvider extends CustomBeanContextServiceProvider implements HistoryService 32 { 33 34 protected HistoryService.Holder holder = null; 35 36 protected int maxHistory; 37 38 protected Vector menus = null; 39 40 protected HistoryService service = null; 41 42 protected KeyLimitedStack stack = null; 43 44 private static Logger logger = Logger.getLogger(HistoryServiceProvider.class); 45 46 protected final static int MAX_WIDTH = 30; 47 48 49 55 public HistoryServiceProvider(HistoryService.Holder holder, int maxHistory) 56 { 57 this.service = this; 58 this.holder = holder; 59 this.maxHistory = maxHistory; 60 this.stack = new KeyLimitedStack(maxHistory); 61 this.menus = new Vector (maxHistory); 62 } 63 64 65 72 public Iterator getCurrentServiceSelectors(BeanContextServices bcs, Class serviceClass) 73 { 74 return new Vector ().iterator(); 75 } 76 77 78 87 public Object getService(BeanContextServices bcs, Object requestor, Class serviceClass, Object serviceSelector) 88 { 89 return service; 90 } 91 92 93 99 public void push(URL url, Object context) 100 { 101 this.stack.push(new HistoryContext(url, context)); 102 103 for (int i = 0; i < stack.size(); i++) 104 { 105 HistoryContext hc = (HistoryContext) this.stack.elementAt(i); 106 URL theUrl = hc.getURL(); 107 String s = theUrl.toString(); 108 109 if (s.length() > MAX_WIDTH) 110 { 111 s = "" + (i + 1) + ": " + theUrl.getProtocol() + "..." + s.substring(s.length() - 25); 112 } 113 else 114 { 115 s = "" + (i + 1) + ": " + theUrl.toString(); 116 } 117 118 CommandAction action = (CommandAction) menus.elementAt(i); 119 action.putValue(Action.NAME, s); 120 action.setEnabled(true); 121 } 122 } 123 124 125 132 public void releaseService(BeanContextServices bcs, Object requestor, Object service) { } 133 134 135 138 protected Class [] getServiceClass() 139 { 140 return new Class []{HistoryService.class}; 141 } 142 143 144 145 protected void initializeBeanContextResources() 146 { 147 super.initializeBeanContextResources(); 148 149 CommandAction action; 150 151 for (int i = 0; i < this.maxHistory; i++) 152 { 153 if (i == 0) 154 { 155 action = new FileHistoryActionTop( 156 new FileHistoryCommand(i) 157 ); 158 } 159 else 160 { 161 action = new FileHistoryAction( 162 new FileHistoryCommand(i) 163 ); 164 } 165 action.setEnabled(false); 166 menus.add(action); 167 this.add(action); 168 } 169 170 logger.debug("HistoryService added"); 171 } 172 173 174 180 private class FileHistoryCommand implements Command 181 { 182 183 private int position; 184 185 186 191 public FileHistoryCommand(int position) 192 { 193 this.position = position; 194 } 195 196 197 198 public void execute() 199 { 200 HistoryContext hc = (HistoryContext) HistoryServiceProvider.this.stack.elementAt(position); 201 logger.info("Loading " + hc.getURL()); 202 HistoryServiceProvider.this.holder.loadResource(hc.getURL(), hc.getContext()); 203 } 204 } 205 206 207 213 private class HistoryContext 214 { 215 216 private Object context; 217 218 private URL url; 219 220 221 227 public HistoryContext(URL url, Object context) 228 { 229 this.url = url; 230 this.context = context; 231 } 232 233 234 240 public boolean equals(Object o) 241 { 242 logger.debug("Object to test " + ((HistoryContext) o).getURL().toString()); 243 logger.debug("The URL " + this.url.toString()); 244 return ((HistoryContext) o).getURL().equals(this.url); 245 } 246 247 248 253 public Object getContext() 254 { 255 return this.context; 256 } 257 258 259 264 public URL getURL() 265 { 266 return this.url; 267 } 268 } 269 } 270 271 | Popular Tags |