1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.Utils.equal;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 | import java.util.regex.Matcher;
|
---|
8 | import java.util.regex.Pattern;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * A source entry primarily used to save the user's selection of mappaint styles,
|
---|
12 | * but also for preset sources.
|
---|
13 | */
|
---|
14 | public class SourceEntry {
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * A URL can be anything that MirroredInputStream understands, i.e.
|
---|
18 | * a local file, http://, or a file from the current jar
|
---|
19 | */
|
---|
20 | public String url;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Indicates, that {@link #url} is a zip file and the resource is
|
---|
24 | * inside the zip file.
|
---|
25 | */
|
---|
26 | public boolean isZip;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * If {@link #isZip} is true, denotes the path inside the zip file.
|
---|
30 | */
|
---|
31 | public String zipEntryPath;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Name is used as a namespace for color preferences and (currently) only
|
---|
35 | * one file with a name can be loaded at a time. Additional styles must
|
---|
36 | * either have the same name as the main style or no name at all.
|
---|
37 | * If no name is provided, it will be set to the default value "standard".
|
---|
38 | * The name can also be given in the xml file as attribute for the rules tag.
|
---|
39 | * (This overrides the name given in the preferences, otherwise both
|
---|
40 | * methods are equivalent.)
|
---|
41 | */
|
---|
42 | public String name;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * A title that can be used as menu entry.
|
---|
46 | */
|
---|
47 | public String title;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * active is a boolean flag that can be used to turn the style on or off
|
---|
51 | * at runtime.
|
---|
52 | */
|
---|
53 | public boolean active;
|
---|
54 |
|
---|
55 | public SourceEntry(String url, boolean isZip, String zipEntryPath, String name, String title, boolean active) {
|
---|
56 | this.url = url;
|
---|
57 | this.isZip = isZip;
|
---|
58 | this.zipEntryPath = equal(zipEntryPath, "") ? null : zipEntryPath;
|
---|
59 | this.name = equal(name, "") ? null : name;
|
---|
60 | this.title = equal(title, "") ? null : title;
|
---|
61 | this.active = active;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public SourceEntry(String url, String name, String title, Boolean active) {
|
---|
65 | this(url, false, null, name, title, active);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public SourceEntry(SourceEntry e) {
|
---|
69 | this.url = e.url;
|
---|
70 | this.isZip = e.isZip;
|
---|
71 | this.zipEntryPath = e.zipEntryPath;
|
---|
72 | this.name = e.name;
|
---|
73 | this.title = e.title;
|
---|
74 | this.active = e.active;
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public boolean equals(Object obj) {
|
---|
79 | if (obj == null || getClass() != obj.getClass())
|
---|
80 | return false;
|
---|
81 | final SourceEntry other = (SourceEntry) obj;
|
---|
82 | return equal(other.url, url) &&
|
---|
83 | other.isZip == isZip &&
|
---|
84 | equal(other.zipEntryPath, zipEntryPath) &&
|
---|
85 | equal(other.name, name) &&
|
---|
86 | equal(other.title, title) &&
|
---|
87 | other.active == active;
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | public int hashCode() {
|
---|
92 | int hash = 5;
|
---|
93 | hash = 89 * hash + (this.url != null ? this.url.hashCode() : 0);
|
---|
94 | hash = 89 * hash + (this.isZip ? 1 : 0);
|
---|
95 | hash = 89 * hash + (this.zipEntryPath != null ? this.zipEntryPath.hashCode() : 0);
|
---|
96 | hash = 89 * hash + (this.name != null ? this.name.hashCode() : 0);
|
---|
97 | hash = 89 * hash + (this.title != null ? this.title.hashCode() : 0);
|
---|
98 | hash = 89 * hash + (this.active ? 1 : 0);
|
---|
99 | return hash;
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Override
|
---|
103 | public String toString() {
|
---|
104 | return title != null ? title : url;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * String to show in menus and error messages.
|
---|
109 | * @return Usually the shortdescription, but can be the file name
|
---|
110 | * if no shortdescription is available.
|
---|
111 | */
|
---|
112 | public String getDisplayString() {
|
---|
113 | if (title != null)
|
---|
114 | return title;
|
---|
115 | else
|
---|
116 | return getFileNamePart();
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * extract file part from url, e.g.:
|
---|
121 | * http://www.test.com/file.xml?format=text --> file.xml
|
---|
122 | */
|
---|
123 | public String getFileNamePart() {
|
---|
124 | Pattern p = Pattern.compile("([^/\\\\]*?)([?].*)?$");
|
---|
125 | Matcher m = p.matcher(url);
|
---|
126 | if (m.find()) {
|
---|
127 | return m.group(1);
|
---|
128 | } else {
|
---|
129 | System.err.println("Warning: Unexpected URL format: "+url);
|
---|
130 | return url;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * the name / identifier that should be used to save custom color values
|
---|
136 | * and similar stuff to the preference file
|
---|
137 | * @return the identifier; never null. Usually the result is "standard"
|
---|
138 | */
|
---|
139 | public String getPrefName() {
|
---|
140 | return name == null ? "standard" : name;
|
---|
141 | }
|
---|
142 |
|
---|
143 | public boolean isLocal() {
|
---|
144 | if (url.startsWith("http://") || url.startsWith("resource://"))
|
---|
145 | return false;
|
---|
146 | return true;
|
---|
147 | }
|
---|
148 |
|
---|
149 | public String getLocalSourceDir() {
|
---|
150 | if (!isLocal())
|
---|
151 | return null;
|
---|
152 | File f = new File(url);
|
---|
153 | File dir = f.getParentFile();
|
---|
154 | if (dir == null)
|
---|
155 | return null;
|
---|
156 | return dir.getPath();
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Returns the parent directory of the resource inside the zip file.
|
---|
161 | * @return the parent directory of the resource inside the zip file,
|
---|
162 | * "." if zipEntryPath is a top level file; null, if zipEntryPath is null
|
---|
163 | */
|
---|
164 | public String getZipEntryDirName() {
|
---|
165 | if (zipEntryPath == null) return null;
|
---|
166 | File file = new File(zipEntryPath);
|
---|
167 | File dir = file.getParentFile();
|
---|
168 | if (dir == null) return ".";
|
---|
169 | return dir.getPath();
|
---|
170 | }
|
---|
171 | }
|
---|