Skip to content

Commit

Permalink
For #85 - moved device icon selection to a separate dialog window
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalidze committed May 20, 2015
1 parent 4394729 commit 92519bd
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/traccar/web/Traccar.gwt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<inherits name="com.google.gwt.xml.XML"/>
<inherits name='com.sencha.gxt.ui.GXT'/>
<inherits name='org.gwtopenmaps.openlayers.OpenLayers' />
<inherits name="com.google.gwt.resources.Resources" />

<set-property name="gwt.logging.enabled" value="FALSE"/>
<set-property name="gwt.logging.consoleHandler" value="DISABLED"/>
Expand Down
55 changes: 27 additions & 28 deletions src/main/java/org/traccar/web/client/view/DeviceDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.gwt.user.client.ui.VerticalPanel;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.core.client.util.ToggleGroup;
import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.form.NumberField;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor;
Expand Down Expand Up @@ -70,9 +71,6 @@ public interface DeviceHandler {
@UiField
TextField uniqueId;

@UiField
VerticalLayoutContainer devicePictures;

@UiField(provided = true)
NumberPropertyEditor<Integer> integerPropertyEditor = new NumberPropertyEditor.IntegerPropertyEditor();

Expand All @@ -85,14 +83,17 @@ public interface DeviceHandler {
@UiField
NumberField<Double> idleSpeedThreshold;

@UiField(provided = true)
final Image markerImage;
@UiField
HorizontalLayoutContainer panelMarkers;

ToggleGroup iconRadioGroup = new ToggleGroup();
@UiField
Image markerImage;

DeviceIconType selectedIcon;

public DeviceDialog(Device device, DeviceHandler deviceHandler) {
this.deviceHandler = deviceHandler;
markerImage = new Image(device.getIconType().getPositionIconType(Position.Status.OFFLINE).getURL(false));
selectedIcon = device.getIconType();

uiBinder.createAndBindUi(this);

Expand All @@ -103,24 +104,7 @@ public DeviceDialog(Device device, DeviceHandler deviceHandler) {
driver.edit(device);

idleSpeedThreshold.setValue(device.getIdleSpeedThreshold() * ApplicationContext.getInstance().getUserSettings().getSpeedUnit().getFactor());


HorizontalPanel nextPanel = null;
DeviceIconType[] deviceIconTypes = DeviceIconType.values();
for (int i = 0; i < deviceIconTypes.length; i++) {
DeviceIconType deviceIconType = deviceIconTypes[i];
if (nextPanel == null || i % 5 == 0) {
nextPanel = new HorizontalPanel();
devicePictures.add(nextPanel, new VerticalLayoutContainer.VerticalLayoutData(-1, -1, new Margins(5, 0, 5, 5)));
}

Radio radio = new Radio();
radio.setBoxLabel("<img src=\"" + deviceIconType.getPositionIconType(Position.Status.OFFLINE).getURL(false) + "\">");
nextPanel.add(radio);
iconRadioGroup.add(radio);
radio.setValue(deviceIconType == device.getIconType());
radio.setId(deviceIconType.name());
}
updateIcon();
}

public void show() {
Expand All @@ -136,9 +120,7 @@ public void onSaveClicked(SelectEvent event) {
window.hide();
Device device = driver.flush();
device.setIdleSpeedThreshold(ApplicationContext.getInstance().getUserSettings().getSpeedUnit().toKnots(device.getIdleSpeedThreshold()));
if (iconRadioGroup.getValue() != null) {
device.setIconType(DeviceIconType.valueOf(((Radio) iconRadioGroup.getValue()).getId()));
}
device.setIconType(selectedIcon);
deviceHandler.onSave(device);
}

Expand All @@ -147,4 +129,21 @@ public void onCancelClicked(SelectEvent event) {
window.hide();
}

@UiHandler("selectButton")
public void onSelectMarkerIconClicked(SelectEvent event) {
new DeviceMarkersDialog(selectedIcon, new DeviceMarkersDialog.DeviceMarkerHandler() {
@Override
public void onSave(DeviceIconType icon) {
if (icon != null) {
selectedIcon = icon;
updateIcon();
}
}
}).show();
}

private void updateIcon() {
markerImage.setUrl(selectedIcon.getPositionIconType(Position.Status.OFFLINE).getURL(false));
panelMarkers.forceLayout();
}
}
7 changes: 2 additions & 5 deletions src/main/java/org/traccar/web/client/view/DeviceDialog.ui.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<ui:with field='i18n' type='org.traccar.web.client.i18n.Messages' />

<gxt:Window ui:field="window" pixelSize="375, 280" modal="true" headingText="{i18n.device}" focusWidget="{saveButton}">
<gxt:Window ui:field="window" pixelSize="375, 210" modal="true" headingText="{i18n.device}" focusWidget="{saveButton}">
<container:VerticalLayoutContainer>
<container:child layoutData="{verticalLayoutData}">
<form:FieldLabel text="{i18n.name}" labelWidth="140">
Expand Down Expand Up @@ -69,7 +69,7 @@
<container:child layoutData="{verticalLayoutData}">
<form:FieldLabel text="{i18n.markers}" labelWidth="140">
<form:widget>
<container:HorizontalLayoutContainer>
<container:HorizontalLayoutContainer ui:field="panelMarkers">
<container:child layoutData="{horizontalLayoutData}">
<g:Image ui:field="markerImage" />
</container:child>
Expand All @@ -80,9 +80,6 @@
</form:widget>
</form:FieldLabel>
</container:child>
<container:child layoutData="{verticalLayoutData}">
<container:VerticalLayoutContainer ui:field="devicePictures" />
</container:child>
</container:VerticalLayoutContainer>

<gxt:button>
Expand Down
186 changes: 186 additions & 0 deletions src/main/java/org/traccar/web/client/view/DeviceMarkersDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* Copyright 2015 Vitaly Litvak ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.web.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.text.shared.AbstractSafeHtmlRenderer;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.cell.core.client.SimpleSafeHtmlCell;
import com.sencha.gxt.core.client.IdentityValueProvider;
import com.sencha.gxt.core.client.Style;
import com.sencha.gxt.core.client.resources.CommonStyles;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.theme.base.client.listview.ListViewCustomAppearance;
import com.sencha.gxt.widget.core.client.ListView;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import org.traccar.web.shared.model.Device;
import org.traccar.web.shared.model.DeviceIconType;
import org.traccar.web.shared.model.Position;

public class DeviceMarkersDialog {
private static DeviceMarkersDialogUiBinder uiBinder = GWT.create(DeviceMarkersDialogUiBinder.class);

interface DeviceMarkersDialogUiBinder extends UiBinder<Widget, DeviceMarkersDialog> {
}

interface Resources extends ClientBundle {
@ClientBundle.Source("PicturesListView.css")
Style css();
}

interface Style extends CssResource {
String thumb();

String thumbWrap();

String over();

String select();
}

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

@UiField
Window window;

ListStore<Marker> store;

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

final DeviceMarkerHandler handler;

static abstract class Marker {
abstract String getKey();
abstract String getURL();
}

class BuiltInMarker extends Marker {
final DeviceIconType icon;

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

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

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

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

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

Marker selectedMarker = null;
store = new ListStore<Marker>(keyProvider);
for (DeviceIconType icon : DeviceIconType.values()) {
Marker marker = new BuiltInMarker(icon);
store.add(marker);
if (selectedIcon == icon) {
selectedMarker = marker;
}
}

final Resources resources = GWT.create(Resources.class);
resources.css().ensureInjected();

final Style style = resources.css();

ListViewCustomAppearance<Marker> appearance = new ListViewCustomAppearance<Marker>("." + style.thumbWrap(), style.over(), style.select()) {
@Override
public void renderEnd(SafeHtmlBuilder builder) {
String markup = new StringBuilder("<div class=\"").append(CommonStyles.get().clear()).append("\"></div>").toString();
builder.appendHtmlConstant(markup);
}

@Override
public void renderItem(SafeHtmlBuilder builder, SafeHtml content) {
builder.appendHtmlConstant("<div class='" + style.thumbWrap() + "' style='border: 1px solid white'>");
builder.append(content);
builder.appendHtmlConstant("</div>");
}
};

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

view.setCell(new SimpleSafeHtmlCell<Marker>(new AbstractSafeHtmlRenderer<Marker>() {
@Override
public SafeHtml render(Marker object) {
SafeHtmlBuilder builder = new SafeHtmlBuilder();
return builder
.appendHtmlConstant("<div class=\"")
.appendHtmlConstant(style.thumb())
.appendHtmlConstant("\" style=\"background: url(")
.appendHtmlConstant(object.getURL())
.appendHtmlConstant(") no-repeat center center;\"></div>")
.toSafeHtml();
}
}));

view.getSelectionModel().setSelectionMode(com.sencha.gxt.core.client.Style.SelectionMode.SINGLE);

uiBinder.createAndBindUi(this);

view.getSelectionModel().select(selectedMarker, false);
}

public void show() {
window.show();
}

public void hide() {
window.hide();
}

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

@UiHandler("cancelButton")
public void onCancelClicked(SelectEvent event) {
hide();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder
xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:gxt="urn:import:com.sencha.gxt.widget.core.client"
xmlns:container="urn:import:com.sencha.gxt.widget.core.client.container"
xmlns:button="urn:import:com.sencha.gxt.widget.core.client.button">

<ui:with type="com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData" field="verticalLayoutData">
<ui:attributes width="1" height="1" />
</ui:with>

<ui:with field='i18n' type='org.traccar.web.client.i18n.Messages' />

<gxt:Window ui:field="window" pixelSize="386, 280" modal="true" headingText="{i18n.markers}" focusWidget="{saveButton}">
<container:VerticalLayoutContainer>
<container:child layoutData="{verticalLayoutData}">
<gxt:ListView ui:field="view"/>
</container:child>
</container:VerticalLayoutContainer>

<gxt:button>
<button:TextButton ui:field="saveButton" text="OK" />
</gxt:button>
<gxt:button>
<button:TextButton ui:field="cancelButton" text="{i18n.cancel}" />
</gxt:button>
</gxt:Window>

</ui:UiBinder>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.thumb {
padding: 3px;
height: 40px;
width: 70px;
}

.thumbWrap {
border: 1px solid white;
float: left;
display: block;
margin: 4px 0 4px 4px;
padding: 5px;
font: 11px Arial, Helvetica, sans-serif;
}

.over {
background: repeat-x scroll left top #EFEFEF;
border: 1px solid #ddd !important;
padding: 5px;
}

.select {
background: none repeat scroll 0 50% #DFE8F6;
border: 1px dotted #A3BAE9 !important;
cursor: pointer;
}

.select .thumb {
background: none repeat scroll 0 0 transparent;
}

0 comments on commit 92519bd

Please sign in to comment.