package de.FeatureModellingTool.GraphicalEditor;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import research.figure.TextFigure;
import research.store.StorableInput;
import de.FeatureModellingTool.Customize.Customization;
import de.FeatureModellingTool.FeatureModel.BindingTime;
import de.FeatureModellingTool.FeatureModel.FeatureProperties;
import de.FeatureModellingTool.FeatureModel.Variability;
/**
* author: zhangwei
* Date: 2003-5-28
* Time: 19:40:08
*/
public class FeatureFigure extends TextFigure {
public final static String IS_A_LINK = "isALink";
public final static int MIN_WIDTH = 62;
protected int titleHeight = 0;
protected int handleWidth = 0;
protected final int LinkRightInsets = 5;
protected transient int[] xPoints = new int[6];
protected transient int[] yPoints = new int[6];
protected transient boolean valid = false;
public FeatureFigure() {
super();
setAttribute("fillColor", new Color(153, 204, 255, 0x88));
setAttribute("frameColor", Color.BLACK);
setAttribute("type", "feature");
setAttribute(TEXT, "Feature");
titleHeight = textDisplayBox().height / 2;
setAttribute("minWidth", MIN_WIDTH);
Insets insets = (Insets) getAttribute("insets");
insets.top += titleHeight;
initInerListeners();
}
protected void initInerListeners() {
this.addAttributeChangeListener("font", new FontPropertyChangeListener());
//gh start
this.addAttributeChangeListener(ConstantDefinition.CURRENT_CUSTOMIZATION, new CustomizationPropertyChangeListener());
//gh end
}
protected class FontPropertyChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
if ("font".equals(e.getPropertyName())) {
int newTitleHeight = (FeatureFigure.super.textDisplayBox().height) / 3;
int deltaHeight = newTitleHeight - titleHeight;
if (deltaHeight != 0) {
titleHeight = newTitleHeight;
willChange();
Insets insets = (Insets) getAttribute("insets");
insets.top += deltaHeight;
changed();
}
}
}
}
//gh start
protected class CustomizationPropertyChangeListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
if (ConstantDefinition.CURRENT_CUSTOMIZATION.equals(e.getPropertyName())) {
willChange();
changed();
}
}
}
//gh end
@Override
public void drawBackground(Graphics g) {
boolean isALink = isALink();
Rectangle r = getDisplayBox();
if (!isALink) {
g.fillRect(r.x, r.y, r.width, r.height);
} else {
int gap = titleHeight;
if (r.width < titleHeight) {
gap = r.width;
}
xPoints[0] = r.x;
yPoints[0] = r.y;
xPoints[1] = r.x + r.width - gap;
yPoints[1] = r.y;
xPoints[2] = r.x + r.width;
yPoints[2] = r.y + titleHeight;
xPoints[3] = r.x + r.width;
yPoints[3] = r.y + r.height;
xPoints[4] = r.x;
yPoints[4] = r.y + r.height;
xPoints[5] = r.x;
yPoints[5] = r.y;
valid = true;
g.fillPolygon(xPoints, yPoints, 6);
}
}
@Override
public void drawFrame(Graphics g) {
Rectangle r = getDisplayBox();
boolean isALink = isALink();
g.drawRect(r.x, r.y, r.width, r.height);
g.drawLine(r.x, r.y + titleHeight, r.x + r.width, r.y + titleHeight);
String fv = (String) getAttribute(FeatureProperties.VARIABILITY);
if ((fv != null) && (fv.equals(Variability.Optional.getName()))) {
if (r.width < titleHeight) {
g.drawOval(r.x, r.y, r.width, titleHeight);
} else {
g.drawOval(r.x + (r.width - titleHeight) / 2, r.y, titleHeight, titleHeight);
}
String fb = (String) getAttribute(FeatureProperties.BINDINGTIME);
if (fb != null) {
if (fb.equals(BindingTime.RunTime.getName())) {
g.drawLine(r.x + (r.width + titleHeight) / 2, r.y + titleHeight / 2, r.x + (r.width + titleHeight) / 2 + titleHeight, r.y + titleHeight / 2);
g.drawPolyline(
new int[]{r.x + r.width / 2 + titleHeight, r.x + r.width / 2 + titleHeight * 3 / 2, r.x + r.width / 2 + titleHeight},
new int[]{r.y + 1, r.y + titleHeight / 2, r.y + titleHeight - 1},
3);
} else if (!fb.equals(BindingTime.Null.getName())) {
g.drawLine(r.x + (r.width - titleHeight) / 2 - titleHeight, r.y + titleHeight / 2, r.x + (r.width - titleHeight) / 2, r.y + titleHeight / 2);
g.drawPolyline(
new int[]{r.x + r.width / 2 - titleHeight, r.x + r.width / 2 - titleHeight * 3 / 2, r.x + r.width / 2 - titleHeight},
new int[]{r.y + 1, r.y + titleHeight / 2, r.y + titleHeight - 1},
3);
}
}
//gh start
Customization ct = (Customization) getAttribute(ConstantDefinition.CURRENT_CUSTOMIZATION);
if (ct != null) {
int x = 0;
int y = 0;
int width = 0;
int height = 0;
if (r.width < titleHeight) {
x = r.x + 1;
y = r.y + 1;
width = r.width - 1;
height = titleHeight - 1;
} else {
x = r.x + (r.width - titleHeight) / 2 + 1;
y = r.y + 1;
width = titleHeight - 1;
height = titleHeight - 1;
}
Boolean customizable = (Boolean) getAttribute(ConstantDefinition.CURRENT_CUSTOMIZATION_CUSTOMIZABLE);
if (customizable != null && !customizable.booleanValue()) {
g.setColor(Color.GRAY);
g.fillOval(x, y, width, height);
}
Color oldColor = g.getColor();
g.setColor(Color.BLACK);
if (Customization.Selected.getName().equals(ct.getName())) {
g.drawLine(x, y + height / 2, x + width, y + height / 2);
g.drawLine(x + width / 2, y, x + width / 2, y + height);
}
if (Customization.Unselected.getName().equals(ct.getName())) {
g.drawLine(x, y + height / 2, x + width, y + height / 2);
}
g.setColor(oldColor);
}
//gh end
}
if (isALink) {
int gap = titleHeight;
if (r.width < titleHeight) {
gap = r.width;
}
g.drawLine(r.x + r.width - gap, r.y,
r.x + r.width - gap, r.y + titleHeight);
g.drawLine(r.x + r.width - gap, r.y,
r.x + r.width, r.y + titleHeight);
}
}
protected boolean isALink() {
Boolean isALinkObject = (Boolean) getAttribute(IS_A_LINK);
boolean isALink = false;
if ((isALinkObject != null) && isALinkObject) {
isALink = true;
}
return isALink;
}
@Override
public Object clone() {
FeatureFigure rv = (FeatureFigure) super.clone();
rv.initInerListeners();
return rv;
}
@Override
public void read(StorableInput dr) throws IOException {
super.read(dr);
setAttribute("minWidth", MIN_WIDTH);
}
}