//Copyright (c) 2007 University of Auckland
//Contributed to the FBench extension of the OOONEIDA Workbench project under the Common Public License
package fbench.graph.popup;
import static javax.swing.GroupLayout.Alignment.BASELINE;
import static javax.swing.GroupLayout.Alignment.CENTER;
import static javax.swing.GroupLayout.Alignment.LEADING;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultCellEditor;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import fbench.FBench;
import fbench.LibraryElementView;
import fbench.table.ECActionsTableModel;
import org.w3c.dom.DOMException;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* Dialog which extends DialogModel for adding, editing and
* deleting components of <B>ECC GraphModel</B>.</br>
* <B>ECC GraphModel</B> consists of:
* <TT>ECState</TT> and
* <TT>ECTransition</TT>
*
* @author JP
* @version 20070228/JP
*/
public class ECCDialog extends DialogModel{
/**
*
*/
private static final long serialVersionUID = 5550426519671043358L;
private JCheckBox setInitialCheckBox = new JCheckBox("Initial State");
private JLabel nameLabel = new JLabel("Name :");
private JLabel commentLabel = new JLabel("Comment :");
private JLabel sourceLabel = new JLabel("Source State:");
private JLabel destinationLabel = new JLabel("Destination State:");
private JTextField nameField = new JTextField();
private JTextField commentField = new JTextField();
private JButton acceptButton;
private JButton cancelButton = new JButton("Cancel");
private JButton helpButton = new JButton("Help");
private JComboBox sourceStateBox, destinationStateBox, eventListBox, guardConditionListBox;
private JPanel actionPane, conditionPane;
private JTable aoTable;
private JScrollPane aoTablePane;
private String sourceStateName = null;
private String destinationStateName = null;
/**
* element is GraphModel element when ECCDialog is created for
* the purpose of <B>adding</B> a new state or transition,
* otherwise (if its purpose is for <b>editing</b> or <b>deleting</b>),
* element is the selected GraphElement element.
*/
public ECCDialog(Element element, MouseEvent mouseEvt){
super(element, mouseEvt);
}
@Override
public void create(String command) {
if(command.equals("Add State")){
createDialogForState(null);
} else if(command.equals("Add Transition")) {
createDialogForTransition(null);
}
else if(command.equals("Edit State")) {
Element stateElement = rearrangeElement();
createDialogForState(stateElement);
} else if(command.equals("Edit Transition")){
Element transitionElement = rearrangeElement();
createDialogForTransition(transitionElement);
}
else if(command.equals("Delete State") || command.equals("Delete Transition")){
createRemoveDialog();
}
setVisible(true);
}
// To Avoid Issues!! (getting clicked component's element
/*public void create(String command, Element clickedOn) {
if(command.equals("Add State")){
createDialogForState(null);
} else if(command.equals("Add Transition")) {
createDialogForTransition(null);
}
else if(command.equals("Edit State")) {
createDialogForState(clickedOn);
} else if(command.equals("Edit Transition")){
createDialogForTransition(clickedOn);
}
else if(command.equals("Delete State") || command.equals("Delete Transition")){
createRemoveDialog();
}
setVisible(true);
}*/
/**
* stateElement represents element of the selected ECState from ECC
*/
private Element stateElement = null;
private boolean isForAddingState = false;
private void createDialogForState(Element stateElem){
stateElement = stateElem;
if (stateElem == null)
isForAddingState = true;
else
isForAddingState = false;
setTitle(isForAddingState ? "Add Execution Control State" : "Edit Execution Control State");
setDialogSize(315, 285);
nameField.setText(isForAddingState ? "" : stateElement.getAttribute("Name"));
commentField.setText(isForAddingState ? "" : stateElement.getAttribute("Comment"));
actionPane = createActionPane();
acceptButton = new JButton(isForAddingState ? "Add" : "Apply");
acceptButton.addActionListener(new ConfirmEventButtonListener());
cancelButton.addActionListener(new CancelButtonActionListener());
setLayoutForEvent();
}
/**
* Uses GroupLayout to allocate components of Events
*/
private void setLayoutForEvent() {
layout.linkSize(SwingConstants.HORIZONTAL, nameLabel, commentLabel);
layout.setHorizontalGroup(layout.createParallelGroup(CENTER)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(nameLabel)
.addComponent(commentLabel))
.addGroup(layout.createParallelGroup(LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(nameField)
.addComponent(setInitialCheckBox))
.addComponent(commentField)))
.addComponent(actionPane)
.addGroup(layout.createSequentialGroup()
.addComponent(acceptButton)
.addComponent(cancelButton)
.addComponent(helpButton)));
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(BASELINE)
.addComponent(nameLabel)
.addComponent(nameField)
.addComponent(setInitialCheckBox))
.addGroup(layout.createParallelGroup(CENTER)
.addComponent(commentLabel)
.addComponent(commentField))
.addComponent(actionPane)
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(acceptButton)
.addComponent(cancelButton)
.addComponent(helpButton)));
if(!isForAddingState && stateElement != null ){
NodeList ecStateList = getElement().getElementsByTagName("ECState");
Element initialState = (Element)ecStateList.item(0);
if(initialState == null || initialState.equals(stateElement))
setInitialCheckBox.setSelected(true);
}
}
/**
* Creates and returns actionPane
* @return <TT>JPanel</TT> <B>actionPane</B> consists of buttons and a table which
* represents actions of the selected <TT>ECState</TT>
*/
private JPanel createActionPane() {
JPanel actionPane = new JPanel();
setComponentSize(actionPane, new Dimension(285, 150));
GroupLayout actionPaneLayout = new GroupLayout(actionPane);
actionPane.setLayout(actionPaneLayout);
actionPaneLayout.setAutoCreateContainerGaps(false);
actionPaneLayout.setAutoCreateGaps(false);
TitledBorder actionPaneBorder = BorderFactory.createTitledBorder(" Action ");
Font titleFont = actionPaneBorder.getTitleFont();
float newFontSize = 10;
actionPaneBorder.setTitleFont(titleFont.deriveFont(newFontSize));
actionPane.setBorder(actionPaneBorder);
JButton addActionButton = new JButton("Add new action");
Font addActionButtonFont = addActionButton.getFont();
addActionButton.setFont(addActionButtonFont.deriveFont(newFontSize));
addActionButton.setSize(addActionButton.getMinimumSize());
addActionButton.addActionListener(new ActionListener() {
public void actionPerformed( ActionEvent e) {//add a child to the table's nodelist
Element newEle = document.createElement("ECAction");
((ECActionsTableModel)aoTable.getModel()).getState().appendChild(newEle);//add an empty ECAction element to table's NodeList
aoTable.addNotify();
}
});
JButton deleteActionButton = new JButton("Delete selected actions");
Font deleteActionButtonFont = deleteActionButton.getFont();
deleteActionButton.setFont(deleteActionButtonFont.deriveFont(newFontSize));
deleteActionButton.setMargin(new Insets(2,2,2,2));
deleteActionButton.addActionListener(new ActionListener() {
public void actionPerformed( ActionEvent e) {
int[] selRows = aoTable.getSelectedRows();
if(selRows.length > 0){
aoTable.getCellEditor(aoTable.getSelectedRow(), aoTable.getSelectedColumn()).cancelCellEditing();
aoTable.clearSelection();
((ECActionsTableModel)aoTable.getModel()).removeRows(selRows);
aoTable.addNotify();
}
}
});
Border border1 = BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.black), BorderFactory
.createEmptyBorder(2, 4, 2, 4));
aoTable = new JTable();
aoTablePane = new JScrollPane(aoTable);
aoTablePane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
aoTable.setFont(getFont());
aoTable.setBorder(border1);
aoTable.setGridColor(Color.black);
aoTable.setRowSelectionAllowed(true);
setComponentSize(aoTablePane, new Dimension(255, 83));
Element newStateElement = document.createElement("ECState");
Element newActionElement = document.createElement("ECAction");
newStateElement.appendChild(newActionElement);
aoTable.setModel(new ECActionsTableModel(isForAddingState? newStateElement : stateElement));
//Setting properties of the first column of aoTable
TableColumn firstColumn = aoTable.getColumnModel().getColumn(0);
DefaultTableCellRenderer firstColumnRenderer = new DefaultTableCellRenderer();
firstColumnRenderer.setHorizontalAlignment(SwingConstants.CENTER);
firstColumn.setCellRenderer(firstColumnRenderer);
firstColumn.setMaxWidth(20);
firstColumn.setMinWidth(20);
firstColumn.setResizable(false);
//Adding items to algorithmCell (column index : 1)
JComboBox algorithmCell = new JComboBox();
algorithmCell.setFont(getFont());
algorithmCell.setEditable(false);
algorithmCell.addItem("");
NodeList algorithmList = document.getElementsByTagName("Algorithm");
for(int i = 0; i < algorithmList.getLength(); i++)
algorithmCell.addItem(((Element) algorithmList.item(i)).getAttribute("Name"));
aoTable.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(algorithmCell));
//Adding items to outputCell (column index : 2)
JComboBox outputCell = new JComboBox();
outputCell.setFont(getFont());
outputCell.setEditable(false);
outputCell.addItem("");
NodeList interfaceList = document.getElementsByTagName("InterfaceList");
NodeList eventOutputList = ((Element) interfaceList.item(0)).getElementsByTagName("EventOutputs");
NodeList eventOutputNodes = eventOutputList.item(0).getChildNodes();
for(int i = 0; i < eventOutputNodes.getLength(); i++){
outputCell.addItem(((Element) eventOutputNodes.item(i)).getAttribute("Name"));
}
aoTable.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(outputCell));
actionPaneLayout.linkSize(SwingConstants.HORIZONTAL, addActionButton, deleteActionButton);
actionPaneLayout.setHorizontalGroup(actionPaneLayout.createSequentialGroup()
.addGap(10)
.addGroup(actionPaneLayout.createParallelGroup(LEADING)
.addComponent(aoTablePane)
.addGroup(actionPaneLayout.createSequentialGroup()
.addComponent(addActionButton)
.addGap(5)
.addComponent(deleteActionButton))));
actionPaneLayout.setVerticalGroup(actionPaneLayout.createSequentialGroup()
.addGap(5)
.addComponent(aoTablePane)
.addGap(5)
.addGroup(actionPaneLayout.createParallelGroup(LEADING)
.addComponent(addActionButton)
.addComponent(deleteActionButton)));
return actionPane;
}
/**
* transitionElement represents element of the selected ECTransition from ECC
*/
private Element transitionElement = null;
private boolean isForAddingTransition = false;
private void createDialogForTransition(Element transitionElem){
transitionElement = transitionElem;
if(transitionElem == null)
isForAddingTransition = true;
else
isForAddingTransition = false;
setTitle(isForAddingState ? "Add Execution Control Transition" : "Edit Execution Control Transition");
setDialogSize(290, 260);
Vector<String> stateNames = getStateNameList();
sourceStateBox = new JComboBox(stateNames);
destinationStateBox = new JComboBox(stateNames);
conditionPane = createConditionPane();
acceptButton = new JButton(isForAddingState ? "Add" : "Apply");
acceptButton.addActionListener(new ConfirmTransitionButtonListener());
cancelButton.addActionListener(new CancelButtonActionListener());
setLayoutForTransition();
}
private JPanel createConditionPane() {
JPanel contentPane = new JPanel();
GroupLayout cpLayout = new GroupLayout(contentPane);
contentPane.setLayout(cpLayout);
TitledBorder cpBorder = new TitledBorder(" Transition Condition ");
Font sizeTenFont = cpBorder.getTitleFont().deriveFont(new Float(10));
cpBorder.setTitleFont(sizeTenFont);
contentPane.setBorder(cpBorder);
JLabel eventLabel = new JLabel("Event");
eventLabel.setFont(sizeTenFont);
JLabel conditionLabel = new JLabel("Guard Condition");
conditionLabel.setFont(sizeTenFont);
NodeList tempList,
eventList = null,
inputVarsList = null,
internalVarsList = null;
tempList = document.getElementsByTagName("EventInputs");
if(tempList != null && tempList.getLength() > 0){
eventList = tempList.item(0).getChildNodes();
}
tempList = document.getElementsByTagName("InputVars");
if(tempList != null && tempList.getLength() > 0){
inputVarsList = tempList.item(0).getChildNodes();
}
tempList = document.getElementsByTagName("InternalVars");
if(tempList != null && tempList.getLength() > 0){
internalVarsList = tempList.item(0).getChildNodes();
}
Vector<String> eventNames = new Vector<String>();
Vector<String> conditionList = new Vector<String>();
eventNames.add("");
eventNames.add("1");
if(eventList != null){
for(int i = 0; i < eventList.getLength(); i++)
eventNames.add(((Element)eventList.item(i)).getAttribute("Name"));
}
conditionList.add("");
if(inputVarsList != null){
for(int i = 0; i < inputVarsList.getLength(); i++)
conditionList.add(((Element)inputVarsList.item(i)).getAttribute("Name"));
}
if(internalVarsList != null){
for(int i = 0; i < internalVarsList.getLength(); i++)
conditionList.add(((Element)internalVarsList.item(i)).getAttribute("Name"));
}
eventListBox = new JComboBox(eventNames);
eventListBox.setFont(sizeTenFont);
guardConditionListBox = new JComboBox(conditionList);
guardConditionListBox.setFont(sizeTenFont);
guardConditionListBox.setEditable(true);
cpLayout.setHorizontalGroup(cpLayout.createSequentialGroup()
.addGap(10)
.addGroup(cpLayout.createParallelGroup(LEADING)
.addComponent(eventLabel)
.addComponent(conditionLabel))
.addGap(10)
.addGroup(cpLayout.createParallelGroup(LEADING)
.addComponent(eventListBox)
.addComponent(guardConditionListBox))
.addGap(10));
cpLayout.setVerticalGroup(cpLayout.createSequentialGroup()
.addGap(5)
.addGroup(cpLayout.createParallelGroup(CENTER)
.addComponent(eventLabel)
.addComponent(eventListBox))
.addGap(5)
.addGroup(cpLayout.createParallelGroup(CENTER)
.addComponent(conditionLabel)
.addComponent(guardConditionListBox))
.addGap(5));
return contentPane;
}
private void setLayoutForTransition() {
layout.setHorizontalGroup(layout.createParallelGroup(CENTER)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(sourceLabel)
.addComponent(destinationLabel))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(sourceStateBox)
.addComponent(destinationStateBox)))
.addComponent(conditionPane)
.addGroup(layout.createSequentialGroup()
.addComponent(acceptButton)
.addComponent(cancelButton)
.addComponent(helpButton))
.addGroup(layout.createSequentialGroup()
.addComponent(commentLabel)
.addComponent(commentField)));
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(CENTER)
.addComponent(sourceLabel)
.addComponent(sourceStateBox))
.addGroup(layout.createParallelGroup(CENTER)
.addComponent(destinationLabel)
.addComponent(destinationStateBox))
.addComponent(conditionPane)
.addGroup(layout.createParallelGroup(CENTER)
.addComponent(commentLabel)
.addComponent(commentField))
.addGroup(layout.createParallelGroup(CENTER)
.addComponent(acceptButton)
.addComponent(cancelButton)
.addComponent(helpButton)));
if(!isForAddingTransition){
sourceStateBox.setSelectedItem(transitionElement.getAttribute("Source"));
destinationStateBox.setSelectedItem(transitionElement.getAttribute("Destination"));
commentField.setText(transitionElement.getAttribute("Comment"));
String fullCondition = transitionElement.getAttribute("Condition");
int dividerIndex = fullCondition.indexOf("&");
if(dividerIndex != -1 && fullCondition.length() != 0){
String eventString = fullCondition.substring(0, dividerIndex);
String gcString = fullCondition.substring(dividerIndex+1, fullCondition.length());
eventListBox.setSelectedItem(eventString);
guardConditionListBox.setSelectedItem(gcString);
} else if(dividerIndex == -1 && fullCondition.length() != 0){
ComboBoxModel eventBoxModel = eventListBox.getModel();
ComboBoxModel gcBoxModel = guardConditionListBox.getModel();
boolean isMatchingItemFound = false;
for(int i = 0; i < eventBoxModel.getSize(); i++){
if(eventBoxModel.getElementAt(i).toString().equals(fullCondition)){
eventListBox.setSelectedIndex(i);
isMatchingItemFound = true;
break;
}
else if( i < gcBoxModel.getSize())
{
if(gcBoxModel.getElementAt(i).toString().equals(fullCondition)){
guardConditionListBox.setSelectedIndex(i);
isMatchingItemFound = true;
break;
}
}
}
if(!isMatchingItemFound){
guardConditionListBox.setSelectedItem(fullCondition);
}
}
}
else{
if(sourceStateName != null)
this.sourceStateBox.setSelectedItem(sourceStateName);
if(destinationStateName != null)
this.destinationStateBox.setSelectedItem(destinationStateName);
}
}
private Vector<String> getStateNameList() {
Vector<String> stateNames = new Vector<String>();
stateNames.add("");
NodeList stateList = getElement().getElementsByTagName("ECState");
for(int i = 0; i < stateList.getLength(); i++)
stateNames.add(((Element)stateList.item(i)).getAttribute("Name"));
return stateNames;
}
class ConfirmEventButtonListener implements ActionListener{
private String name, comment;
private NodeList eccComponentList, ecStateList, ecTransitionList;
public void actionPerformed(ActionEvent evt){
/*
* Reminder : getElement() returns the ECC element of the document
*/
//name = nameField.getText().toUpperCase();
name = nameField.getText();
comment = commentField.getText();
if(name.equals("")){
JOptionPane.showMessageDialog(me, "Name field is empty.",
"ECC State Warning", JOptionPane.WARNING_MESSAGE);
return;
}
eccComponentList = getElement().getChildNodes();
ecStateList = document.getElementsByTagName("ECState");
ecTransitionList = document.getElementsByTagName("ECTransition");
if(isForAddingState){
appendNewECState();
appendECActions();
}
//dialog for Editing purpose
else {
updateEditedState();
appendECActions();
}
// Refresh view
//getGraphView().setElement(getElement());
LibraryElementView fileView = FBench.theBench.getSelectedView();
fileView.getGraphView().refresh();
dispose();
}
/**
* Create ECAction elements using list of ECAction from aoTable.
* Hence, append them to the ECState as children.
*/
private void appendECActions() {
/**
* Clone of ECState which has been modified through aoTable that uses a ECActionTabelModel as its TableModel
* It consists of a new ECState element and children that represent new ECAction elements
*/
Element newECState = ((ECActionsTableModel)aoTable.getModel()).getState();
newECState.setAttribute("Name", name);
//TODO Change x,y position
if(isForAddingState){
newECState.setAttribute("x", ""+getMouseEvent().getX()/scaleFactor);
newECState.setAttribute("y", ""+getMouseEvent().getY()/scaleFactor);
}
if(!(comment.equals("")))
newECState.setAttribute("Comment", comment);
else if(comment.equals(""))
newECState.removeAttribute("Comment");
//Remove empty item from the table if there is any
NodeList ecActionList = newECState.getChildNodes();
for(int j = 0; j < ecActionList.getLength(); j++){
Element ecAction = ((Element)ecActionList.item(j));
if((ecAction.getAttribute("Algorithm").equals("")) && (ecAction.getAttribute("Output").equals(""))){
newECState.removeChild(ecAction);
j = 0; //TODO: AHHH What?
}
}
//TODO: remove FORCED ECACTION
// Now will need 'add action' dialog...
//if no ECAction exist, create one
if(ecActionList.getLength() == 0){
newECState.appendChild(createNewElement("ECAction"));
}
//copy the actions from the JTable into the currently edited node
for(int i = 0; i < eccComponentList.getLength(); i++){
if(((Element)eccComponentList.item(i)).getAttribute("Name")
.equalsIgnoreCase(newECState.getAttribute("Name"))){
try{
getElement().replaceChild(newECState, ((Element)eccComponentList.item(i)));
} catch( DOMException ex){
if(ex.getMessage().equalsIgnoreCase("NOT_FOUND_ERR")){
getElement().appendChild(newECState);
}
}
}
}
}
private boolean appendNewECState(){
/*
for(int i = 0; i < ecStateList.getLength(); i++){
if(((Element)ecStateList.item(i)).getAttribute("Name").equalsIgnoreCase(name)){
JOptionPane.showMessageDialog(me, "There is already a state with this name.",
"ECC State Warning", JOptionPane.WARNING_MESSAGE);
}
}*/
// TODO: The above was a duplicate
//check to see if a state with the same name already exist
for(int i = 0; i < ecStateList.getLength(); i++){
if(((Element)ecStateList.item(i)).getAttribute("Name").equalsIgnoreCase(name)){
JOptionPane.showMessageDialog(me, "There is already a state with this name.",
"ECC State Warning", JOptionPane.WARNING_MESSAGE);
return false;
}
}
Element newECState = createNewElement("ECState");
newECState.setAttribute("Name", name);
//find the last ECState position in the children list, and append
boolean hasECState = false;
if(eccComponentList.getLength() > 0){
for(int i = 0; i < eccComponentList.getLength(); i++){
String nodeName = eccComponentList.item(i).getNodeName();
if(nodeName.equalsIgnoreCase("ECState")){
//determine if there is at least one ECState node in the child list
hasECState = true;
if(!(i == eccComponentList.getLength()-1)){
continue;
}
}
if(hasECState){
if(nodeName.equalsIgnoreCase("ECTransition")){
if(eccComponentList.item(i-1).getNodeName().equalsIgnoreCase("ECState")){
getElement().insertBefore(newECState, eccComponentList.item(i));
break;
}
}
else if(i == eccComponentList.getLength()-1){
//when there are only ECStates in the system, add at end.. UNLESS init state
getElement().appendChild(newECState);
//.insertBefore(newECState, getElement().getLastChild());
break;
}
}
else{
//Children of the ECC element are all ECTransition
getElement().insertBefore(newECState, getElement().getFirstChild());
break;
}
}
}
else{
//ECC has nothing in it, add at end
getElement().appendChild(newECState);
}
// Silly.. but what ever
if(setInitialCheckBox.isSelected()){
if(!((Element)getElement().getFirstChild()).getAttribute("Name").equalsIgnoreCase(newECState.getAttribute("Name")))
getElement().insertBefore(getElement().removeChild(newECState), getElement().getFirstChild());
}
return true;
}
private void updateEditedState(){
if(setInitialCheckBox.isSelected()){
if(!((Element)getElement().getFirstChild()).getAttribute("Name").equalsIgnoreCase(stateElement.getAttribute("Name")))
getElement().insertBefore(stateElement, getElement().getFirstChild());
}
String oldStateName = stateElement.getAttribute("Name");
//String newStateName = nameField.getText().toUpperCase();
String newStateName = nameField.getText();
stateElement.setAttribute("Name", nameField.getText());
stateElement.setAttribute("Comment", comment);
for(int i = 0; i < ecTransitionList.getLength(); i++){
Element transitionElement = ((Element)ecTransitionList.item(i));
if(transitionElement.getAttribute("Source").equalsIgnoreCase(oldStateName)){
transitionElement.setAttribute("Source", newStateName);
}
if(transitionElement.getAttribute("Destination").equalsIgnoreCase(oldStateName)){
transitionElement.setAttribute("Destination", newStateName);
}
}
}
}
class ConfirmTransitionButtonListener implements ActionListener{
public void actionPerformed(ActionEvent evt){
if(!checkFields())
return;
String event = eventListBox.getSelectedItem().toString();
String guardCondition = guardConditionListBox.getSelectedItem().toString();
String condition = new String();
if(event.length() > 0)
if(guardCondition.length() > 0)
condition = event+"&"+guardCondition;
else
condition = event;
else
if(guardCondition.length() > 0)
condition = guardCondition;
else
condition = "";
String source = sourceStateBox.getSelectedItem().toString();
String destination = destinationStateBox.getSelectedItem().toString();
if(isForAddingTransition){
Element transitionElement = createNewElement("ECTransition");
transitionElement.setAttribute("Condition", condition);
transitionElement.setAttribute("Destination", destination);
transitionElement.setAttribute("Source", source);
if(!setLabelPosition(transitionElement))
return;
setComment(transitionElement);
getElement().appendChild(transitionElement);
}
else {
transitionElement.setAttribute("Condition", condition);
transitionElement.setAttribute("Source", source);
transitionElement.setAttribute("Destination", destination);
setComment(transitionElement);
}
//Refresh View
//getGraphView().setElement(getElement());
LibraryElementView fileView = FBench.theBench.getSelectedView();
fileView.getGraphView().refresh();
dispose();
}
private boolean checkFields() {
if(sourceStateBox.getSelectedIndex() <= 0){
JOptionPane.showMessageDialog(me, "Source state is not specified",
"ECC Transition Warning", JOptionPane.WARNING_MESSAGE);
return false;
}
if(destinationStateBox.getSelectedIndex() <= 0){
JOptionPane.showMessageDialog(me, "Destination state is not specified",
"ECC Transition Warning", JOptionPane.WARNING_MESSAGE);
return false;
}
// if(sourceStateBox.getSelectedIndex() == destinationStateBox.getSelectedIndex()){
// JOptionPane.showMessageDialog(me, "Source and Destination State are the same",
// "ECC Transition Warning", JOptionPane.WARNING_MESSAGE);
// return false;
// }
boolean isEventSpecified = false;
boolean isConditionSpecified = false;
if(eventListBox.getSelectedIndex() > 0){
isEventSpecified = true;
}
if(guardConditionListBox.getSelectedIndex() > 0 ||
!guardConditionListBox.getSelectedItem().toString().equals(""))
isConditionSpecified = true;
if(!isEventSpecified && !isConditionSpecified){
JOptionPane.showMessageDialog(me, "Event and Guard Condition are both empty",
"ECC Transition Warning", JOptionPane.WARNING_MESSAGE);
return false;
}
return true;
}
private void setComment(Element transitionElement) {
String comment = commentField.getText();
if(!(comment.equals(""))){
transitionElement.setAttribute("Comment", comment);
System.out.println("edited comment");
}
else if(comment.equals("")){
transitionElement.removeAttribute("Comment");
}
}
private boolean setLabelPosition(Element transitionElement) {
NodeList transList = document.getElementsByTagName("ECTransition");
if(transList.getLength() > 0){
for(int i = 0; i < transList.getLength(); i++){
Element ele = (Element)transList.item(i);
if(ele.getAttribute("Destination").equalsIgnoreCase(transitionElement.getAttribute("Destination"))){
if(ele.getAttribute("Source").equalsIgnoreCase(transitionElement.getAttribute("Source"))){
JOptionPane.showMessageDialog(me, "There is already a Transision with the same source and destination.",
"ECC Transition Warning", JOptionPane.WARNING_MESSAGE);
return false;
}
}
}
}
NodeList stateList = document.getElementsByTagName("ECState");
//search for trans
Element sourceState = null;
Element destState = null;
for(int i = 0; i < stateList.getLength(); i++){
Element tempElement = (Element)stateList.item(i);
if(tempElement.getAttribute("Name").equalsIgnoreCase(sourceStateBox.getSelectedItem().toString())){
sourceState = tempElement;
}
if(tempElement.getAttribute("Name").equalsIgnoreCase(destinationStateBox.getSelectedItem().toString())){
destState = tempElement;
}
}
float sourceX = Float.parseFloat(sourceState.getAttribute("x"));
float sourceY = Float.parseFloat(sourceState.getAttribute("y"));
float destinationX = Float.parseFloat(destState.getAttribute("x"));
float destinationY = Float.parseFloat(destState.getAttribute("y"));
float transitionX = 0;
float transitionY = 0;
if(destinationX < sourceX){
if(destinationY > sourceY){
transitionX = destinationX + Math.abs((float)((sourceX - destinationX)*0.4));
transitionY = destinationY - Math.abs((float)((sourceY - destinationY)*0.6));
}
else if(destinationY < sourceY){
transitionX = sourceX - Math.abs((float)((sourceX - destinationX)*0.4));
transitionY = sourceY - Math.abs((float)((sourceY - destinationY)*0.6));
}
}
else if(destinationX > sourceX){
if(destinationY > sourceY){
transitionX = sourceX + Math.abs((float)((sourceX - destinationX)*0.4));
transitionY = sourceY + Math.abs((float)((sourceY - destinationY)*0.6));
}
else if(destinationY < sourceY){
transitionX = destinationX - Math.abs((float)((sourceX - destinationX)*0.4));
transitionY = destinationY + Math.abs((float)((sourceY - destinationY)*0.6));
}
}
transitionElement.setAttribute("x", ""+transitionX);
transitionElement.setAttribute("y", ""+transitionY);
return true;
}
}
public void setDestinationStateName(String destinationStateName) {
this.destinationStateName = destinationStateName;
}
public void setSourceStateName(String sourceStateName) {
this.sourceStateName = sourceStateName;
}
}