NavigationBarManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  *  2004 Modified by Romain Beaugrand
00027  * 
00028  */
00029 package org.openmobileis.services.navigation;
00030 
00031 import java.util.Enumeration;
00032 import java.util.Hashtable;
00033 
00034 import javax.servlet.http.HttpServletRequest;
00035 
00036 import org.openmobileis.common.context.ApplicationContextManager;
00037 import org.openmobileis.common.util.collection.Array;
00038 import org.openmobileis.services.Service;
00039 
00040 public class NavigationBarManager {
00041 
00042   private static NavigationBarManager manager;
00043 
00044   protected static NavigationBarDescription description;
00045 
00046   protected Array serviceList;
00047 
00048   protected int serviceCounter = 0;
00049 
00050   protected boolean displayFormExitMessage = false;
00051 
00055   protected NavigationBarManager() {
00056     super();
00057     serviceList = new Array();
00058   }
00059   
00060   public static void registerNavigationBarManager(NavigationBarManager new_manager) {
00061     manager = new_manager;
00062   }
00063 
00064   public static NavigationBarManager getManager() {
00065     if (manager == null) {
00066       synchronized (NavigationBarManager.class) {
00067         if (manager == null) {
00068           manager = new NavigationBarManager();
00069           ApplicationContextManager.getManager().addManager(manager);
00070         }
00071       }
00072     }
00073     return manager;
00074   }
00075 
00076   public static void initializeNavigationBar(NavigationBarDescription desc) {
00077     description = desc;
00078   }
00079 
00080   public void setCurrentService(NavigationBarService service, HttpServletRequest req) {
00081 /*    NavigationBarServiceData data = new NavigationBarServiceData(service, req.getPathTranslated(),
00082         (Hashtable) ((WebServerConnection) req).getParametersTable().clone());*/
00083 /*    NavigationBarServiceData data = new NavigationBarServiceData(service, req.getRequestURI(),
00084             new Hashtable(req.getParameterMap()));*/
00085         NavigationBarServiceData data = new NavigationBarServiceData(service, ((Service)service).getServiceUri(),
00086                 new Hashtable(req.getParameterMap()));
00087     //validate that the service is not already registered.
00088     Object serv = null;
00089 
00090     if (!service.displayRecursive()) {
00091       for (int i = 0; i < serviceCounter; i++) {
00092         serv = serviceList.get(i);
00093         if (serv.equals(data)) {
00094           serviceCounter = i;
00095         }
00096       }
00097     }
00098 
00099     /*
00100      * If getNavigationBarLastServiceURI() is not null the fonction find
00101      * the equal between the uri done and the serviceList to place the 
00102      * specified service uri
00103      *      */
00104 
00105     NavigationBarServiceData serv2 = null;
00106     
00107     String servUri = service.getNavigationBarLastServiceURI(); 
00108     if (servUri != null) {
00109       if (servUri.equals(NavigationBarService.NAVBAR_ROOT)) {
00110         serviceCounter = 0;
00111       } else {
00112                 for (int i = 0; i < serviceCounter; i++) {
00113                   serv2 = (NavigationBarServiceData)serviceList.get(i);
00114                   String vartemp = serv2.getUri();
00115         
00116                   if(servUri.equals(vartemp)) {
00117                         serviceCounter = i+1;
00118                   }
00119                 }
00120       }
00121     }
00122 
00123     //*************************************************************//
00124     this.displayFormExitMessage = service.displayFormExitMessage();
00125 
00126     data.setNavigationBarLabel(service.getNavigationBarLabel(req));
00127     serviceList.add(serviceCounter++, data);
00128   }
00129         
00130         public void updateRequestParametersForRedirectService(String serviceUri, HttpServletRequest req)        {
00131                 if (serviceList.size() == 0) return;
00132                 for (int i=serviceList.size() -1; i>=0; i--)    {
00133                         NavigationBarServiceData data = (NavigationBarServiceData)serviceList.get(i);
00134                         if (data.getUri().equals(serviceUri))   {
00135                                 Hashtable oldParameterList = data.getParameters();
00136                                 //Hashtable newTable = ((WebServerConnection)req).getParametersTable();
00137                                 Hashtable newTable = new Hashtable(req.getParameterMap());
00138                                 Enumeration enumera = newTable.keys();
00139                                 while (enumera.hasMoreElements())       {
00140                                         Object key = enumera.nextElement();
00141                                         if ((!key.equals("to")) && (!key.equals("from")))       {  //remove specific redirect service parameters
00142                                                 Object value = newTable.get(key);
00143                                                 oldParameterList.put(key, value);       
00144                                         }       
00145                                 }
00146                                 break;
00147                         }
00148                 }
00149         }
00150 
00151   public NavigationBarServiceData getCurrentServiceData() {
00152     if (serviceCounter == 0)
00153       return null; //no service
00154     NavigationBarServiceData data = (NavigationBarServiceData) serviceList.get(serviceCounter - 1);
00155     if (data != null) {
00156       return data;
00157     }
00158     return null;
00159   }
00160 
00161   public void popService() {
00162     serviceCounter--;
00163     serviceList.remove(serviceCounter);
00164   }
00165 
00166   public void popToService(int nb) {
00167     serviceCounter = nb + 1;
00168   }
00169 
00170   public void resetServiceTable() {
00171     if (serviceCounter == 0)
00172       return;
00173     serviceList.add(0, serviceList.get(serviceCounter - 1));
00174     serviceCounter = 1;
00175   }
00176 
00177   public String getNavigationBar() {
00178     if (description == null)
00179       return "";
00180     if (serviceCounter == 0)
00181       return "";
00182     StringBuffer str = new StringBuffer();
00183     NavigationBarServiceData data = null;
00184     for (int i = 0; i < serviceCounter - 1; i++) {
00185       data = (NavigationBarServiceData) serviceList.get(i);
00186       str.append(description.getPastLinkFontTag());
00187       if (this.displayFormExitMessage) {
00188         str.append("<a href=\"#\" onClick=\"displayNavFormMessageExit('/services/admin/managereturn?servnb=" + i + "')\">");
00189       } else {
00190         str.append("<a href=\"/services/admin/managereturn?servnb=" + i + "\">");
00191       }
00192       str.append(data.getNavigationBarLabel());
00193       str.append("</a>");
00194                         str.append("</font>");
00195       str.append(description.getLinkSeparator());
00196     }
00197     str.append(description.getCurrentLinkFontTag());
00198     data = (NavigationBarServiceData) serviceList.get(serviceCounter - 1);
00199     str.append(data.getNavigationBarLabel());
00200                 str.append("</font>");
00201     return str.toString();
00202   }
00203 
00204 }

Generated on Mon Dec 4 11:03:28 2006 for OpenMobileIS by  doxygen 1.5.1-p1