1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
3 |
|
---|
4 | import java.util.regex.Matcher;
|
---|
5 | import java.util.regex.Pattern;
|
---|
6 |
|
---|
7 | import static org.openstreetmap.josm.tools.Utils.equal;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * A source entry primarily used to save the user's selection of mappaint styles,
|
---|
11 | * but also for preset sources.
|
---|
12 | */
|
---|
13 | public class SourceEntry {
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * A URL can be anything that MirroredInputStream understands, i.e.
|
---|
17 | * a local file, http://, or a file from the current jar
|
---|
18 | */
|
---|
19 | public String url;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Name is used as a namespace for color preferences and (currently) only
|
---|
23 | * one file with a name can be loaded at a time. Additional styles must
|
---|
24 | * either have the same name as the main style or no name at all.
|
---|
25 | * If no name is provided, it will be set to the default value "standard".
|
---|
26 | * The name can also be given in the xml file as attribute for the rules tag.
|
---|
27 | * (This overrides the name given in the preferences, otherwise both
|
---|
28 | * methods are equivalent.)
|
---|
29 | */
|
---|
30 | public String name;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * A short description that can be used as menu entry.
|
---|
34 | */
|
---|
35 | public String shortdescription;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * active is a boolean flag that can be used to turn the style on or off
|
---|
39 | * at runtime.
|
---|
40 | */
|
---|
41 | public boolean active;
|
---|
42 |
|
---|
43 | public SourceEntry(String url, String name, String shortdescription, Boolean active) {
|
---|
44 | this.url = url;
|
---|
45 | this.name = equal(name, "") ? null : name;
|
---|
46 | this.shortdescription = equal(shortdescription, "") ? null : shortdescription;
|
---|
47 | this.active = active;
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public boolean equals(Object obj) {
|
---|
52 | if (obj == null || getClass() != obj.getClass())
|
---|
53 | return false;
|
---|
54 | final SourceEntry other = (SourceEntry) obj;
|
---|
55 | return equal(other.url, url) &&
|
---|
56 | equal(other.name, name) &&
|
---|
57 | equal(other.shortdescription, shortdescription) &&
|
---|
58 | other.active == active;
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public int hashCode() {
|
---|
63 | int hash = 5;
|
---|
64 | hash = 89 * hash + (this.url != null ? this.url.hashCode() : 0);
|
---|
65 | hash = 89 * hash + (this.name != null ? this.name.hashCode() : 0);
|
---|
66 | hash = 89 * hash + (this.shortdescription != null ? this.shortdescription.hashCode() : 0);
|
---|
67 | hash = 89 * hash + (this.active ? 1 : 0);
|
---|
68 | return hash;
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public String toString() {
|
---|
73 | return shortdescription != null ? shortdescription : url;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * String to show in menus and error messages.
|
---|
78 | * @return Usually the shortdescription, but can be the file name
|
---|
79 | * if no shortdescription is available.
|
---|
80 | */
|
---|
81 | public String getDisplayString() {
|
---|
82 | if (shortdescription != null)
|
---|
83 | return shortdescription;
|
---|
84 | /**
|
---|
85 | * extract file part from url, e.g.:
|
---|
86 | * http://www.test.com/file.xml?format=text --> file.xml
|
---|
87 | */
|
---|
88 | Pattern p = Pattern.compile("([^/\\\\]*?)([?].*)?$");
|
---|
89 | Matcher m = p.matcher(url);
|
---|
90 | if (m.find()) {
|
---|
91 | return m.group(1);
|
---|
92 | } else {
|
---|
93 | System.err.println("Warning: Unexpected URL format: "+url);
|
---|
94 | return url;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * the name / identifier that should be used to save custom color values
|
---|
100 | * and similar stuff to the preference file
|
---|
101 | * @return the identifier; never null. Usually the result is "standard"
|
---|
102 | */
|
---|
103 | public String getPrefName() {
|
---|
104 | return name == null ? "standard" : name;
|
---|
105 | }
|
---|
106 | }
|
---|