package de.FeatureModellingTool.Customize.ui;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import de.FeatureModellingTool.Customize.Customization;
import de.FeatureModellingTool.Customize.CustomizationVersion;
import de.FeatureModellingTool.FeatureModel.CFRModifier;
import de.FeatureModellingTool.FeatureModel.CFRelation;
import de.FeatureModellingTool.FeatureModel.CompositeConstraint;
import de.FeatureModellingTool.FeatureModel.CompositeConstraintPortType;
import de.FeatureModellingTool.FeatureModel.CompositeConstraintType;
import de.FeatureModellingTool.FeatureModel.ConstraintModel;
import de.FeatureModellingTool.FeatureModel.Feature;
import de.FeatureModellingTool.FeatureModel.FeatureRelation;
import de.FeatureModellingTool.FeatureModel.GroupConstraint;
import de.FeatureModellingTool.FeatureModel.GroupConstraintType;
import de.FeatureModellingTool.FeatureModel.VPConstraint;
import de.FeatureModellingTool.FeatureModel.VPConstraintType;
public class ConstraintExplorerCellRenderHelperManager {
private static ConstraintExplorerCellRenderHelperManager self = null;
public static ConstraintExplorerCellRenderHelperManager getInstance() {
if (self==null) {
self = new ConstraintExplorerCellRenderHelperManager();
}
return self;
}
private Hashtable<Class , ConstraintExplorerCellRenderHelper> htHelper = null;
public ConstraintExplorerCellRenderHelper getHelper(Object constraintObject) {
if (htHelper==null) {
htHelper = new Hashtable<Class , ConstraintExplorerCellRenderHelper>();
htHelper.put(GroupConstraint.class , new GroupConstraintHelper());
htHelper.put(VPConstraint.class , new VPConstraintHelper());
htHelper.put(CompositeConstraint.class , new CompositeConstraintHelper());
htHelper.put(FeatureRelation.class , new FeatureRelationHelper());
}
if (constraintObject instanceof GroupConstraint) {
return htHelper.get(GroupConstraint.class);
} else if (constraintObject instanceof VPConstraint) {
return htHelper.get(VPConstraint.class);
} else if (constraintObject instanceof CompositeConstraint) {
return htHelper.get(CompositeConstraint.class);
} else if (constraintObject instanceof FeatureRelation) {
return htHelper.get(FeatureRelation.class);
}
return null;
}
public class ConstraintExplorerCellRenderHelper {
protected int getCustomizationCount(List<Customization> cus , Customization target) {
int result = 0;
for (Iterator<Customization> itCustomization=cus.iterator() ; itCustomization.hasNext() ; ) {
Customization c = itCustomization.next();
if (c.getName().equals(target.getName())) {
result ++;
}
}
return result;
}
protected Customization convertCustomization(Customization cus , CFRelation cf) {
if (cus.getName().equals(Customization.Undecided.getName())) {
return Customization.Undecided;
} else if (cus.getName().equals(Customization.Selected.getName())) {
if (cf.getModifier().getName().equals(CFRModifier.Affirmation.getName())) {
return Customization.Selected;
} else {
return Customization.Unselected;
}
} else /*if (cus.getName().equals(Customization.Selected.getName()))*/ {
if (cf.getModifier().getName().equals(CFRModifier.Affirmation.getName())) {
return Customization.Unselected;
} else {
return Customization.Selected;
}
}
}
public boolean setLabelStat(Object constraint , ConstraintExplorerTreeNode node , JLabel label , DefaultTableCellRenderer render) {
return node!=null && node.getFeature()!=null && node.getCustomizationVersion()!=null;
}
public String getConstraintName(Object cons) {
return "";
}
public ImageIcon getConstraintIcon(Object cons , ConstraintModel constraintModel , CustomizationVersion cv) {
return null;
}
public boolean isRelatedFeature(Object cons , Feature feature) {
return false;
}
public Set<Feature> getRelatedFeatures(Object cons) {
return null;
}
}
// private static Hashtable<String , Icon> htIcon = null;
protected static Icon getNodeIcon(ConstraintExplorerTreeNode node) {
// Customization customization = node.getCustomizationVersion().getFinalCustomizationById(node.getFeature().getID());
// if (htIcon==null) {
// htIcon = new Hashtable<String , Icon>();
// htIcon.put("ci_selected" , new ImageIcon(ConstraintExplorerCellRenderHelperManager.class.getResource("image/ci_selected.png")));
// htIcon.put("ci_unselected" , new ImageIcon(ConstraintExplorerCellRenderHelperManager.class.getResource("image/ci_unselected.png")));
// htIcon.put("ci_undecided" , new ImageIcon(ConstraintExplorerCellRenderHelperManager.class.getResource("image/ci_undecided.png")));
// }
//
// String iconName = "ci_";
// if (customization.equals(Customization.Selected)) {
// iconName += "selected";
// } else if (customization.equals(Customization.Unselected)) {
// iconName += "unselected";
// } else if (customization.equals(Customization.Undecided)) {
// iconName += "undecided";
// }
//
// return htIcon.get(iconName);
Customization cFinal = node.getCustomizationVersion().getFinalCustomizationById(node.getFeature().getID());
Customization cSelf = node.getCustomizationVersion().getCustomizationById(node.getFeature().getID());
return ImageManager.getInstance().getCustomizationIcon(cFinal , cSelf , !node.getCustomizationVersion().isFeatureCustomizedByParentVersion(node.getFeature().getID()));
}
protected class GroupConstraintHelper extends ConstraintExplorerCellRenderHelper {
public boolean setLabelStat(Object constraint , ConstraintExplorerTreeNode node , JLabel label , DefaultTableCellRenderer render) {
if (!super.setLabelStat(constraint , node , label , render)) {
return false;
}
if (!(constraint instanceof GroupConstraint)) {
return false;
}
GroupConstraint gc = (GroupConstraint)constraint;
if (!gc.getFeatureSet().contains(node.getFeature())) {
return false;
}
label.setIcon(getNodeIcon(node));
render.setHorizontalAlignment(SwingConstants.CENTER);
return true;
}
public String getConstraintName(Object cons) {
if (!(cons instanceof GroupConstraint)) {
return "";
}
GroupConstraint constraint = (GroupConstraint)cons;
return constraint.getType().getName();
}
public ImageIcon getConstraintIcon(Object cons , ConstraintModel constraintModel , CustomizationVersion cv) {
if (!(cons instanceof GroupConstraint)) {
return null;
}
GroupConstraint gc = (GroupConstraint)cons;
if (cv==null) {
return ImageManager.getInstance().getGroupConstraintHeader(gc.getType() , null , ImageManager.ConstraintStat.csNone);
} else {
List<Customization> sCustomization = new ArrayList<Customization>();
for (Iterator<Feature> itFeature=gc.getFeatureSet().iterator() ; itFeature.hasNext() ; ) {
Feature feature = itFeature.next();
CFRelation cf = constraintModel.getCFRelation(feature , gc , true);
if (cf==null) {
cf = constraintModel.getCFRelation(feature , gc , false);
}
sCustomization.add(super.convertCustomization(cv.getFinalCustomizationById(feature.getID()) , cf));
}
Customization value = Customization.Undecided;
if (gc.getType().getName().equals(GroupConstraintType.SingleGroup.getName())) {
if (super.getCustomizationCount(sCustomization , Customization.Selected)>1) {
value = Customization.Unselected;
} else if (super.getCustomizationCount(sCustomization , Customization.Undecided)==0) {
value = Customization.Selected;
} else {
value = Customization.Undecided;
}
} else if (gc.getType().getName().equals(GroupConstraintType.AllGroup.getName())) {
if (super.getCustomizationCount(sCustomization , Customization.Undecided)>0) {
value = Customization.Undecided;
} else if (super.getCustomizationCount(sCustomization , Customization.Unselected)==0) {
value = Customization.Selected;
} else {
value = Customization.Unselected;
}
} else /*if (gc.getType().getName().equals(GroupConstraintType.SingleGroup))*/ {
value = Customization.Selected;
}
return ImageManager.getInstance().getGroupConstraintHeader(gc.getType() , value , ImageManager.ConstraintStat.csNone);
}
}
public boolean isRelatedFeature(Object cons , Feature feature) {
if (!(cons instanceof GroupConstraint)) {
return false;
}
GroupConstraint gc = (GroupConstraint)cons;
return gc.getFeatureSet().contains(feature);
}
public Set<Feature> getRelatedFeatures(Object cons) {
if (!(cons instanceof GroupConstraint)) {
return null;
}
GroupConstraint gc = (GroupConstraint)cons;
return gc.getFeatureSet();
}
}
protected class VPConstraintHelper extends ConstraintExplorerCellRenderHelper {
public boolean setLabelStat(Object constraint , ConstraintExplorerTreeNode node , JLabel label , DefaultTableCellRenderer render) {
if (!super.setLabelStat(constraint , node , label , render)) {
return false;
}
if (!(constraint instanceof VPConstraint)) {
return false;
}
VPConstraint vpc = (VPConstraint)constraint;
if (vpc.getSourceFeatureSet().contains(node.getFeature())) {
label.setIcon(getNodeIcon(node));
render.setHorizontalAlignment(SwingConstants.LEFT);
return true;
}
if (vpc.getSinkFeatureSet().contains(node.getFeature())) {
label.setIcon(getNodeIcon(node));
render.setHorizontalAlignment(SwingConstants.RIGHT);
return true;
}
return false;
}
public String getConstraintName(Object cons) {
return "VPConstraint";
}
public ImageIcon getConstraintIcon(Object cons , ConstraintModel constraintModel , CustomizationVersion cv) {
if (!(cons instanceof VPConstraint)) {
return null;
}
VPConstraint vp = (VPConstraint)cons;
CompositeConstraintPortType lType = null;
if (vp.getType().equals(VPConstraintType.SingleVP)) {
lType = CompositeConstraintPortType.Single;
} else if (vp.getType().equals(VPConstraintType.MultiVP)) {
lType = CompositeConstraintPortType.Multi;
} else if (vp.getType().equals(VPConstraintType.AllVP)) {
lType = CompositeConstraintPortType.All;
}
CompositeConstraintPortType rType = lType;
if (cv==null) {
return ImageManager.getInstance().getCompositeConstraintHeader(lType , rType , CompositeConstraintType.Equivalence , null , null , ImageManager.ConstraintStat.csNone);
} else {
List<Customization> sCustomization = new ArrayList<Customization>();
for (Iterator<Feature> itFeature=vp.getSourceFeatureSet().iterator() ; itFeature.hasNext() ; ) {
Feature feature = itFeature.next();
CFRelation cf = constraintModel.getCFRelation(feature , vp , true);
if (cf==null) {
cf = constraintModel.getCFRelation(feature , vp , false);
}
sCustomization.add(super.convertCustomization(cv.getFinalCustomizationById(feature.getID()) , cf));
}
Customization lValue = Customization.Undecided;
if (lType.getName().equals(CompositeConstraintPortType.Single.getName())) {
if (super.getCustomizationCount(sCustomization , Customization.Undecided)>0) {
lValue = Customization.Undecided;
} else if (super.getCustomizationCount(sCustomization , Customization.Selected)==1) {
lValue = Customization.Selected;
} else {
lValue = Customization.Unselected;
}
} else if (lType.getName().equals(CompositeConstraintPortType.All.getName())) {
if (super.getCustomizationCount(sCustomization , Customization.Undecided)>0) {
lValue = Customization.Undecided;
} else if (super.getCustomizationCount(sCustomization , Customization.Unselected)==0) {
lValue = Customization.Selected;
} else {
lValue = Customization.Unselected;
}
} else /*if (gc.getType().getName().equals(GroupConstraintType.SingleGroup))*/ {
if (super.getCustomizationCount(sCustomization , Customization.Selected)>0) {
lValue = Customization.Selected;
} else if (super.getCustomizationCount(sCustomization , Customization.Undecided)>0) {
lValue = Customization.Undecided;
} else {
lValue = Customization.Unselected;
}
}
sCustomization = new ArrayList<Customization>();
for (Iterator<Feature> itFeature=vp.getSinkFeatureSet().iterator() ; itFeature.hasNext() ; ) {
Feature feature = itFeature.next();
CFRelation cf = constraintModel.getCFRelation(feature , vp , true);
if (cf==null) {
cf = constraintModel.getCFRelation(feature , vp , false);
}
sCustomization.add(super.convertCustomization(cv.getFinalCustomizationById(feature.getID()) , cf));
}
Customization rValue = Customization.Undecided;
if (rType.getName().equals(CompositeConstraintPortType.Single.getName())) {
if (super.getCustomizationCount(sCustomization , Customization.Undecided)>0) {
rValue = Customization.Undecided;
} else if (super.getCustomizationCount(sCustomization , Customization.Selected)==1) {
rValue = Customization.Selected;
} else {
rValue = Customization.Unselected;
}
} else if (rType.getName().equals(CompositeConstraintPortType.All.getName())) {
if (super.getCustomizationCount(sCustomization , Customization.Undecided)>0) {
rValue = Customization.Undecided;
} else if (super.getCustomizationCount(sCustomization , Customization.Unselected)==0) {
rValue = Customization.Selected;
} else {
rValue = Customization.Unselected;
}
} else /*if (gc.getType().getName().equals(GroupConstraintType.SingleGroup))*/ {
if (super.getCustomizationCount(sCustomization , Customization.Selected)>0) {
rValue = Customization.Selected;
} else if (super.getCustomizationCount(sCustomization , Customization.Undecided)>0) {
rValue = Customization.Undecided;
} else {
rValue = Customization.Unselected;
}
}
return ImageManager.getInstance().getCompositeConstraintHeader(lType , rType , CompositeConstraintType.Equivalence , lValue , rValue , ImageManager.ConstraintStat.csNone);
}
}
public boolean isRelatedFeature(Object cons , Feature feature) {
if (!(cons instanceof VPConstraint)) {
return false;
}
VPConstraint vpc = (VPConstraint)cons;
return vpc.getSourceFeatureSet().contains(feature) || vpc.getSinkFeatureSet().contains(feature);
}
public Set<Feature> getRelatedFeatures(Object cons) {
if (!(cons instanceof VPConstraint)) {
return null;
}
VPConstraint vpc = (VPConstraint)cons;
Set<Feature> result = new HashSet<Feature>();
result.addAll(vpc.getSourceFeatureSet());
result.addAll(vpc.getSinkFeatureSet());
return result;
}
}
protected class CompositeConstraintHelper extends ConstraintExplorerCellRenderHelper {
public boolean setLabelStat(Object constraint , ConstraintExplorerTreeNode node , JLabel label , DefaultTableCellRenderer render) {
if (!super.setLabelStat(constraint , node , label , render)) {
return false;
}
if (!(constraint instanceof CompositeConstraint)) {
return false;
}
CompositeConstraint cc = (CompositeConstraint)constraint;
if (cc.getSourceFeatureSet().contains(node.getFeature())) {
label.setIcon(getNodeIcon(node));
render.setHorizontalAlignment(SwingConstants.LEFT);
return true;
}
if (cc.getSinkFeatureSet().contains(node.getFeature())) {
label.setIcon(getNodeIcon(node));
render.setHorizontalAlignment(SwingConstants.RIGHT);
return true;
}
return false;
}
public String getConstraintName(Object cons) {
if (!(cons instanceof FeatureRelation)) {
return "";
}
CompositeConstraint cc = (CompositeConstraint)cons;
return "Composite";
}
public ImageIcon getConstraintIcon(Object cons , ConstraintModel constraintModel , CustomizationVersion cv) {
if (!(cons instanceof CompositeConstraint)) {
return null;
}
CompositeConstraint cc = (CompositeConstraint)cons;
if (cv!=null) {
List<Customization> sCustomization = new ArrayList<Customization>();
for (Iterator<Feature> itFeature=cc.getSourceFeatureSet().iterator() ; itFeature.hasNext() ; ) {
Feature feature = itFeature.next();
CFRelation cf = constraintModel.getCFRelation(feature , cc , true);
if (cf==null) {
cf = constraintModel.getCFRelation(feature , cc , false);
}
sCustomization.add(super.convertCustomization(cv.getFinalCustomizationById(feature.getID()) , cf));
}
Customization lValue = Customization.Undecided;
if (cc.getSourceType().getName().equals(CompositeConstraintPortType.Single.getName())) {
if (super.getCustomizationCount(sCustomization , Customization.Undecided)>0) {
lValue = Customization.Undecided;
} else if (super.getCustomizationCount(sCustomization , Customization.Selected)==1) {
lValue = Customization.Selected;
} else {
lValue = Customization.Unselected;
}
} else if (cc.getSourceType().getName().equals(CompositeConstraintPortType.All.getName())) {
if (super.getCustomizationCount(sCustomization , Customization.Undecided)>0) {
lValue = Customization.Undecided;
} else if (super.getCustomizationCount(sCustomization , Customization.Unselected)==0) {
lValue = Customization.Selected;
} else {
lValue = Customization.Unselected;
}
} else /*if (gc.getType().getName().equals(GroupConstraintType.SingleGroup))*/ {
if (super.getCustomizationCount(sCustomization , Customization.Selected)>0) {
lValue = Customization.Selected;
} else if (super.getCustomizationCount(sCustomization , Customization.Undecided)>0) {
lValue = Customization.Undecided;
} else {
lValue = Customization.Unselected;
}
}
sCustomization = new ArrayList<Customization>();
for (Iterator<Feature> itFeature=cc.getSinkFeatureSet().iterator() ; itFeature.hasNext() ; ) {
Feature feature = itFeature.next();
CFRelation cf = constraintModel.getCFRelation(feature , cc , true);
if (cf==null) {
cf = constraintModel.getCFRelation(feature , cc , false);
}
sCustomization.add(super.convertCustomization(cv.getFinalCustomizationById(feature.getID()) , cf));
}
Customization rValue = Customization.Undecided;
if (cc.getSinkType().getName().equals(CompositeConstraintPortType.Single.getName())) {
if (super.getCustomizationCount(sCustomization , Customization.Undecided)>0) {
rValue = Customization.Undecided;
} else if (super.getCustomizationCount(sCustomization , Customization.Selected)==1) {
rValue = Customization.Selected;
} else {
rValue = Customization.Unselected;
}
} else if (cc.getSinkType().getName().equals(CompositeConstraintPortType.All.getName())) {
if (super.getCustomizationCount(sCustomization , Customization.Undecided)>0) {
rValue = Customization.Undecided;
} else if (super.getCustomizationCount(sCustomization , Customization.Unselected)==0) {
rValue = Customization.Selected;
} else {
rValue = Customization.Unselected;
}
} else /*if (gc.getType().getName().equals(GroupConstraintType.SingleGroup))*/ {
if (super.getCustomizationCount(sCustomization , Customization.Selected)>0) {
rValue = Customization.Selected;
} else if (super.getCustomizationCount(sCustomization , Customization.Undecided)>0) {
rValue = Customization.Undecided;
} else {
rValue = Customization.Unselected;
}
}
return ImageManager.getInstance().getCompositeConstraintHeader(cc.getSourceType() , cc.getSinkType() , cc.getPLType() , lValue , rValue , ImageManager.ConstraintStat.csNone);
}
else
{
return ImageManager.getInstance().getCompositeConstraintHeader(cc.getSourceType() , cc.getSinkType() , cc.getPLType() , null , null , ImageManager.ConstraintStat.csNone);
}
}
public boolean isRelatedFeature(Object cons , Feature feature) {
if (!(cons instanceof CompositeConstraint)) {
return false;
}
CompositeConstraint cc = (CompositeConstraint)cons;
return cc.getSourceFeatureSet().contains(feature) || cc.getSinkFeatureSet().contains(feature);
}
public Set<Feature> getRelatedFeatures(Object cons) {
if (!(cons instanceof CompositeConstraint)) {
return null;
}
CompositeConstraint cc = (CompositeConstraint)cons;
Set<Feature> result = new HashSet<Feature>();
result.addAll(cc.getSourceFeatureSet());
result.addAll(cc.getSinkFeatureSet());
return result;
}
}
protected class FeatureRelationHelper extends ConstraintExplorerCellRenderHelper {
public boolean setLabelStat(Object constraint , ConstraintExplorerTreeNode node , JLabel label , DefaultTableCellRenderer render) {
if (!super.setLabelStat(constraint , node , label , render)) {
return false;
}
if (!(constraint instanceof FeatureRelation)) {
return false;
}
FeatureRelation fr = (FeatureRelation)constraint;
if (fr.getStartFeature().getID().equals(node.getFeature().getID())) {
label.setIcon(getNodeIcon(node));
render.setHorizontalAlignment(SwingConstants.LEFT);
return true;
}
if (fr.getEndFeature().getID().equals(node.getFeature().getID())) {
label.setIcon(getNodeIcon(node));
render.setHorizontalAlignment(SwingConstants.RIGHT);
return true;
}
return false;
}
public String getConstraintName(Object cons) {
if (!(cons instanceof FeatureRelation)) {
return "";
}
FeatureRelation relation = (FeatureRelation)cons;
if (relation.getName().equals(FeatureRelation.REQUIRE)) {
return "-->";
}
if (relation.getName().equals(FeatureRelation.EXCLUDE)) {
return "-x-";
}
return "";
}
public ImageIcon getConstraintIcon(Object cons , ConstraintModel constraintModel , CustomizationVersion cv) {
if (!(cons instanceof FeatureRelation)) {
return null;
}
FeatureRelation relation = (FeatureRelation)cons;
if (cv==null) {
return ImageManager.getInstance().getRelationHeader(relation.getName() , null , null , ImageManager.ConstraintStat.csNone);
} else {
return ImageManager.getInstance().getRelationHeader(relation.getName() , cv.getFinalCustomizationById(relation.getStartFeature().getID()) , cv.getFinalCustomizationById(relation.getEndFeature().getID()) , ImageManager.ConstraintStat.csNone);
}
}
public boolean isRelatedFeature(Object cons , Feature feature) {
if (!(cons instanceof FeatureRelation)) {
return false;
}
FeatureRelation fr = (FeatureRelation)cons;
return fr.getStartFeature().getID().equals(feature.getID()) || fr.getEndFeature().getID().equals(feature.getID());
}
public Set<Feature> getRelatedFeatures(Object cons) {
if (!(cons instanceof FeatureRelation)) {
return null;
}
FeatureRelation fr = (FeatureRelation)cons;
Set<Feature> result = new HashSet<Feature>();
result.add(fr.getStartFeature());
result.add(fr.getEndFeature());
return result;
}
}
}