Skip to content

Commit

Permalink
Added context menu for device grid
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalidze committed May 12, 2016
1 parent 5032e59 commit 8e2cb33
Showing 1 changed file with 90 additions and 13 deletions.
103 changes: 90 additions & 13 deletions src/main/java/org/traccar/web/client/view/DeviceView.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.BrowserEvents;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.EventTarget;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
Expand All @@ -38,6 +39,7 @@
import com.sencha.gxt.core.client.ToStringValueProvider;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.core.client.XTemplates;
import com.sencha.gxt.core.client.dom.XElement;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.Store;
import com.sencha.gxt.data.shared.event.StoreAddEvent;
Expand Down Expand Up @@ -673,8 +675,30 @@ public String getPath() {

columnModel = new ColumnModel<>(columnConfigList);

grid = new TreeGrid<>(deviceStore, columnModel, colName);
grid = new TreeGrid<GroupedDevice>(deviceStore, columnModel, colName) {
@Override
protected void onRightClick(Event event) {
EventTarget eventTarget = event.getEventTarget();
List<GroupedDevice> selectedItems = getSelectionModel().getSelectedItems();
boolean onSelectedRow = false;
for (GroupedDevice selectedItem : selectedItems) {
if (deviceStore.isDevice(selectedItem)) {
int index = store.indexOf(selectedItem);
Element selectedRow = getView().getRow(index);
if (selectedRow.isOrHasChild(XElement.as(eventTarget))) {
onSelectedRow = true;
break;
}
}
}

if (onSelectedRow) {
super.onRightClick(event);
}
}
};
grid.setView(view);
grid.setContextMenu(createDeviceGridContextMenu());

// configure device store filtering
deviceFilter = new StoreFilterField<GroupedDevice>() {
Expand Down Expand Up @@ -785,10 +809,14 @@ public void onEditClicked(SelectEvent event) {
if (editingGeoFences()) {
geoFenceHandler.onEdit(geoFenceList.getSelectionModel().getSelectedItem());
} else {
GroupedDevice node = grid.getSelectionModel().getSelectedItem();
if (deviceStore.isDevice(node)) {
deviceHandler.onEdit(deviceStore.getDevice(node));
}
editDevice();
}
}

private void editDevice() {
GroupedDevice node = grid.getSelectionModel().getSelectedItem();
if (deviceStore.isDevice(node)) {
deviceHandler.onEdit(deviceStore.getDevice(node));
}
}

Expand All @@ -797,10 +825,14 @@ public void onShareClicked(SelectEvent event) {
if (editingGeoFences()) {
geoFenceHandler.onShare(geoFenceList.getSelectionModel().getSelectedItem());
} else {
GroupedDevice node = grid.getSelectionModel().getSelectedItem();
if (deviceStore.isDevice(node)) {
deviceHandler.onShare(deviceStore.getDevice(node));
}
shareDevice();
}
}

private void shareDevice() {
GroupedDevice node = grid.getSelectionModel().getSelectedItem();
if (deviceStore.isDevice(node)) {
deviceHandler.onShare(deviceStore.getDevice(node));
}
}

Expand All @@ -809,15 +841,23 @@ public void onRemoveClicked(SelectEvent event) {
if (editingGeoFences()) {
geoFenceHandler.onRemove(geoFenceList.getSelectionModel().getSelectedItem());
} else {
GroupedDevice node = grid.getSelectionModel().getSelectedItem();
if (deviceStore.isDevice(node)) {
deviceHandler.onRemove(deviceStore.getDevice(node));
}
removeDevice();
}
}

private void removeDevice() {
GroupedDevice node = grid.getSelectionModel().getSelectedItem();
if (deviceStore.isDevice(node)) {
deviceHandler.onRemove(deviceStore.getDevice(node));
}
}

@UiHandler("commandButton")
public void onCommandClicked(SelectEvent event) {
sendCommand();
}

private void sendCommand() {
commandHandler.onCommand(deviceStore.getDevice(grid.getSelectionModel().getSelectedItem()));
}

Expand Down Expand Up @@ -872,4 +912,41 @@ interface Resources extends ClientBundle {
@Source("org/traccar/web/client/theme/icon/footprints.png")
ImageResource footprints();
}

private Menu createDeviceGridContextMenu() {
Menu menu = new Menu();
MenuItem edit = new MenuItem(i18n.edit());
edit.addSelectionHandler(new SelectionHandler<Item>() {
@Override
public void onSelection(SelectionEvent<Item> event) {
editDevice();
}
});
menu.add(edit);
MenuItem share = new MenuItem(i18n.share());
share.addSelectionHandler(new SelectionHandler<Item>() {
@Override
public void onSelection(SelectionEvent<Item> event) {
shareDevice();
}
});
menu.add(share);
MenuItem remove = new MenuItem(i18n.remove());
remove.addSelectionHandler(new SelectionHandler<Item>() {
@Override
public void onSelection(SelectionEvent<Item> event) {
removeDevice();
}
});
menu.add(remove);
MenuItem command = new MenuItem(i18n.command());
command.addSelectionHandler(new SelectionHandler<Item>() {
@Override
public void onSelection(SelectionEvent<Item> event) {
sendCommand();
}
});
menu.add(command);
return menu;
}
}

0 comments on commit 8e2cb33

Please sign in to comment.