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 | public SourceEntry(SourceEntry e) {
|
---|
51 | this.url = e.url;
|
---|
52 | this.name = e.name;
|
---|
53 | this.shortdescription = e.shortdescription;
|
---|
54 | this.active = e.active;
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public boolean equals(Object obj) {
|
---|
59 | if (obj == null || getClass() != obj.getClass())
|
---|
60 | return false;
|
---|
61 | final SourceEntry other = (SourceEntry) obj;
|
---|
62 | return equal(other.url, url) &&
|
---|
63 | equal(other.name, name) &&
|
---|
64 | equal(other.shortdescription, shortdescription) &&
|
---|
65 | other.active == active;
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public int hashCode() {
|
---|
70 | int hash = 5;
|
---|
71 | hash = 89 * hash + (this.url != null ? this.url.hashCode() : 0);
|
---|
72 | hash = 89 * hash + (this.name != null ? this.name.hashCode() : 0);
|
---|
73 | hash = 89 * hash + (this.shortdescription != null ? this.shortdescription.hashCode() : 0);
|
---|
74 | hash = 89 * hash + (this.active ? 1 : 0);
|
---|
75 | return hash;
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public String toString() {
|
---|
80 | return shortdescription != null ? shortdescription : url;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * String to show in menus and error messages.
|
---|
85 | * @return Usually the shortdescription, but can be the file name
|
---|
86 | * if no shortdescription is available.
|
---|
87 | */
|
---|
88 | public String getDisplayString() {
|
---|
89 | if (shortdescription != null)
|
---|
90 | return shortdescription;
|
---|
91 | /**
|
---|
92 | * extract file part from url, e.g.:
|
---|
93 | * http://www.test.com/file.xml?format=text --> file.xml
|
---|
94 | */
|
---|
95 | Pattern p = Pattern.compile("([^/\\\\]*?)([?].*)?$");
|
---|
96 | Matcher m = p.matcher(url);
|
---|
97 | if (m.find()) {
|
---|
98 | return m.group(1);
|
---|
99 | } else {
|
---|
100 | System.err.println("Warning: Unexpected URL format: "+url);
|
---|
101 | return url;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * the name / identifier that should be used to save custom color values
|
---|
107 | * and similar stuff to the preference file
|
---|
108 | * @return the identifier; never null. Usually the result is "standard"
|
---|
109 | */
|
---|
110 | public String getPrefName() {
|
---|
111 | return name == null ? "standard" : name;
|
---|
112 | }
|
---|
113 |
|
---|
114 | public boolean isLocal() {
|
---|
115 | if (url.startsWith("http://") || url.startsWith("resource://"))
|
---|
116 | return false;
|
---|
117 | return true;
|
---|
118 | }
|
---|
119 | }
|
---|