package de.FeatureModellingTool.Customize;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import de.FeatureModellingTool.FeatureModel.BindingTime;
import de.FeatureModellingTool.FeatureModel.Feature;
import de.FeatureModellingTool.FeatureModel.FeatureModel;
import de.FeatureModellingTool.FeatureModel.Variability;
public class CustomizationModelImplementation implements CustomizationModel {
public CustomizationModelImplementation(FeatureModel featureModel) {
this.featureModel = featureModel;
this.propertyChangeSupport = new PropertyChangeSupport(this);
}
FeatureModel featureModel = null;
// ��������ͨ����Ӽ����������ֶ���ģ���з����ı仯
private PropertyChangeSupport propertyChangeSupport = null;
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
}
public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(propertyName, listener);
}
// �Զ���ģ���е�ǰѡ���Ķ��ư汾�IJ���
private CustomizationVersion currentCustomizationVersion = null;
public CustomizationVersion getCurrentCustomizationVersion() {
return this.currentCustomizationVersion;
}
public void setCurrentCustomizationVersionId(String customizationVersionId) {
CustomizationVersion newCustomizationVersion = this.customizationVersions.get(customizationVersionId);
if (this.currentCustomizationVersion != newCustomizationVersion) {
CustomizationVersion oldCustomizationVersion = this.currentCustomizationVersion;
this.currentCustomizationVersion = newCustomizationVersion;
this.propertyChangeSupport.firePropertyChange(ConstantDefinition.PROPERTYCHANGE_CURRENTCUSTOMIZATIONVERSION , oldCustomizationVersion, newCustomizationVersion);
}
}
// �������еĶ��ư汾
private HashMap<String, CustomizationVersion> customizationVersions = new HashMap<String, CustomizationVersion>();
public CustomizationVersion newCustomizationVersion(String parentId , String id , String name , String bindingTimeId) {
CustomizationVersion result = new CustomizationVersionImplementation(id);
result.setName(name);
result.setParentId(parentId);
result.setBindingTimeId(bindingTimeId);
this.customizationVersions.put(id , result);
return result;
}
public boolean removeCustomizationVersion(String id) {
CustomizationVersion target = this.customizationVersions.get(id);
if (target == null || target.getChildVersionIds().size() > 0) {
return false;
}
this.customizationVersions.remove(id);
if (this.currentCustomizationVersion == target) {
this.currentCustomizationVersion = null;
this.propertyChangeSupport.firePropertyChange(ConstantDefinition.PROPERTYCHANGE_CURRENTCUSTOMIZATIONVERSION , target , this.currentCustomizationVersion);
}
return true;
}
public CustomizationVersion getCustomizationVersion(String id) {
return this.customizationVersions.get(id);
}
public Collection<String> getCustomizationVersionIds() {
return this.customizationVersions.keySet();
}
public Collection<CustomizationVersion> getRootCustomizationVersions() {
Collection<CustomizationVersion> result = new LinkedList<CustomizationVersion>();
for (Iterator<CustomizationVersion> itCV = this.customizationVersions.values().iterator() ; itCV.hasNext() ; ) {
CustomizationVersion cv = itCV.next();
if (cv.getParent() == null) {
result.add(cv);
}
}
return result;
}
public class CustomizationVersionImplementation implements
CustomizationVersion {
public CustomizationVersionImplementation(String id) {
this.id = id;
}
private String id = null;
public String getId() {
return this.id;
}
private String name = null;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
private String parentId = null;
public void setParentId(String parentId) {
this.parentId = parentId;
}
public CustomizationVersion getParent() {
return customizationVersions.get(this.parentId);
}
private String bindingTimeId = null;
public void setBindingTimeId(String bindingTimeId) {
this.bindingTimeId = bindingTimeId;
if (this == currentCustomizationVersion) {
propertyChangeSupport.firePropertyChange(ConstantDefinition.PROPERTYCHANGE_CURRENTCUSTOMIZATIONVERSION , null , currentCustomizationVersion);
}
}
public BindingTime getBindingTime() {
if (this.bindingTimeId == null) {
return BindingTime.Null;
}
BindingTime result = BindingTime.getInstance(this.bindingTimeId);
if (result == null) {
return BindingTime.Null;
}
return result;
}
public Collection<String> getChildVersionIds() {
Collection<String> result = new LinkedList<String>();
for (Iterator<CustomizationVersion> itVersion = customizationVersions
.values().iterator(); itVersion.hasNext();) {
CustomizationVersion cv = itVersion.next();
if (cv.getParent() == this) {
result.add(cv.getId());
}
}
return result;
}
public CustomizationVersion getChildVersionById(String id) {
return customizationVersions.get(id);
}
private Map<String, Customization> customizationMap = new HashMap<String, Customization>();
public Collection<String> getCustomizationIds() {
return this.customizationMap.keySet();
}
public Customization getCustomizationById(String id) {
Customization parentResult = Customization.Undecided;
if (this.getParent() != null) {
parentResult = this.getParent().getCustomizationById(id);
}
Customization result = this.customizationMap.get(id);
if (result == null) {
result = Customization.Undecided;
}
return result.compareTo(parentResult) > 0 ? result : parentResult;
}
public void setCustomization(String featureId,
Customization customization) {
if (!this.isFeatureCustomizable(featureId)) {
return;
}
if (this.customizationMap.containsKey(featureId)) {
this.customizationMap.remove(featureId);
}
this.customizationMap.put(featureId, customization);
}
public boolean isFeatureCustomizedByParentVersion(String featureId) {
if (this.getParent() != null) {
return !Customization.Undecided_S.equals(this.getParent().getCustomizationById(featureId).getName());
}
return false;
}
public boolean isFeatureCustomizable(String featureId) {
if (this.isFeatureCustomizedByParentVersion(featureId)) {
return false;
}
if (!featureModel.containsFeature(featureId)) {
return false;
}
Feature feature = featureModel.getFeature(featureId);
if (!Variability.Optional.getName().equals(feature.getVariability().getName())) {
return false;
}
if (feature.getBindingTime().getBindingIndex() < this.getBindingTime().getBindingIndex()) {
return false;
}
return true;
}
public Customization getFinalCustomizationById(String id) {
Feature feature = featureModel.getFeature(id);
if (feature==null) {
return Customization.Selected;
}
Customization result = this.getCustomizationById(id);
if (feature.getVariability().equals(Variability.Mandatory)) {
result = Customization.Selected;
}
Feature parent = featureModel.getParentFeature(id);
if (parent==null) {
return result;
}
Customization parentCustomization = this.getFinalCustomizationById(parent.getID());
if (result.equals(Customization.Selected)) {
return parentCustomization;
} else if (result.equals(Customization.Unselected)) {
return Customization.Unselected;
} else {
if (parentCustomization.equals(Customization.Unselected)) {
return Customization.Unselected;
} else {
return Customization.Undecided;
}
}
}
}
}