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

package com.togethersoft.modules.inspector.customproperties;

import com.togethersoft.openapi.ide.IdeAccess;
import com.togethersoft.openapi.ide.inspector.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;

public abstract class IdeInspectorPropertySetRemoveButton extends JButton implements ActionListener, ListSelectionListener {
  public IdeInspectorPropertySetRemoveButton(String title, JTable table, IdeInspectorPropertySetComponent component) {
    super(title);
    myTable = table;
    myComponent = component;
    addActionListener(this);
    myTable.getSelectionModel().addListSelectionListener(this);
    mySelectedIndex = myTable.getSelectionModel().getMaxSelectionIndex();
    setEnabled(canDelete(getSelectedProperty()));
  }

  public void actionPerformed(ActionEvent e) {
    final ListSelectionModel sel = myTable.getSelectionModel();
    final int rowToDelete = mySelectedIndex;
    final IdeInspectorProperty selectedProperty = getSelectedProperty();

    if(myTable.isEditing()) {
      myTable.getCellEditor(myTable.getEditingRow(), myTable.getEditingColumn()).cancelCellEditing();
    }
    IdeAccess.getIdeManager().addCommandToQueue(new Runnable() {
      public void run() {
        doDelete(selectedProperty);
        myComponent.update();
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            if (myComponent.getProperties().length == 0 ) {
              sel.clearSelection();
            } else if (rowToDelete >= myComponent.getProperties().length) {
              sel.setSelectionInterval(myComponent.getProperties().length-1, myComponent.getProperties().length-1);
            }
          }
        });
      }
    }, "Deleted element");
  }

  public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting()) return;
    mySelectedIndex = ((ListSelectionModel)e.getSource()).getMaxSelectionIndex();
    IdeInspectorProperty selectedProperty = getSelectedProperty();
    setEnabled(canDelete(selectedProperty));
  }

  protected void doDelete(IdeInspectorProperty toBeDeleted) {
    if (toBeDeleted != null) {
      if (toBeDeleted.isWritable())
        toBeDeleted.setValue(null);
    }
  }

  protected boolean canDelete(IdeInspectorProperty toBeDeleted) {
    if ( toBeDeleted != null ) {
      return toBeDeleted.isWritable();
    }
    return false;
  }

  protected IdeInspectorProperty getSelectedProperty() {
    IdeInspectorProperty properties[] = myComponent.getProperties();
    if ( mySelectedIndex < 0 || properties.length == 0 || properties.length <= mySelectedIndex ) return null;
    return properties[mySelectedIndex];
  }

  protected JTable myTable;
  protected IdeInspectorPropertySetComponent myComponent;
//  protected RwiElement myRwiElement;
  private int mySelectedIndex;
}
