| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.student.common.ui.client.mvc.history; | 
  | 17 |  |   | 
  | 18 |  |  import java.util.HashMap; | 
  | 19 |  |  import java.util.Iterator; | 
  | 20 |  |  import java.util.Map; | 
  | 21 |  |   | 
  | 22 |  |  import org.kuali.student.common.ui.client.application.ViewContext; | 
  | 23 |  |  import org.kuali.student.common.ui.client.mvc.Controller; | 
  | 24 |  |  import org.kuali.student.common.ui.client.mvc.breadcrumb.BreadcrumbManager; | 
  | 25 |  |   | 
  | 26 |  |  import com.google.gwt.event.logical.shared.ValueChangeEvent; | 
  | 27 |  |  import com.google.gwt.event.logical.shared.ValueChangeHandler; | 
  | 28 |  |  import com.google.gwt.user.client.DOM; | 
  | 29 |  |  import com.google.gwt.user.client.Element; | 
  | 30 |  |  import com.google.gwt.user.client.History; | 
  | 31 |  |  import com.google.gwt.user.client.Timer; | 
  | 32 |  |  import com.google.gwt.user.client.Window; | 
  | 33 |  |  import com.google.gwt.user.client.ui.Hidden; | 
  | 34 |  |   | 
  | 35 |  |   | 
  | 36 |  |   | 
  | 37 |  |   | 
  | 38 |  |   | 
  | 39 |  |   | 
  | 40 |  |   | 
  | 41 |  |   | 
  | 42 |  |   | 
  | 43 |  |   | 
  | 44 |  |   | 
  | 45 |  |   | 
  | 46 |  |   | 
  | 47 |  |   | 
  | 48 |  |   | 
  | 49 |  |   | 
  | 50 |  |   | 
  | 51 |  |   | 
  | 52 | 0 |  public class HistoryManager { | 
  | 53 | 0 |      private static final NavigationEventMonitor monitor = new NavigationEventMonitor(); | 
  | 54 | 0 |          public static String VIEW_ATR = "view"; | 
  | 55 |  |      private static Controller root; | 
  | 56 | 0 |      private static boolean logNavigationHistory = true; | 
  | 57 |  |      private static Locations locations; | 
  | 58 |  |       | 
  | 59 |  |       | 
  | 60 |  |   | 
  | 61 |  |   | 
  | 62 |  |   | 
  | 63 |  |   | 
  | 64 |  |   | 
  | 65 |  |   | 
  | 66 |  |      public static void bind(Controller controller, Locations views) { | 
  | 67 | 0 |              locations = views; | 
  | 68 | 0 |          root = controller; | 
  | 69 | 0 |          root.addApplicationEventHandler(NavigationEvent.TYPE, monitor); | 
  | 70 | 0 |          History.addValueChangeHandler(new ValueChangeHandler<String>() { | 
  | 71 |  |   | 
  | 72 |  |              @Override | 
  | 73 |  |              public void onValueChange(ValueChangeEvent<String> event) { | 
  | 74 | 0 |                  String token = event.getValue(); | 
  | 75 | 0 |                  if (token != null) { | 
  | 76 | 0 |                      root.onHistoryEvent(token); | 
  | 77 |  |                  } | 
  | 78 | 0 |              } | 
  | 79 |  |               | 
  | 80 |  |          }); | 
  | 81 | 0 |      } | 
  | 82 |  |       | 
  | 83 |  |      public static String[] splitHistoryStack(String historyStack){ | 
  | 84 | 0 |              return historyStack.split("/"); | 
  | 85 |  |      } | 
  | 86 |  |       | 
  | 87 |  |       | 
  | 88 |  |   | 
  | 89 |  |   | 
  | 90 |  |   | 
  | 91 |  |   | 
  | 92 |  |   | 
  | 93 |  |   | 
  | 94 |  |      public static Map<String, String> getTokenMap(String token){ | 
  | 95 | 0 |              Map<String, String> pairs = new HashMap<String, String>(); | 
  | 96 | 0 |              String[] arr = token.split("&"); | 
  | 97 | 0 |              for (String s : arr) { | 
  | 98 | 0 |                      if(s.contains("=")){ | 
  | 99 | 0 |                              String[] tmp = s.split("="); | 
  | 100 | 0 |                              if(tmp.length == 2){ | 
  | 101 | 0 |                                      pairs.put(tmp[0], tmp[1]); | 
  | 102 |  |                              } | 
  | 103 | 0 |                      } | 
  | 104 |  |                      else{ | 
  | 105 |  |                               | 
  | 106 |  |                               | 
  | 107 | 0 |                              pairs.put("view", s); | 
  | 108 |  |                      } | 
  | 109 |  |              } | 
  | 110 | 0 |              return pairs; | 
  | 111 |  |      } | 
  | 112 |  |       | 
  | 113 |  |       | 
  | 114 |  |   | 
  | 115 |  |   | 
  | 116 |  |   | 
  | 117 |  |   | 
  | 118 |  |   | 
  | 119 |  |      public static String nextHistoryStack(String historyStack){ | 
  | 120 | 0 |              String[] arr= historyStack.split("/", 2); | 
  | 121 | 0 |              if(arr.length == 2){ | 
  | 122 | 0 |                      return arr[1]; | 
  | 123 |  |              } | 
  | 124 |  |              else{ | 
  | 125 | 0 |                      return ""; | 
  | 126 |  |              } | 
  | 127 |  |      } | 
  | 128 |  |       | 
  | 129 |  |       | 
  | 130 |  |   | 
  | 131 |  |   | 
  | 132 |  |   | 
  | 133 |  |   | 
  | 134 |  |   | 
  | 135 |  |      public static void processWindowLocation(){ | 
  | 136 | 0 |              boolean navigateSuccess = false; | 
  | 137 | 0 |              Element loc = DOM.getElementById("locationHash"); | 
  | 138 |  |               | 
  | 139 | 0 |              String value = loc.getAttribute("value"); | 
  | 140 | 0 |              if(value != null && !value.equals("") && value.startsWith("/")){ | 
  | 141 | 0 |                      navigate(value); | 
  | 142 |  |              } | 
  | 143 | 0 |              else if(Window.Location.getQueryString() != null &&  | 
  | 144 |  |                              !Window.Location.getQueryString().isEmpty()){ | 
  | 145 | 0 |                      String view = Window.Location.getParameter(VIEW_ATR); | 
  | 146 | 0 |                      String docId = Window.Location.getParameter(ViewContext.ID_ATR); | 
  | 147 | 0 |                      String idType = Window.Location.getParameter(ViewContext.ID_TYPE_ATR); | 
  | 148 | 0 |                      if(view != null && docId != null && idType != null){ | 
  | 149 | 0 |                              String path = locations.getLocation(view); | 
  | 150 | 0 |                              if(path != null){ | 
  | 151 | 0 |                                      ViewContext context = new ViewContext(); | 
  | 152 | 0 |                                      context.setIdType(idType); | 
  | 153 | 0 |                                      context.setId(docId); | 
  | 154 | 0 |                                      navigate(path, context); | 
  | 155 | 0 |                                      navigateSuccess = true; | 
  | 156 |  |                              } | 
  | 157 | 0 |                      }else if(view != null && docId != null){ | 
  | 158 | 0 |                              String path = locations.getLocation(view); | 
  | 159 | 0 |                              if(path != null){ | 
  | 160 | 0 |                                      ViewContext context = new ViewContext(); | 
  | 161 | 0 |                                      context.setId(docId); | 
  | 162 | 0 |                                      navigate(path, context); | 
  | 163 | 0 |                                      navigateSuccess = true; | 
  | 164 |  |                              } | 
  | 165 | 0 |                      } | 
  | 166 | 0 |                      else if(view != null){ | 
  | 167 | 0 |                              String path = locations.getLocation(view); | 
  | 168 | 0 |                              navigate(path); | 
  | 169 | 0 |                                  navigateSuccess = true; | 
  | 170 |  |                      } | 
  | 171 |  |              } | 
  | 172 | 0 |              if(!navigateSuccess){ | 
  | 173 | 0 |                      navigate(Window.Location.getHash().trim()); | 
  | 174 |  |              } | 
  | 175 | 0 |      } | 
  | 176 |  |       | 
  | 177 |  |       | 
  | 178 |  |   | 
  | 179 |  |   | 
  | 180 |  |   | 
  | 181 |  |   | 
  | 182 |  |      public static void navigate(String path){ | 
  | 183 | 0 |              if(path != null && !path.isEmpty() && path.startsWith("/")){ | 
  | 184 | 0 |                      logNavigationHistory = false; | 
  | 185 | 0 |                      root.onHistoryEvent(path); | 
  | 186 | 0 |                      logHistoryChange(); | 
  | 187 | 0 |                      logNavigationHistory = true; | 
  | 188 |  |              } | 
  | 189 | 0 |      } | 
  | 190 |  |       | 
  | 191 |  |       | 
  | 192 |  |   | 
  | 193 |  |   | 
  | 194 |  |   | 
  | 195 |  |   | 
  | 196 |  |      public static String collectHistoryStack() { | 
  | 197 | 0 |          String result = root.collectHistory(""); | 
  | 198 | 0 |                  if(result == null){ | 
  | 199 | 0 |                          result = ""; | 
  | 200 |  |                  } | 
  | 201 | 0 |          return result; | 
  | 202 |  |      } | 
  | 203 |  |       | 
  | 204 |  |       | 
  | 205 |  |   | 
  | 206 |  |   | 
  | 207 |  |   | 
  | 208 |  |      public static void logHistoryChange() { | 
  | 209 | 0 |          String historyStack = collectHistoryStack(); | 
  | 210 | 0 |          if(historyStack.endsWith("/")){ | 
  | 211 | 0 |                  historyStack = historyStack.substring(0, historyStack.length()-1); | 
  | 212 |  |          } | 
  | 213 | 0 |          String currentToken = History.getToken(); | 
  | 214 | 0 |          if(!currentToken.equals(historyStack)){ | 
  | 215 | 0 |                  History.newItem(historyStack, false); | 
  | 216 |  |          } | 
  | 217 | 0 |          BreadcrumbManager.updateLinks(historyStack); | 
  | 218 | 0 |      } | 
  | 219 |  |       | 
  | 220 | 0 |      private static class NavigationEventMonitor implements NavigationEventHandler{ | 
  | 221 |  |          private static final int EVENT_DELAY = 100; | 
  | 222 | 0 |          private long lastEvent = -1; | 
  | 223 |  |           | 
  | 224 | 0 |          private final Timer timer = new Timer() { | 
  | 225 |  |   | 
  | 226 |  |              @Override | 
  | 227 |  |              public void run() { | 
  | 228 | 0 |                  if (lastEvent < (System.currentTimeMillis()-EVENT_DELAY)) { | 
  | 229 | 0 |                      this.cancel(); | 
  | 230 | 0 |                      lastEvent = -1; | 
  | 231 | 0 |                      logHistoryChange(); | 
  | 232 |  |                  } | 
  | 233 | 0 |              } | 
  | 234 |  |               | 
  | 235 |  |          }; | 
  | 236 |  |           | 
  | 237 |  |          @Override | 
  | 238 |  |          public void onNavigationEvent(NavigationEvent event) { | 
  | 239 | 0 |                  if(logNavigationHistory){ | 
  | 240 | 0 |                          logHistoryChange(); | 
  | 241 |  |                  } | 
  | 242 |  |                  else{ | 
  | 243 | 0 |                          String historyStack = collectHistoryStack(); | 
  | 244 | 0 |                          BreadcrumbManager.updateLinks(historyStack); | 
  | 245 | 0 |                          HistoryManager.setLogNavigationHistory(true); | 
  | 246 |  |                  } | 
  | 247 | 0 |          } | 
  | 248 |  |      } | 
  | 249 |  |       | 
  | 250 |  |       | 
  | 251 |  |   | 
  | 252 |  |   | 
  | 253 |  |   | 
  | 254 |  |   | 
  | 255 |  |   | 
  | 256 |  |      public static void setLogNavigationHistory(boolean log){ | 
  | 257 | 0 |              logNavigationHistory = log; | 
  | 258 | 0 |      } | 
  | 259 |  |   | 
  | 260 |  |           | 
  | 261 |  |   | 
  | 262 |  |   | 
  | 263 |  |   | 
  | 264 |  |   | 
  | 265 |  |   | 
  | 266 |  |   | 
  | 267 |  |          public static void navigate(String path, ViewContext context) { | 
  | 268 | 0 |                  path = appendContext(path, context); | 
  | 269 | 0 |                  navigate(path); | 
  | 270 |  |                   | 
  | 271 | 0 |          } | 
  | 272 |  |   | 
  | 273 |  |           | 
  | 274 |  |   | 
  | 275 |  |   | 
  | 276 |  |   | 
  | 277 |  |   | 
  | 278 |  |   | 
  | 279 |  |          public static String appendContext(String path, ViewContext context) { | 
  | 280 | 0 |                  if(context.getId() != null && !context.getId().isEmpty()){ | 
  | 281 | 0 |                          path = path + "&" + ViewContext.ID_ATR + "=" + context.getId(); | 
  | 282 |  |                  } | 
  | 283 | 0 |                  if(context.getIdType() != null){ | 
  | 284 | 0 |                          path = path + "&" + ViewContext.ID_TYPE_ATR + "=" + context.getIdType(); | 
  | 285 |  |                  } | 
  | 286 | 0 |                  if(!context.getAttributes().isEmpty()){ | 
  | 287 | 0 |                          Map<String,String> attributes = context.getAttributes(); | 
  | 288 | 0 |                          Iterator<String> it = attributes.keySet().iterator(); | 
  | 289 | 0 |                          while(it.hasNext()){ | 
  | 290 | 0 |                                  String key = it.next(); | 
  | 291 | 0 |                                  String value = attributes.get(key); | 
  | 292 | 0 |                                  path = path + "&" + key + "=" + value; | 
  | 293 | 0 |                          } | 
  | 294 |  |                  } | 
  | 295 |  |                   | 
  | 296 | 0 |                  return path; | 
  | 297 |  |          } | 
  | 298 |  |  } |