@PageShown
private void setup() {
// if a store was requested, retrieve it here (otherwise, we're editing a new, blank store instance)
if (requestedStoreId != null) {
Store found = em.find(Store.class, requestedStoreId);
if (found == null) {
Window.alert("No such store: " + requestedStoreId);
backToStoresPage.go();
}
storeBinder.setModel(found, InitialState.FROM_MODEL);
}
departmentList.setItems(storeBinder.getModel().getDepartments());
MultiWordSuggestOracle dso = (MultiWordSuggestOracle) addDepartment.getSuggestOracle();
for (Department d : em.createNamedQuery("allDepartments", Department.class).getResultList()) {
dso.add(d.getName());
}
addDepartment.getTextBox().addKeyPressHandler(new KeyPressHandler() {
@Override
public void onKeyPress(KeyPressEvent event) {
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
if (addDepartment.getText().trim().length() == 0)
return;
Department department = Department.resolve(em, addDepartment.getText());
if (!storeBinder.getModel().getDepartments().contains(department)) {
storeBinder.getModel().getDepartments().add(department);
departmentList.setItems(storeBinder.getModel().getDepartments());
}
addDepartment.setText("");
}
}
});
GoogleMapBootstrapper.whenReady(new Runnable() {
@Override
public void run() {
MapOptions defaultMapOptions = new MapOptions();
defaultMapOptions.setNumZoomLevels(16);
final MapWidget mapWidget = new MapWidget("100%", "100%", defaultMapOptions);
mapContainer.add(mapWidget);
final Map map = mapWidget.getMap();
OSM osm = OSM.Mapnik("Mapnik");
osm.setIsBaseLayer(true);
map.addLayer(osm);
vectorLayer = new Vector("Fence");
map.addLayer(vectorLayer);
markers = new Markers("Markers");
map.addLayer(markers);
map.addControl(new OverviewMap());
map.addControl(new ScaleLine());
ModifyFeatureOptions featureOptions = new ModifyFeatureOptions();
featureOptions.setMode(ModifyFeature.RESIZE);
featureOptions.onModificationEnd(new ModifyFeature.OnModificationEndListener() {
@Override
public void onModificationEnd(VectorFeature vectorFeature) {
float diameter = vectorFeature.getGeometry().getBounds().getWidth();
storeBinder.getModel().setRadius(diameter / 2);
}
});
storeBinder.addPropertyChangeHandler("radius", new PropertyChangeHandler<Double>() {
@Override
public void onPropertyChange(PropertyChangeEvent<Double> event) {
reDrawGeoFence(map);
}
});
modifyControl = new ModifyFeature(vectorLayer, featureOptions);
map.addControl(modifyControl);
placeMarkerAtStoreLocation(map);
map.addMapMoveEndListener(new MapMoveEndListener() {
@Override
public void onMapMoveEnd(MapMoveEndEvent eventObject) {
Bounds extent = map.getExtent();
extent.transform(DEFAULT_PROJECTION, new Projection("EPSG:900913"));
// set up autocomplete search box for this place
AutocompleteType[] types = new AutocompleteType[2];
types[0] = AutocompleteType.ESTABLISHMENT;
types[1] = AutocompleteType.GEOCODE;
AutocompleteOptions options = AutocompleteOptions.newInstance();
options.setTypes(types);
LatLng sw = LatLng.newInstance(extent.getLowerLeftX(), extent.getLowerLeftY());
LatLng ne = LatLng.newInstance(extent.getUpperRightX(), extent.getUpperRightY());
options.setBounds(LatLngBounds.newInstance(sw, ne));
final Autocomplete autoComplete = Autocomplete.newInstance(locationSearchBox.getElement(), options);
autoComplete.addPlaceChangeHandler(new PlaceChangeMapHandler() {
@Override
public void onEvent(PlaceChangeMapEvent event) {
PlaceResult result = autoComplete.getPlace();
PlaceGeometry geometry = result.getGeometry();
LatLng center = geometry.getLocation();
Store store = storeBinder.getModel();
store.setName(result.getName());
store.setAddress(result.getFormatted_Address());
store.setLatitude(center.getLatitude());
store.setLongitude(center.getLongitude());
store.setRadius(DEFAULT_RADIUS);
placeMarkerAtStoreLocation(map);
}
});
}