KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > adwt > service > HistoryServiceProvider


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.adwt.service;
8
9 import java.beans.beancontext.BeanContextServices JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Vector JavaDoc;
13
14 import javax.swing.Action JavaDoc;
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 /**
25  * Description of the Class
26  *
27  * @author Laurent Etiemble
28  * @version $Revision: 1.5 $
29  * @todo Javadoc to complete
30  */

31 public class HistoryServiceProvider extends CustomBeanContextServiceProvider implements HistoryService
32 {
33    /** Description of the Field */
34    protected HistoryService.Holder holder = null;
35    /** Description of the Field */
36    protected int maxHistory;
37    /** Description of the Field */
38    protected Vector JavaDoc menus = null;
39    /** Description of the Field */
40    protected HistoryService service = null;
41    /** Description of the Field */
42    protected KeyLimitedStack stack = null;
43    /** Description of the Field */
44    private static Logger logger = Logger.getLogger(HistoryServiceProvider.class);
45    /** Description of the Field */
46    protected final static int MAX_WIDTH = 30;
47
48
49    /**
50     * Constructor for the HistoryServiceProvider object
51     *
52     * @param maxHistory Description of Parameter
53     * @param holder Description of Parameter
54     */

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 JavaDoc(maxHistory);
62    }
63
64
65    /**
66     * Gets the currentServiceSelectors attribute of the HistoryServiceProvider object
67     *
68     * @param bcs Description of Parameter
69     * @param serviceClass Description of Parameter
70     * @return The currentServiceSelectors value
71     */

72    public Iterator JavaDoc getCurrentServiceSelectors(BeanContextServices JavaDoc bcs, Class JavaDoc serviceClass)
73    {
74       return new Vector JavaDoc().iterator();
75    }
76
77
78    /**
79     * Gets the service attribute of the HistoryServiceProvider object
80     *
81     * @param bcs Description of Parameter
82     * @param requestor Description of Parameter
83     * @param serviceClass Description of Parameter
84     * @param serviceSelector Description of Parameter
85     * @return The service value
86     */

87    public Object JavaDoc getService(BeanContextServices JavaDoc bcs, Object JavaDoc requestor, Class JavaDoc serviceClass, Object JavaDoc serviceSelector)
88    {
89       return service;
90    }
91
92
93    /**
94     * Description of the Method
95     *
96     * @param url Description of Parameter
97     * @param context Description of Parameter
98     */

99    public void push(URL JavaDoc url, Object JavaDoc 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 JavaDoc theUrl = hc.getURL();
107          String JavaDoc 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    /**
126     * Description of the Method
127     *
128     * @param bcs Description of Parameter
129     * @param requestor Description of Parameter
130     * @param service Description of Parameter
131     */

132    public void releaseService(BeanContextServices JavaDoc bcs, Object JavaDoc requestor, Object JavaDoc service) { }
133
134
135    /**
136     * @return The serviceClass value
137     */

138    protected Class JavaDoc[] getServiceClass()
139    {
140       return new Class JavaDoc[]{HistoryService.class};
141    }
142
143
144    /** Description of the Method */
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    /**
175     * Description of the Class
176     *
177     * @author letiembl
178     * @version $Revision: 1.5 $
179     */

180    private class FileHistoryCommand implements Command
181    {
182       /** Description of the Field */
183       private int position;
184
185
186       /**
187        * Constructor for the FileHistoryCommand object
188        *
189        * @param position Description of Parameter
190        */

191       public FileHistoryCommand(int position)
192       {
193          this.position = position;
194       }
195
196
197       /** Description of the Method */
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    /**
208     * Description of the Class
209     *
210     * @author letiembl
211     * @version $Revision: 1.5 $
212     */

213    private class HistoryContext
214    {
215       /** Description of the Field */
216       private Object JavaDoc context;
217       /** Description of the Field */
218       private URL JavaDoc url;
219
220
221       /**
222        * Constructor for the HistoryContext object
223        *
224        * @param url Description of Parameter
225        * @param context Description of Parameter
226        */

227       public HistoryContext(URL JavaDoc url, Object JavaDoc context)
228       {
229          this.url = url;
230          this.context = context;
231       }
232
233
234       /**
235        * Description of the Method
236        *
237        * @param o Description of Parameter
238        * @return Description of the Returned Value
239        */

240       public boolean equals(Object JavaDoc 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       /**
249        * Gets the context attribute of the HistoryContext object
250        *
251        * @return The context value
252        */

253       public Object JavaDoc getContext()
254       {
255          return this.context;
256       }
257
258
259       /**
260        * Gets the uRL attribute of the HistoryContext object
261        *
262        * @return The uRL value
263        */

264       public URL JavaDoc getURL()
265       {
266          return this.url;
267       }
268    }
269 }
270
271
Popular Tags