Skip to content
Snippets Groups Projects
Commit fd436394 authored by Raphael GIRARDOT's avatar Raphael GIRARDOT
Browse files

- sensor/actuator/timbase list reordering compatible with multiple selection (SCAN-931)

- new Top/Bottom buttons (SCAN-933)
- new Reverse button (SCAN-934)
parent 26a92e2f
No related branches found
No related tags found
No related merge requests found
......@@ -16,7 +16,13 @@ public interface IGenericDeviceListController extends IController<GenericDeviceL
public void notifyDeleteAction();
public void switchDevicePosition(int position1, int position2);
public void changeDevicePositions(boolean up, int... positions);
public void changeDevicePositionsToExtreme(boolean top, int... positions);
public void switchDevicePositions(int... positions);
public void reversePositions(int... positions);
public void notifyDeviceSelectedAll(boolean selected);
......@@ -24,5 +30,4 @@ public interface IGenericDeviceListController extends IController<GenericDeviceL
public void notifyDeviceRenamed(int deviceIndex, String newDeviceName);
}
package fr.soleil.salsa.client.controller.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.slf4j.Logger;
......@@ -8,6 +10,7 @@ import org.slf4j.Logger;
import com.google.common.base.Preconditions;
import com.google.common.eventbus.Subscribe;
import fr.soleil.lib.project.ObjectUtils;
import fr.soleil.salsa.api.ScanApi;
import fr.soleil.salsa.client.controller.AErrorManagingController;
import fr.soleil.salsa.client.controller.IErrorController;
......@@ -33,14 +36,14 @@ import fr.soleil.salsa.tool.EventBusHandler;
*/
public abstract class GenericDeviceListController extends AErrorManagingController<GenericDeviceListView>
implements IGenericDeviceListController {
/** The logger for messages */
public static final Logger LOGGER = LoggingUtil.getLogger(GenericDeviceListController.class);
protected static final String DEFAULT_SENSOR_NAME = "";
protected static final String DEFAULT_SENSOR_NAME = ObjectUtils.EMPTY_STRING;
/**
* Redirects EntityPropertyChangedEvent on the config to
* DeviceListController.notifyConfigChangedEvent.
* Redirects EntityPropertyChangedEvent on the config to DeviceListController.notifyConfigChangedEvent.
*/
protected final IListener<EntityPropertyChangedEvent<IConfig<?>>> configListener;
......@@ -59,7 +62,7 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
/**
* The device currently selected.
*/
protected IDevice deviceSelected;
protected String[] selectedDevices;
/**
* The name of the scan server.
......@@ -75,20 +78,18 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
* @param config the config
* @param errorController the error controller
*/
GenericDeviceListController(final GenericDeviceListView view, final IConfig<?> config,
protected GenericDeviceListController(final GenericDeviceListView view, final IConfig<?> config,
final IErrorController errorController) {
super(view, errorController);
configListener = new ConfigListener();
deviceList = null;
scanServerName = null;
this.config = null;
this.setConfig(config);
setConfig(config);
if (config == null) {
// setDevice calls refresh if the device changes. Before
// setDevice is called, this.device is null.
// So if the device is not null, setDevice has called refresh
// already.
this.refresh();
// setDevice calls refresh if the device changes. Before setDevice is called, this.device is null.
// So if the device is not null, setDevice has called refresh already.
refresh();
}
updateSuggestionsList();
......@@ -120,7 +121,7 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
} else {
initDeviceList();
}
this.refresh();
refresh();
}
setViewVisible(config != null);
if (view != null) {
......@@ -134,7 +135,7 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
IDevice device = config.getDevice(deviceName, deviceList);
if (device != null) {
device.setEnabled(selected);
deviceSelected = device;
selectedDevices = new String[] { device.getName() };
}
}
......@@ -158,26 +159,167 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
return device;
}
/**
* switch DevicePosition
*/
protected String[] getDevices(int... positions) {
String[] devices = new String[positions.length];
for (int i = 0; i < positions.length; i++) {
devices[i] = getDeviceName(getDeviceName(positions[i]));
}
return devices;
}
protected void changeDevicePositionWithoutSelection(int position, boolean up) {
IDevice device1 = getDeviceName(position);
IDevice device2 = getDeviceName(position + (up ? -1 : 1));
if ((device1 != null) && (device2 != null)) {
if (listener != null) {
listener.deviceSwap(device1.getName(), device2.getName());
}
swapDevice(device1.getName(), device2.getName());
}
}
@Override
public void switchDevicePosition(final int position1, final int position2) {
IDevice device1 = getDeviceName(position1);
IDevice device2 = getDeviceName(position2);
public void changeDevicePositions(boolean up, int... positions) {
if ((positions != null) && positions.length > 0) {
String[] selected = getDevices(positions);
Arrays.sort(positions);
int refPos;
if (up) {
refPos = -1;
} else {
refPos = 1;
for (int i = 0; i < positions.length / 2; i++) {
int temp = positions[i];
positions[i] = positions[positions.length - i - 1];
positions[positions.length - i - 1] = temp;
}
}
int[] devices = new int[positions.length * 2];
int index = 0;
for (int position : positions) {
devices[2 * index] = position;
devices[2 * index + 1] = position + refPos;
index++;
}
selectedDevices = selected;
for (int i = 0; i < devices.length / 2; i++) {
IDevice device1 = getDeviceName(devices[2 * i]);
IDevice device2 = getDeviceName(devices[2 * i + 1]);
if ((device1 != null) && (device2 != null)) {
if (listener != null) {
listener.deviceSwap(device1.getName(), device2.getName());
}
swapDevice(device1.getName(), device2.getName());
}
}
}
}
if (device1 != null) {
deviceSelected = device1;
@Override
public void changeDevicePositionsToExtreme(boolean top, int... positions) {
if ((positions != null) && positions.length > 0) {
String[] selected = getDevices(positions);
Arrays.sort(positions);
int[] refPositions = new int[positions.length];
int refPos;
if (top) {
refPos = -1;
for (int i = 0; i < positions.length; i++) {
refPositions[i] = i;
}
} else {
refPos = 1;
for (int i = 0; i < positions.length / 2; i++) {
int temp = positions[i];
positions[i] = positions[positions.length - i - 1];
positions[positions.length - i - 1] = temp;
}
for (int i = 0; i < positions.length; i++) {
refPositions[i] = deviceList.size() - (i + 1);
}
}
selectedDevices = selected;
int index = 0;
String[] devices = new String[positions.length];
for (int position : positions) {
IDevice device1 = getDeviceName(position);
devices[index++] = getDeviceName(device1);
}
index = 0;
for (String device : devices) {
int goal = refPositions[index];
int tmp = positions[index++];
if (device != null) {
List<String> devicesToSwap = new LinkedList<>();
while (tmp != goal) {
tmp += refPos;
IDevice device2 = getDeviceName(tmp);
if (device2 != null) {
devicesToSwap.add(device2.getName());
}
}
while (!devicesToSwap.isEmpty()) {
String swap = devicesToSwap.remove(0);
if (listener != null) {
listener.deviceSwap(device, swap);
}
swapDevice(device, swap);
}
}
}
}
}
@Override
public void switchDevicePositions(int... positions) {
if ((positions != null) && positions.length > 0) {
String[] selected = new String[positions.length];
for (int i = 0; i < positions.length; i++) {
selected[i] = getDeviceName(getDeviceName(positions[i]));
}
selectedDevices = selected;
for (int i = 0; i < positions.length / 2; i++) {
IDevice device1 = getDeviceName(positions[2 * i]);
IDevice device2 = getDeviceName(positions[2 * i + 1]);
if ((device1 != null) && (device2 != null)) {
if (listener != null) {
listener.deviceSwap(device1.getName(), device2.getName());
}
swapDevice(device1.getName(), device2.getName());
}
}
}
}
@Override
public void reversePositions(int... positions) {
if ((positions != null) && positions.length > 0) {
String[] selected = new String[positions.length];
for (int i = 0; i < positions.length; i++) {
selected[i] = getDeviceName(getDeviceName(positions[i]));
}
Arrays.sort(positions);
boolean contiguous = true;
for (int i = 1; i < positions.length && contiguous; i++) {
if (positions[i] != positions[i - 1] + 1) {
contiguous = false;
}
}
if (contiguous) {
selectedDevices = selected;
for (int i = 0; i < positions.length / 2; i++) {
IDevice device1 = getDeviceName(positions[i]);
IDevice device2 = getDeviceName(positions[positions.length - i - 1]);
if ((device1 != null) && (device2 != null)) {
if (listener != null) {
listener.deviceSwap(device1.getName(), device2.getName());
}
swapDevice(device1.getName(), device2.getName());
}
}
}
}
}
protected abstract void swapDevice(String deviceName1, String deviceName2);
......@@ -195,6 +337,10 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
refresh();
}
protected String getDeviceName(IDevice device) {
return device == null ? null : device.getName();
}
/**
* Refreshes the view.
*/
......@@ -202,28 +348,30 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
// Add the list devices for the model list
if (deviceList != null && view != null) {
view.setControlEnable(true);
deviceErrorMessage(true, this.view.addRowInModel(deviceList));
deviceErrorMessage(true, view.addRowInModel(deviceList));
boolean stillThere = false;
if ((deviceList != null) && (!deviceList.isEmpty())) {
int newRow = 0;
if (deviceSelected != null) {
List<String> devices = new ArrayList<>();
if (selectedDevices != null) {
for (IDevice deviceSearch : deviceList) {
if (deviceSelected.equals(deviceSearch)) {
for (String device : selectedDevices) {
if ((device != null) && (deviceSearch != null) && device.equals(deviceSearch.getName())) {
stillThere = true;
devices.add(device);
break;
}
++newRow;
}
}
}
if (stillThere) {
this.view.setRowSelected(newRow);
selectedDevices = devices.toArray(new String[devices.size()]);
view.setSelectedDevices(selectedDevices);
} else {
deviceSelected = deviceList.get(0);
this.view.setRowSelected(0);
selectedDevices = new String[] { getDeviceName(deviceList.get(0)) };
view.setSelectedRows(0);
}
} else {
deviceSelected = null;
selectedDevices = null;
}
} else {
refreshWhenConfigIsNull();
......@@ -243,7 +391,7 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
}
}
if (changeOccurred) {
this.refresh();
refresh();
}
}
......@@ -275,7 +423,7 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
// JIRA SCAN-375
device.setEnabled(false);
boolean addSucces = this.view.addRow(device);
boolean addSucces = view.addRow(device);
if (addSucces) {
addDevice(device);
} else if (!view.isNullModel()) {
......@@ -311,7 +459,7 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
public void notifyDeleteAction() {
if (deviceList != null) {
updateDeviceList(false);
int[] indexToRemove = this.view.getRowsSelected();
int[] indexToRemove = view.getSelectedRows();
if ((indexToRemove != null) && (indexToRemove.length > 0)) {
List<IDevice> deviceToRemove = new ArrayList<>();
for (int deviceIndex : indexToRemove) {
......@@ -321,7 +469,7 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
notifyDeleteAction(deviceToRemove);
for (int deviceIndex : indexToRemove) {
this.view.removeRow(deviceIndex);
view.removeRow(deviceIndex);
}
if (config != null) {
config.setModified(true);
......@@ -353,7 +501,7 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
* for duplicates.
*/
protected void updateDeviceList(final boolean checkForDuplicates) {
if ((deviceList != null) && (this.view.getModel().getRowCount() > 0)) {
if ((deviceList != null) && (view.getModel().getRowCount() > 0)) {
ArrayList<String> canceledNames = new ArrayList<>();
IDevice deviceInModel = null;
IDevice deviceInView = null;
......@@ -362,7 +510,7 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
Boolean isEnabledInView = false;
boolean isCommonInView = false;
boolean addDevice = false;
for (int i = 0; i < this.view.getModel().getRowCount(); i++) {
for (int i = 0; i < view.getModel().getRowCount(); i++) {
addDevice = false;
if (i < deviceList.size()) {
deviceInModel = deviceList.get(i);
......@@ -372,9 +520,9 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
deviceInModel = generateDevice();
}
deviceInView = (IDevice) this.view.getModel().getValueAt(i, 1);
deviceInView = (IDevice) view.getModel().getValueAt(i, 1);
deviceNameInView = deviceInView.getName();
isEnabledInView = (Boolean) this.view.getModel().getValueAt(i, 0);
isEnabledInView = (Boolean) view.getModel().getValueAt(i, 0);
isCommonInView = deviceInView.isCommon();
deviceInModel.setName(deviceNameInView);
......@@ -386,7 +534,7 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
} else if (checkForDuplicates && (!isDeviceNameAllowed(deviceInModel, deviceList))) {
canceledNames.add(deviceNameInView);
deviceList.get(i).setName(deviceNameInModel);
this.view.getModel().setValueAt(deviceInModel, i, 1);
view.getModel().setValueAt(deviceInModel, i, 1);
}
}
deviceErrorMessage(false, canceledNames.toArray(new String[canceledNames.size()]));
......@@ -551,7 +699,7 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
@SuppressWarnings("unchecked")
private void addListenersTo() {
if (config instanceof IEventSource) {
((IEventSource<EntityPropertyChangedEvent<IConfig<?>>>) this.config).addListener(configListener);
((IEventSource<EntityPropertyChangedEvent<IConfig<?>>>) config).addListener(configListener);
deviceList = config.getSensorsList();
}
}
......@@ -564,7 +712,7 @@ public abstract class GenericDeviceListController extends AErrorManagingControll
@SuppressWarnings("unchecked")
private void removeListenersTo() {
if (config instanceof IEventSource) {
((IEventSource<EntityPropertyChangedEvent<IConfig<?>>>) this.config).removeListener(configListener);
((IEventSource<EntityPropertyChangedEvent<IConfig<?>>>) config).removeListener(configListener);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment