package com.connor.erp.processresource.EquipmentAcquiredFromERP;

import com.connor.erp.toolkit.UtilApi;
import com.teamcenter.rac.aif.AbstractAIFDialog;
import com.teamcenter.rac.kernel.TCComponentFolder;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;

public class EquipmentAcquiredFromERPDialog extends AbstractAIFDialog implements ActionListener {
  private static final long serialVersionUID = 1L;
  
  private TCSession session;
  
  private TCComponentFolder targetFolder;
  
  private JTable table;
  
  private Vector<String> vecCanImp;
  
  private JButton btnImport;
  
  private JButton btnCancel;
  
  private JScrollPane scrollPane;
  
  public class CheckHeaderCellRenderer implements TableCellRenderer {
    EquipmentAcquiredFromERPDialog.CheckTableModle tableModel;
    
    JTableHeader tableHeader;
    
    final JCheckBox selectBox;
    
    public CheckHeaderCellRenderer(JTable table) {
      this.tableModel = (EquipmentAcquiredFromERPDialog.CheckTableModle)table.getModel();
      this.tableHeader = table.getTableHeader();
      this.selectBox = new JCheckBox(this.tableModel.getColumnName(2));
      this.selectBox.setSelected(true);
      this.tableHeader.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
              if (e.getClickCount() > 0) {
                int selectColumn = EquipmentAcquiredFromERPDialog.CheckHeaderCellRenderer.this.tableHeader.columnAtPoint(e.getPoint());
                if (selectColumn == 2) {
                  boolean value = !EquipmentAcquiredFromERPDialog.CheckHeaderCellRenderer.this.selectBox.isSelected();
                  EquipmentAcquiredFromERPDialog.CheckHeaderCellRenderer.this.selectBox.setSelected(value);
                  EquipmentAcquiredFromERPDialog.CheckHeaderCellRenderer.this.tableModel.selectAllOrNull(value);
                  EquipmentAcquiredFromERPDialog.CheckHeaderCellRenderer.this.tableHeader.repaint();
                } 
              } 
            }
          });
    }
    
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
      String valueStr = (String)value;
      JLabel label = new JLabel(valueStr);
      label.setHorizontalAlignment(0);
      this.selectBox.setHorizontalAlignment(0);
      this.selectBox.setBorderPainted(true);
      JComponent component = (column == 2) ? this.selectBox : label;
      component.setForeground(this.tableHeader.getForeground());
      component.setBackground(this.tableHeader.getBackground());
      component.setFont(this.tableHeader.getFont());
      component.setBorder(UIManager.getBorder("TableHeader.cellBorder"));
      return component;
    }
  }
  
  private StringBuffer errorInfo = new StringBuffer("错误信息如下：");
  
  private List<Integer> listNotEditableRow = new ArrayList<Integer>();
  
  public EquipmentAcquiredFromERPDialog(TCSession pSession, TCComponentFolder ptargetFolder, Vector<String> pVecCanImp) {
    super(false);
    this.session = pSession;
    this.targetFolder = ptargetFolder;
    this.vecCanImp = pVecCanImp;
    initUI();
  }
  
  private void initUI() {
    setTitle("导入新设备");
    this.table = new JTable() {
        private static final long serialVersionUID = 1L;
        
        public boolean isCellEditable(int row, int column) {
          if (column == 0 || column == 1)
            return false; 
          int NotEditableRowCount = EquipmentAcquiredFromERPDialog.this.listNotEditableRow.size();
          if (NotEditableRowCount > 0)
            for (int i = 0; i < NotEditableRowCount; i++) {
              if (row == ((Integer)EquipmentAcquiredFromERPDialog.this.listNotEditableRow.get(i)).intValue())
                return false; 
            }  
          return true;
        }
      };
    Vector<String> headerNames = new Vector<String>();
    headerNames.add("设备编号");
    headerNames.add("设备名称");
    headerNames.add("是否导入ERP系统");
    this.table.setModel(new CheckTableModle(getData(), headerNames));
    this.table.getTableHeader().setDefaultRenderer(new CheckHeaderCellRenderer(this.table));
    int totalWidth = UtilApi.FitTableColumns(this.table);
    this.btnImport = new JButton("新设备导入PLM系统");
    this.btnImport.addActionListener(this);
    this.btnCancel = new JButton("取消");
    this.btnCancel.addActionListener(this);
    JPanel paneButton = new JPanel();
    paneButton.add(this.btnImport);
    paneButton.add(this.btnCancel);
    this.scrollPane = new JScrollPane(this.table);
    JPanel paneMain = new JPanel(new BorderLayout());
    paneMain.add("North", new JLabel("ERP新增设备明细如下："));
    paneMain.add("Center", this.scrollPane);
    paneMain.add("South", paneButton);
    getContentPane().add(paneMain);
    setPreferredSize(new Dimension(totalWidth, 400));
  }
  
  public class CheckTableModle extends DefaultTableModel {
    private static final long serialVersionUID = 1L;
    
    public CheckTableModle(Vector<Object> data, Vector<String> columnNames) {
      super();
    }
    
    public Class<?> getColumnClass(int c) {
      return getValueAt(0, c).getClass();
    }
    
    public void selectAllOrNull(boolean value) {
      int rowNum = getRowCount();
      for (int row = 0; row < rowNum; row++) {
        if (!EquipmentAcquiredFromERPDialog.this.listNotEditableRow.contains(Integer.valueOf(row)))
          setValueAt(Boolean.valueOf(value), row, 2); 
      } 
    }
  }
  
  public List<Integer> getListNotEditableRow() {
    return this.listNotEditableRow;
  }
  
  public void setListNotEditableRow(List<Integer> listNotEditableRow) {
    this.listNotEditableRow = listNotEditableRow;
  }
  
  private Vector<Object> getData() {
    Vector<Object> data = new Vector();
    int vecSize = this.vecCanImp.size();
    for (int i = 0; i < vecSize; i++) {
      Vector<Object> rowVector = new Vector();
      String[] str = ((String)this.vecCanImp.get(i)).split(",");
      rowVector.add(str[0]);
      rowVector.add(str[1]);
      rowVector.add(Boolean.valueOf(true));
      data.add(rowVector);
    } 
    return data;
  }
  
  private boolean doImport() {
    int rowNum = this.table.getRowCount();
    List<String[]> list = (List)new ArrayList<String>();
    for (int row = 0; row < rowNum; row++) {
      if (((Boolean)this.table.getValueAt(row, 2)).booleanValue() && !this.listNotEditableRow.contains(Integer.valueOf(row))) {
        String[] str = new String[3];
        str[0] = this.table.getValueAt(row, 0).toString();
        str[1] = this.table.getValueAt(row, 1).toString();
        str[2] = String.valueOf(row);
        list.add(str);
      } 
    } 
    if (list.size() == 0) {
      MessageBox.post((Window)this, "请选择需要导入的设备", "警告", 4);
      return false;
    } 
    postDialog(list);
    if (!this.errorInfo.toString().equals("错误信息如下："))
      return false; 
    return true;
  }
  
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.btnImport) {
      doImport();
    } else if (e.getSource() == this.btnCancel) {
      dispose();
    } 
  }
  
  protected void postDialog(List<String[]> list) {
    SwingUtilities.invokeLater((Runnable)new ORNewEquipmentDialog(this, this.session, list, this.targetFolder));
  }
}
