package application.actions;
import application.editor.stages.ControlFlowModelingStage;
import application.editor.stages.ControlFlowModelingStageStatus;
import com.mxgraph.model.mxCell;
import com.mxgraph.swing.mxGraphComponent;
import models.controlFlowModel.ObjectNodeAttribute;
import javax.swing.*;
import java.awt.event.ActionEvent;
public class IntroduceMediatorObjectAction extends AbstractPopupAction {
private ControlFlowModelingStage stage = null;
public IntroduceMediatorObjectAction(final ControlFlowModelingStage stage, final mxGraphComponent graphComponent, final mxCell cell) {
super(/*propName*/"insertMediator", cell, graphComponent);
this.stage = stage;
}
@Override
public void actionPerformed(ActionEvent e) {
super.actionPerformed(e);
insertObjectNode(targetCell);
this.stage.setState(ControlFlowModelingStageStatus.SELECTING_AN_EDGE);
}
/**
* 仲介者オブジェクトの導入を実行する.
*
* @param cellObj 選択されたエッジのセル
*/
private void insertObjectNode(final Object cellObj) {
if (cellObj == null) return;
mxCell edgeCell = null;
if (cellObj instanceof mxCell) edgeCell = (mxCell) cellObj;
else return;
// Inputing name to the dialog.
String objName = JOptionPane.showInputDialog("Object Name:");
if (objName == null) return;
if (objName.isEmpty()) {
JOptionPane.showMessageDialog(graphComponent, "You must input a name. \nIt mustn't be empty.");
return;
}
if (isDuplicatedName(objName)) {
JOptionPane.showMessageDialog(graphComponent, "The named object has already existed.");
return;
}
stage.insertObjectNodeCellInControlFlowLayer(edgeCell, objName);
}
/**
* 同名のノードがすでにあるか判定する
*
* @param name つけたいノード名
*/
private boolean isDuplicatedName(final String name) {
mxCell root = (mxCell) graphComponent.getGraph().getDefaultParent();
for (int i = 0; i < root.getChildCount(); i++) {
mxCell layerCell = (mxCell) root.getChildAt(i);
for (int j = 0; j < layerCell.getChildCount(); j++) {
mxCell cell = (mxCell) layerCell.getChildAt(j);
ObjectNodeAttribute attr = null;
if (cell.getValue() instanceof ObjectNodeAttribute)
attr = (ObjectNodeAttribute) cell.getValue();
else continue;
if (!(attr.getObjectNode().getName().equals(name))) continue;
return true;
}
}
return false;
}
}