File: SaveConfig.java

package info (click to toggle)
gpsprune 13.4-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,008 kB
  • sloc: java: 28,802; sh: 25; makefile: 17; python: 15
file content (173 lines) | stat: -rw-r--r-- 4,711 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package tim.prune.function;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;

import tim.prune.App;
import tim.prune.GenericFunction;
import tim.prune.I18nManager;
import tim.prune.config.Config;

/**
 * Class to provide the function to save the config settings
 */
public class SaveConfig extends GenericFunction
{
	private JDialog _dialog = null;

	/**
	 * Constructor
	 * @param inApp application object for callback
	 */
	public SaveConfig(App inApp)
	{
		super(inApp);
	}

	/** Get the name key */
	public String getNameKey() {
		return "function.saveconfig";
	}

	/**
	 * Begin the function
	 */
	public void begin()
	{
		// Make new dialog window (don't reuse it)
		_dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
		_dialog.setLocationRelativeTo(_parentFrame);
		_dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		_dialog.getContentPane().add(makeDialogComponents());
		_dialog.pack();
		_dialog.setVisible(true);
	}


	/**
	 * Create dialog components
	 * @return Panel containing all gui elements in dialog
	 */
	private Component makeDialogComponents()
	{
		JPanel dialogPanel = new JPanel();
		dialogPanel.setLayout(new BorderLayout());
		dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
		JLabel descLabel = new JLabel(I18nManager.getText("dialog.saveconfig.desc"));
		dialogPanel.add(descLabel, BorderLayout.NORTH);

		// Grid panel in centre
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new GridLayout(0, 2, 15, 2));
		mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
		Properties conf = Config.getAllConfig();
		Enumeration<Object> keys = conf.keys();
		while (keys.hasMoreElements())
		{
			String key = keys.nextElement().toString();
			String keyLabel = I18nManager.getText("dialog.saveconfig." + key);
			if (!keyLabel.equals("dialog.saveconfig." + key))
			{
				mainPanel.add(new JLabel(keyLabel));
				String val = conf.getProperty(key);
				if (Config.isKeyBoolean(key)) {
					val = Config.getConfigBoolean(key)?I18nManager.getText("dialog.about.yes"):I18nManager.getText("dialog.about.no");
				}
				else if (val != null && val.length() > 30) {
					val = val.substring(0, 30) + " ...";
				}
				mainPanel.add(new JLabel(val));
			}
		}
		dialogPanel.add(mainPanel, BorderLayout.CENTER);

		// button panel at bottom
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		JButton okButton = new JButton(I18nManager.getText("button.ok"));
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				finish();
			}
		});
		buttonPanel.add(okButton);
		JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				_dialog.dispose();
				_dialog = null;
			}
		});
		buttonPanel.add(cancelButton);
		dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
		return dialogPanel;
	}


	/**
	 * Finish the dialog when OK pressed
	 */
	private void finish()
	{
		File configFile = Config.getConfigFile();
		if (configFile == null) {configFile = Config.HOME_CONFIG_FILE;}
		JFileChooser chooser = new JFileChooser(configFile.getAbsoluteFile().getParent());
		chooser.setSelectedFile(configFile);
		int response = chooser.showSaveDialog(_parentFrame);
		if (response == JFileChooser.APPROVE_OPTION)
		{
			File saveFile = chooser.getSelectedFile();
			saveConfig(saveFile);
		}
		_dialog.dispose();
		_dialog = null;
	}

	/**
	 * Autosave the settings file without any prompts
	 */
	public void silentSave()
	{
		saveConfig(Config.getConfigFile());
	}

	/**
	 * Actually save the config file
	 * @param inSaveFile file to save to
	 */
	private void saveConfig(File inSaveFile)
	{
		FileOutputStream outStream = null;
		try
		{
			outStream = new FileOutputStream(inSaveFile);
			Config.getAllConfig().store(outStream, "GpsPrune config file");
		}
		catch (IOException ioe) {
			_app.showErrorMessageNoLookup(getNameKey(),
				I18nManager.getText("error.save.failed") + " : " + ioe.getMessage());
		}
		catch (NullPointerException npe) {} // no config file given
		finally {
			try {outStream.close();} catch (Exception e) {}
		}
	}
}