Skip to content

Commit

Permalink
For #85 - reuse MarkerIcon in device dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalidze committed May 28, 2015
1 parent fe4a247 commit 188ad4a
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 108 deletions.
20 changes: 11 additions & 9 deletions src/main/java/org/traccar/web/client/view/DeviceDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.sencha.gxt.widget.core.client.form.validator.MaxNumberValidator;
import com.sencha.gxt.widget.core.client.form.validator.MinNumberValidator;
import org.traccar.web.client.ApplicationContext;
import org.traccar.web.shared.model.Device;
import org.traccar.web.shared.model.*;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
Expand All @@ -40,9 +40,6 @@
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.TextField;
import org.traccar.web.shared.model.DeviceIconType;
import org.traccar.web.shared.model.Position;
import org.traccar.web.shared.model.PositionIconType;

public class DeviceDialog implements Editor<Device> {

Expand Down Expand Up @@ -89,11 +86,15 @@ public interface DeviceHandler {
@UiField
Image markerImage;

DeviceIconType selectedIcon;
MarkerIcon selectedIcon;

public DeviceDialog(Device device, DeviceHandler deviceHandler) {
this.deviceHandler = deviceHandler;
selectedIcon = device.getIconType();
if (device.getIconType() == null) {
selectedIcon = new MarkerIcon.Database(device.getIcon());
} else {
selectedIcon = new MarkerIcon.BuiltIn(device.getIconType());
}

uiBinder.createAndBindUi(this);

Expand All @@ -120,7 +121,8 @@ public void onSaveClicked(SelectEvent event) {
window.hide();
Device device = driver.flush();
device.setIdleSpeedThreshold(ApplicationContext.getInstance().getUserSettings().getSpeedUnit().toKnots(device.getIdleSpeedThreshold()));
device.setIconType(selectedIcon);
device.setIconType(selectedIcon.getBuiltInIcon());
device.setIcon(selectedIcon.getDatabaseIcon());
deviceHandler.onSave(device);
}

Expand All @@ -133,7 +135,7 @@ public void onCancelClicked(SelectEvent event) {
public void onSelectMarkerIconClicked(SelectEvent event) {
new DeviceMarkersDialog(selectedIcon, new DeviceMarkersDialog.DeviceMarkerHandler() {
@Override
public void onSave(DeviceIconType icon) {
public void onSave(MarkerIcon icon) {
if (icon != null) {
selectedIcon = icon;
updateIcon();
Expand All @@ -143,7 +145,7 @@ public void onSave(DeviceIconType icon) {
}

private void updateIcon() {
markerImage.setUrl(selectedIcon.getPositionIconType(Position.Status.OFFLINE).getURL(false));
markerImage.setUrl(selectedIcon.getOfflineURL());
panelMarkers.forceLayout();
}
}
134 changes: 35 additions & 99 deletions src/main/java/org/traccar/web/client/view/DeviceMarkersDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,21 @@ interface MarkerListItemTemplate extends XTemplates {
SafeHtml renderEndListItem(CommonStyles.Styles styles);

@XTemplates.XTemplate("<div class='{style.thumb}' style='background: url(\"{marker.offlineURL}\") no-repeat center center;'></div>")
SafeHtml listCell(Style style, Marker marker);
SafeHtml listCell(Style style, MarkerIcon marker);

@XTemplates.XTemplate("<div class='{style.thumbWrap}' style='background: #ffffff;'><span class=\"x-editable\">{text}</span><div class='{style.thumb}' style='background: url(\"{pictureURL}\") no-repeat center center;'></div></div>")
SafeHtml pictureView(Style style, String text, String pictureURL);
}

public interface DeviceMarkerHandler {
void onSave(DeviceIconType icon);
void onSave(MarkerIcon icon);
}

@UiField
Window window;

@UiField(provided = true)
ListView<Marker, Marker> view;
ListView<MarkerIcon, MarkerIcon> view;

@UiField(provided = true)
BorderLayoutContainer.BorderLayoutData northData;
Expand Down Expand Up @@ -140,118 +140,55 @@ public interface DeviceMarkerHandler {

final DeviceMarkerHandler handler;

static abstract class Marker {
abstract String getKey();
abstract String getDefaultURL();
abstract String getSelectedURL();
abstract String getOfflineURL();
}

static class BuiltInMarker extends Marker {
final DeviceIconType icon;

BuiltInMarker(DeviceIconType icon) {
this.icon = icon;
}

@Override
String getKey() {
return icon.name();
}

@Override
String getOfflineURL() {
return icon.getPositionIconType(Position.Status.OFFLINE).getURL(false);
}

@Override
String getDefaultURL() {
return icon.getPositionIconType(Position.Status.LATEST).getURL(false);
}

@Override
String getSelectedURL() {
return icon.getPositionIconType(Position.Status.LATEST).getURL(true);
}
}

static class DatabaseMarker extends Marker {
DeviceIcon icon;

DatabaseMarker(DeviceIcon icon) {
this.icon = icon;
}

@Override
String getKey() {
return Long.toString(icon.getId());
}

@Override
String getDefaultURL() {
return icon.defaultURL();
}

@Override
String getSelectedURL() {
return icon.selectedURL();
}

@Override
String getOfflineURL() {
return icon.offlineURL();
}
}

static class MergingCallback extends BaseAsyncCallback<List<DeviceIcon>> {
final AsyncCallback<List<Marker>> markerLoaderCallback;
final AsyncCallback<List<MarkerIcon>> markerLoaderCallback;

MergingCallback(Messages i18n, AsyncCallback<List<Marker>> markerLoaderCallback) {
MergingCallback(Messages i18n, AsyncCallback<List<MarkerIcon>> markerLoaderCallback) {
super(i18n);
this.markerLoaderCallback = markerLoaderCallback;
}

@Override
public void onSuccess(List<DeviceIcon> loaded) {
List<Marker> result = new ArrayList<Marker>(loaded.size() + DeviceIconType.values().length);
List<MarkerIcon> result = new ArrayList<MarkerIcon>(loaded.size() + DeviceIconType.values().length);
for (DeviceIcon icon : loaded) {
result.add(new DatabaseMarker(icon));
result.add(new MarkerIcon.Database(icon));
}
for (DeviceIconType icon : DeviceIconType.values()) {
result.add(new BuiltInMarker(icon));
result.add(new MarkerIcon.BuiltIn(icon));
}
markerLoaderCallback.onSuccess(result);
}
}

RpcProxy<Object, List<Marker>> hybridProxy = new RpcProxy<Object, List<Marker>>() {
RpcProxy<Object, List<MarkerIcon>> hybridProxy = new RpcProxy<Object, List<MarkerIcon>>() {
@Override
public void load(Object loadConfig, AsyncCallback<List<Marker>> callback) {
public void load(Object loadConfig, AsyncCallback<List<MarkerIcon>> callback) {
picturesService.getMarkerPictures(new MergingCallback(i18n, callback));
}
};

Marker selected;
MarkerIcon selected;

final ListStore<Marker> store;
final ListStore<MarkerIcon> store;

final Resources resources = GWT.create(Resources.class);
final MarkerListItemTemplate renderer = GWT.create(MarkerListItemTemplate.class);

public DeviceMarkersDialog(final DeviceIconType selectedIcon, DeviceMarkerHandler handler) {
public DeviceMarkersDialog(final MarkerIcon selectedIcon, DeviceMarkerHandler handler) {
this.handler = handler;

ModelKeyProvider<Marker> keyProvider = new ModelKeyProvider<Marker>() {
ModelKeyProvider<MarkerIcon> keyProvider = new ModelKeyProvider<MarkerIcon>() {
@Override
public String getKey(Marker item) {
public String getKey(MarkerIcon item) {
return item.getKey();
}
};

resources.css().ensureInjected();
final Style style = resources.css();

ListViewCustomAppearance<Marker> appearance = new ListViewCustomAppearance<Marker>("." + style.thumbWrap(), style.over(), style.select()) {
ListViewCustomAppearance<MarkerIcon> appearance = new ListViewCustomAppearance<MarkerIcon>("." + style.thumbWrap(), style.over(), style.select()) {
@Override
public void renderEnd(SafeHtmlBuilder builder) {
renderer.renderEndListItem(CommonStyles.get());
Expand All @@ -263,27 +200,27 @@ public void renderItem(SafeHtmlBuilder builder, SafeHtml content) {
}
};

store = new ListStore<Marker>(keyProvider);
Loader<Object, List<Marker>> loader = new Loader<Object, List<Marker>>(hybridProxy);
loader.addLoadHandler(new ListStoreBinding<Object, Marker, List<Marker>>(store));
store = new ListStore<MarkerIcon>(keyProvider);
Loader<Object, List<MarkerIcon>> loader = new Loader<Object, List<MarkerIcon>>(hybridProxy);
loader.addLoadHandler(new ListStoreBinding<Object, MarkerIcon, List<MarkerIcon>>(store));

view = new ListView<Marker, Marker>(store, new IdentityValueProvider<Marker>() {
view = new ListView<MarkerIcon, MarkerIcon>(store, new IdentityValueProvider<MarkerIcon>() {
@Override
public void setValue(Marker object, Marker value) {
public void setValue(MarkerIcon object, MarkerIcon value) {
}
}, appearance);

view.setCell(new SimpleSafeHtmlCell<Marker>(new AbstractSafeHtmlRenderer<Marker>() {
view.setCell(new SimpleSafeHtmlCell<MarkerIcon>(new AbstractSafeHtmlRenderer<MarkerIcon>() {
@Override
public SafeHtml render(Marker object) {
public SafeHtml render(MarkerIcon object) {
return renderer.listCell(style, object);
}
}));

view.getSelectionModel().setSelectionMode(com.sencha.gxt.core.client.Style.SelectionMode.SINGLE);
view.getSelectionModel().addSelectionHandler(new SelectionHandler<Marker>() {
view.getSelectionModel().addSelectionHandler(new SelectionHandler<MarkerIcon>() {
@Override
public void onSelection(SelectionEvent<Marker> event) {
public void onSelection(SelectionEvent<MarkerIcon> event) {
selectionChanged();
}
});
Expand All @@ -300,8 +237,8 @@ public void onSelection(SelectionEvent<Marker> event) {

uiBinder.createAndBindUi(this);

selected = new BuiltInMarker(selectedIcon);
store.addStoreHandlers(new BaseStoreHandlers<Marker>() {
selected = selectedIcon;
store.addStoreHandlers(new BaseStoreHandlers<MarkerIcon>() {
@Override
public void onAnything() {
for (int i = 0; i < store.size(); i++) {
Expand Down Expand Up @@ -329,8 +266,7 @@ public void hide() {

@UiHandler("saveButton")
public void onOKClicked(SelectEvent event) {
Marker selected = view.getSelectionModel().getSelectedItem();
handler.onSave(selected == null ? null : ((BuiltInMarker) selected).icon);
handler.onSave(view.getSelectionModel().getSelectedItem());
hide();
}

Expand All @@ -340,7 +276,7 @@ public void onCancelClicked(SelectEvent event) {
}

private void selectionChanged() {
Marker marker = view.getSelectionModel().getSelectedItem();
MarkerIcon marker = view.getSelectionModel().getSelectedItem();
if (marker == null) {
defaultImage.setHTML("");
selectedImage.setHTML("");
Expand All @@ -352,8 +288,8 @@ private void selectionChanged() {
}
panelImages.forceLayout();

editButton.setEnabled(marker instanceof DatabaseMarker);
removeButton.setEnabled(marker instanceof DatabaseMarker);
editButton.setEnabled(marker instanceof MarkerIcon.Database);
removeButton.setEnabled(marker instanceof MarkerIcon.Database);
}

@UiHandler("addButton")
Expand All @@ -368,7 +304,7 @@ public void uploaded(Picture defaultIcon, Picture selectedIcon, Picture offlineI
picturesService.addMarkerPicture(marker, new BaseAsyncCallback<DeviceIcon>(i18n) {
@Override
public void onSuccess(DeviceIcon added) {
Marker marker = new DatabaseMarker(added);
MarkerIcon marker = new MarkerIcon.Database(added);
selected = marker;
store.add(0, marker);
}
Expand All @@ -379,7 +315,7 @@ public void onSuccess(DeviceIcon added) {

@UiHandler("editButton")
public void editIcon(SelectEvent event) {
final DatabaseMarker marker = (DatabaseMarker) view.getSelectionModel().getSelectedItem();
final MarkerIcon.Database marker = (MarkerIcon.Database) view.getSelectionModel().getSelectedItem();
new DeviceIconDialog(true, new DeviceIconDialog.DeviceIconHandler() {
@Override
public void uploaded(Picture defaultIcon, Picture selectedIcon, Picture offlineIcon) {
Expand All @@ -403,13 +339,13 @@ public void onSuccess(DeviceIcon updated) {

@UiHandler("removeButton")
public void removeIcon(SelectEvent event) {
final Marker marker = view.getSelectionModel().getSelectedItem();
final MarkerIcon.Database marker = (MarkerIcon.Database) view.getSelectionModel().getSelectedItem();
final ConfirmMessageBox dialog = new ConfirmMessageBox(i18n.confirm(), i18n.confirmDeviceIconRemoval());
dialog.addDialogHideHandler(new DialogHideEvent.DialogHideHandler() {
@Override
public void onDialogHide(DialogHideEvent event) {
if (event.getHideButton() == Dialog.PredefinedButton.YES) {
picturesService.removeMarkerPicture(((DatabaseMarker) marker).icon, new BaseAsyncCallback<Void>(i18n) {
picturesService.removeMarkerPicture(marker.icon, new BaseAsyncCallback<Void>(i18n) {
@Override
public void onSuccess(Void result) {
view.getSelectionModel().deselectAll();
Expand Down
Loading

0 comments on commit 188ad4a

Please sign in to comment.