File: SaveConfig.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (138 lines) | stat: -rw-r--r-- 3,738 bytes parent folder | download | duplicates (4)
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
package tim.prune.function.settings;

import java.awt.Rectangle;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JFileChooser;

import tim.prune.App;
import tim.prune.GenericFunction;
import tim.prune.I18nManager;
import tim.prune.config.Config;
import tim.prune.config.ConfigPaths;
import tim.prune.data.DoubleRange;
import tim.prune.data.Track;
import tim.prune.tips.TipManager;

/**
 * Class to provide the function to save the config settings
 */
public class SaveConfig extends GenericFunction
{
	/**
	 * 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()
	{
		File configFile = getConfig().getConfigFile();
		if (configFile == null) {
			configFile = ConfigPaths.getSuggestedConfigFile();
		}
		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(getConfig(), saveFile);
			if (!saveFile.getName().equals(ConfigPaths.getDefaultFilename()))
			{
				// User chose a name which isn't the default, so show a tip
				_app.showTip(TipManager.Tip_NonStandardConfigFile);
			}
		}
	}

	/**
	 * Autosave the settings file without any prompts
	 * @return true if save was successful
	 */
	public boolean silentSave() {
		return saveConfig(getConfig(), getConfig().getConfigFile());
	}

	/**
	 * Autosave has been turned on or off, so maybe need to save
	 * @param inSaveOn true if autosave was switched on
	 */
	public void autosaveSwitched(boolean inSaveOn)
	{
		File configFile = getConfig().getConfigFile();
		if (inSaveOn && configFile == null) {
			begin();
		}
		else if (!inSaveOn && configFile != null) {
			silentSave();
		}
	}

	/**
	 * Actually save the config file
	 * @param inConfig config to save
	 * @param inSaveFile file to save
	 * @return true if save was successful
	 */
	private boolean saveConfig(Config inConfig, File inSaveFile)
	{
		// Set current window position in config
		Rectangle currBounds = _app.getFrame().getBounds();
		String windowBounds = "" + currBounds.x + "x" + currBounds.y + "x"
			+ currBounds.width + "x" + currBounds.height;
		inConfig.setConfigString(Config.KEY_WINDOW_BOUNDS, windowBounds);

		final String latlonString = createLatLonStringForConfig();
		if (latlonString != null) {
			inConfig.setConfigString(Config.KEY_LATLON_RANGE, latlonString);
		}

		boolean saved = false;
		try (FileOutputStream outStream = new FileOutputStream(inSaveFile))
		{
			inConfig.getAllConfig().store(outStream, "GpsPrune config file");
			saved = true;
		} catch (IOException ioe) {
			_app.showErrorMessageNoLookup(getNameKey(),
				I18nManager.getText("error.save.failed") + " : " + ioe.getMessage());
		} catch (NullPointerException npe) {
		}
		// Remember where it was saved to
		inConfig.setConfigFile(inSaveFile);
		return saved;
	}

	/**
	 * @return semicolon-separated string containing the four values, or null
	 */
	private String createLatLonStringForConfig()
	{
		Track track = _app.getTrackInfo().getTrack();
		if (track.getNumPoints() >= 2)
		{
			final DoubleRange latRange = track.getLatRange();
			final DoubleRange lonRange = track.getLonRange();
			if (latRange.getRange() > 0.0 && lonRange.getRange() > 0.0)
			{
				return latRange.getMinimum() + ";"
					+ latRange.getMaximum() + ";"
					+ lonRange.getMinimum() + ";"
					+ lonRange.getMaximum();
			}
		}
		return null;
	}
}