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

package com.togethersoft.modules.inspector;

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 InspectorUtils {

   // Create "view" tab in object inspector
   public static void createViewPage(IdeContext context, IdeInspector inspector, RwiReference[] rwiReferences) {
      IdeInspectorComponent viewComponent = inspector.findComponent("View");
      IdeInspectorPropertySetComponent viewPage;
      if (viewComponent instanceof IdeInspectorPropertySetComponent) {
        viewPage = (IdeInspectorPropertySetComponent)viewComponent;
      } else {
        viewPage = new IdeInspectorPropertySetComponent(context);
        viewPage.setName("View");
        viewPage.setService(UIComponentService.class, new PropertyTableComponentUI(viewPage));
        inspector.addComponent(viewPage, null, ComponentWeight.VIEW);
      }

      IdeInspectorProperty property;
      property = new RwiInspectorColorProperty(rwiReferences, "backgroundColor");
      property.setName("background color");
      property.setPropertyEditor(new SwingColorEditor());
      viewPage.addProperty(property, null);


      property = new RwiInspectorColorProperty(rwiReferences, "foregroundColor");
      property.setName("foreground color");
      property.setPropertyEditor(new SwingColorEditor());
      viewPage.addProperty(property, null);

      property = new RwiInspectorInversedBooleanProperty(rwiReferences, "2DLook");
      property.setName("3D look");
      viewPage.addProperty(property, null);
   }

   // Create "description" tab in object inspector
   public static void createDescriptionPage(IdeContext context, IdeInspector inspector, RwiElement[] rwiElements) {
      if (rwiElements.length > 1) return;
      inspector.addComponent(new DescriptionComponent(rwiElements[0]), null, ComponentWeight.DESCRIPTION);
   }

   // Create "requirements" tab in object inspector
   public static void createReqPage(IdeContext context, IdeInspector inspector, RwiElement[] rwiElements) {
      IdeInspectorComponent reqComponent = inspector.findComponent("Req");
      IdeInspectorPropertySetComponent reqPage;
      if (reqComponent instanceof IdeInspectorPropertySetComponent) {
        reqPage = (IdeInspectorPropertySetComponent)reqComponent;
      } else {
        reqPage = new IdeInspectorPropertySetComponent(context);
        reqPage.setName("Req");
        reqPage.setService(UIComponentService.class, new PropertyTableComponentUI(reqPage));
        inspector.addComponent(reqPage, null, ComponentWeight.REQ);
      }

      IdeInspectorProperty property;

      property = new RwiInspectorStringProperty(rwiElements, "type");
      SwingComboBoxEditor editor = new SwingComboBoxEditor(
        new DefaultComboBoxModel(
          new String[] {
            "Business Rule", "User Need", "Feature", "Product Requirement", "Performance"
          }
        )
      );
      editor.setValueEditable(true);
      property.setPropertyEditor(editor);
      reqPage.addProperty(property, null);

      property = new RwiInspectorStringProperty(rwiElements, "number");
      property.setName("number");
      reqPage.addProperty(property, null);

      property = new RwiInspectorStringProperty(rwiElements, "description");
      property.setDisplayName("description");
      property.setPropertyEditor(new ExtendedStringEditor("description"));
      reqPage.addProperty(property, null);

      property = new RwiInspectorStringProperty(rwiElements, "document");
      property.setPropertyEditor(new ExtendedFileNameEditor("open"));
      reqPage.addProperty(property, null);

      property = new RwiInspectorMultipleStringProperty(rwiElements, "author");
      property.setDisplayName("author");
      reqPage.addProperty(property, null);

      property = new RwiInspectorStringProperty(rwiElements, "priority"){
        public void setValue(Object value){
          if ( "medium".equals(value) ) {
            value = null;
          }
          super.setValue(value);
        }
        public Object getValue(){
          Object value = super.getValue();
          if ( value == null ) {
            value = "medium";
          }
          return value;
        }
      };
      editor = new SwingComboBoxEditor(
        new DefaultComboBoxModel(
          new String[] {
            "high", "medium", "low"
          }
        ),
        new String[] {
          "High", "Medium", "Low"
        },
        new String[] {
          null, null, null
//          "/DropDowns/hiPriority.gif", "/DropDowns/medPriority.gif", "/DropDowns/lowPriority.gif"
        }
      );
      editor.setValueEditable(false);
      property.setPropertyEditor(editor);
      reqPage.addProperty(property, null);

      property = new RwiInspectorStringProperty(rwiElements, "difficulty"){
        public void setValue(Object value){
          if ( "medium".equals(value) ) {
            value = null;
          }
          super.setValue(value);
        }
        public Object getValue(){
          Object value = super.getValue();
          if ( value == null ) {
            value = "medium";
          }
          return value;
        }
      };
      editor = new SwingComboBoxEditor(
        new DefaultComboBoxModel(
          new String[] {
            "high", "medium", "low"
          }
        ),
        new String[] {
          "High", "Medium", "Low"
        },
        new String[] {
          null, null, null
//          "/transparent.gif", "/transparent.gif", "/transparent.gif"
        }
      );
      editor.setValueEditable(false);
      property.setPropertyEditor(editor);
      reqPage.addProperty(property, null);

      property = new RwiInspectorStringProperty(rwiElements, "testcase");
      property.setName("testcase");
      reqPage.addProperty(property, null);
   }
}
