package de.FeatureModellingTool.Pattern.UI;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import de.FeatureModellingTool.FeatureModel.Feature;
import de.FeatureModellingTool.FeatureModel.FeatureRelation;
/*
* ��ģ�����ڼ�����浥Ԫ�Զ��Ų�
*
*/
public class AutoLayout {
public class FeatureLayout {
Feature feature;
Feature parentFeature = null;
public int x = 0;
public int y = 0;
public int width;
public int height;
}
private List<Feature> rootFeatures = new ArrayList<Feature>();
private Hashtable<String , FeatureLayout> featureLayouts = new Hashtable<String , FeatureLayout>();
public void addFeature(Feature feature , int width , int height) {
FeatureLayout layout = new FeatureLayout();
layout.feature = feature;
layout.width = width;
layout.height = height;
this.featureLayouts.put(feature.getID() , layout);
}
public void caculateLayout() {
int[] xoffset = new int [128];
for (Iterator<FeatureLayout> itFeatureLayout = this.featureLayouts.values().iterator() ; itFeatureLayout.hasNext() ; ) {
Feature feature = itFeatureLayout.next().feature;
if (feature.getAllRelatedRelation()!=null)
for (Iterator<FeatureRelation> itFeatureRelation = (Iterator<FeatureRelation>)feature.getAllRelatedRelation().iterator() ; itFeatureRelation.hasNext() ; ) {
FeatureRelation relation = itFeatureRelation.next();
if (de.FeatureModellingTool.Pattern.ConstantDefinition.isStructRelation(relation)
&& relation.getStartFeature().getID().equals(feature.getID()))
this.featureLayouts.get(relation.getEndFeature().getID()).parentFeature = feature;
}
}
int rowIndex = 0;
for (Iterator<FeatureLayout> itFeatureLayout = this.featureLayouts.values().iterator() ; itFeatureLayout.hasNext() ; ) {
FeatureLayout featureLayout = itFeatureLayout.next();
if (featureLayout.parentFeature == null) {
for (int i=0 ; i<128 ; i++)
xoffset[i] = 0;
rowIndex = caculateLayout(featureLayout , xoffset , rowIndex , 10 , 70) + 1;
}
}
}
private int caculateLayout(FeatureLayout featureLayout , int[] xoffset , int rowIndex , int widthStep , int heightStep) {
int result = rowIndex;
int tmp = rowIndex;
boolean hasChild = false;
int minx = Integer.MAX_VALUE;
int maxx = 0;
if (featureLayout.feature.getAllRelatedRelation()!=null)
for (Iterator<FeatureRelation> itFeatureRelation = (Iterator<FeatureRelation>)featureLayout.feature.getAllRelatedRelation().iterator() ; itFeatureRelation.hasNext() ; ) {
FeatureRelation relation = itFeatureRelation.next();
if (de.FeatureModellingTool.Pattern.ConstantDefinition.isStructRelation(relation)
&& featureLayout.feature.getID().equals(relation.getStartFeature().getID())) {
hasChild = true;
FeatureLayout childLayout = this.featureLayouts.get(relation.getEndFeature().getID());
tmp = caculateLayout(childLayout , xoffset , rowIndex + 1 , widthStep , heightStep);
if (tmp>result)
result = tmp;
minx = minx>childLayout.x ? childLayout.x : minx;
maxx = childLayout.x+childLayout.width;
}
}
if (hasChild) {
featureLayout.x = minx + (maxx - minx - featureLayout.width)/2;
if (featureLayout.x < xoffset[rowIndex])
featureLayout.x = xoffset[rowIndex];
} else {
featureLayout.x = xoffset[rowIndex];
}
xoffset[rowIndex] = featureLayout.x + featureLayout.width + widthStep;
featureLayout.y = rowIndex * heightStep;
return result;
}
public FeatureLayout getFeatureLayout(Feature feature) {
return this.featureLayouts.get(feature.getID());
}
}