/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.dicom.model;
import clips.dicom.Image.DICOMImage;
import clips.dicom.dicombaseclass.DICOMChekup;
import clips.dicom.dicombaseclass.DICOMDeviceParam;
import clips.dicom.dicomdataimpl.DICOMDataDump;
import clips.dicom.waveform.DICOMWaveformGroup;
import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
/**
*
* @author finder.
*/
public class AgregatorItem implements ChangeListener {
public static enum ItemsType{
waveform,
image;
public static ItemsType indefine(DICOMChekup chekup){
if (chekup instanceof DICOMWaveformGroup) {
return waveform;
}
else if (chekup instanceof DICOMImage) {
return image;
}
else {
throw new InvalidParameterException("Обект не является обектом дикома.");
}
}
}
protected EventListenerList listenerList = new EventListenerList();
private ArrayList<DICOMChekup> items;
private final ItemsType itemsType;
private String name = null;
private String seriesUID = null;
private DicomModel model;
private boolean notifyListeners = true;
public AgregatorItem(Collection<? extends DICOMChekup> items, ItemsType itemsType) {
this.items = new ArrayList<DICOMChekup>(items);
if (items.size() > 0) {
seriesUID = items.iterator().next().getSeriesUID();
}
this.itemsType = itemsType;
}
public void addListener(ItemListener listener){
listenerList.add(ItemListener.class, listener);
}
public void removeListener(ItemListener listener){
listenerList.remove(ItemListener.class, listener);
}
public ItemsType getItemsType() {
return itemsType;
}
public String getName() {
if (name == null){
if (items.size() > 0){
DICOMDeviceParam param = items.get(0).getDeviceParam();
if (param == null){
name = items.get(0).getAcquisitionDateTime().toString();
}
else{
name = param.getName();
}
name += " (" + items.size() + ")";
}
}
return name;
}
public ArrayList<DICOMChekup> getItems(){
return new ArrayList<DICOMChekup>(items);
}
private void putSorted(DICOMChekup item){
int newID = item.getInstanceNumber();
Date newDate = item.getAcquisitionDateTime();
for (int i = 0; i < items.size(); i++) {
DICOMChekup chekup = items.get(i);
int curID = chekup.getInstanceNumber();
Date curDate = item.getAcquisitionDateTime();
if (newID != -1 && curID != newID){
if (newID < curID){
items.add(i, item);
return;
}
}
else{
if (curDate != null && newDate != null && curDate.after(newDate)) {
items.add(i, item);
return;
}
}
}
items.add(item);
}
public void addItems(Collection<? extends DICOMChekup> newitems){
for (DICOMChekup chekup : newitems) {
putSorted(chekup);
}
name = null;
if (newitems.size() > 0 && seriesUID == null) {
seriesUID = newitems.iterator().next().getSeriesUID();
}
onObjectListChange(ChangeType.addedItems);
}
public void addItemsWithCheck(Collection<? extends DICOMChekup> newitems){
ArrayList<DICOMChekup> newItm = new ArrayList<DICOMChekup>(newitems.size());
topfor: for (DICOMChekup newhekup : newitems) {
for (DICOMChekup contnent : items) {
String insanse = contnent.getSopInstanceID();
if (insanse != null && insanse.equals(newhekup.getSopInstanceID())) {
continue topfor;
}
}
newItm.add(newhekup);
}
for (DICOMChekup chekup : newItm) {
putSorted(chekup);
}
name = null;
if (newitems.size() > 0 && seriesUID == null) {
seriesUID = newitems.iterator().next().getSeriesUID();
}
onObjectListChange(ChangeType.addedItems);
}
public String getSeriesUID() {
return seriesUID;
}
public ImageModel getImageModel() {
if (itemsType != ItemsType.image) {
return null;
}
if (model == null) {
model = new ImageModel(this);
((ImageModel)model).addChangeListener(null, this);
}
return (ImageModel)model;
}
public WaveModel getWaveModel() {
if (itemsType != ItemsType.waveform) {
return null;
}
if (model == null) {
model = new WaveModel(this);
((WaveModel)model).addChangeListener(null, this);
}
return (WaveModel)model;
}
protected void onObjectListChange(ChangeType type){
if (model != null){
notifyListeners = false;
try{
if (model instanceof ImageModel){
((ImageModel)model).setImages(getItems());
}
}
finally{
notifyListeners = true;
}
}
notifyListeners(new ItemEvent(this, ChangeType.modifedItems));
}
protected void notifyListeners(ItemEvent e){
if (notifyListeners){
ItemListener[] itemListeners = listenerList.getListeners(ItemListener.class);
for (ItemListener itemListener : itemListeners) {
itemListener.itemStateChanged(e);
}
}
}
@Override
public String toString() {
return getName();
}
void clearSafeStore() throws IOException{
for (DICOMChekup chekup : items) {
chekup.getOriginalData().delete();
}
}
void saveToSafeStore(){
ArrayList<DICOMDataDump> list = new ArrayList<DICOMDataDump>();
for (DICOMChekup chekup : items) {
if (!list.contains(list)) {
list.add(chekup.getOriginalData());
}
}
for (DICOMDataDump dump : list) {
dump.soreDump();
}
}
@Override
public void stateChanged(ChangeEvent e) {
notifyListeners(new ItemEvent(this, ChangeType.modifedItems));
}
}