aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/web/client/controller/DeviceController.java
blob: 0c42e8a246e02e410fa9f663ea56c65ec861d10f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package org.traccar.web.client.controller;

import java.util.List;

import org.traccar.web.client.Traccar;
import org.traccar.web.client.database.BaseAsyncCallback;
import org.traccar.web.client.view.DeviceDialog;
import org.traccar.web.client.view.DevicePanel;
import org.traccar.web.shared.model.Device;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickHandler;

public class DeviceController implements PanelController, DevicePanel.DeviceHandler, DeviceDialog.SaveHandler  {

    public interface DeviceHandler {
        public void onChanged(List<Device> devices);
        public void onSelected(Device device);
    }

    private DeviceHandler deviceHandler;

    public void setHandler(DeviceHandler deviceHandler) {
        this.deviceHandler = deviceHandler;
    }

    private final DevicePanel devicePanel;

    public DeviceController() {
        devicePanel = new DevicePanel();
        devicePanel.setDeviceHandler(this);
        Traccar.getDatabaseService().getDevices(updateCallback);
    }

    @Override
    public Canvas getView() {
        return devicePanel;
    }

    public void setSettingsClickHandler(ClickHandler handler) {
        if (devicePanel != null) {
            devicePanel.setSettingsClickHandler(handler);
        }
    }

    DeviceDialog dialog;

    @Override
    public void onAdd() {
        onEdit(new Device());
    }

    @Override
    public void onEdit(Device device) {
        dialog = new DeviceDialog();
        dialog.setSaveHandler(this);
        dialog.setDevice(device);
        dialog.draw();
        dialog.centerInPage();
    }

    @Override
    public void onRemove(Device device) {
        Traccar.getDatabaseService().removeDevice(device, saveCallback);
    }

    @Override
    public void onSelected(Device device) {
        if (deviceHandler != null) {
            deviceHandler.onSelected(device);
        }
    }

    @Override
    public void onSave(Device device) {
        Traccar.getDatabaseService().storeDevice(device, saveCallback);
    }

    private AsyncCallback<Boolean> saveCallback = new BaseAsyncCallback<Boolean>() {
        @Override
        public void onSuccess(Boolean result) {
            if (result) {
                if (dialog != null) {
                    dialog.destroy();
                    dialog = null;
                }
                Traccar.getDatabaseService().getDevices(updateCallback);
            }
        }
    };

    private AsyncCallback<List<Device>> updateCallback = new BaseAsyncCallback<List<Device>>() {
        @Override
        public void onSuccess(List<Device> result) {
            devicePanel.updateDevices(result);
            if (deviceHandler != null) {
                deviceHandler.onChanged(result);
            }
        }
    };
}