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

package com.togethersoft.modules.inspector.examples;

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

/**
 * Example module to show how to add your own page with fields to the inspector.
 * Adds a page "My page" with some fields to the class's inspector.
 */
public class AddPageToInspector implements IdeStartup, IdeInspectorBuilder {
  public void autorun() {
    IdeInspectorManager manager = IdeInspectorManagerAccess.getInspectorManager();
    manager.addInspectorBuilder(this);
  }

  public IdeInspector buildInspector(IdeContext context, IdeInspector inspector) {
    // checks whether the inspected element(s) is a class
    // if not, doesn't add anything to the inspector
    if (!RwiElementsUtil.checkProperty(context, RwiProperty.SHAPE_TYPE, RwiShapeType.CLASS)) {
      return inspector;
    }

    RwiElement[] rwiElements = RwiElementsUtil.getRwiElements(context);

    IdeInspectorPropertySetComponent page = new IdeInspectorPropertySetComponent(context);
    page.setName("My page");
    page.setService(UIComponentService.class, new PropertyTableComponentUI(page));
    inspector.addComponent(page, null);

    IdeInspectorProperty property;

    // creates a string property
    property = new RwiInspectorStringProperty(rwiElements, "myStringProperty");
    // sets the name to be shown in the UI
    property.setName("My string property");
    // adds it to the page
    // default editor - just a text field - is used
    page.addProperty(property, null);

    // creates a boolean property
    property = new RwiInspectorBooleanProperty(rwiElements, "myBooleanProperty");
    // sets the name to be shown in the UI
    property.setName("My boolean property");
    // adds it to the page
    // shown as a checkbox in the UI
    page.addProperty(property, null);

    // creates a string property
    property = new RwiInspectorStringProperty(rwiElements, "myChoiceProperty");
    // sets the name to be shown in the UI
    property.setName("My choice property");
    // creates and sets an editor for it - combobox(choice)
    SwingComboBoxEditor editor = new SwingComboBoxEditor(
      new DefaultComboBoxModel(
        new String[] {
          "value1", "value2", "value3"
        }
      )
    );
    property.setPropertyEditor(editor);
    // allows to edit value - combobox
    // setting value to false would make a choice field
    editor.setValueEditable(true);
    // adds property to the page
    page.addProperty(property, null);

    // creates a string property, which will be shown, when some condition is met
    property = new RwiInspectorStringProperty(rwiElements, "myConditionalProperty");
    // sets the name to be shown in the UI
    property.setName("My conditional property");
    // creates a Condition class
    // execute(IdeContext) returns true, if the field should be shown, false otherwise
    Condition condition = new Condition() {
      public boolean execute(IdeContext context) {
        // the field will be shown only if the inspected element(s) has property "myBooleanProperty"
        return RwiElementsUtil.checkProperty(context, "myBooleanProperty", true);
      }
    };
    // adds it to the page with the condition
    // the field will be hidden/shown in the UI on changing the "myBooleanProperty" value automatically
    page.addProperty(property, condition);

    return inspector;
  }
}
