/*------------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved.
------------------------------------------------------------------------------*/

package com.togethersoft.modules.inspector;

import com.togethersoft.openapi.ide.IdeStartup;
import com.togethersoft.openapi.ide.IdeContext;
import com.togethersoft.openapi.ide.inspector.*;
import com.togethersoft.openapi.ide.inspector.util.*;
import com.togethersoft.openapi.ide.inspector.util.property.*;
import com.togethersoft.openapi.ide.inspector.util.table.*;
import com.togethersoft.openapi.ide.inspector.util.editors.*;
import com.togethersoft.openapi.ide.util.*;
import com.togethersoft.openapi.rwi.*;
import com.togethersoft.openapi.util.RwiElementsUtil;

/**
 * @author TogetherSoft
 */
public class TransitionLinkInspector implements IdeStartup, IdeInspectorBuilder {
  public void autorun(){
    IdeInspectorManager manager = IdeInspectorManagerAccess.getInspectorManager();
    manager.addInspectorBuilder(this);
  }
  public IdeInspector buildInspector(IdeContext context, IdeInspector inspector) {
    if (!RwiElementsUtil.checkProperty(context, RwiProperty.SHAPE_TYPE, "TransitionLink") &&
        !RwiElementsUtil.checkProperty(context, RwiProperty.SHAPE_TYPE, RwiShapeType.INTERNAL_TRANSITION))
      return inspector;
    RwiElement[] rwiElements = RwiElementsUtil.getRwiElements(context);
    RwiReference[] rwiReferences = RwiElementsUtil.getRwiReferences(context);

    IdeInspectorComponent propertiesComponent = inspector.findComponent("Properties");
    IdeInspectorPropertySetComponent propertiesPage;
    if (propertiesComponent instanceof IdeInspectorPropertySetComponent) {
      propertiesPage = (IdeInspectorPropertySetComponent)propertiesComponent;
    }
    else {
      propertiesPage = new IdeInspectorPropertySetComponent(context);
      propertiesPage.setName("Link");
      propertiesPage.setService(UIComponentService.class, new PropertyTableComponentUI(propertiesPage));
      inspector.addComponent(propertiesPage, null, ComponentWeight.PROPERTIES);
    }

    IdeInspectorProperty property;

    if (rwiElements.length == 1 && rwiElements[0] instanceof RwiLink) {
      String sourceName = ((RwiLink)rwiElements[0]).getSource().getProperty(RwiProperty.NAME);
      property = new ReadOnlyProperty(sourceName, String.class);
      property.setName("client");
      propertiesPage.addProperty(property, null);

      String destinationName = ((RwiLink)rwiElements[0]).getDestination().getProperty(RwiProperty.NAME);
      property = new ReadOnlyProperty(destinationName, String.class);
      property.setName("supplier");
      propertiesPage.addProperty(property, null);
    }

    property = new RwiInspectorStringProperty(rwiElements, "$event");
    property.setName("event name");
    propertiesPage.addProperty(property, new Condition () {
      public boolean execute(IdeContext context) {
        return RwiElementsUtil.checkProperty(context, RwiProperty.SHAPE_TYPE, "TransitionLink");
      }
    });

    property = new RwiInspectorStringProperty(rwiElements, RwiProperty.NAME);
    property.setName("event name");
    propertiesPage.addProperty(property, new Condition () {
      public boolean execute(IdeContext context) {
        return RwiElementsUtil.checkProperty(context, RwiProperty.SHAPE_TYPE, RwiShapeType.INTERNAL_TRANSITION);
      }
    });

    property = new RwiInspectorStringProperty(rwiElements, "eventArguments");
    property.setName("event arguments");
    propertiesPage.addProperty(property, null);

    property = new RwiInspectorStringProperty(rwiElements, "condition");
    property.setName("guard condition");
    propertiesPage.addProperty(property, null);

    if (rwiElements.length == 1) {
      final RwiElement _rwiElement = rwiElements[0];
      property = new RwiInspectorStringProperty(rwiElements[0], "$action");
      property.setName("action expression");

      SingleElementChooserEditor secEditor = new SingleElementChooserEditor(rwiElements[0],
        com.togethersoft.openapi.ide.window.IdeSelectorDialogType.MESSAGE_OPERATION) {
           protected Object extractValueFromRwiElement(RwiElement rwiElement) {
             return rwiElement.getProperty("$operationDetailedName");
           }
      };
      property.setPropertyEditor(secEditor);
      propertiesPage.addProperty(property, new Condition() {
        public boolean execute(IdeContext context) {
          return _rwiElement.hasProperty("$stateMachineTarget");
        }
      });

      property = new RwiInspectorStringProperty(rwiElements[0], "$action");
      property.setName("action expression");
      propertiesPage.addProperty(property, new Condition() {
        public boolean execute(IdeContext context) {
          return ! _rwiElement.hasProperty("$stateMachineTarget");
        }
      });
    }

    property = new RwiInspectorStringProperty(rwiElements, "sendClause");
    property.setName("send clause");
    propertiesPage.addProperty(property, null);

    property = new RwiInspectorStringProperty(rwiElements, "sendTime");
    property.setName("send time");
    propertiesPage.addProperty(property, new Condition () {
      public boolean execute(IdeContext context) {
        return RwiElementsUtil.checkProperty(context, RwiProperty.SHAPE_TYPE, "TransitionLink");
      }
    });

    property = new RwiInspectorStringProperty(rwiElements, "receiveTime");
    property.setName("receive time");
    propertiesPage.addProperty(property, new Condition () {
      public boolean execute(IdeContext context) {
        return RwiElementsUtil.checkProperty(context, RwiProperty.SHAPE_TYPE, "TransitionLink");
      }
    });

    property = new RwiInspectorStringProperty(rwiElements, "constraint");
    property.setName("constraint");
    propertiesPage.addProperty(property, new Condition () {
      public boolean execute(IdeContext context) {
        return RwiElementsUtil.checkProperty(context, RwiProperty.SHAPE_TYPE, "TransitionLink");
      }
    });

    if (rwiElements.length == 1) {
      InspectorUtils.createDescriptionPage (context, inspector, rwiElements);
      inspector.addComponent(new DocComponent(rwiElements[0]), null, ComponentWeight.HTMLDOC);
      InspectorUtils.createReqPage (context, inspector, rwiElements);
    }

    return inspector;
  }
}

