You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

405 lines
14 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.langtech.plm.project;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
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.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jdesktop.swingx.JXDatePicker;
import com.connor.sbplm.plm1.util.TCUtil;
import com.teamcenter.rac.aif.AbstractAIFDialog;
import com.teamcenter.rac.aif.kernel.AIFComponentContext;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentDataset;
import com.teamcenter.rac.kernel.TCComponentForm;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCComponentItemType;
import com.teamcenter.rac.kernel.TCComponentTcFile;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;
public class ProjectECRDialog extends AbstractAIFDialog {
private static final long serialVersionUID = 1L;
private TCSession session;
private JTextField field;
private JTextField dateAfterField;
private JTextField dateBeforeField;
private JButton searchBtn;
private JButton exportBtn;
private JTable impTable;
private DefaultTableModel impModel;
private Vector<String> exports;
private Vector<String> excludes;
private String[] tableHeader;
public ProjectECRDialog(TCSession session, String[] options) throws Exception {
super(AIFUtility.getActiveDesktop());
this.session = session;
Vector<String[]> props = new Vector<>();
this.exports = new Vector<>();
setTitle("<22><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѯ");
// <09><>ѯ<EFBFBD><D1AF>Ŀ
JLabel label = new JLabel("<22><>Ŀ&<26><>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD>");
JLabel appDateLabel0 = new JLabel("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
JLabel appDateLabel1 = new JLabel("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
// <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
field = new JTextField();
dateAfterField = new JTextField(10);
dateBeforeField = new JTextField(10);
field.setPreferredSize(new Dimension(118, 23));
//<2F><><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>
DateChooser dateAfterChooser = DateChooser.getInstance("yyyy-M-dd");
dateAfterChooser.register(this.dateAfterField);
DateChooser dateBeforeChooser = DateChooser.getInstance("yyyy-M-dd");
dateBeforeChooser.register(this.dateBeforeField);
JPanel msgPanel = new JPanel(new FlowLayout());
msgPanel.add(label);
msgPanel.add(field);
msgPanel.add(appDateLabel1);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
msgPanel.add(dateBeforeField);
msgPanel.add(appDateLabel0);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
msgPanel.add(dateAfterField);
searchBtn = new JButton("<22><>ѯ");
exportBtn = new JButton("<22><><EFBFBD><EFBFBD>");
JPanel btnPanel = new JPanel(new FlowLayout());
btnPanel.add(searchBtn);
btnPanel.add(exportBtn);
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.add(BorderLayout.WEST, msgPanel);
topPanel.add(BorderLayout.EAST, btnPanel);
// <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ֵ<EFBFBD><D6B5>ȡ<EFBFBD><C8A1>ͷ<EFBFBD><CDB7>Ϣ
tableHeader = new String[options.length-1];
int[] weights = new int[options.length-1];
for(int i=0; i<options.length-1; i++) {
//split = [{ECN<43><4E><EFBFBD><EFBFBD>,ecn.rev.item_id,120}]
String[] split = options[i+1].split("=");
if(split[1].equals("<22><><EFBFBD><EFBFBD>"))
tableHeader[i] = split[1];
else if(split[0].startsWith("#")) {
tableHeader[i] = split[0].substring(1);
exports.add(tableHeader[i]);
}else
tableHeader[i] = split[0];
//props = [{ecn,rev,item_id},{pr,master.sb6_PRRM04},...]
props.add(split[1].split("\\."));
weights[i] = Integer.parseInt(split[2]);
}
// TODO <20>²⣺tableHeaderΪ<72><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
impModel = new DefaultTableModel(tableHeader, 0);
impTable = new JTable() {
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int row, int col) {
return false;
}
};
impTable.getTableHeader().setReorderingAllowed(false);
impTable.setFillsViewportHeight(true);
// impTable.setEnabled(false);
impTable.setEnabled(true);
// impTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
impTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
impTable.setModel(impModel);
for(int i=0; i<impModel.getColumnCount(); i++)
impTable.getColumnModel().getColumn(i).setPreferredWidth(weights[i]);
JScrollPane tablePanel = new JScrollPane(impTable);
addActionListener(options[0], props);
setLayout(new BorderLayout());
add(BorderLayout.NORTH, topPanel);
add(BorderLayout.CENTER, tablePanel);
pack();
setSize(new Dimension(1400, 700));
setDefaultLookAndFeelDecorated(true);
Dimension screen = getToolkit().getScreenSize();
setLocation((screen.width - getSize().width) / 2, (screen.height - getSize().height) / 2);
// setAlwaysOnTop(true);
setVisible(true);
}
/**
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD>
* @param id
* @param props = [{ecn,rev,item_id},{pr,master.sb6_PRRM04},...]<5D><><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD>ѡ<EFBFBD>
*/
private void addActionListener(String id, Vector<String[]> props) {
searchBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String dateString0 = dateBeforeField.getText();
String dateString1 = dateAfterField.getText();
try {
String project = field.getText();
if(project.isEmpty() && dateString0.isEmpty() && dateString1.isEmpty()) {
MessageBox.post(ProjectECRDialog.this, "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD>", "ERROR", MessageBox.ERROR);
return;
}
if(impModel.getRowCount()>0) {
for(int i=impModel.getRowCount(); i>0; i--)
impModel.removeRow(i-1);
}
Vector<String> keyV = new Vector<>();
Vector<String> valueV = new Vector<>();
// <09><>װ<EFBFBD><D7B0>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD>š<EFBFBD><C5A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (!project.isEmpty()) {
keyV.add("projnum");
valueV.add(project);
}else if (!dateString0.isEmpty()) {
keyV.add("dateafter");
valueV.add(dateString0+" 00:00");
}else if (!dateString1.isEmpty()) {
keyV.add("datebefore");
valueV.add(dateString1+" 23:59");
}
//<2F><><EFBFBD>ò<EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѯ<EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD>ecn<63><6E>-result
TCComponent[] result = TCUtil.query(session, "CONNOR_SearchProjectECN", keyV, valueV);
System.out.println("<22><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF>"+result.length+"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
for(int i=0; i<result.length; i++) {
TCComponentItemRevision ecn = (TCComponentItemRevision) result[i];
TCComponentForm ecnform = TCUtil.getItemRevisionMasterForm(ecn);
//<2F><>ʵʩ<CAB5><CAA9><EFBFBD><EFBFBD>ϵ<EFBFBD>µĶ<C2B5><C4B6><EFBFBD><EFBFBD><EFBFBD>Ŀǰֻ<C7B0><D6BB>ecr<63><72><EFBFBD><EFBFBD>-ecrs
AIFComponentContext[] ecrs = TCUtil.getPseudoFolder(ecn, "CMImplements").getChildren();
//TODO 1.<2E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰecn<63>ġ<EFBFBD>ʵʩ<CAB5><CAA9><EFBFBD><EFBFBD>ϵ<EFBFBD>µĶ<C2B5><C4B6><EFBFBD>-ecr<63><EFBFBD><E6B1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>ECR<43><52><EFBFBD><EFBFBD>ƴ<EFBFBD>ӳ<EFBFBD><D3B3>ַ<EFBFBD><D6B7><EFBFBD>
String ercBM = "";
for (AIFComponentContext ecr:ecrs){
//<2F><>ȡʵʩĿ¼<C4BF>µ<EFBFBD>ECR<43><EFBFBD><E6B1BE><EFBFBD><EFBFBD><EFBFBD>ͱ<EFBFBD><CDB1><EFBFBD>
TCComponentItemRevision ecrRev = (TCComponentItemRevision) ecr.getComponent();
ercBM =ercBM + ecrRev.getProperty("item_id") + "/";
}
//<2F>Ƴ<EFBFBD>erc<72><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>
if (ercBM.endsWith("/")){
ercBM = ercBM.substring(0, ercBM.length()-1);
}
// 2.ȡ<>õ<EFBFBD>ǰECN<43>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
Vector<String> v = new Vector<>();
for(String[] prop:props) {
if (prop[0].equals("<22><><EFBFBD><EFBFBD>")){
v.add(impTable.getRowCount()+1+"");
} else if (prop[0].equals("ecn")) {
if (prop[1].equals("rev")){
//ecn.rev.*<2A><>ֻ<EFBFBD><D6BB>ͨ<EFBFBD><CDA8>ecn<63><EFBFBD><E6B1BE><EFBFBD><EFBFBD>ȡֵ
v.add(ecn.getProperty(prop[2]));
} else if (prop[1].equals("master")) {
//ecn.master.*<2A><>ֻ<EFBFBD><D6BB>ͨ<EFBFBD><CDA8>ecnformȡֵ
v.add(ecnform.getProperty(prop[2]));
}
} else if (prop[0].equals("ecr")) {
if (prop[1].equals("rev")) {
v.add(ercBM);
}
}else if (prop[0].equals("״̬")) {
// <20><>ȡECN<43><4E><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-<2D>رգ<D8B1>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><>ر<EFBFBD>
String releaseStatus = ecn.getProperty("date_released");
// if (releaseStatus.equals("<22><><EFBFBD><EFBFBD>")){
if (releaseStatus == null || releaseStatus.isEmpty()){
//δ<><CEB4><EFBFBD><EFBFBD>״̬
v.add(<>ر<EFBFBD>");
}else {
v.add("<22>ر<EFBFBD>");
}
}
}
impModel.addRow(v);
}
} catch (Exception e1) {
e1.printStackTrace();
MessageBox.post(ProjectECRDialog.this, "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:"+e1.getMessage(), "ERROR", MessageBox.ERROR);
}
}
});
exportBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
FileOutputStream output = null;
InputStream input = null;
try {
if(impTable.getRowCount()==0)
throw new Exception("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>գ<EFBFBD><D5A3>޷<EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD>");
TCComponentItem[] item = ((TCComponentItemType) session.getTypeService().getTypeComponent("Item")).findItems(id);
if(item==null || item.length==0)
throw new Exception(<>ҵ<EFBFBD>ģ<EFBFBD><C4A3>Item");
TCComponentDataset dataset = null;
AIFComponentContext[] contexts = item[0].getLatestItemRevision().getChildren();
for(AIFComponentContext c : contexts) {
TCComponent child = (TCComponent) c.getComponent();
if(child instanceof TCComponentDataset && child.isTypeOf("MSExcelX")) {
dataset = (TCComponentDataset) child;
break;
}
}
if(dataset == null)
throw new Exception(<>ҵ<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD>ݼ<EFBFBD>");
System.out.println("export");
TCComponentTcFile[] files = dataset.getTcFiles();
if (files==null || files.length==0)
return;
File directory = TCUtil.saveExcelChooser();
if(directory == null)
return;
String path = directory.getPath();
if (!path.endsWith(".xlsx")) {
path = path+".xlsx";
}
File file = files[0].getFile(directory.getParent());
input = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(input);
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row = sheet.getRow(0);
if(row == null)
row = sheet.createRow(0);
int col = 0;
for(int i=0; i<tableHeader.length; i++) {
XSSFCell cell = getCell(row, col);
cell.setCellValue(tableHeader[i]);
col++;
}
for(int i=0; i<impTable.getRowCount(); i++) {
row = sheet.getRow(i+1);
if(row==null)
row = sheet.createRow(i+1);
col = 0;
for(int j=0; j<impTable.getColumnCount(); j++) {
// if(excludes.contains(tableHeader[j]))
// continue;
XSSFCell cell = getCell(row, col);
cell.setCellValue(impTable.getValueAt(i, j).toString());
col++;
}
}
output = new FileOutputStream(file.getPath());
wb.write(output);
wb.close();
input.close();
output.close();
File pathFile = new File(path);
if(!path.equals(file.getPath())) {
if(pathFile.exists())
pathFile.delete();
file.renameTo(pathFile);
}
MessageBox.post(ProjectECRDialog.this, "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<22><>ʾ", MessageBox.INFORMATION);
dispose();
} catch (Exception e1) {
e1.printStackTrace();
MessageBox.post(ProjectECRDialog.this, "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:"+e1.getMessage(), "ERROR", MessageBox.ERROR);
}finally{
if(output!=null)
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
if(input!=null)
try {
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
private XSSFCell getCell(XSSFRow row, int i) {
XSSFCell cell = row.getCell(i);
if(cell==null)
cell = row.createCell(i);
return cell;
}
/**
* "<22><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><>ϵ<EFBFBD><CFB5><EFBFBD>
* @param id
* @param props
*/
class ExportDialog extends AbstractAIFDialog {
/**
*
*/
private static final long serialVersionUID = 1L;
private ExportDialog(){
super(ProjectECRDialog.this, true);
setTitle("<22><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
JPanel boxPanel = new JPanel(new FlowLayout());
Vector<JCheckBox> boxes = new Vector<>();
for(int i=0; i<exports.size(); i++) {
JCheckBox box = new JCheckBox(exports.get(i));
boxPanel.add(box);
boxes.add(box);
box.setSelected(true);
}
JButton okBtn = new JButton(<><C8B7>");
okBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
excludes = new Vector<>();
for(JCheckBox box : boxes) {
if(!box.isSelected()) {
excludes.add(box.getText());
}
}
dispose();
}
});
JButton cancelBtn = new JButton(<><C8A1>");
cancelBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
excludes = null;
dispose();
}
});
JPanel btnPanel = new JPanel(new FlowLayout());
btnPanel.add(okBtn);
btnPanel.add(cancelBtn);
setLayout(new BorderLayout());
add(BorderLayout.CENTER, boxPanel);
add(BorderLayout.SOUTH, btnPanel);
pack();
setSize(new Dimension(200, 200));
setDefaultLookAndFeelDecorated(true);
Dimension screen = getToolkit().getScreenSize();
setLocation((screen.width - getSize().width) / 2, (screen.height - getSize().height) / 2);
// setAlwaysOnTop(true);
setVisible(true);
}
}
}