|
9 | 9 |
|
10 | 10 | import static io.sloeber.core.Gson.GsonConverter.*;
|
11 | 11 |
|
12 |
| -import java.util.ArrayList; |
13 |
| -import java.util.List; |
| 12 | +import java.util.Collection; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.TreeMap; |
14 | 15 |
|
15 | 16 | import org.eclipse.core.runtime.IPath;
|
16 | 17 |
|
17 | 18 | import com.google.gson.JsonElement;
|
18 | 19 | import com.google.gson.JsonObject;
|
19 | 20 | import com.google.gson.JsonParseException;
|
20 | 21 |
|
21 |
| -import io.sloeber.core.common.ConfigurationPreferences; |
| 22 | +import io.sloeber.core.api.VersionNumber; |
22 | 23 |
|
23 |
| -public class ArduinoPlatformTool { |
| 24 | +public class ArduinoPlatformTool extends Node { |
24 | 25 |
|
25 | 26 | private static final String TOOLS = "tools"; //$NON-NLS-1$
|
26 |
| - private String name; |
27 |
| - private String version; |
28 |
| - private List<ArduinpPlatformToolSystem> systems = new ArrayList<>(); |
| 27 | + private String myName; |
| 28 | + private TreeMap<VersionNumber, ArduinoPlatformToolVersion> myVersions = new TreeMap<>(Collections.reverseOrder()); |
29 | 29 |
|
30 |
| - private transient ArduinoPackage pkg; |
| 30 | + private transient ArduinoPackage myParentPackage; |
31 | 31 |
|
32 | 32 | @SuppressWarnings("nls")
|
33 | 33 | public ArduinoPlatformTool(JsonElement json, ArduinoPackage pkg) {
|
34 |
| - this.pkg = pkg; |
| 34 | + myParentPackage = pkg; |
35 | 35 | JsonObject jsonObject = json.getAsJsonObject();
|
36 | 36 |
|
37 | 37 | try {
|
38 |
| - name = getSafeString(jsonObject, "name"); |
39 |
| - version = getSafeString(jsonObject, "version"); |
40 |
| - if (jsonObject.get("systems") != null) { |
41 |
| - for (JsonElement curElement : jsonObject.get("systems").getAsJsonArray()) { |
42 |
| - systems.add(new ArduinpPlatformToolSystem(curElement, this)); |
43 |
| - } |
44 |
| - } |
| 38 | + myName = getSafeString(jsonObject, "name"); |
| 39 | + addVersion(jsonObject); |
45 | 40 | } catch (Exception e) {
|
46 | 41 | throw new JsonParseException("failed to parse Tool json " + e.getMessage());
|
47 | 42 | }
|
| 43 | + } |
48 | 44 |
|
| 45 | + protected void addVersion(JsonElement json) { |
| 46 | + ArduinoPlatformToolVersion version = new ArduinoPlatformToolVersion(json, this); |
| 47 | + myVersions.put(version.getVersion(), version); |
49 | 48 | }
|
50 | 49 |
|
51 | 50 | public ArduinoPackage getPackage() {
|
52 |
| - return this.pkg; |
| 51 | + return myParentPackage; |
53 | 52 | }
|
54 | 53 |
|
| 54 | + @Override |
55 | 55 | public String getName() {
|
56 |
| - return this.name; |
| 56 | + return myName; |
57 | 57 | }
|
58 | 58 |
|
59 |
| - public String getVersion() { |
60 |
| - return this.version; |
| 59 | + public ArduinoPlatformToolVersion getVersion(VersionNumber version) { |
| 60 | + return myVersions.get(version); |
61 | 61 | }
|
62 | 62 |
|
63 |
| - public List<ArduinpPlatformToolSystem> getSystems() { |
64 |
| - return this.systems; |
| 63 | + public IPath getInstallPath() { |
| 64 | + return myParentPackage.getInstallPath().append(TOOLS).append(getID()); |
| 65 | + |
65 | 66 | }
|
66 | 67 |
|
67 |
| - public IPath getInstallPath() { |
68 |
| - return ConfigurationPreferences.getInstallationPathPackages().append(this.pkg.getName()).append(TOOLS) |
69 |
| - .append(this.name).append(this.version); |
| 68 | + @Override |
| 69 | + public Node[] getChildren() { |
| 70 | + return myVersions.values().toArray(new Node[myVersions.size()]); |
| 71 | + } |
70 | 72 |
|
| 73 | + @Override |
| 74 | + public Node getParent() { |
| 75 | + return myParentPackage; |
71 | 76 | }
|
72 | 77 |
|
73 |
| - public boolean isInstalled() { |
74 |
| - return getInstallPath().toFile().exists(); |
| 78 | + @Override |
| 79 | + public String getID() { |
| 80 | + return getName(); |
75 | 81 | }
|
76 | 82 |
|
77 |
| - /* |
78 |
| - * Get the installable for this tool on this system |
79 |
| - * May return null if none is found |
| 83 | + /** |
| 84 | + * Get the newest version of this tool |
| 85 | + * |
| 86 | + * @return the newest version of this tool |
80 | 87 | */
|
81 |
| - public ArduinoInstallable getInstallable() { |
82 |
| - for (ArduinpPlatformToolSystem system : this.systems) { |
83 |
| - if (system.isApplicable()) { |
84 |
| - return system; |
| 88 | + public ArduinoPlatformToolVersion getNewest() { |
| 89 | + return myVersions.firstEntry().getValue(); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * return the installed version with the newest version number |
| 94 | + * Null if no version is installed |
| 95 | + * |
| 96 | + * @return |
| 97 | + */ |
| 98 | + public ArduinoPlatformToolVersion getNewestInstalled() { |
| 99 | + for (ArduinoPlatformToolVersion curVersion : myVersions.values()) { |
| 100 | + if (curVersion.isInstalled()) { |
| 101 | + return curVersion; |
85 | 102 | }
|
86 | 103 | }
|
87 | 104 | return null;
|
88 | 105 | }
|
89 | 106 |
|
| 107 | + public Collection<ArduinoPlatformToolVersion> getVersions() { |
| 108 | + return myVersions.values(); |
| 109 | + } |
| 110 | + |
90 | 111 | }
|
0 commit comments