001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.preferences; 003 004import java.awt.Color; 005import java.util.List; 006 007import org.openstreetmap.josm.tools.ColorHelper; 008 009/** 010 * Data class to hold information on a named color setting. 011 */ 012public class ColorInfo { 013 014 private String category; 015 private String source; 016 private String name; 017 private Color value; 018 private Color defaultValue; 019 020 /** 021 * Constructs a new {@code ColorInfo}. 022 */ 023 public ColorInfo() { 024 } 025 026 /** 027 * Constructs a new {@code ColorInfo}. 028 * @param category the category of the color setting 029 * @param source the source (related file), can be null 030 * @param name the color name 031 * @param value the color value set in the preferences, null if not set 032 * @param defaultValue the default value for this color setting, can be null 033 * @see org.openstreetmap.josm.data.preferences.NamedColorProperty 034 */ 035 public ColorInfo(String category, String source, String name, Color value, Color defaultValue) { 036 this.category = category; 037 this.source = source; 038 this.name = name; 039 this.value = value; 040 this.defaultValue = defaultValue; 041 } 042 043 /** 044 * Get the category. 045 * @return the category 046 */ 047 public String getCategory() { 048 return category; 049 } 050 051 /** 052 * Get the source. 053 * @return the source, can be null 054 */ 055 public String getSource() { 056 return source; 057 } 058 059 /** 060 * Get the name. 061 * @return the name 062 */ 063 public String getName() { 064 return name; 065 } 066 067 /** 068 * Get the color value in the preferences (if set). 069 * @return the color value, can be null 070 */ 071 public Color getValue() { 072 return value; 073 } 074 075 /** 076 * Get the default value for this color setting. 077 * @return the default value, can be null 078 */ 079 public Color getDefaultValue() { 080 return defaultValue; 081 } 082 083 /** 084 * Set the category. 085 * @param category the category 086 */ 087 public void setCategory(String category) { 088 this.category = category; 089 } 090 091 /** 092 * Set the source. 093 * @param source the source 094 */ 095 public void setSource(String source) { 096 this.source = source; 097 } 098 099 /** 100 * Set the name. 101 * @param name the name 102 */ 103 public void setName(String name) { 104 this.name = name; 105 } 106 107 /** 108 * Set the color value. 109 * @param value the value 110 */ 111 public void setValue(Color value) { 112 this.value = value; 113 } 114 115 /** 116 * Set the default value. 117 * @param defaultValue the default value 118 */ 119 public void setDefaultValue(Color defaultValue) { 120 this.defaultValue = defaultValue; 121 } 122 123 /** 124 * Constructs a new {@code ColorInfo} from raw preference value. 125 * @param lst the list 126 * @param isDefault if the list represents a default value or not 127 * @return corresponding {@code ColorInfo} object or null in case of invalid input 128 */ 129 public static ColorInfo fromPref(List<String> lst, boolean isDefault) { 130 if (lst == null || lst.size() < 4) { 131 return null; 132 } 133 Color clr = ColorHelper.html2color(lst.get(0)); 134 if (clr == null) { 135 return null; 136 } 137 ColorInfo info = new ColorInfo(); 138 if (isDefault) { 139 info.defaultValue = clr; 140 } else { 141 info.value = clr; 142 } 143 info.category = lst.get(1); 144 info.source = lst.get(2); 145 if (info.source.isEmpty()) { 146 info.source = null; 147 } 148 info.name = lst.get(3); 149 return info; 150 } 151 152 @Override 153 public String toString() { 154 return "ColorInfo [" + (category != null ? "category=" + category + ", " : "") 155 + (source != null ? "source=" + source + ", " : "") + (name != null ? "name=" + name + ", " : "") 156 + (value != null ? "value=" + value + ", " : "") 157 + (defaultValue != null ? "defaultValue=" + defaultValue : "") + "]"; 158 } 159}