package com.connor.jd.plm.dialogs; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.connor.jd.operations.ExcelOperation; import com.connor.jd.plm.utils.JDMethodUtil; import com.teamcenter.rac.kernel.TCComponent; import com.teamcenter.rac.kernel.TCComponentDataset; import com.teamcenter.rac.kernel.TCComponentProjectSmartFolder; import com.teamcenter.rac.kernel.TCComponentTcFile; import com.teamcenter.rac.kernel.TCException; import com.teamcenter.rac.kernel.TCSession; import com.teamcenter.rac.util.MessageBox; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.Pane; import javafx.stage.FileChooser; import javafx.stage.FileChooser.ExtensionFilter; import javafx.stage.Stage; public class QTXJCDialog extends Application { private TCSession session; private TextField road = null; private ComboBox pjsName = null; private List sfList = new ArrayList(); private TCComponent[] com; private String type; private Stage primaryStage = null; public QTXJCDialog(List sfList, TCComponent[] com, TCSession session, String type) throws TCException { this.sfList = sfList; this.com = com; this.session = session; this.type = type; // initUI(); } @Override public void start(Stage arg0) throws Exception { primaryStage = new Stage(); Pane root = new Pane(); Label pjStage = new Label("项目阶段"); pjStage.setLayoutX(37.0); pjStage.setLayoutY(51.0); pjsName = new ComboBox(); pjsName.setLayoutX(89.0); pjsName.setLayoutY(47.0); pjsName.setPrefSize(161.0, 23.0); pjsName.setValue(""); pjsName.getItems().add(""); for (int i = 0; i < sfList.size(); i++) { pjsName.getItems().add(sfList.get(i)); } Label roadLabel = new Label("路径"); roadLabel.setLayoutX(37.0); roadLabel.setLayoutY(107.0); road = new TextField(); road.setLayoutX(89.0); road.setLayoutY(103.0); road.setDisable(true); Button open = new Button("选择"); open.setLayoutX(263.0); open.setLayoutY(103.0); open.setOnAction((arg) -> { openBtn(primaryStage); }); Button export = new Button("导出"); export.setLayoutX(89.0); export.setLayoutY(158.0); export.setPrefSize(48.0, 23.0); export.setOnAction((arg) -> { exportBtn(); }); Button out = new Button("取消"); out.setLayoutX(208.0); out.setLayoutY(158.0); out.setPrefSize(48.0, 23.0); out.setOnAction((arg) -> { primaryStage.close(); }); root.getChildren().add(pjStage); root.getChildren().add(pjsName); root.getChildren().add(roadLabel); root.getChildren().add(road); root.getChildren().add(open); root.getChildren().add(export); root.getChildren().add(out); Scene scene = new Scene(root, 338.0, 207); primaryStage.setScene(scene); primaryStage.setResizable(false); primaryStage.show(); } // private void initUI() { // this.setAutoRequestFocus(true); // this.setPreferredSize(new Dimension(360, 150)); // this.setResizable(false); // // JLabel pjStage = new JLabel("项目阶段"); // pjsName.addItem(""); // for (int i = 0; i < sfList.size(); i++) { // pjsName.addItem(sfList.get(i)); // } // JLabel roadLabel = new JLabel("路径"); // // JButton open = new JButton("..."); // open.addActionListener(new ActionListener() { // @Override // public void actionPerformed(ActionEvent e) { // openBtn(); // } // }); // JButton export = new JButton("导出"); // export.addActionListener(new ActionListener() { // @Override // public void actionPerformed(ActionEvent e) { // exportBtn(); // } // }); // JButton out = new JButton("取消"); // out.addActionListener(new ActionListener() { // @Override // public void actionPerformed(ActionEvent e) { // dispose(); // } // }); // JPanel flow1 = new JPanel(); // Box b1 = Box.createHorizontalBox(); // flow1.add(b1); // // b1.add(Box.createHorizontalStrut(20)); // b1.add(pjStage); // b1.add(Box.createHorizontalStrut(50)); // b1.add(pjsName); // JPanel flow2 = new JPanel(); // Box b2 = Box.createHorizontalBox(); // flow2.add(b2); // b2.add(Box.createHorizontalStrut(20)); // b2.add(roadLabel); // b2.add(Box.createHorizontalStrut(20)); // b2.add(road); // b2.add(Box.createHorizontalStrut(80)); // b2.add(open); // JPanel btn = new JPanel(new ButtonLayout()); // btn.add(export); // btn.add(out); // JPanel center = new JPanel(new BorderLayout()); // center.add(flow1, BorderLayout.NORTH); // center.add(flow2, BorderLayout.CENTER); // center.add(btn, BorderLayout.SOUTH); // this.add(center, "Center"); // // this.pack(); // this.centerToScreen(); // this.showDialog(); // } // String roadName = null; private void openBtn(Stage primaryStage) { FileChooser chooser = new FileChooser(); chooser.setTitle("请选择要保存的文件夹"); chooser.getExtensionFilters().addAll(new ExtensionFilter("Excel工作簿(*.xlsx)", "xlsx")); File getfile = chooser.showSaveDialog(primaryStage); try { if (getfile.getAbsolutePath() != null) { String filePath = getfile.getAbsolutePath() + ".xlsx"; roadName = filePath; if (filePath.length() > 25) { road.setText(filePath.substring(0, 25) + "..."); } else { road.setText(filePath); } } } catch (Exception e) { } } private void exportBtn() { String jdname = "JDJFW"; if (roadName.equals("")) { MessageBox.post("没有选择保存路径", "错误", MessageBox.ERROR); } else { System.out.println(road.getText()); String outpath = roadName; System.out.println(outpath); // System.out.println("type是" + type); File file = getExcle(jdname, type); String sel = pjsName.getSelectionModel().getSelectedItem(); Map> jfw = new HashMap>(); if (sel.equals("")) { for (int i = 0; i < sfList.size(); i++) { TCComponentProjectSmartFolder sf = (TCComponentProjectSmartFolder) com[i]; try { TCComponent[] psf = sf.getTCProperty("project_data").getReferenceValueArray(); List jfList = new ArrayList(); for (int j = 0; j < psf.length; j++) { // TCComponentItem jf = (TCComponentItem) psf[j]; jfList.add(psf[j]); } jfw.put(sf.getProperty("object_string"), jfList); } catch (TCException e) { e.printStackTrace(); } } } else { for (int i = 0; i < sfList.size(); i++) { TCComponentProjectSmartFolder sf = (TCComponentProjectSmartFolder) com[i]; try { if (sel.equals(sf.getProperty("object_string"))) { TCComponent[] psf = sf.getTCProperty("project_data").getReferenceValueArray(); List jfList = new ArrayList(); for (int j = 0; j < psf.length; j++) { // TCComponentItem jf = (TCComponentItem) psf[j]; jfList.add(psf[j]); } jfw.put(sel, jfList); } } catch (TCException e) { e.printStackTrace(); } } } ExcelOperation.writeExcel(file, outpath, jfw); System.out.println("导出成功"); primaryStage.close(); MessageBox.post("导出成功", "信息", MessageBox.INFORMATION); } } private File getExcle(String ssx, String type) { File fmsFile = null; String allpuid = JDMethodUtil.getPrefStr(ssx, session); String[] relpuid = allpuid.split(";"); if (relpuid.length != 3) { MessageBox.post("请联系管理员检查首选项", "错误", MessageBox.ERROR); return null; } String puid = null; switch (type) { case "GT": puid = relpuid[2]; break; case "BL": puid = relpuid[1]; break; case "BX": puid = relpuid[0]; break; default: break; } if (puid == null) { MessageBox.post("请联系管理员检查首选项", "错误", MessageBox.ERROR); return null; } TCComponentDataset dateset = null; try { dateset = (TCComponentDataset) session.stringToComponent(puid); } catch (TCException e) { e.printStackTrace(); } if (dateset == null) { MessageBox.post("未找到数据集,请联系管理员检查首选项", "错误", MessageBox.ERROR); return null; } try { TCComponentTcFile[] file = dateset.getTcFiles(); if (file.length == 0 || file.length != 1) { MessageBox.post("数据集没有命名引用的文件或文件过多,请检查!", "警告", MessageBox.WARNING); return null; } else { fmsFile = file[0].getFmsFile(); } } catch (Exception e) { e.printStackTrace(); } return fmsFile; } }