001 // License: GPL. For details, see LICENSE file.
002 package org.openstreetmap.josm.gui.preferences;
003
004 import java.io.File;
005 import java.util.regex.Matcher;
006 import java.util.regex.Pattern;
007
008 import static org.openstreetmap.josm.tools.Utils.equal;
009
010 /**
011 * A source entry primarily used to save the user's selection of mappaint styles,
012 * but also for preset sources.
013 */
014 public class SourceEntry {
015
016 /**
017 * A URL can be anything that MirroredInputStream understands, i.e.
018 * a local file, http://, or a file from the current jar
019 */
020 public String url;
021
022 /**
023 * Name is used as a namespace for color preferences and (currently) only
024 * one file with a name can be loaded at a time. Additional styles must
025 * either have the same name as the main style or no name at all.
026 * If no name is provided, it will be set to the default value "standard".
027 * The name can also be given in the xml file as attribute for the rules tag.
028 * (This overrides the name given in the preferences, otherwise both
029 * methods are equivalent.)
030 */
031 public String name;
032
033 /**
034 * A title that can be used as menu entry.
035 */
036 public String title;
037
038 /**
039 * active is a boolean flag that can be used to turn the style on or off
040 * at runtime.
041 */
042 public boolean active;
043
044 public SourceEntry(String url, String name, String title, Boolean active) {
045 this.url = url;
046 this.name = equal(name, "") ? null : name;
047 this.title = equal(title, "") ? null : title;
048 this.active = active;
049 }
050
051 public SourceEntry(SourceEntry e) {
052 this.url = e.url;
053 this.name = e.name;
054 this.title = e.title;
055 this.active = e.active;
056 }
057
058 @Override
059 public boolean equals(Object obj) {
060 if (obj == null || getClass() != obj.getClass())
061 return false;
062 final SourceEntry other = (SourceEntry) obj;
063 return equal(other.url, url) &&
064 equal(other.name, name) &&
065 equal(other.title, title) &&
066 other.active == active;
067 }
068
069 @Override
070 public int hashCode() {
071 int hash = 5;
072 hash = 89 * hash + (this.url != null ? this.url.hashCode() : 0);
073 hash = 89 * hash + (this.name != null ? this.name.hashCode() : 0);
074 hash = 89 * hash + (this.title != null ? this.title.hashCode() : 0);
075 hash = 89 * hash + (this.active ? 1 : 0);
076 return hash;
077 }
078
079 @Override
080 public String toString() {
081 return title != null ? title : url;
082 }
083
084 /**
085 * String to show in menus and error messages.
086 * @return Usually the shortdescription, but can be the file name
087 * if no shortdescription is available.
088 */
089 public String getDisplayString() {
090 if (title != null)
091 return title;
092 else
093 return getFileNamePart();
094 }
095
096 /**
097 * extract file part from url, e.g.:
098 * http://www.test.com/file.xml?format=text --> file.xml
099 */
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 }