package com.connor.erp.processresource.EquipmentAcquiredFromERP;

import cn.com.origin.autocode.classification.ClassificationPropertyNames;
import com.teamcenter.rac.aif.AbstractAIFDialog;
import com.teamcenter.rac.aif.kernel.AIFComponentContext;
import com.teamcenter.rac.commands.newitem.NewItemDialog;
import com.teamcenter.rac.commands.newitem.NewItemPanel;
import com.teamcenter.rac.common.TCConstants;
import com.teamcenter.rac.common.controls.LOVUIComponent;
import com.teamcenter.rac.kernel.ItemValidationInfo;
import com.teamcenter.rac.kernel.TCClassificationService;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentFolder;
import com.teamcenter.rac.kernel.TCComponentForm;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemType;
import com.teamcenter.rac.kernel.TCComponentType;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.kernel.ics.ICSApplicationObject;
import com.teamcenter.rac.kernel.ics.ICSView;
import com.teamcenter.rac.util.FilterDocument;
import com.teamcenter.rac.util.HorizontalLayout;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.PropertyLayout;
import com.teamcenter.rac.util.Registry;
import com.teamcenter.rac.util.Separator;
import com.teamcenter.rac.util.VerticalLayout;
import com.teamcenter.rac.util.iTextArea;
import com.teamcenter.rac.util.iTextField;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.LayoutManager;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.border.TitledBorder;
import javax.swing.text.Document;

public class ORNewEquipmentDialog extends AbstractAIFDialog {
  private static final long serialVersionUID = 1L;
  
  public Frame parentFrame;
  
  public TCSession session;
  
  public NewItemPanel itemPanel;
  
  public JPanel textPanel;
  
  public JPanel itemInfoPanel;
  
  public JPanel namingAndNumberingPanel;
  
  public JPanel itemIdPanel;
  
  public JPanel revisionIdPanel;
  
  public JComboBox itemIdCombobox;
  
  public JComboBox revisionIdCombobox;
  
  public JPanel revIdPanel;
  
  public JPanel namePanel;
  
  public JPanel otherPropertyPanel;
  
  public JPanel nonPropertiesPanel;
  
  public JLabel infoLabel;
  
  public JLabel itemLabel;
  
  public JLabel descLabel;
  
  public JLabel uomLabel;
  
  public iTextField idTextField;
  
  public iTextField revTextField;
  
  public iTextField nameTextField;
  
  public LOVUIComponent nameLOVUI;
  
  public iTextArea descTextArea;
  
  public JScrollPane descScrollPane;
  
  public LOVUIComponent uomLovComboBox;
  
  public iTextField uomTextField;
  
  public JCheckBox classifyCheckBox;
  
  public TCComponentType[] preLoadTypes;
  
  public ItemValidationInfo valObj;
  
  public String classifyPref;
  
  public String classifyNewPref;
  
  public String[] classifiableTypes;
  
  public String allowCopyAsRevisionPref;
  
  public String idRevSeparator;
  
  public String revNameSeparator;
  
  public static final String IDREV_SEPARATOR_PREF = "FLColumnCatIVFSeparatorPref";
  
  public static final String REVNAME_SEPARATOR_PREF = "FLColumnCatObjSeparatorPref";
  
  public static final String DISPLAY_ITEM_ID_NAME_MAX_COLUMNS_PREF = "TC_UI_DisplayMaxColumnsForItemIDNameFields";
  
  public static final String ICS_CLASSIFY_NEW = "ICS_classify_new";
  
  public static final String CLASSIFY_IMMEDIATE_PREFERENCE = "ICS_classify_immediately";
  
  public static final String ICS_CLASSIFIABLE_TYPES = "ICS_classifiable_types";
  
  public static final String CLASSIFY_ON = "1";
  
  public static final String CLASSIFY_OFF = "0";
  
  public boolean showUOM;
  
  public boolean showQuantity;
  
  private boolean displayMaxColumnsForItemIDNameFields;
  
  public JPanel quantityPanel;
  
  protected Registry packageReg;
  
  public static final String PROPERTY_ITEM_ID = "item_id";
  
  public static final String PROPERTY_ITEM_REVISION_ID = "item_revision_id";
  
  public static final String PROPERTY_OBJECT_NAME = "object_name";
  
  public static final String PROPERTY_OBJECT_DESCRIPTION = "object_desc";
  
  public static final String PROPERTY_UOM = "uom_tag";
  
  public static final String PROPERTY_OBJECT_TYPE = "object_type";
  
  protected boolean revDescRequired;
  
  public boolean isRevRuleAttached;
  
  private JSplitPane classificationPanel;
  
  private JScrollPane tclassificationScrollPane;
  
  public JButton finishButton;
  
  public JButton closeButton;
  
  private JPanel buttonPanel;
  
  private PackClassificationInfos classificationInfos = null;
  
  private ICSApplicationObject icsapplicationobject;
  
  private ICSView icsView;
  
  private TCClassificationService classificationService;
  
  private List<String[]> list;
  
  private TCComponentItemType item_type;
  
  private TCComponentFolder equipmentFolder;
  
  private String folderName = null;
  
  private EquipmentAcquiredFromERPDialog dialog;
  
  private ORCreateClassificationPanel createClassificationPanel;
  
  private String origin_classificationMatchItemProperties = "origin_classificationMatchItemProperties";
  
  public ORNewEquipmentDialog(EquipmentAcquiredFromERPDialog dialog, TCSession session, List<String[]> list, TCComponentFolder equipmentFolder) {
    super(true);
    this.session = session;
    this.list = list;
    this.equipmentFolder = equipmentFolder;
    this.dialog = dialog;
    initUI();
  }
  
  public void initUI() {
    this.packageReg = Registry.getRegistry(NewItemDialog.class);
    setTitle("新建设备");
    getPreferenceSettings();
    JPanel mainPanel = new JPanel();
    setLayout(new BorderLayout());
    mainPanel.setLayout((LayoutManager)new VerticalLayout(5, 2, 2, 2, 2));
    JLabel infoLabel = new JLabel(getPageTitle(), getPageIcon(), 2);
    infoLabel.setForeground(Color.BLUE);
    buildBaseItemInfoPanel();
    this.otherPropertyPanel = buildStaticPropertiesPanel();
    mainPanel.add("top.bind.left.top", infoLabel);
    mainPanel.add("top.bind", (Component)new Separator());
    mainPanel.add("top.bind.left.top", this.itemInfoPanel);
    mainPanel.add("top.bind.left.top", this.otherPropertyPanel);
    this.classificationPanel = buildItemClassficationPanel();
    this.tclassificationScrollPane = new JScrollPane();
    this.tclassificationScrollPane.setHorizontalScrollBarPolicy(32);
    this.tclassificationScrollPane.setVerticalScrollBarPolicy(22);
    this.tclassificationScrollPane.getViewport().setView(this.classificationPanel);
    Dimension dimension = this.classificationPanel.getPreferredSize();
    this.tclassificationScrollPane.setPreferredSize(new Dimension(dimension.width, dimension.height));
    buildButtonPanel();
    mainPanel.add("top.bind.left.top", this.tclassificationScrollPane);
    add("Center", mainPanel);
    add("South", this.buttonPanel);
    setPreferredSize(new Dimension(700, 600));
    pack();
    validate();
    centerToScreen();
  }
  
  public String getPageTitle() {
    String s = getClass().getName() + "." + "TITLE";
    String s1 = this.packageReg.getString(s, null);
    if (s1 == null || s1.length() <= 0)
      s1 = this.packageReg.getString("itemInfo.LABEL"); 
    return s1;
  }
  
  public Icon getPageIcon() {
    String s = getClass().getName() + "." + "ICON";
    ImageIcon imageicon = this.packageReg.getImageIcon(s, null);
    if (imageicon == null)
      imageicon = this.packageReg.getImageIcon("info.ICON"); 
    return imageicon;
  }
  
  public void buildButtonPanel() {
    this.buttonPanel = new JPanel();
    this.finishButton = new JButton("完成");
    this.finishButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            List<TCComponentFolder> listFolder = new ArrayList<TCComponentFolder>();
            if (ORNewEquipmentDialog.this.folderName != null) {
              ORNewEquipmentDialog.this.getFolder(ORNewEquipmentDialog.this.equipmentFolder, listFolder, ORNewEquipmentDialog.this.folderName);
            } else {
              MessageBox.post((Window)ORNewEquipmentDialog.this, "请选择要发送的分类对象", "警告", 1);
              return;
            } 
            int folderSize = listFolder.size();
            if (folderSize == 0) {
              MessageBox.post((Window)ORNewEquipmentDialog.this, "未找到"+ ORNewEquipmentDialog.this.folderName + "文件夹！", "出现问题", 1);
              return;
            } 
            if (folderSize > 1) {
              MessageBox.post((Window)ORNewEquipmentDialog.this, "找到多个"+ ORNewEquipmentDialog.this.folderName + "文件夹！", "出现问题", 1);
              return;
            } 
            TCComponentItem item = null;
            try {
              item = ORNewEquipmentDialog.this.item_type.create(ORNewEquipmentDialog.this.idTextField.getText(), ORNewEquipmentDialog.this.revTextField.getText(), "NHL8_Equipments", ORNewEquipmentDialog.this.nameTextField.getText(), ORNewEquipmentDialog.this.descTextArea.getText(), null);
            } catch (TCException e2) {
              e2.printStackTrace();
            } 
            if (item != null) {
              TCComponentFolder parentFolder = listFolder.get(0);
              try {
                parentFolder.add("contents", (TCComponent)item);
                parentFolder.refresh();
              } catch (TCException e1) {
                e1.printStackTrace();
              } 
              boolean isSuccess = false;
              HashMap<Integer, String> icsPorpertyMap = null;
              if (ORNewEquipmentDialog.this.classificationInfos != null) {
                isSuccess = (new ORCreateICOObj()).createICObject(ORNewEquipmentDialog.this.getIcsView(), ORNewEquipmentDialog.this.classificationInfos, (TCComponent)item);
                icsPorpertyMap = ORNewEquipmentDialog.this.classificationInfos.getPropertyMaps2();
              } 
              if (isSuccess) {
                ORNewEquipmentDialog.this.dialog.getListNotEditableRow().add(Integer.valueOf(Integer.parseInt(((String[])ORNewEquipmentDialog.this.list.get(0))[2])));
                ORNewEquipmentDialog.this.list.remove(0);
                if (ORNewEquipmentDialog.this.list.size() > 0) {
                  ORNewEquipmentDialog.this.idTextField.setText("NHLM" + ((String[])ORNewEquipmentDialog.this.list.get(0))[0]);
                  ORNewEquipmentDialog.this.nameTextField.setText(((String[])ORNewEquipmentDialog.this.list.get(0))[1]);
                  ORNewEquipmentDialog.this.setRevMasterProperties(item, icsPorpertyMap);
                  MessageBox.post((Window)ORNewEquipmentDialog.this, item + "创建成功！", "成功", 2);
                } else {
                  ORNewEquipmentDialog.this.setRevMasterProperties(item, icsPorpertyMap);
                  ORNewEquipmentDialog.this.dispose();
                  MessageBox.post((Window)ORNewEquipmentDialog.this, "所选设备全部创建成功！", "成功", 2);
                  return;
                } 
              } 
            } 
          }
        });
    this.closeButton = new JButton("关闭");
    this.closeButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            ORNewEquipmentDialog.this.dispose();
          }
        });
    this.buttonPanel.add(this.finishButton);
    this.buttonPanel.add(this.closeButton);
  }
  
  public void setRevMasterProperties(TCComponentItem item, HashMap<Integer, String> icsPorpertyMap) {
    HashMap<String, String> cfghm = TCPreferenceUitl.getPreferenceHashMap(this.session, 0, this.origin_classificationMatchItemProperties);
    if (cfghm == null)
      return; 
    String revisionMasterProps = cfghm.get("NHL8_Equipments_RevisionMaster");
    if (revisionMasterProps != null && revisionMasterProps.length() > 0) {
      TCComponentForm form = null;
      try {
        form = (TCComponentForm)item.getLatestItemRevision().getRelatedComponent("IMAN_master_form_rev");
      } catch (TCException e2) {
        e2.printStackTrace();
      } 
      String[] str1 = revisionMasterProps.split(";");
      Map<String, String> propMap = new HashMap<String, String>();
      for (int i = 0; i < str1.length; i++) {
        String[] str2 = str1[i].split("&&");
        propMap.put(str2[1], icsPorpertyMap.get(Integer.valueOf(str2[0])));
      } 
      try {
        form.setProperties(propMap);
        form.save();
        form.refresh();
      } catch (TCException e1) {
        e1.printStackTrace();
      } 
    } 
  }
  
  public void getFolder(TCComponentFolder parentFolder, List<TCComponentFolder> listFolder, String folderName) {
    try {
      AIFComponentContext[] children = parentFolder.getChildren();
      int childCount = children.length;
      for (int i = 0; i < childCount; i++) {
        if (children[i].getComponent() instanceof TCComponentFolder) {
          TCComponentFolder childFolder = (TCComponentFolder)children[i].getComponent();
          try {
            if (children[i].getComponent().getProperty("object_name").equals(folderName))
              listFolder.add(childFolder); 
          } catch (Exception e) {
            e.printStackTrace();
          } 
          getFolder(childFolder, listFolder, folderName);
        } 
      } 
    } catch (TCException e) {
      e.printStackTrace();
    } 
  }
  
  public void buildBaseItemInfoPanel() {
    String s = this.packageReg.getString("itemId") + " " + this.idRevSeparator + " " + this.packageReg.getString("rev") + " " + this.revNameSeparator + " " + this.packageReg.getString("name");
    this.itemLabel = new JLabel(s);
    String s1 = TCSession.getServerEncodingName(this.session);
    FilterDocument filterdocument = new FilterDocument(TCConstants.getDefaultMaxNameSize(this.session), s1);
    this.idTextField = new iTextField((Document)filterdocument, "", this.displayMaxColumnsForItemIDNameFields ? 32 : 15, TCConstants.getDefaultMaxNameSize(this.session), true, null);
    filterdocument = new FilterDocument(32, s1);
    this.revTextField = new iTextField((Document)filterdocument, "", 5, 32, true, null);
    filterdocument = new FilterDocument(TCConstants.getDefaultMaxNameSize(this.session), s1);
    this.nameTextField = new iTextField((Document)filterdocument, "", this.displayMaxColumnsForItemIDNameFields ? 32 : 10, TCConstants.getDefaultMaxNameSize(this.session), true, null);
    this.namePanel = new JPanel((LayoutManager)new HorizontalLayout());
    if (this.list.size() > 0) {
      this.idTextField.setText("NHLM" + ((String[])this.list.get(0))[0]);
      this.idTextField.setEnabled(false);
      try {
        this.item_type = (TCComponentItemType)this.session.getTypeComponent("NHL8_Equipments");
        this.revTextField.setText(this.item_type.getNewRev(null));
      } catch (TCException e1) {
        e1.printStackTrace();
      } 
      this.revTextField.setEnabled(false);
      this.nameTextField.setText(((String[])this.list.get(0))[1]);
      this.nameTextField.setEnabled(false);
    } 
    this.namePanel.add("unbound.bind.left.center", (Component)this.nameTextField);
    this.revIdPanel = new JPanel((LayoutManager)new HorizontalLayout());
    this.revIdPanel.add("left.bind.left.center", (Component)this.revTextField);
    this.textPanel = new JPanel((LayoutManager)new HorizontalLayout());
    this.textPanel.add("left.nobind", (Component)this.idTextField);
    this.textPanel.add("left", new JLabel(this.idRevSeparator));
    this.textPanel.add("left.nobind", this.revIdPanel);
    this.textPanel.add("left", new JLabel(this.revNameSeparator));
    this.textPanel.add("unbound.bind", this.namePanel);
    this.itemIdCombobox = new JComboBox();
    Dimension dimension = new Dimension(100, 20);
    this.itemIdCombobox.setPreferredSize(dimension);
    this.itemIdCombobox.setSelectedIndex(-1);
    this.itemIdPanel = new JPanel((LayoutManager)new HorizontalLayout());
    this.itemIdPanel.add("left", new JLabel(this.packageReg.getString("itemId")));
    this.itemIdPanel.add("left", this.itemIdCombobox);
    this.revisionIdCombobox = new JComboBox();
    this.revisionIdCombobox.setPreferredSize(dimension);
    this.revisionIdCombobox.setSelectedIndex(-1);
    this.revisionIdPanel = new JPanel((LayoutManager)new HorizontalLayout());
    this.revisionIdPanel.add("left", new JLabel(this.packageReg.getString("rev")));
    this.revisionIdPanel.add("left", this.revisionIdCombobox);
    this.itemInfoPanel = new JPanel((LayoutManager)new VerticalLayout());
    this.itemInfoPanel.setBorder(new TitledBorder(BorderFactory.createEtchedBorder(), this.packageReg.getString("itemInformation")));
    this.itemInfoPanel.add("top", this.itemLabel);
    this.itemInfoPanel.add("top.bind", this.textPanel);
  }
  
  public void getPreferenceSettings() {
    TCPreferenceService tcpreferenceservice = this.session.getPreferenceService();
    int[] ai = { 0, 0, 4, 4, 4 };
    String[] as = { "FLColumnCatIVFSeparatorPref", "FLColumnCatObjSeparatorPref", "ICS_classify_new", "ICS_classify_immediately", "TC_UI_DisplayMaxColumnsForItemIDNameFields" };
    String[] as1 = tcpreferenceservice.getStrings(ai, as);
    if (as1 != null && as1.length > 0) {
      this.idRevSeparator = as1[0];
    } else {
      this.idRevSeparator = tcpreferenceservice.getString(0, "FLColumnCatIVFSeparatorPref");
    } 
    if (as1 != null && as1.length > 1) {
      this.revNameSeparator = as1[1];
    } else {
      this.revNameSeparator = tcpreferenceservice.getString(0, "FLColumnCatObjSeparatorPref");
    } 
    if (as1 != null && as1.length > 2) {
      this.classifyNewPref = as1[2];
    } else {
      this.classifyNewPref = tcpreferenceservice.getString(4, "ICS_classify_new");
    } 
    if (as1 != null && as1.length > 3) {
      this.classifyPref = as1[3];
    } else {
      this.classifyPref = tcpreferenceservice.getString(4, "ICS_classify_immediately");
    } 
    this.displayMaxColumnsForItemIDNameFields = tcpreferenceservice.isTrue(4, "TC_UI_DisplayMaxColumnsForItemIDNameFields");
    this.allowCopyAsRevisionPref = tcpreferenceservice.getString(3, "Allow_Copy_As_Revision");
    this.classifiableTypes = tcpreferenceservice.getStringArray(4, "ICS_classifiable_types", null);
  }
  
  public JPanel buildStaticPropertiesPanel() {
    String s = TCSession.getServerEncodingName(this.session);
    this.descLabel = new JLabel(this.packageReg.getString("description"));
    FilterDocument filterdocument = new FilterDocument(240, s);
    this.descTextArea = new iTextArea((Document)filterdocument, "", 3, 30, 240, false, null);
    this.descTextArea.setLineWrap(true);
    this.descTextArea.setWrapStyleWord(true);
    this.descScrollPane = new JScrollPane((Component)this.descTextArea);
    this.uomLabel = new JLabel(this.packageReg.getString("unitOfMeasure"));
    this.uomTextField = new iTextField();
    this.uomTextField.setText("每个");
    this.uomTextField.setEnabled(false);
    JPanel jpanel = new JPanel((LayoutManager)new PropertyLayout());
    jpanel.add("1.1.left.top", this.descLabel);
    jpanel.add("1.2.center.center.resizable.resizable", this.descScrollPane);
    jpanel.add("2.1.right.top", this.uomLabel);
    jpanel.add("2.2", (Component)this.uomTextField);
    return jpanel;
  }
  
  public JSplitPane buildItemClassficationPanel() {
    JSplitPane jsp = new JSplitPane();
    this.createClassificationPanel = new ORCreateClassificationPanel(this);
    try {
      JScrollPane treePanel = this.createClassificationPanel.CreateClassificationInfoPanel("ICM", "Classification root", ClassificationPropertyNames.icsGroupType);
      JScrollPane propertyPanel = this.createClassificationPanel.createPropertyInfoPanel();
      jsp.setBorder(new TitledBorder(BorderFactory.createEtchedBorder(), "请选择分类"));
      jsp.setOrientation(1);
      jsp.setDividerLocation(0.5D);
      jsp.setDividerSize(7);
      jsp.setLeftComponent(treePanel);
      jsp.setRightComponent(propertyPanel);
      jsp.setAutoscrolls(true);
    } catch (Exception e) {
      e.printStackTrace();
    } 
    return jsp;
  }
  
  public PackClassificationInfos getClassificationInfos() {
    return this.classificationInfos;
  }
  
  public void setClassificationInfos(PackClassificationInfos classificationInfos) {
    this.classificationInfos = classificationInfos;
  }
  
  public ICSApplicationObject getIcsapplicationobject() {
    return this.icsapplicationobject;
  }
  
  public void setIcsapplicationobject(ICSApplicationObject icsapplicationobject) {
    this.icsapplicationobject = icsapplicationobject;
  }
  
  public ICSView getIcsView() {
    return this.icsView;
  }
  
  public void setIcsView(ICSView icsView) {
    this.icsView = icsView;
  }
  
  public TCClassificationService getClassificationService() {
    return this.classificationService;
  }
  
  public void setClassificationService(TCClassificationService classificationService) {
    this.classificationService = classificationService;
  }
  
  public static long getSerialVersionUID() {
    return 1L;
  }
  
  public String getFolderName() {
    return this.folderName;
  }
  
  public void setFolderName(String folderName) {
    this.folderName = folderName;
  }
}
