Newer
Older
Multi-StageDesignTool / AlgebraicDataflowArchitectureModel / src / application / actions / IntroduceMediatorObjectAction.java
package application.actions;

import java.awt.event.ActionEvent;

import javax.swing.JOptionPane;

import com.mxgraph.model.mxCell;
import com.mxgraph.swing.mxGraphComponent;

import application.editor.stages.ControlFlowDelegationStage;
import application.editor.stages.ControlFlowDelegationStageStatus;
import models.controlFlowModel.ObjectNodeAttribute;

/*************************************************************
 * 
 */
public class IntroduceMediatorObjectAction extends AbstractPopupAction{

	private ControlFlowDelegationStage stage = null;
	
	/************************************************************
	/*************************************************************
	 *  [ *constructor ]
	/*************************************************************
	 */
	public IntroduceMediatorObjectAction(final ControlFlowDelegationStage stage, final mxGraphComponent graphComponent ,final mxCell cell) {
		super(/*propName*/"insertMediator", cell, graphComponent);
		this.stage = stage;
	}

	/************************************************************
	/*************************************************************
	 * [ *public ]
	/*************************************************************
	 * 
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		super.actionPerformed(e);
		insertObjectNode(targetCell);
		this.stage.setState( ControlFlowDelegationStageStatus.SELECTING_AN_EDGE);
	}

	/************************************************************
	/************************************************************
	 * [ *private ]
	/************************************************************
	 * 仲介者オブジェクトの導入を実行する. 
	 * @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;
	}


}