1 /*
2  * Copyright (c) 2017 Helmut Neemann
3  * Use of this source code is governed by the GPL v3 license
4  * that can be found in the LICENSE file.
5  */
6 package de.neemann.digital.builder.tt2;
7 
8 import de.neemann.digital.builder.ATF150x.ATFDialog;
9 import de.neemann.digital.builder.ExpressionToFileExporter;
10 import de.neemann.digital.core.element.Keys;
11 import de.neemann.digital.gui.SaveAsHelper;
12 import de.neemann.digital.gui.Settings;
13 import de.neemann.digital.lang.Lang;
14 
15 import javax.swing.*;
16 import java.io.File;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 
20 import static de.neemann.gui.Screen.isLinux;
21 
22 /**
23  * Starts a fitter to create a JEDEC file.
24  */
25 public class StartATF150xFitter implements ExpressionToFileExporter.PostProcess {
26     private final File fitterExe;
27     private final ATFDialog atfDialog;
28 
getFitterExe(String fitterName)29     private static File getFitterExe(String fitterName) {
30         File fitter = Settings.getInstance().get(Keys.SETTINGS_ATF1502_FITTER);
31         return new File(fitter, fitterName);
32     }
33 
34     /**
35      * Creates a new instance
36      *
37      * @param atfDialog    the dialog to show the result
38      * @param deviceNumber number of the device
39      */
StartATF150xFitter(ATFDialog atfDialog, int deviceNumber)40     public StartATF150xFitter(ATFDialog atfDialog, int deviceNumber) {
41         this(atfDialog, getFitterExe("fit" + deviceNumber + ".exe"));
42     }
43 
44     /**
45      * Creates a new instance
46      *
47      * @param atfDialog the dialog to show the result
48      * @param fitterExe fitter executable
49      */
StartATF150xFitter(ATFDialog atfDialog, File fitterExe)50     private StartATF150xFitter(ATFDialog atfDialog, File fitterExe) {
51         this.atfDialog = atfDialog;
52         this.fitterExe = fitterExe;
53     }
54 
55     @Override
execute(File file)56     public File execute(File file) throws IOException {
57         final String tt2Name = file.getName();
58         if (tt2Name.indexOf(' ') >= 0)
59             throw new IOException(Lang.get("err_whiteSpaceNotAllowedInTT2Name"));
60 
61         ArrayList<String> args = new ArrayList<>();
62         if (isLinux())
63             args.add("wine");
64         args.add(fitterExe.getPath());
65         args.add(tt2Name);
66 
67         try {
68             String message = new OSExecute(args)
69                     .setEnvVar("FITTERDIR", fitterExe.getParentFile().getPath())
70                     .setWorkingDir(file.getParentFile())
71                     .startAndWait();
72 
73             SwingUtilities.invokeLater(() -> atfDialog.setFitterResult(message));
74 
75             return SaveAsHelper.checkSuffix(file, "jed");
76         } catch (IOException e) {
77             throw new IOException(Lang.get("err_errorRunningFitter"), e);
78         }
79     }
80 
81     @Override
getName()82     public String getName() {
83         return Lang.get("msg_startExternalFitter");
84     }
85 }
86