Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.69 KB | None | 0 0
  1. package com.google.gwt.chargingstationfinder.client;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.util.ArrayList;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7.  
  8. import javax.script.ScriptEngine;
  9. import javax.script.ScriptEngineManager;
  10. import javax.script.ScriptException;
  11.  
  12. import com.google.gwt.chargingstationfinder.server.Station;
  13. import com.google.gwt.core.client.Callback;
  14. import com.google.gwt.core.client.EntryPoint;
  15. import com.google.gwt.core.client.GWT;
  16. import com.google.gwt.dom.client.Document;
  17. import com.google.gwt.dom.client.Style.Unit;
  18. import com.google.gwt.event.dom.client.ClickEvent;
  19. import com.google.gwt.geolocation.client.Geolocation;
  20. import com.google.gwt.geolocation.client.Position;
  21. import com.google.gwt.geolocation.client.PositionError;
  22. import com.google.gwt.user.client.Window;
  23. import com.google.gwt.user.client.rpc.AsyncCallback;
  24. import com.google.gwt.user.client.ui.Anchor;
  25. import com.google.gwt.user.client.ui.Button;
  26. import com.google.gwt.user.client.ui.DockLayoutPanel;
  27. import com.google.gwt.user.client.ui.FlexTable;
  28. import com.google.gwt.user.client.ui.FormPanel;
  29. import com.google.gwt.user.client.ui.HorizontalPanel;
  30. import com.google.gwt.user.client.ui.RootLayoutPanel;
  31. import com.google.gwt.user.client.ui.RootPanel;
  32. import com.google.gwt.user.client.ui.TextBox;
  33. import com.google.gwt.user.client.ui.VerticalPanel;
  34. import com.google.maps.gwt.client.GoogleMap;
  35. import com.google.maps.gwt.client.InfoWindow;
  36. import com.google.maps.gwt.client.InfoWindowOptions;
  37. import com.google.maps.gwt.client.LatLng;
  38. import com.google.maps.gwt.client.MapOptions;
  39. import com.google.maps.gwt.client.MapTypeId;
  40. import com.google.maps.gwt.client.KmlLayer;
  41. import com.google.maps.gwt.client.Marker;
  42. import com.google.maps.gwt.client.Marker.ClickHandler;
  43. import com.google.maps.gwt.client.MarkerOptions;
  44. import com.google.maps.gwt.client.MouseEvent;
  45. import com.google.gwt.user.client.ui.Label;
  46.  
  47. /**
  48. * Entry point classes define <code>onModuleLoad()</code>.
  49. */
  50. public class ChargingStationFinderApp implements EntryPoint {
  51.  
  52. private LatLng userPosition;
  53. private String[][] stations;
  54. private GoogleMap gMap;
  55. private FormPanel formPanel;
  56. //private Logger logger = Logger.getLogger(ChargingStationFinderApp.class.getName());
  57. private static final String SERVER_ERROR = "An error occurred while "
  58. + "attempting to contact the server. Please check your network "
  59. + "connection and try again.";
  60. private final StationServiceAsync stationService = GWT.create(StationService.class);
  61. private VerticalPanel mainPanel = new VerticalPanel();
  62. private FlexTable addressFlexTable = new FlexTable();
  63. private HorizontalPanel addPanel = new HorizontalPanel();
  64. private TextBox newSymbolTextBox = new TextBox();
  65. private Button addAddressButton = new Button("Add");
  66. private Button addStationsButton = new Button("AddStations");
  67. private Label lastUpdatedLabel = new Label();
  68. private Anchor signInLink = new Anchor("Sign In");
  69. private Anchor signOutLink = new Anchor("Sign Out");
  70. private LoginInfo loginInfo = null;
  71. private Label loginLabel = new Label(
  72. "Please sign in to your Google Account to access the StationFinder application.");
  73. private VerticalPanel loginPanel = new VerticalPanel();
  74. private CSVParser parser = new CSVParser(this);
  75. private final Button refreshBtn = Button.wrap(Document.get().getElementById("refreshBtn"));
  76.  
  77. /**
  78. * Create a remote service proxy to talk to the server-side Greeting service.
  79. */
  80.  
  81. /**
  82. * This is the entry point method.
  83. */
  84. public void onModuleLoad() {
  85.  
  86. // Check login status using login service.
  87. System.out.println("HI");
  88. LoginServiceAsync loginService = GWT.create(LoginService.class);
  89. loginService.login(GWT.getHostPageBaseURL(), new AsyncCallback<LoginInfo>() {
  90. public void onFailure(Throwable error) {
  91. handleError(error);
  92. }
  93.  
  94. public void onSuccess(LoginInfo result) {
  95. loginInfo = result;
  96. if(loginInfo.isLoggedIn()) {
  97. loadStationFinderApp();
  98. } else {
  99. loadLogin();
  100. }
  101. }
  102. });
  103. }
  104.  
  105. protected void loadLogin() {
  106. // Assemble login panel.
  107. signInLink.setHref(loginInfo.getLoginUrl());
  108. loginPanel.add(loginLabel);
  109. loginPanel.add(signInLink);
  110. RootPanel.get("stationList").add(loginPanel);
  111. }
  112.  
  113. private void loadStationFinderApp() {
  114.  
  115. initializeStations();
  116. // Set up sign out hyperlink.
  117. signOutLink.setHref(loginInfo.getLogoutUrl());
  118. RootPanel.get("signout").add(signOutLink);
  119. //mainPanel.add(signOutLink);
  120.  
  121. addressFlexTable.setText(0, 0, "Address");
  122.  
  123. // Assemble Add Address panel.
  124. addPanel.add(newSymbolTextBox);
  125. addPanel.add(addAddressButton);
  126.  
  127. initializeAddStationsButton();
  128.  
  129. // Assemble Main panel.
  130. mainPanel.add(addressFlexTable);
  131. mainPanel.add(addPanel);
  132. mainPanel.add(lastUpdatedLabel);
  133.  
  134. // Associate the Main panel with the HTML host page.
  135. RootPanel.get("Address").add(mainPanel);
  136.  
  137. // Move cursor focus to the input box.
  138. newSymbolTextBox.setFocus(true);
  139.  
  140. loadMap();
  141. }
  142.  
  143. private void initializeAddStationsButton() {
  144. try {
  145. stationService.checkIsAdmin(new AsyncCallback<Void>(){
  146.  
  147. @Override
  148. public void onFailure(Throwable caught) {
  149. caught.getMessage();
  150.  
  151. }
  152.  
  153. @Override
  154. public void onSuccess(Void result) {
  155. mainPanel.add(addStationsButton);
  156. //Unfortunately GWT has two classes ClickHandler that do very different things
  157. addStationsButton.addClickHandler(new com.google.gwt.event.dom.client.ClickHandler() {
  158.  
  159. @Override
  160. public void onClick(ClickEvent event) {
  161. parser.run(stations);
  162. }
  163. });}
  164. });
  165. } catch (NotAdminException e) {
  166. //do nothing, user is not an admin
  167. }
  168. }
  169.  
  170. private void initializeStations() {
  171. stationService.getStations(new AsyncCallback<String[][]>(){
  172.  
  173. @Override
  174. public void onFailure(Throwable caught) {
  175. return;
  176.  
  177. }
  178.  
  179. @Override
  180. public void onSuccess(String[][] result) {
  181. if (result != null) {
  182. stations = result;
  183. displayStations();
  184. }
  185.  
  186. }});
  187. }
  188.  
  189. private void loadMap() {
  190.  
  191.  
  192.  
  193. formPanel = new FormPanel();
  194. Geolocation geoLocation = Geolocation.getIfSupported();
  195.  
  196. userPosition = LatLng.create(49.259909, -123.162542);
  197.  
  198. geoLocation.getCurrentPosition(new Callback<Position,PositionError>() {
  199.  
  200. public void onFailure(PositionError reason) {
  201. reason.getMessage();
  202. }
  203.  
  204.  
  205. public void onSuccess(Position result) {
  206. userPosition = LatLng.create(result.getCoordinates().getLatitude(),
  207. result.getCoordinates().getLongitude());
  208.  
  209. }});
  210.  
  211. displayMap(formPanel);
  212. }
  213.  
  214. protected void addStations(String[][] stations) {
  215. this.stations = stations;
  216. }
  217.  
  218. protected void addStation(final String[] station) {
  219. stationService.addStation(Double.parseDouble(station[0]),
  220. Double.parseDouble(station[1]),station[2], station[3],
  221. new AsyncCallback<Void>() {
  222.  
  223. public void onFailure(Throwable caught) {
  224. handleError(caught);
  225. }
  226. public void onSuccess(Void result) {
  227. displayStation(station);
  228. }
  229. });
  230. }
  231.  
  232. private void displayStations() {
  233. int i =0;
  234. for (String[] s: stations) {
  235. displayStation(s);
  236. i++;
  237. }
  238. }
  239.  
  240. private void displayStation(String[] s) {
  241. LatLng position = LatLng.create(Double.parseDouble(s[0]), Double.parseDouble(s[1]));
  242.  
  243. InfoWindowOptions windowOptions = InfoWindowOptions.create();
  244. windowOptions.setContent(s[2] + "\r\n," + s[3]);
  245. final InfoWindow iw = InfoWindow.create(windowOptions);
  246.  
  247. MarkerOptions markerOptions = MarkerOptions.create();
  248. markerOptions.setPosition(position);
  249. final Marker m = Marker.create(markerOptions);
  250.  
  251. m.setMap(gMap);
  252. m.addClickListener(new ClickHandler(){
  253.  
  254. @Override
  255. public void handle(MouseEvent event) {
  256. iw.open(gMap, m);
  257.  
  258. }});
  259. }
  260.  
  261. private void displayMap(FormPanel formPanel) {
  262. formPanel.setWidth("800px");
  263. formPanel.setHeight("950px");
  264.  
  265. RootPanel.get().add(formPanel);
  266. MapOptions options = MapOptions.create();
  267.  
  268. options.setZoom(6);
  269. options.setMapTypeId(MapTypeId.ROADMAP);
  270. options.setDraggable(true);
  271. options.setMapTypeControl(true);
  272. options.setScaleControl(true);
  273. options.setScrollwheel(true);
  274. gMap = GoogleMap.create(RootPanel.get().getElement(), options);
  275. }
  276.  
  277. private void handleError(Throwable error) {
  278. Window.alert(error.getMessage());
  279. if (error instanceof NotLoggedInException) {
  280. Window.Location.replace(loginInfo.getLogoutUrl());
  281. }
  282. }
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement