org.bidib.wizard.model.dmx.DmxDimmer Maven / Gradle / Ivy
package org.bidib.wizard.model.dmx;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jgoodies.binding.beans.Model;
public class DmxDimmer extends Model {
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = LoggerFactory.getLogger(DmxDimmer.class);
public static final String PROPERTY_NAME = "name";
public static final String PROPERTY_DMX_CHANNELS = "dmxChannels";
private final String id;
private String name;
private final List dmxChannels = new ArrayList<>();
/**
* Creates a new instance of DmxDimmer with the provided dimmer id.
*
* @param id
* the dimmer id
*/
public DmxDimmer(final String id) {
this.id = id;
}
public DmxDimmer() {
this(UUID.randomUUID().toString());
}
/**
* @return the scenery id
*/
public String getId() {
return id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
String oldValue = this.name;
this.name = name;
firePropertyChange(PROPERTY_NAME, oldValue, name);
}
public List getDmxChannels() {
return dmxChannels;
}
public void setDmxChannels(final List dmxChannels) {
List oldValue = new ArrayList<>(this.dmxChannels);
LOGGER.info("Set dmxChannels: {}", dmxChannels);
this.dmxChannels.clear();
this.dmxChannels.addAll(dmxChannels);
firePropertyChange(PROPERTY_DMX_CHANNELS, oldValue, Collections.unmodifiableList(this.dmxChannels));
}
public void addDmxChannel(final DmxChannel dmxChannel) {
List oldValue = new ArrayList<>(this.dmxChannels);
this.dmxChannels.add(dmxChannel);
firePropertyChange(PROPERTY_DMX_CHANNELS, oldValue, Collections.unmodifiableList(this.dmxChannels));
}
public void removeDmxChannel(final DmxChannel dmxChannel) {
List oldValue = new ArrayList<>(this.dmxChannels);
this.dmxChannels.remove(dmxChannel);
firePropertyChange(PROPERTY_DMX_CHANNELS, oldValue, Collections.unmodifiableList(this.dmxChannels));
}
@Override
public String toString() {
return "DmxDimmer[name=" + name + ",id=" + id;
}
public DmxDimmer withName(String name) {
this.setName(name);
return this;
}
public DmxDimmer withNumberOfChannels(int channelCount, final List availableChannelIds) {
int existingChannelCount = this.dmxChannels.size();
if (channelCount > existingChannelCount) {
for (int channelId = existingChannelCount; channelId < channelCount; channelId++) {
// add DMX channel
DmxChannelWrapper dmxChannelId = availableChannelIds.get(channelId - existingChannelCount);
DmxChannel dmxChannel = new DmxChannel(this, dmxChannelId);
switch (channelId) {
case 0:
dmxChannel.setLineColor(LineColors.RED.getDisplayColor());
break;
case 1:
dmxChannel.setLineColor(LineColors.GREEN.getDisplayColor());
break;
case 2:
dmxChannel.setLineColor(LineColors.BLUE.getDisplayColor());
break;
case 3:
dmxChannel.setLineColor(LineColors.WHITE.getDisplayColor());
break;
default:
dmxChannel.setLineColor(LineColors.WARMWHITE.getDisplayColor());
break;
}
LOGGER.info("Add channel: {}", dmxChannel);
this.addDmxChannel(dmxChannel);
}
}
else if (channelCount < existingChannelCount) {
// remove the channels
for (int channelId = existingChannelCount - 1; channelId >= channelCount; channelId--) {
DmxChannel dmxChannel = this.dmxChannels.get(channelId);
LOGGER.info("Remove channel: {}", dmxChannel);
removeDmxChannel(dmxChannel);
}
}
return this;
}
}