Skip to content

Commit

Permalink
Merge 7e77fb4 into 78f416b
Browse files Browse the repository at this point in the history
  • Loading branch information
dukesun99 committed Mar 10, 2019
2 parents 78f416b + 7e77fb4 commit 7e00338
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 17 deletions.
105 changes: 105 additions & 0 deletions docs/displaymap/index-Gmap.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<!DOCTYPE html>
<html>

<head>
<title>Equipment Display</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */

#map {
height: 100%;
}

/* Optional: Makes the sample page fill the window. */

html,
body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>

<body>
<div id="map"></div>
<script>
/**
This function parse the url parameters and return the points as an array of objects.
Params: Url(String): The url to be parsed.
Returns: The parameters object.
*/
function parseUrlParams(url) {
// get query string from url
var queryString = url.split('?')[1];
var points = [];
var tempObject = {};
var keyList = [];
if (queryString) {
// stuff after # is not part of query string, so get rid of it
queryString = queryString.split('#')[0];
// split our query string into its component parts
var arr = queryString.split('&');
for (var i = 0; i < arr.length; i++) {
// separate the keys and the values
var a = arr[i].split('=');
// set parameter name and value (use 'null' if empty)
var paramName = a[0];
var paramValue = typeof (a[1]) === 'undefined' ? null : a[1];
if (paramValue == null) {
console.error("Bad parameters. Parameter value should not be empty. Parsing failed.");
}
console.log(paramValue);
obj = JSON.parse(paramValue);
tempObject[paramName] = obj;
keyList.push(paramName);
}
if (!("coordinates" in tempObject)) {
console.error("Bad parameters. Must have coordinates. Parsing failed.");
return [];
} else {
console.log(tempObject);
console.log(keyList);
for (var i = 0; i < tempObject["coordinates"].length; i++) {
var tempPoint = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": tempObject["coordinates"][i]
},
"properties": {}
};
for (var j = 0; j < keyList.length; j++) {
if (keyList[j] != "coordinates") {
tempPoint["properties"][keyList[j]] = tempObject[keyList[j]][i];;
}
}
points.push(tempPoint);
}
}
}
return points;
}
var map;
function initMap() {
var points = parseUrlParams(decodeURI(window.location.href));
map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 1.291610266, lng: 103.8497472 },
zoom: 12
});
var markers = []
for (var i = 0; i < points.length; i++) {
var marker = { lat: points[i]["geometry"]["coordinates"][1], lng: points[i]["geometry"]["coordinates"][0] };
markers.push(new google.maps.Marker({ position: marker, map: map }));
}
}

</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDhN4_NQ94uS9Z0Ix6AfRgeXZN1rVahjBE&callback=initMap" async
defer></script>
</body>

</html>
40 changes: 29 additions & 11 deletions src/main/java/seedu/address/ui/BrowserPanel.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package seedu.address.ui;

import static java.util.Objects.requireNonNull;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;

Expand All @@ -19,7 +18,6 @@
import javafx.fxml.FXML;
import javafx.scene.layout.Region;
import javafx.scene.web.WebView;
import seedu.address.MainApp;
import seedu.address.commons.core.LogsCenter;
import seedu.address.model.equipment.Equipment;

Expand All @@ -28,9 +26,8 @@
*/
public class BrowserPanel extends UiPart<Region> {

public static final URL DEFAULT_PAGE =
requireNonNull(MainApp.class.getResource(FXML_FILE_FOLDER + "default.html"));
public static final String SEARCH_PAGE_URL = "https://se-education.org/dummy-search-page/?name=";
public static final String MAP_PAGE_BASE_URL = "https://cs2103-ay1819s2-w10-3.github.io/main/DisplayGmap";
public static final URL DEFAULT_PAGE = processDefaultPage(MAP_PAGE_BASE_URL);
private static final String FXML = "BrowserPanel.fxml";

private final Logger logger = LogsCenter.getLogger(getClass());
Expand All @@ -52,7 +49,6 @@ public BrowserPanel(ObservableValue<Equipment> selectedPerson) {
}
loadEquipmentPage(newValue);
});

loadDefaultPage();
}

Expand All @@ -65,22 +61,44 @@ private void loadEquipmentPage(Equipment equipment) {
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("AIzaSyBQ5YiOpupDO8JnZqmqYTujAwP9U4R5JBA")
.build();
String url = MAP_PAGE_BASE_URL;
try {
GeocodingResult[] results = GeocodingApi.geocode(context,
equipment.getAddress().toString()).await();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
//System.out.println(gson.toJson(results[0].geometry.location));

if (results.length > 0) {
System.out.println();
url = MAP_PAGE_BASE_URL + "?coordinates=[[" + results[0].geometry.location.lng + ","
+ results[0].geometry.location.lat + "]]&title=[\"" + equipment.getName()
+ "\"]&icon=[\"monument\"]";
}
} catch (ApiException e) {
System.err.println(e.getMessage());
} catch (InterruptedException e) {
System.err.println(e.getMessage());
} catch (IOException e) {
System.err.println(e.getMessage());
} finally {

System.out.println("Loading page: " + url);
loadPage(url);
}
loadPage(SEARCH_PAGE_URL + equipment.getName().serialNumber);
}

/**
* Process the default url String.
* @param urlString the default url string
* @return the URL object
*/
private static URL processDefaultPage(String urlString) {
try {
URL url = new URL(MAP_PAGE_BASE_URL);
return url;
} catch (MalformedURLException mue) {
System.err.println("Fatal error: Default url cannot be formatted.");
return null;
}
}
public void loadPage(String url) {
Platform.runLater(() -> browser.getEngine().load(url));
}
Expand All @@ -89,7 +107,7 @@ public void loadPage(String url) {
* Loads a default HTML file with a background that matches the general theme.
*/
private void loadDefaultPage() {
loadPage(DEFAULT_PAGE.toExternalForm());
loadPage(MAP_PAGE_BASE_URL);
}

}
39 changes: 35 additions & 4 deletions src/test/java/seedu/address/ui/BrowserPanelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
import static org.junit.Assert.assertEquals;
import static seedu.address.testutil.TypicalEquipments.ALICE;

import java.io.IOException;
import java.net.URL;

import org.junit.Before;
import org.junit.Test;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.maps.GeoApiContext;
import com.google.maps.GeocodingApi;
import com.google.maps.errors.ApiException;
import com.google.maps.model.GeocodingResult;

import guitests.guihandles.BrowserPanelHandle;
import javafx.beans.property.SimpleObjectProperty;
import seedu.address.model.equipment.Equipment;
Expand All @@ -33,10 +41,33 @@ public void display() throws Exception {

// associated web page of a equipment
guiRobot.interact(() -> selectedPerson.set(ALICE));
URL expectedPersonUrl = new URL(BrowserPanel.SEARCH_PAGE_URL + ALICE.getName().serialNumber
.replaceAll(" ", "%20"));

GeoApiContext context = new GeoApiContext.Builder()
.apiKey("AIzaSyBQ5YiOpupDO8JnZqmqYTujAwP9U4R5JBA")
.build();
String expectedUrlString = BrowserPanel.MAP_PAGE_BASE_URL;
URL expectedUrl;
try {
GeocodingResult[] results = GeocodingApi.geocode(context,
ALICE.getAddress().toString()).await();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
if (results.length > 0) {
System.out.println();
expectedUrlString = BrowserPanel.MAP_PAGE_BASE_URL + "?coordinates=[["
+ results[0].geometry.location.lng + ","
+ results[0].geometry.location.lat + "]]&title=[\""
+ ALICE.getName()
+ "\"]&icon=[\"monument\"]";
}
} catch (ApiException e) {
System.err.println(e.getMessage());
} catch (InterruptedException e) {
System.err.println(e.getMessage());
} catch (IOException e) {
System.err.println(e.getMessage());
} finally {
expectedUrl = new URL(expectedUrlString.replace("\"", "%22").replace(" ", "%20"));
}
waitUntilBrowserLoaded(browserPanelHandle);
assertEquals(expectedPersonUrl, browserPanelHandle.getLoadedUrl());
assertEquals(expectedUrl, browserPanelHandle.getLoadedUrl());
}
}
37 changes: 35 additions & 2 deletions src/test/java/systemtests/EquipmentManagerSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static seedu.address.ui.StatusBarFooter.SYNC_STATUS_UPDATED;
import static seedu.address.ui.testutil.GuiTestAssert.assertListMatching;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
Expand All @@ -21,6 +22,13 @@
import org.junit.BeforeClass;
import org.junit.ClassRule;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.maps.GeoApiContext;
import com.google.maps.GeocodingApi;
import com.google.maps.errors.ApiException;
import com.google.maps.model.GeocodingResult;

import guitests.guihandles.BrowserPanelHandle;
import guitests.guihandles.CommandBoxHandle;
import guitests.guihandles.EquipmentListPanelHandle;
Expand Down Expand Up @@ -208,13 +216,38 @@ protected void assertSelectedCardDeselected() {
*/
protected void assertSelectedCardChanged(Index expectedSelectedCardIndex) {
getPersonListPanel().navigateToCard(getPersonListPanel().getSelectedCardIndex());
String selectedCardName = getPersonListPanel().getHandleToSelectedCard().getName();
URL expectedUrl;
try {
expectedUrl = new URL(BrowserPanel.SEARCH_PAGE_URL + selectedCardName.replaceAll(" ", "%20"));
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("AIzaSyBQ5YiOpupDO8JnZqmqYTujAwP9U4R5JBA")
.build();
String expectedUrlString = BrowserPanel.MAP_PAGE_BASE_URL;
try {
GeocodingResult[] results = GeocodingApi.geocode(context,
getPersonListPanel().getHandleToSelectedCard().getAddress().toString()).await();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
if (results.length > 0) {
System.out.println();
expectedUrlString = BrowserPanel.MAP_PAGE_BASE_URL + "?coordinates=[["
+ results[0].geometry.location.lng + ","
+ results[0].geometry.location.lat + "]]&title=[\""
+ getPersonListPanel().getHandleToSelectedCard().getName()
+ "\"]&icon=[\"monument\"]";
}
} catch (ApiException e) {
System.err.println(e.getMessage());
} catch (InterruptedException e) {
System.err.println(e.getMessage());
} catch (IOException e) {
System.err.println(e.getMessage());
} finally {
expectedUrl = new URL(expectedUrlString.replace("\"", "%22").replace(" ", "%20"));
}
} catch (MalformedURLException mue) {
System.out.println("SHOULD NOT BE ERROR AH");
throw new AssertionError("URL expected to be valid.", mue);
}
waitUntilBrowserLoaded(getBrowserPanel());
assertEquals(expectedUrl, getBrowserPanel().getLoadedUrl());

assertEquals(expectedSelectedCardIndex.getZeroBased(), getPersonListPanel().getSelectedCardIndex());
Expand Down

0 comments on commit 7e00338

Please sign in to comment.