source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/SourceEntry.java@ 6248

Last change on this file since 6248 was 6248, checked in by Don-vip, 11 years ago

Rework console output:

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