File: WaypointIcons.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 (56 lines) | stat: -rw-r--r-- 1,779 bytes parent folder | download | duplicates (2)
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
package tim.prune.gui.map;

import java.io.File;
import java.net.MalformedURLException;

import javax.swing.ImageIcon;

import tim.prune.config.Config;
import tim.prune.gui.IconManager;

public class WaypointIcons
{
	/** @return an icon definition to use for waypoints based on the current config */
	public static WpIconDefinition getDefinition(Config inConfig, IconManager inIconManager)
	{
		if (inConfig.getConfigBoolean(Config.KEY_WAYPOINT_ICON_CUSTOM))
		{
			final String iconPath = inConfig.getConfigString(Config.KEY_WAYPOINT_ICON_PATH);
			WpIconDefinition iconDef = getCustomIcon(iconPath);
			if (iconDef != null) {
				return iconDef;
			}
		}
		// No valid custom icon, so find out the built-in type
		final int wpType = inConfig.getConfigInt(Config.KEY_WAYPOINT_ICONS);
		if (wpType == WpIconLibrary.WAYPT_DEFAULT) {
			return null;
		}
		final int wpSize = inConfig.getConfigInt(Config.KEY_WAYPOINT_ICON_SIZE);
		return WpIconLibrary.getIconDefinition(wpType, wpSize, inIconManager);
	}

	/** @return an icon definition from the given file path, or null */
	private static WpIconDefinition getCustomIcon(String inIconPath)
	{
		if (inIconPath == null || inIconPath.isEmpty()) {
			return null;
		}
		File iconFile = new File(inIconPath);
		if (!iconFile.exists() || !iconFile.isFile() || !iconFile.canRead()) {
			return null;
		}
		try
		{
			ImageIcon icon = new ImageIcon(iconFile.toURI().toURL());
			WpIconDefinition iconDef = new WpIconDefinition(icon);
			// If icon is too small or too big, we ignore it and use a built-in one instead
			if (iconDef.getXOffset() > 2 && iconDef.getXOffset() <= 64
				&& iconDef.getYOffset() > 2 && iconDef.getYOffset() <= 64)
			{
				return iconDef;
			}
		} catch (MalformedURLException ignored) {}
		return null;
	}
}