]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/SetPathsFunction.java
3ded22310994a6850a033423fb3d546f979db4d1
[GpsPrune.git] / tim / prune / function / SetPathsFunction.java
1 package tim.prune.function;
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(3, 3, 10, 1));
91                 _editFields = new JTextField[NUM_KEYS];
92                 _installedLabels = new JLabel[NUM_KEYS];
93                 for (int i=0; i<NUM_KEYS; i++)
94                 {
95                         JLabel label = new JLabel(I18nManager.getText("dialog.saveconfig.prune." + LABEL_KEYS[i] + "path"));
96                         label.setHorizontalAlignment(SwingConstants.RIGHT);
97                         mainPanel.add(label);
98                         String configVal = Config.getConfigString(CONFIG_KEYS[i]);
99                         if (configVal == null) {configVal = "";}
100                         _editFields[i] = new JTextField(configVal);
101                         mainPanel.add(_editFields[i]);
102                         _installedLabels[i] = new JLabel("...");
103                         mainPanel.add(_installedLabels[i]);
104                 }
105                 dialogPanel.add(mainPanel, BorderLayout.CENTER);
106
107                 // button panel at bottom
108                 JPanel buttonPanel = new JPanel();
109                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
110                 JButton checkButton = new JButton(I18nManager.getText("button.check"));
111                 checkButton.addActionListener(new ActionListener() {
112                         public void actionPerformed(ActionEvent e)
113                         {
114                                 checkPaths();
115                         }
116                 });
117                 buttonPanel.add(checkButton);
118                 JButton okButton = new JButton(I18nManager.getText("button.ok"));
119                 ActionListener okListener = new ActionListener() {
120                         public void actionPerformed(ActionEvent e)
121                         {
122                                 finish();
123                         }
124                 };
125                 okButton.addActionListener(okListener);
126                 buttonPanel.add(okButton);
127                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
128                 cancelButton.addActionListener(new ActionListener() {
129                         public void actionPerformed(ActionEvent e)
130                         {
131                                 _dialog.dispose();
132                         }
133                 });
134                 buttonPanel.add(cancelButton);
135                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
136                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
137                 return dialogPanel;
138         }
139
140         /**
141          * Check all the programs to see if they're installed
142          */
143         private void checkPaths()
144         {
145                 String yesText = I18nManager.getText("dialog.about.yes");
146                 String noText = I18nManager.getText("dialog.about.no");
147                 for (int i=0; i<NUM_KEYS; i++)
148                 {
149                         String command = _editFields[i].getText();
150                         _installedLabels[i].setText(ExternalTools.isToolInstalled(i, command)?yesText:noText);
151                 }
152         }
153
154         /**
155          * Set the given paths in the configuration and exit
156          */
157         private void finish()
158         {
159                 for (int i=0; i<NUM_KEYS; i++)
160                 {
161                         String val = _editFields[i].getText();
162                         // TODO: Check path values?
163                         Config.setConfigString(CONFIG_KEYS[i], val);
164                 }
165                 _dialog.dispose();
166         }
167 }