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

package com.togethersoft.modules.inspector.customproperties;

import com.togethersoft.openapi.ide.window.IdeButtonType;
import com.togethersoft.openapi.ide.window.IdeWindowManagerAccess;
import com.togethersoft.openapi.ide.inspector.*;
import com.togethersoft.openapi.ide.inspector.util.table.*;
import com.togethersoft.openapi.ide.inspector.util.property.*;
import com.togethersoft.openapi.ide.util.UIComponentService;
import com.togethersoft.openapi.rwi.RwiElement;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class CustomPropertiesInspectorComponentUI implements UIComponentService {
  public CustomPropertiesInspectorComponentUI(IdeInspector inspector, IdeInspectorPropertySetComponent component, RwiElement rwiElement) {
    myPanel.setLayout(new GridBagLayout());
    PropertyTableModel tableModel = new PropertyTableModel(component) {
      public boolean isCellEditable(int row, int column) {
        IdeInspectorProperty property = getInspectorProperty(row, column);
        if (property == null) return false;
        return (property.isWritable() && ((String)property.getValue()).length() > 0);
      }
    };
    myTable = new JTable(tableModel, new PropertyColumnModel());
    TableUtil.setUpStandardTable(myTable, myScrollPane);

    final IdeInspector _inspector = inspector;
    final IdeInspectorPropertySetComponent _component = component;
    final RwiElement _rwiElement = rwiElement;
    myAddButton = new IdeInspectorPropertySetAddButton("add property", myTable, component) {
      protected void doAdd() {
          final NewCustomPropertyDialog dialog = new NewCustomPropertyDialog();
          dialog.addOkListener (new ActionListener () {
                  public void actionPerformed (ActionEvent e)
                  {
                      if (!CustomPropertiesUtil.checkPropertyName
                          (_inspector, dialog.propertyName)) {
                          // TO DO : show message
                          return;
                      }
                      
                      boolean isBoolean = false;
                      if (dialog.propertyName == null) {
                          return;
                      }
                      if (dialog.propertyValue == null) {
                          isBoolean = true;
                      }

                      IdeInspectorProperty property;
                      if (isBoolean) {
                          _rwiElement.setProperty(dialog.propertyName, true);
                          property = new RwiInspectorBooleanProperty
                              (_rwiElement, dialog.propertyName);
                      } 
                      else {
                          _rwiElement.setProperty
                              (dialog.propertyName, dialog.propertyValue);
                          property = new RwiInspectorStringProperty
                              (_rwiElement, dialog.propertyName);
                      }
                      _component.addProperty(property, null);
                      _component.update();                        
                  }
              });
          dialog.show();
      }
    };
    myRemoveButton = new IdeInspectorPropertySetRemoveButton("remove property", myTable, component) {
      protected void doDelete(IdeInspectorProperty toBeDeleted) {
        if (!isRemoveApproved()) return;
        super.doDelete(toBeDeleted);
      }
    };

    JPanel internalPanel = TableUtil.getStandardButtonTablePanel(myScrollPane, myAddButton, myRemoveButton, "");
    myPanel.add(internalPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 12, 12, 11, 11), 0, 0));
  }

  public Component getUIComponent() {
    return myPanel;
  }

  private boolean isRemoveApproved() {
    return IdeButtonType.OK.equals(IdeWindowManagerAccess.getWindowManager().showConfirmDialog("Warning", "This will remove the property", IdeButtonType.OK_CANCEL_BUTTONS));
  }

  private IdeInspectorPropertySetAddButton myAddButton;
  private IdeInspectorPropertySetRemoveButton myRemoveButton;
  private JTable myTable;
  private JPanel myPanel = new JPanel();
  private JScrollPane myScrollPane = new JScrollPane();
}
