package de.FeatureModellingTool.GraphicalEditor;
import research.tool.ConnectionTool;
import research.tool.Tool;
import research.ConnectionFigure;
import research.Figure;
import research.FigureHelper;
import research.figure.TextFigure;
import de.FeatureModellingTool.FeatureModel.*;
import de.reuse.GroupMap;
import java.beans.PropertyChangeEvent;
import java.awt.event.MouseEvent;
import java.awt.*;
import research.util.Geom;
/**
* Created by IntelliJ IDEA.
* User: saturn
* Date: 2004-4-3
* Time: 23:12:08
* To change this template use File | Settings | File Templates.
*/
class InteractionCreationTool extends ConnectionTool implements ConstantDefinition {
private FeatureModel featureModel = null;
private InteractionModel interactionModel = null;
private InteractionModelEditor interactionModelEditor = 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 (INTERACTION_MODEL.equals(propertyName)) {
end();
interactionModel = (InteractionModel) e.getNewValue();
} else if (INTERACTION_MODEL_EDITOR.equals(propertyName)) {
end();
interactionModelEditor = (InteractionModelEditor) e.getNewValue();
} else if (ID_TO_FIGURE_MAP.equals(propertyName)) {
idToFigureMap = (GroupMap) e.getNewValue();
}
}
public boolean isEnabled() {
if (!super.isEnabled()) return false;
if (featureModel == null) return false;
if (interactionModel == null) return false;
if (interactionModelEditor == 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 + 1, y + 1);
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);
} else if (fEditedConnection != null) {
Point pp = new Point(x, y);
fEditedConnection.setPointAt(pp, fSplitPoint);
}
}
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));
if ((getEndConnector() != null)) {//�����������Ӳ�Ϊ�գ���������ͼԪ֮��û����ͬ�Ĺ�ϵ����
//ȷ�����ͼԪ
getConnection().connectStart(getStartConnector());
getConnection().connectEnd(getEndConnector());
getConnection().updateConnection();
String sID = (String) getConnection().startFigure().getAttribute("id");
String eID = (String) getConnection().endFigure().getAttribute("id");
if (!interactionModel.containsInteraction("type", "name", sID, eID)) {
Interaction interaction = interactionModelEditor.addInteraction("type", "name", sID, eID);
getConnection().setAttribute("id", interaction.getID());
getConnection().setAttribute("type", "interaction");
TextFigure title = new TextFigure();
title.setText(interaction.getType() + ":" + interaction.getName());
title.setAttribute("id", interaction.getID());
title.setAttribute("type", "interactionTitle");
title.setAttribute("isALink", Boolean.TRUE);
drawingView.add(title);
title.connect(getConnection());
//����id��figure��ӳ���
if (idToFigureMap != null) {
idToFigureMap.add(interaction.getID(), getConnection());
idToFigureMap.add(interaction.getID(), title);
}
} else if (getConnection() != null) {
//ɾ��ͼԪ
drawingView.remove(getConnection());
restoreConsumedFigure();
}
} 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);
}
}