package com.connor.erp.processresource.EquipmentAcquiredFromERP;

import com.teamcenter.rac.aifrcp.AifrcpPlugin;
import com.teamcenter.rac.kernel.TCClassificationService;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.kernel.ics.ICSApplicationObject;
import com.teamcenter.rac.kernel.ics.ICSFormat;
import com.teamcenter.rac.kernel.ics.ICSPropertyDescription;
import com.teamcenter.rac.kernel.ics.ICSView;
import com.teamcenter.rac.util.Painter;
import com.teamcenter.rac.util.VerticalLayout;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class CreateClassificationPropertyPanel {
  JPanel mainPanel;
  
  JScrollPane scrollPane;
  
  ICSApplicationObject icsapplicationobject;
  
  ICSView icsView;
  
  List notNullClassificationPropTextField;
  
  HashMap propHashMap;
  
  HashMap<String, Integer> nameToIdHashMap;
  
  TCClassificationService classificationService;
  
  public TCSession getSession() {
    TCSession session = (TCSession)AifrcpPlugin.getSessionService().getDefaultSession();
    return session;
  }
  
  public CreateClassificationPropertyPanel() {
    this.propHashMap = new HashMap<Object, Object>();
    this.nameToIdHashMap = new HashMap<String, Integer>();
    this.notNullClassificationPropTextField = new ArrayList();
    this.mainPanel = new JPanel(new BorderLayout());
    this.scrollPane = new JScrollPane();
    this.scrollPane.getViewport().setView(this.mainPanel);
    this.scrollPane.setHorizontalScrollBarPolicy(32);
    this.scrollPane.setVerticalScrollBarPolicy(22);
    this.classificationService = getSession().getClassificationService();
    try {
      this.icsapplicationobject = this.classificationService.newICSApplicationObject("ICM");
    } catch (TCException e) {
      e.printStackTrace();
    } 
  }
  
  public JScrollPane getPropertyPanel() {
    return this.scrollPane;
  }
  
  public ICSApplicationObject getICSApplicationObject() {
    return this.icsapplicationobject;
  }
  
  public ICSView getICSView() {
    return this.icsView;
  }
  
  public TCClassificationService getClassificationService() {
    return this.classificationService;
  }
  
  public List getNotNullClassificationPropTextField() {
    return this.notNullClassificationPropTextField;
  }
  
  public void setNotNullClassificationPropTextField(List notNullClassificationPropTextField) {
    this.notNullClassificationPropTextField = notNullClassificationPropTextField;
  }
  
  public JScrollPane createPropertyPanel(String icsName, String icsID) {
    this.mainPanel.removeAll();
    try {
      this.icsapplicationobject.setView("Base", icsID);
    } catch (TCException e1) {
      e1.printStackTrace();
    } 
    this.icsView = this.icsapplicationobject.getView();
    if (this.icsView == null)
      return this.scrollPane; 
    ICSPropertyDescription[] aicspropertydescription = this.icsapplicationobject.getView().getPropertyDescriptions();
    JLabel jlabel = new JLabel(icsName);
    JPanel formPanel = new JPanel();
    if (aicspropertydescription != null && aicspropertydescription.length > 0) {
      formPanel.setLayout((LayoutManager)new VerticalLayout(aicspropertydescription.length));
      for (int i = 0; i < aicspropertydescription.length; i++) {
        String dispalayName = aicspropertydescription[i].getDisplayName();
        boolean isMandatory = aicspropertydescription[i].isMandatory();
        ICSFormat isformat = aicspropertydescription[i].getFormat();
        JPanel panel = new JPanel(new GridLayout(1, 3, 2, 2));
        panel.setPreferredSize(new Dimension(0, 25));
        JLabel labelDisplayName = new JLabel(dispalayName);
        JLabel labelUnit = new JLabel(aicspropertydescription[i].getUnit());
        int lenth = (isformat.getSize() <= 20) ? isformat.getSize() : 20;
        Object jtextField = null;
        if (isformat.isList()) {
          String[] keyLovs = isformat.getKeyLov().getKeyValues();
          if (isMandatory) {
            jtextField = (Object)new JComboBox((Object[])keyLovs) {
                public void paint(Graphics g) {
                  super.paint(g);
                  Painter.paintIsRequired(this, g);
                }
              };
          } else {
            jtextField = (Object)new JComboBox<String>(keyLovs);
          } 
        } else if (isMandatory) {
          jtextField = (Object)new JTextField(lenth) {
              public void paint(Graphics g) {
                super.paint(g);
                Painter.paintIsRequired(this, g);
              }
            };
        } else {
          jtextField = (Object)new JTextField(lenth);
        } 
        panel.add(labelDisplayName);
        if (isMandatory)
          this.notNullClassificationPropTextField.add(jtextField); 
        if (jtextField instanceof JTextField) {
          panel.add((JTextField)jtextField);
        } else if (jtextField instanceof JComboBox) {
          panel.add((JComboBox)jtextField);
        } 
        panel.add(labelUnit);
        if (this.icsapplicationobject.getView().isAbstract()) {
          Component[] components = panel.getComponents();
          for (int m = 0; m < components.length; m++)
            components[m].setEnabled(false); 
        } 
        formPanel.add("top.bind.left.top", panel);
        String propName = aicspropertydescription[i].getName();
        this.propHashMap.put(propName, jtextField);
        this.nameToIdHashMap.put(propName, Integer.valueOf(aicspropertydescription[i].getId()));
      } 
    } 
    this.mainPanel.add(jlabel, "North");
    this.mainPanel.add(formPanel, "Center");
    this.mainPanel.repaint();
    this.mainPanel.validate();
    this.scrollPane.repaint();
    this.scrollPane.validate();
    return this.scrollPane;
  }
  
  public HashMap getPropertyInfos() {
    return this.propHashMap;
  }
  
  public HashMap getNameToIdPropertyInfos() {
    return this.nameToIdHashMap;
  }
}
