/*------------------------------------------------------------------------------
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 javax.swing.DefaultComboBoxModel;
import com.togethersoft.openapi.util.RwiElementsUtil;

/**
 * @author TogetherSoft
 */
public class DiagramInspector implements IdeStartup, IdeInspectorBuilder {
  public void autorun(){
    IdeInspectorManager manager = IdeInspectorManagerAccess.getInspectorManager();
    manager.addInspectorBuilder(this);
  }
  public IdeInspector buildInspector(IdeContext context, IdeInspector inspector) {
    RwiElement[] rwiElements = RwiElementsUtil.getRwiElements(context);
    for(int i = 0; i < rwiElements.length; i++){
      if (rwiElements[i] == null) return inspector;
      if (!(rwiElements[i] instanceof RwiDiagram)) return inspector;
    }
    RwiReference[] rwiReferences = RwiElementsUtil.getRwiReferences(context);

    IdeInspectorComponent propertiesComponent = inspector.findComponent("Properties");
    IdeInspectorPropertySetComponent propertiesPage = null;
    if (propertiesComponent == null) {
      propertiesComponent = new DiagramComponent(context, rwiElements);
      if (propertiesComponent instanceof IdeInspectorPropertySetComponent) {
        propertiesPage = (IdeInspectorPropertySetComponent)propertiesComponent;
      }
      propertiesPage.setDisplayName("Properties");
      inspector.addComponent(propertiesPage, null, ComponentWeight.PROPERTIES);
    }

    if (rwiReferences != null) InspectorUtils.createViewPage (context, inspector, rwiReferences);

    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;
  }
}

class DiagramComponent extends IdeInspectorPropertySetComponent {
  public DiagramComponent(IdeContext context, RwiElement[] rwiElements) {
    super(context);
    myContext = context;
    myRwiElements = rwiElements;
    setName("Diagram");
    setService(UIComponentService.class, new PropertyTableComponentUI(this));
  }

  public void update() {
    IdeInspectorProperty[] properties = getProperties();
    for (int i = 0; i < properties.length; i++) {
      removeProperty(properties[i]);
    }
    refill();
    super.update();
  }

  private void refill() {
    IdeInspectorProperty property;

    property = new RwiInspectorStringProperty(myRwiElements, "$presentableName");
    property.setName("diagram type");
    addProperty(property, null);

    if (myRwiElements.length == 1) {
      if (myRwiElements[0].getProperty(RwiProperty.NAME)=="<default>")
        property = new ReadOnlyProperty(myRwiElements[0].getProperty(RwiProperty.NAME), String.class);
      else
        property = new RwiInspectorStringProperty(myRwiElements[0], RwiProperty.NAME);
      property.setName("name");
      addProperty(property, null);

      String packageName = ((RwiDiagram)myRwiElements[0]).getContainingPackage().getProperty(RwiProperty.FULL_NAME);
      if (packageName.length() == 0) {
        packageName = "<default>";
      }
      property = new ReadOnlyProperty(packageName, String.class);
      property.setName("package");
      addProperty(property, null);
    }

    property = new RwiInspectorStringProperty(myRwiElements, "stereotype");
    String[] values = new String[0];
    if (RwiElementsUtil.checkProperty(myContext, RwiProperty.SHAPE_TYPE, RwiShapeType.CLASS_DIAGRAM)) {
      values = new String[] {
        "data management", "human interaction", "problem domain", "system interaction",
        "facade", "framework", "stub", "subsystem", "system"
      };
    }
    SwingComboBoxEditor editor = new SwingComboBoxEditor(new DefaultComboBoxModel(values));
    editor.setValueEditable(true);
    property.setPropertyEditor(editor);
    addProperty(property, null);

    property = new RwiInspectorStringProperty(myRwiElements, "alias");
    property.setName("alias");
    addProperty(property, null);

    if (myRwiElements.length == 1) {
      property = new RwiInspectorStringProperty(myRwiElements[0], "$stateMachineTarget");
      property.setName("target class");
      property.setPropertyEditor(new SingleElementChooserEditor(
        myRwiElements[0], com.togethersoft.openapi.ide.window.IdeSelectorDialogType.CLASSES_AND_INTERFACES
      ));
      addProperty(property, new Condition () {
        public boolean execute(IdeContext context) {
          return RwiElementsUtil.checkProperty(myContext, RwiProperty.SHAPE_TYPE, RwiShapeType.STATE_DIAGRAM);
        }
      });
    }
  }

  private RwiElement[] myRwiElements;
  private IdeContext myContext;
}

