]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/settings/SetPathsFunction.java
Version 20.4, May 2021
[GpsPrune.git] / src / tim / prune / function / settings / SetPathsFunction.java
1 package tim.prune.function.settings;
2
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.awt.GridLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8
9 import javax.swing.BorderFactory;
10 import javax.swing.JButton;
11 import javax.swing.JDialog;
12 import javax.swing.JLabel;
13 import javax.swing.JPanel;
14 import javax.swing.JTextField;
15 import javax.swing.SwingConstants;
16
17 import tim.prune.App;
18 import tim.prune.ExternalTools;
19 import tim.prune.GenericFunction;
20 import tim.prune.I18nManager;
21 import tim.prune.config.Config;
22
23 /**
24  * Function to set the paths for the external programs (eg gnuplot)
25  */
26 public class SetPathsFunction extends GenericFunction
27 {
28         /** dialog object, cached */
29         private JDialog _dialog = null;
30         /** edit boxes */
31         private JTextField[] _editFields = null;
32         /** yes/no labels */
33         private JLabel[] _installedLabels = null;
34         /** Config keys */
35         private static final String[] CONFIG_KEYS = {Config.KEY_GPSBABEL_PATH, Config.KEY_GNUPLOT_PATH, Config.KEY_EXIFTOOL_PATH};
36         /** Label keys */
37         private static final String[] LABEL_KEYS = {"gpsbabel", "gnuplot", "exiftool"};
38         /** Number of entries */
39         private static final int NUM_KEYS = CONFIG_KEYS.length;
40
41         /**
42          * Constructor from superclass
43          * @param inApp app object
44          */
45         public SetPathsFunction(App inApp)
46         {
47                 super(inApp);
48         }
49
50         /**
51          * @return key for function name
52          */
53         public String getNameKey()
54         {
55                 return "function.setpaths";
56         }
57
58         /**
59          * Show the dialog
60          */
61         public void begin()
62         {
63                 // Make dialog window
64                 if (_dialog == null)
65                 {
66                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
67                         _dialog.setLocationRelativeTo(_parentFrame);
68                         _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
69                         _dialog.getContentPane().add(makeDialogComponents());
70                         _dialog.pack();
71                 }
72                 checkPaths();
73                 // Show dialog
74                 _dialog.setVisible(true);
75         }
76
77
78         /**
79          * Make the dialog components
80          * @return panel containing gui elements
81          */
82         private JPanel makeDialogComponents()
83         {
84                 JPanel dialogPanel = new JPanel();
85                 dialogPanel.setLayout(new BorderLayout());
86                 dialogPanel.add(new JLabel(I18nManager.getText("dialog.setpaths.intro")), BorderLayout.NORTH);
87
88                 // Main panel with edit boxes for paths
89                 JPanel mainPanel = new JPanel();
90                 mainPanel.setLayout(new GridLayout(NUM_KEYS+1, 3, 10, 1));
91                 mainPanel.add(new JLabel(" "));
92                 mainPanel.add(new JLabel(" "));
93                 mainPanel.add(new JLabel(I18nManager.getText("dialog.setpaths.found")));
94                 _editFields = new JTextField[NUM_KEYS];
95                 _installedLabels = new JLabel[NUM_KEYS];
96                 for (int i=0; i<NUM_KEYS; i++)
97                 {
98                         JLabel label = new JLabel(I18nManager.getText("dialog.paths.prune." + LABEL_KEYS[i] + "path"));
99                         label.setHorizontalAlignment(SwingConstants.RIGHT);
100                         mainPanel.add(label);
101                         String configVal = Config.getConfigString(CONFIG_KEYS[i]);
102                         if (configVal == null) {configVal = "";}
103                         _editFields[i] = new JTextField(configVal);
104                         mainPanel.add(_editFields[i]);
105                         _installedLabels[i] = new JLabel("...");
106                         mainPanel.add(_installedLabels[i]);
107                 }
108                 dialogPanel.add(mainPanel, BorderLayout.CENTER);
109
110                 // button panel at bottom
111                 JPanel buttonPanel = new JPanel();
112                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
113                 JButton checkButton = new JButton(I18nManager.getText("button.check"));
114                 checkButton.addActionListener(new ActionListener() {
115                         public void actionPerformed(ActionEvent e)
116                         {
117                                 checkPaths();
118                         }
119                 });
120                 buttonPanel.add(checkButton);
121                 JButton okButton = new JButton(I18nManager.getText("button.ok"));
122                 ActionListener okListener = new ActionListener() {
123                         public void actionPerformed(ActionEvent e)
124                         {
125                                 finish();
126                         }
127                 };
128                 okButton.addActionListener(okListener);
129                 buttonPanel.add(okButton);
130                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
131                 cancelButton.addActionListener(new ActionListener() {
132                         public void actionPerformed(ActionEvent e)
133                         {
134                                 _dialog.dispose();
135                         }
136                 });
137                 buttonPanel.add(cancelButton);
138                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
139                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
140                 return dialogPanel;
141         }
142
143         /**
144          * Check all the programs to see if they're installed
145          */
146         private void checkPaths()
147         {
148                 String yesText = I18nManager.getText("dialog.about.yes");
149                 String noText = I18nManager.getText("dialog.about.no");
150                 for (int i=0; i<NUM_KEYS; i++)
151                 {
152                         String command = _editFields[i].getText();
153                         _installedLabels[i].setText("   " + (ExternalTools.isToolInstalled(i, command)?yesText:noText));
154                 }
155         }
156
157         /**
158          * Set the given paths in the configuration and exit
159          */
160         private void finish()
161         {
162                 for (int i=0; i<NUM_KEYS; i++)
163                 {
164                         String val = _editFields[i].getText();
165                         // TODO: Check path values?
166                         Config.setConfigString(CONFIG_KEYS[i], val);
167                 }
168                 _dialog.dispose();
169         }
170 }