package de.FeatureModellingTool.GraphicalEditor;
import research.tool.ConnectionTool;
import research.tool.Tool;
import research.Figure;
import research.Drawing;
import research.ConnectionFigure;
import research.FigureHelper;
import research.connector.Connector;
import de.FeatureModellingTool.FeatureModel.*;
import de.reuse.GroupMap;
import java.beans.PropertyChangeEvent;
import java.awt.event.MouseEvent;
import java.awt.*;
import research.util.Geom;
import javax.swing.*;
/**
* Created by IntelliJ IDEA.
* User: saturn
* Date: 2003-10-2
* Time: 22:54:56
* To change this template use Options | File Templates.
*/
class PLConnectionCreationTool extends ConnectionTool implements ConstantDefinition {
private FeatureModel featureModel = null;
private ConstraintModel constraintModel = null;
private ConstraintModelEditor constraintModelEditor = null;
private ModelValidation modelValidation = null;
private GroupMap idToFigureMap = null;
protected void contextChange(PropertyChangeEvent e) {
super.contextChange(e);
String propertyName = e.getPropertyName();
if (FEATURE_MODEL.equals(propertyName)) {
end();
featureModel = (FeatureModel) e.getNewValue();
} else if (CONSTRAINT_MODEL.equals(propertyName)) {
end();
constraintModel = (ConstraintModel) e.getNewValue();
} else if (CONSTRAINT_MODEL_EDITOR.equals(propertyName)) {
end();
constraintModelEditor = (ConstraintModelEditor) e.getNewValue();
} else if (ID_TO_FIGURE_MAP.equals(propertyName)) {
idToFigureMap = (GroupMap) e.getNewValue();
} else if (GraphicalEditor.MODEL_VALIDATION.equals(propertyName)) {
modelValidation = (ModelValidation)e.getNewValue();
}
}
public boolean isEnabled() {
if (!super.isEnabled()) return false;
if (featureModel == null) return false;
if (constraintModel == null) return false;
if (constraintModelEditor == null) return false;
return true;
}
public void mouseDown(MouseEvent e, int x, int y) {
if (!isEnabled()) return;
double scale = drawingView.getScale();
x = (int) (x / scale + 0.5);
y = (int) (y / scale + 0.5);
//�����������������ͼԪΪTargetFigure
setTargetFigure(findConnectionStart(x, y, drawingView.getDrawing()));
if (getTargetFigure() != null) {
setStartConnector(findConnector(x, y, getTargetFigure()));
if (getStartConnector() != null) {
consumeFigure();
setConnection((ConnectionFigure) createdFigure);
getConnection().startPoint(x, y);
getConnection().endPoint(x + 5, y + 5);
setAddedFigure(drawingView.add(getConnection()));
}
}
}
public void mouseDrag(MouseEvent e, int x, int y) {
double scale = drawingView.getScale();
x = (int) (x / scale + 0.5);
y = (int) (y / scale + 0.5);
Point p = new Point(x, y);
if (getConnection() != null) {
//�ı䵱ǰ��괦������ͼԪ���ӵ�Ŀɼ����ԣ����ѵ�ǰ���ӵ�ͨ��setTargetConnector()��������
trackConnectors(e, x, y);
//������ͼԪ��endPoint����Ϊ��ǰ���ӵ����������
if (getTargetConnector() != null) {
Rectangle realR = getTargetConnector().getDisplayBox();
p = Geom.center(realR);
}
getConnection().endPoint(p.x, p.y);
}
}
public void mouseUp(MouseEvent e, int x, int y) {
double scale = drawingView.getScale();
x = (int) (x / scale + 0.5);
y = (int) (y / scale + 0.5);
Figure c = null;
if (getTargetFigure() != null) { //���ص�ǰ����ͼԪ�����ӵ�Ŀɼ���
FigureHelper.setConnectorVisible(getTargetFigure(), false);
}
if (getStartConnector() != null) {
c = findTarget(x, y, drawingView.getDrawing());
}
if (c != null) {//����������ͼԪ��Ϊ��
setEndConnector(findConnector(x, y, c));
boolean bIsSource = true;
String constraintId = (String) getStartConnector().owner().getAttribute("id");
String featureId = (String) getEndConnector().owner().getAttribute("id");
int nDirection = getStartConnector().getDirection();
if (nDirection == Connector.EAST_DIRECTION || nDirection == Connector.SOUTH_DIRECTION)
bIsSource = false;
if ((getEndConnector() != null) && !hasExistedBetweenFigures() &&
modelValidation.canConnect(featureId, constraintId, bIsSource)) {//�����������Ӳ�Ϊ�գ���ͼԪ֮��û����ͬ�Ĺ�ϵ����
//ȷ�����ͼԪ
getConnection().connectStart(getStartConnector());
getConnection().connectEnd(getEndConnector());
//��������ͼԪ����״
int sDirect = getStartConnector().getDirection();
int eDirect = getEndConnector().getDirection();
if ((sDirect == Connector.WEST_DIRECTION || sDirect == Connector.EAST_DIRECTION)
&& (eDirect == Connector.WEST_DIRECTION || eDirect == Connector.EAST_DIRECTION)) {
getConnection().setAttribute(PLConnection.DISPLAY_MODE, PLConnection.V_H_MODE);
} else if ((sDirect == Connector.WEST_DIRECTION || sDirect == Connector.EAST_DIRECTION)
&& (eDirect == Connector.NORTH_DIRECTION || eDirect == Connector.SOUTH_DIRECTION)) {
getConnection().setAttribute(PLConnection.DISPLAY_MODE, PLConnection.V_V_MODE);
} else if ((sDirect == Connector.NORTH_DIRECTION || sDirect == Connector.SOUTH_DIRECTION)
&& (eDirect == Connector.WEST_DIRECTION || eDirect == Connector.EAST_DIRECTION)) {
getConnection().setAttribute(PLConnection.DISPLAY_MODE, PLConnection.H_H_MODE);
} else if ((sDirect == Connector.NORTH_DIRECTION || sDirect == Connector.SOUTH_DIRECTION)
&& (eDirect == Connector.NORTH_DIRECTION || eDirect == Connector.SOUTH_DIRECTION)) {
getConnection().setAttribute(PLConnection.DISPLAY_MODE, PLConnection.H_V_MODE);
}
getConnection().updateConnection();
//��ģ����Ϣ
ConstraintFigure cf = (ConstraintFigure) getStartConnector().owner();
CFRelation plRelation = null;
String modifierName = (String) getConnection().getAttribute(PLConnection.PLModifier);
CFRModifier modifier = null;
if (modifierName == null) {
modifier = CFRModifier.Affirmation;
} else {
modifier = CFRModifier.getInstance(modifierName);
}
if (cf instanceof PLFigure) {
PLFigure plf = (PLFigure) cf;
int direction = getStartConnector().getDirection();
String startFID = (String) plf.getAttribute("id");
String endFID = (String) getEndConnector().owner().getAttribute("id");
CompositeConstraint plc = constraintModel.getCompositeConstraint(startFID);
Feature feature = featureModel.getFeature(endFID);
if (direction == Connector.WEST_DIRECTION || direction == Connector.NORTH_DIRECTION) {
plRelation = constraintModelEditor.addCFRelation(feature, plc, true, modifier);
} else if (direction == Connector.EAST_DIRECTION || direction == Connector.SOUTH_DIRECTION) {
plRelation = constraintModelEditor.addCFRelation(feature, plc, false, modifier);
} else {
assert false;
}
} else if (cf instanceof GroupConstraintFigure) {
GroupConstraintFigure gcf = (GroupConstraintFigure) cf;
Integer pp = (Integer) gcf.getAttribute(GroupConstraintFigure.PortPosition);
int value = pp.intValue();
boolean isSource = false;
switch (value) {
case SwingConstants.NORTH:
case SwingConstants.WEST:
isSource = true;
break;
case SwingConstants.EAST:
case SwingConstants.SOUTH:
isSource = false;
break;
}
String startFID = (String) gcf.getAttribute("id");
String endFID = (String) getEndConnector().owner().getAttribute("id");
GroupConstraint gc = constraintModel.getGroupConstraint(startFID);
Feature feature = featureModel.getFeature(endFID);
plRelation = constraintModelEditor.addCFRelation(feature, gc, isSource, modifier);
} else if (cf instanceof VPConstraintFigure) {
VPConstraintFigure vpcf = (VPConstraintFigure) cf;
int direction = getStartConnector().getDirection();
String startFID = (String) vpcf.getAttribute("id");
String endFID = (String) getEndConnector().owner().getAttribute("id");
VPConstraint vpc = constraintModel.getVPConstraint(startFID);
Feature feature = featureModel.getFeature(endFID);
if (direction == Connector.WEST_DIRECTION || direction == Connector.NORTH_DIRECTION) {
plRelation = constraintModelEditor.addCFRelation(feature, vpc, true, modifier);
} else if (direction == Connector.EAST_DIRECTION || direction == Connector.SOUTH_DIRECTION) {
plRelation = constraintModelEditor.addCFRelation(feature, vpc, false, modifier);
} else {
assert false;
}
} else {
assert false;
}
if (plRelation != null) {
getConnection().setAttribute("id", plRelation.getID());
//����id��figure��ӳ���
if (idToFigureMap != null) {
idToFigureMap.add(plRelation.getID(), getConnection());
}
}
} else if (getConnection() != null) {
//ɾ��ͼԪ
drawingView.remove(getConnection());
restoreConsumedFigure();
}
} else if (getConnection() != null) {
//ɾ��ͼԪ
drawingView.remove(getConnection());
restoreConsumedFigure();
}
setConnection(null);
setStartConnector(null);
setEndConnector(null);
setAddedFigure(null);
getPropertyChangeSupport().firePropertyChange(Tool.TOOL_DONE, false, true);
}
protected boolean hasExistedBetweenFigures() {
ConstraintFigure cf = (ConstraintFigure) getStartConnector().owner();
String startFID = (String) cf.getAttribute("id");
String endFID = (String) getEndConnector().owner().getAttribute("id");
Feature feature = featureModel.getFeature(endFID);
Constraint c = constraintModel.getConstraint(startFID);
return constraintModel.containsCFRelation(feature, c);
}
/**
* ����x,y���괦��ConstraintFigure
*/
protected Figure findConnectionStart(int x, int y, Drawing drawing) {
Figure target = findConnectableFigure(x, y, drawing);
if (ConstraintFigure.class.isInstance(target)) {
return target;
} else {
return null;
}
}
/**
//������ƶ�������ͼԪ(����)�Ϸ�ʱ,��ʾ����ͼԪ�����ӵ�
protected void trackConnectors(MouseEvent e, int x, int y) {
Figure c = null;
if (getStartConnector() == null) {
c = findConnectionStart(x, y, drawingView.getDrawing());
} else {
c = findTarget(x, y, drawingView.getDrawing());
}
// track the figure containing the mouse
if (c != getTargetFigure()) {
if (getTargetFigure() != null) {
FigureHelper.setConnectorVisible(getTargetFigure(), false);
}
setTargetFigure(c);
if (getTargetFigure() != null) {
FigureHelper.setConnectorVisible(getTargetFigure(), true);
}
}
Connector cc = null;
if (c != null) {
cc = findConnector(x, y, c);
}
if (cc != getTargetConnector()) {
setTargetConnector(cc);
}
drawingView.checkDamage();
}
**/
protected void trackConnectors(MouseEvent e, int x, int y) {
Figure c = null;
if (getStartConnector() == null) {
c = findConnectionStart(x, y, drawingView.getDrawing());
} else {
c = findTarget(x, y, drawingView.getDrawing());
}
Connector startConnector = getStartConnector();
// track the figure containing the mouse
if (true/*c != getTargetFigure()*/) {
if (getTargetFigure() != null) {
FigureHelper.setConnectorVisible(getTargetFigure(), false);
}
setTargetFigure(c);
if (getTargetFigure() != null) {
if (startConnector == null) { //��û������ͼԪ��һ�ˣ�mouse move
FigureHelper.setConnectorVisible(getTargetFigure(), true);
}
else { //������һ�ˣ�������һ��
String featureId = (String)c.getAttribute("id");
String constraintId = (String)this.getStartConnector().owner().getAttribute("id");
boolean isSource = true;
int direction = this.getStartConnector().getDirection();
if (direction == Connector.EAST_DIRECTION || direction == Connector.SOUTH_DIRECTION)
isSource = false;
if (modelValidation.canConnect(featureId, constraintId, isSource))
FigureHelper.setConnectorVisible(getTargetFigure(), true);
else
c = null;
}
}
}
Connector cc = null;
if (c != null) {
cc = findConnector(x, y, c);
}
if (cc != getTargetConnector()) {
setTargetConnector(cc);
}
drawingView.checkDamage();
}
}