Skip to content

Commit

Permalink
For #100 - moved geo-fences list to the it's controller
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalidze committed Mar 13, 2015
1 parent b14e80f commit d0bf40d
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 40 deletions.
23 changes: 18 additions & 5 deletions src/main/java/org/traccar/web/client/Application.java
Expand Up @@ -30,16 +30,13 @@
import org.traccar.web.client.view.ApplicationView;
import org.traccar.web.client.view.FilterDialog;
import org.traccar.web.client.view.UserSettingsDialog;
import org.traccar.web.shared.model.Device;
import org.traccar.web.shared.model.Position;
import org.traccar.web.shared.model.*;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.RootPanel;
import com.sencha.gxt.data.shared.event.StoreAddEvent;
import com.sencha.gxt.data.shared.event.StoreHandlers;
import com.sencha.gxt.data.shared.event.StoreRemoveEvent;
import org.traccar.web.shared.model.User;
import org.traccar.web.shared.model.UserSettings;

public class Application {

Expand Down Expand Up @@ -68,7 +65,8 @@ public Application() {
settingsController = new SettingsController(userSettingsHandler);
mapController = new MapController(mapHandler);
geoFenceController = new GeoFenceController(mapController);
deviceController = new DeviceController(mapController, geoFenceController, settingsController, this);
geoFenceController.getGeoFenceStore().addStoreHandlers(geoFenceStoreHandler);
deviceController = new DeviceController(mapController, geoFenceController, settingsController, geoFenceController.getGeoFenceStore(), this);
deviceController.getDeviceStore().addStoreHandlers(deviceStoreHandler);
archiveController = new ArchiveController(archiveHandler, userSettingsHandler, deviceController.getDeviceStore());
archiveController.getPositionStore().addStoreHandlers(archiveStoreHandler);
Expand All @@ -83,6 +81,7 @@ public void run() {
deviceController.run();
mapController.run();
archiveController.run();
geoFenceController.run();
}

private MapController.MapHandler mapHandler = new MapController.MapHandler() {
Expand Down Expand Up @@ -140,6 +139,20 @@ public void onAnything() {

};

private StoreHandlers<GeoFence> geoFenceStoreHandler = new BaseStoreHandlers<GeoFence>() {
@Override
public void onAdd(StoreAddEvent<GeoFence> event) {
for (GeoFence geoFence : event.getItems()) {
mapController.drawGeoFence(geoFence);
}
}

@Override
public void onRemove(StoreRemoveEvent<GeoFence> event) {
mapController.removeGeoFence(event.getItem());
}
};

private class UserSettingsHandlerImpl implements UserSettingsDialog.UserSettingsHandler, FilterDialog.FilterSettingsHandler {
@Override
public void onSave(UserSettings userSettings) {
Expand Down
Expand Up @@ -29,7 +29,7 @@
import org.traccar.web.client.view.DeviceShareDialog;
import org.traccar.web.client.view.DeviceView;
import org.traccar.web.client.view.PositionInfoPopup;
import org.traccar.web.shared.model.Device;
import org.traccar.web.shared.model.*;

import com.google.gwt.core.client.GWT;
import com.sencha.gxt.data.shared.ListStore;
Expand All @@ -38,9 +38,6 @@
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.box.ConfirmMessageBox;
import com.sencha.gxt.widget.core.client.event.DialogHideEvent;
import org.traccar.web.shared.model.Position;
import org.traccar.web.shared.model.User;
import org.traccar.web.shared.model.ValidationException;

public class DeviceController implements ContentController, DeviceView.DeviceHandler {
private final MapController mapController;
Expand All @@ -55,7 +52,11 @@ public class DeviceController implements ContentController, DeviceView.DeviceHan

private final PositionInfoPopup positionInfo = new PositionInfoPopup();

public DeviceController(MapController mapController, DeviceView.GeoFenceHandler geoFenceHandler, DeviceView.SettingsHandler settingsHandler, Application application) {
public DeviceController(MapController mapController,
DeviceView.GeoFenceHandler geoFenceHandler,
DeviceView.SettingsHandler settingsHandler,
ListStore<GeoFence> geoFenceStore,
Application application) {
this.application = application;
this.mapController = mapController;
DeviceProperties deviceProperties = GWT.create(DeviceProperties.class);
Expand All @@ -82,7 +83,7 @@ public void onRecordChange(StoreRecordChangeEvent<Device> event) {
}
}
});
deviceView = new DeviceView(this, geoFenceHandler, settingsHandler, deviceStore);
deviceView = new DeviceView(this, geoFenceHandler, settingsHandler, deviceStore, geoFenceStore);
}

public ListStore<Device> getDeviceStore() {
Expand Down
Expand Up @@ -15,15 +15,29 @@
*/
package org.traccar.web.client.controller;

import com.google.gwt.core.client.GWT;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.ContentPanel;
import org.traccar.web.client.Application;
import org.traccar.web.client.i18n.Messages;
import org.traccar.web.client.model.BaseAsyncCallback;
import org.traccar.web.client.model.GeoFenceProperties;
import org.traccar.web.client.view.DeviceView;
import org.traccar.web.client.view.GeoFenceWindow;
import org.traccar.web.shared.model.GeoFence;

public class GeoFenceController implements DeviceView.GeoFenceHandler {
import java.util.List;

public class GeoFenceController implements ContentController, DeviceView.GeoFenceHandler {
private final MapController mapController;
private final ListStore<GeoFence> geoFenceStore;

private Messages i18n = GWT.create(Messages.class);

public GeoFenceController(MapController mapController) {
this.mapController = mapController;
GeoFenceProperties geoFenceProperties = GWT.create(GeoFenceProperties.class);
this.geoFenceStore = new ListStore<GeoFence>(geoFenceProperties.id());
}

@Override
Expand All @@ -35,4 +49,22 @@ public void onSave(GeoFence device) {
}
}).show();
}

@Override
public ContentPanel getView() {
return null;
}

public void run() {
Application.getDataService().getGeoFences(new BaseAsyncCallback<List<GeoFence>>(i18n) {
@Override
public void onSuccess(List<GeoFence> result) {
geoFenceStore.addAll(result);
}
});
}

public ListStore<GeoFence> getGeoFenceStore() {
return geoFenceStore;
}
}
Expand Up @@ -93,7 +93,6 @@ public void onSuccess(List<Position> positions) {
update();
}
});
drawGeoFences();
}

private Map<Long, Position> latestPositionMap = new HashMap<Long, Position>();
Expand Down Expand Up @@ -168,13 +167,12 @@ public void onFailure(Throwable caught) {
});
}

public void drawGeoFences() {
Application.getDataService().getGeoFences(new BaseAsyncCallback<List<GeoFence>>(i18n) {
@Override
public void onSuccess(List<GeoFence> result) {
mapView.showGeoFences(result);
}
});
public void drawGeoFence(GeoFence geoFence) {
mapView.drawGeoFence(geoFence);
}

public void removeGeoFence(GeoFence geoFence) {
mapView.removeGeoFence(geoFence);
}

public void selectDevice(Device device) {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/traccar/web/client/view/DeviceView.java
Expand Up @@ -159,11 +159,16 @@ public ContentPanel getView() {
@UiField(provided = true)
Messages i18n = GWT.create(Messages.class);

public DeviceView(final DeviceHandler deviceHandler, final GeoFenceHandler geoFenceHandler, SettingsHandler settingsHandler, final ListStore<Device> deviceStore) {
public DeviceView(final DeviceHandler deviceHandler,
GeoFenceHandler geoFenceHandler,
SettingsHandler settingsHandler,
final ListStore<Device> deviceStore,
final ListStore<GeoFence> geoFenceStore) {
this.deviceHandler = deviceHandler;
this.geoFenceHandler = geoFenceHandler;
this.settingsHandler = settingsHandler;
this.deviceStore = deviceStore;
this.geoFenceStore = geoFenceStore;

DeviceProperties deviceProperties = GWT.create(DeviceProperties.class);

Expand Down Expand Up @@ -213,7 +218,6 @@ public void onBrowserEvent(Context context, Element parent, String value, Native
// geo-fences
GeoFenceProperties geoFenceProperties = GWT.create(GeoFenceProperties.class);

geoFenceStore = new ListStore<GeoFence>(geoFenceProperties.id());
geoFenceList = new ListView<GeoFence, String>(geoFenceStore, geoFenceProperties.name());
geoFenceList.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

Expand Down
45 changes: 29 additions & 16 deletions src/main/java/org/traccar/web/client/view/GeoFenceRenderer.java
Expand Up @@ -21,12 +21,14 @@
import org.gwtopenmaps.openlayers.client.layer.Vector;
import org.gwtopenmaps.openlayers.client.util.JSObject;
import org.traccar.web.shared.model.GeoFence;
import org.traccar.web.shared.model.Position;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GeoFenceRenderer {
private final MapView mapView;
private final Map<Long, VectorFeature> drawings = new HashMap<Long, VectorFeature>();

public GeoFenceRenderer(MapView mapView) {
this.mapView = mapView;
Expand All @@ -36,22 +38,24 @@ protected Vector getVectorLayer() {
return mapView.getGeofenceLayer();
}

public void showGeoFences(List<GeoFence> geoFences) {
for (GeoFence geoFence : geoFences) {
switch (geoFence.getType()) {
case CIRCLE:
drawCircle(geoFence);
break;
case POLYGON:
drawPolygon(geoFence);
break;
case LINE:
drawLine(geoFence);
break;
}
public void drawGeoFence(GeoFence geoFence) {
switch (geoFence.getType()) {
case CIRCLE:
drawCircle(geoFence);
break;
case POLYGON:
drawPolygon(geoFence);
break;
case LINE:
drawLine(geoFence);
break;
}
}

public void removeGeoFence(GeoFence geoFence) {
// TODO
}

private void drawCircle(GeoFence circle) {
GeoFence.LonLat center = circle.points().get(0);
Polygon circleShape = Polygon.createRegularPolygon(mapView.createPoint(center.lon, center.lat), circle.getRadius(), 40, 0f);
Expand All @@ -63,8 +67,10 @@ private void drawCircle(GeoFence circle) {
st.setStrokeColor('#' + circle.getColor());
st.setFillColor('#' + circle.getColor());

getVectorLayer().addFeature(new VectorFeature(circleShape, st));
VectorFeature drawing = new VectorFeature(circleShape, st);
getVectorLayer().addFeature(drawing);
drawName(circle.getName(), mapView.createPoint(center.lon, center.lat));
drawings.put(circle.getId(), drawing);
}

private void drawPolygon(GeoFence polygon) {
Expand All @@ -83,9 +89,11 @@ private void drawPolygon(GeoFence polygon) {
st.setStrokeColor('#' + polygon.getColor());
st.setFillColor('#' + polygon.getColor());

getVectorLayer().addFeature(new VectorFeature(polygonShape, st));
VectorFeature drawing = new VectorFeature(polygonShape, st);
getVectorLayer().addFeature(drawing);
Point center = getCollectionCentroid(polygonShape);
drawName(polygon.getName(), center);
drawings.put(polygon.getId(), drawing);
}

private void drawLine(GeoFence line) {
Expand All @@ -104,6 +112,7 @@ private void drawLine(GeoFence line) {

getVectorLayer().addFeature(lineFeature);
drawName(line.getName(), getCollectionCentroid(lineString));
drawings.put(line.getId(), lineFeature);
}

private void drawName(String name, Point point) {
Expand All @@ -126,4 +135,8 @@ private static Point getCollectionCentroid(Collection collection) {
public static native JSObject getCollectionCentroid(JSObject collection) /*-{
return collection.getCentroid(false);
}-*/;

public VectorFeature getDrawing(GeoFence geoFence) {
return drawings.get(geoFence.getId());
}
}
8 changes: 6 additions & 2 deletions src/main/java/org/traccar/web/client/view/MapView.java
Expand Up @@ -312,8 +312,12 @@ public void updateIcon(Device device) {
latestPositionRenderer.updateIcon(device);
}

public void showGeoFences(List<GeoFence> geoFences) {
geoFenceRenderer.showGeoFences(geoFences);
public void drawGeoFence(GeoFence geoFence) {
geoFenceRenderer.drawGeoFence(geoFence);
}

public void removeGeoFence(GeoFence geoFence) {
geoFenceRenderer.removeGeoFence(geoFence);
}

/**
Expand Down

0 comments on commit d0bf40d

Please sign in to comment.