1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.util.regex.Matcher;
|
---|
6 | import java.util.regex.Pattern;
|
---|
7 |
|
---|
8 | import static org.openstreetmap.josm.tools.Utils.equal;
|
---|
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 | * Name is used as a namespace for color preferences and (currently) only
|
---|
24 | * one file with a name can be loaded at a time. Additional styles must
|
---|
25 | * either have the same name as the main style or no name at all.
|
---|
26 | * If no name is provided, it will be set to the default value "standard".
|
---|
27 | * The name can also be given in the xml file as attribute for the rules tag.
|
---|
28 | * (This overrides the name given in the preferences, otherwise both
|
---|
29 | * methods are equivalent.)
|
---|
30 | */
|
---|
31 | public String name;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * A title that can be used as menu entry.
|
---|
35 | */
|
---|
36 | public String title;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * active is a boolean flag that can be used to turn the style on or off
|
---|
40 | * at runtime.
|
---|
41 | */
|
---|
42 | public boolean active;
|
---|
43 |
|
---|
44 | public SourceEntry(String url, String name, String shortdescription, Boolean active) {
|
---|
45 | this.url = url;
|
---|
46 | this.name = equal(name, "") ? null : name;
|
---|
47 | this.title = equal(shortdescription, "") ? null : shortdescription;
|
---|
48 | this.active = active;
|
---|
49 | }
|
---|
50 |
|
---|
51 | public SourceEntry(SourceEntry e) {
|
---|
52 | this.url = e.url;
|
---|
53 | this.name = e.name;
|
---|
54 | this.title = e.title;
|
---|
55 | this.active = e.active;
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public boolean equals(Object obj) {
|
---|
60 | if (obj == null || getClass() != obj.getClass())
|
---|
61 | return false;
|
---|
62 | final SourceEntry other = (SourceEntry) obj;
|
---|
63 | return equal(other.url, url) &&
|
---|
64 | equal(other.name, name) &&
|
---|
65 | equal(other.title, title) &&
|
---|
66 | other.active == active;
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public int hashCode() {
|
---|
71 | int hash = 5;
|
---|
72 | hash = 89 * hash + (this.url != null ? this.url.hashCode() : 0);
|
---|
73 | hash = 89 * hash + (this.name != null ? this.name.hashCode() : 0);
|
---|
74 | hash = 89 * hash + (this.title != null ? this.title.hashCode() : 0);
|
---|
75 | hash = 89 * hash + (this.active ? 1 : 0);
|
---|
76 | return hash;
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public String toString() {
|
---|
81 | return title != null ? title : url;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * String to show in menus and error messages.
|
---|
86 | * @return Usually the shortdescription, but can be the file name
|
---|
87 | * if no shortdescription is available.
|
---|
88 | */
|
---|
89 | public String getDisplayString() {
|
---|
90 | if (title != null)
|
---|
91 | return title;
|
---|
92 | else
|
---|
93 | return getFileNamePart();
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * extract file part from url, e.g.:
|
---|
98 | * http://www.test.com/file.xml?format=text --> file.xml
|
---|
99 | */
|
---|
100 | public String getFileNamePart() {
|
---|
101 | Pattern p = Pattern.compile("([^/\\\\]*?)([?].*)?$");
|
---|
102 | Matcher m = p.matcher(url);
|
---|
103 | if (m.find()) {
|
---|
104 | return m.group(1);
|
---|
105 | } else {
|
---|
106 | System.err.println("Warning: Unexpected URL format: "+url);
|
---|
107 | return url;
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * the name / identifier that should be used to save custom color values
|
---|
113 | * and similar stuff to the preference file
|
---|
114 | * @return the identifier; never null. Usually the result is "standard"
|
---|
115 | */
|
---|
116 | public String getPrefName() {
|
---|
117 | return name == null ? "standard" : name;
|
---|
118 | }
|
---|
119 |
|
---|
120 | public boolean isLocal() {
|
---|
121 | if (url.startsWith("http://") || url.startsWith("resource://"))
|
---|
122 | return false;
|
---|
123 | return true;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public String getLocalSourceDir() {
|
---|
127 | if (!isLocal())
|
---|
128 | return null;
|
---|
129 | File f = new File(url);
|
---|
130 | File dir = f.getParentFile();
|
---|
131 | if (dir == null)
|
---|
132 | return null;
|
---|
133 | return dir.getPath();
|
---|
134 | }
|
---|
135 | }
|
---|