File Source: ColourManager.java
/*
P/P * Method: com.dmdirc.ui.messages.ColourManager$1__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.ui.messages;
24
25 import com.dmdirc.config.IdentityManager;
26 import com.dmdirc.interfaces.ConfigChangeListener;
27 import com.dmdirc.logger.ErrorLevel;
28 import com.dmdirc.logger.Logger;
29
30 import java.awt.Color;
31 import java.util.HashMap;
32 import java.util.Map;
33
34 /**
35 * The colour manager manages the colour scheme for the IRC client. It allows
36 * other components to use IRC colour codes instead of absolute colours.
37 * @author chris
38 */
/*
P/P * Method: void access$000()
*
* Preconditions:
* (soft) IRC_COLOURS != null
* (soft) IRC_COLOURS.length >= 16
* (soft) init'ed(DEFAULT_COLOURS[...])
* (soft) IRC_COLOURS[...] != null
*
* Postconditions:
* init'ed(IRC_COLOURS[...])
*/
39 public final class ColourManager {
40
41 /** Colour cache. */
/*
P/P * Method: com.dmdirc.ui.messages.ColourManager__static_init
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@198 != null
* init'ed(java.awt.Color.BLACK)
* init'ed(java.awt.Color.BLUE)
* init'ed(java.awt.Color.GRAY)
* init'ed(java.awt.Color.LIGHT_GRAY)
* ...
*
* Postconditions:
* COLOUR_CACHE == &new HashMap(ColourManager__static_init#1)
* DEFAULT_COLOURS == &new Color[](ColourManager__static_init#2)
* IRC_COLOURS == &new Color[](ColourManager__static_init#12)
* new Color(ColourManager__static_init#10) num objects == 1
* new Color(ColourManager__static_init#11) num objects == 1
* new Color(ColourManager__static_init#3) num objects == 1
* new Color(ColourManager__static_init#4) num objects == 1
* new Color(ColourManager__static_init#5) num objects == 1
* new Color(ColourManager__static_init#6) num objects == 1
* new Color(ColourManager__static_init#7) num objects == 1
* ...
*/
42 private static final Map<String, Color> COLOUR_CACHE = new HashMap<String, Color>();
43
44 /** Default colours used for the standard 16 IRC colours. */
45 private static final Color[] DEFAULT_COLOURS = {
46 Color.WHITE, Color.BLACK, new Color(0, 0, 127), new Color(0, 141, 0),
47 Color.RED, new Color(127, 0, 0), new Color(160, 15, 160), new Color(252, 127, 0),
48 Color.YELLOW, new Color(0, 252, 0), new Color(0, 128, 128), new Color(0, 255, 255),
49 Color.BLUE, new Color(255, 0, 255), Color.GRAY, Color.LIGHT_GRAY,
50 };
51
52 /** Actual colours we're using for the 16 IRC colours. */
53 private static Color[] IRC_COLOURS = DEFAULT_COLOURS.clone();
54
55 /** Creates a new instance of ColourManager. */
/*
P/P * Method: void com.dmdirc.ui.messages.ColourManager()
*/
56 private ColourManager() {
57 }
58
59 /**
60 * Initialises the IRC_COLOURS array.
61 */
62 private static void initColours() {
/*
P/P * Method: void initColours()
*
* Preconditions:
* (soft) IRC_COLOURS != null
* (soft) IRC_COLOURS.length >= 16
* (soft) init'ed(DEFAULT_COLOURS[...])
* (soft) IRC_COLOURS[...] != null
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@64 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@65 != null
*
* Postconditions:
* init'ed(IRC_COLOURS[...])
*
* Test Vectors:
* com.dmdirc.config.ConfigManager:hasOptionColour(...)@64: {0}, {1}
* java.awt.Color:equals(...)@68: {1}, {0}
*/
63 for (int i = 0; i < 16; i++) {
64 if (IdentityManager.getGlobalConfig().hasOptionColour("colour", String.valueOf(i))) {
65 IRC_COLOURS[i] = getColour(IdentityManager.getGlobalConfig()
66 .getOption("colour", String.valueOf(i)));
67 COLOUR_CACHE.remove(String.valueOf(i));
68 } else if (!IRC_COLOURS[i].equals(DEFAULT_COLOURS[i])) {
69 IRC_COLOURS[i] = DEFAULT_COLOURS[i];
70 COLOUR_CACHE.remove(String.valueOf(i));
71 }
72 }
73 }
74
75 /**
76 * Parses either a 1-2 digit IRC colour, or a 6 digit hex colour from the
77 * target string, and returns the corresponding colour. Returns the
78 * specified fallback colour if the spec can't be parsed.
79 * @param spec The string to parse
80 * @param fallback The colour to use if the spec isn't valid
81 * @return A colour representation of the specified string
82 */
83 public static Color parseColour(final String spec, final Color fallback) {
/*
P/P * Method: Color parseColour(String, Color)
*
* Preconditions:
* (soft) IRC_COLOURS != null
* (soft) IRC_COLOURS.length >= 1
* (soft) init'ed(IRC_COLOURS[...])
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* spec: Addr_Set{null}, Inverse{null}
* java.lang.Integer:parseInt(...)@95: {16..232-1}, {0..15}
* java.lang.String:length(...)@103: {0..5, 7..232-1}, {6}
* java.lang.String:length(...)@91: {3..232-1}, {0..2}
* java.util.Map:containsKey(...)@84: {0}, {1}
*/
84 if (COLOUR_CACHE.containsKey(spec)) {
85 return COLOUR_CACHE.get(spec);
86 }
87
88 Color res = null;
89
90 if (spec != null) {
91 if (spec.length() < 3) {
92 int num;
93
94 try {
95 num = Integer.parseInt(spec);
96 } catch (NumberFormatException ex) {
97 num = -1;
98 }
99
100 if (num >= 0 && num <= 15) {
101 res = getColour(num);
102 }
103 } else if (spec.length() == 6) {
104 res = getColour(spec);
105 }
106 }
107
108 if (res == null) {
109 Logger.userError(ErrorLevel.MEDIUM, "Invalid colour format: " + spec);
110 res = fallback;
111 } else {
112 COLOUR_CACHE.put(spec, res);
113 }
114
115 return res;
116 }
117
118 /**
119 * Parses either a 1-2 digit IRC colour, or a 6 digit hex colour from the
120 * target string, and returns the corresponding colour. Returns white if the
121 * spec can't be parsed.
122 * @param spec The string to parse
123 * @return A colour representation of the specified string
124 */
125 public static Color parseColour(final String spec) {
/*
P/P * Method: Color parseColour(String)
*
* Preconditions:
* (soft) IRC_COLOURS != null
* (soft) IRC_COLOURS.length >= 1
* (soft) init'ed(IRC_COLOURS[...])
*
* Presumptions:
* init'ed(java.awt.Color.WHITE)
*
* Postconditions:
* init'ed(return_value)
*/
126 return parseColour(spec, Color.WHITE);
127 }
128
129 /**
130 * Returns a Color object that corresponds to the specified 6-digit hex
131 * string. If the string is invalid, logs a warning and returns white.
132 * @param hex The hex string to convert into a Color
133 * @return A Color object corresponding to the hex input
134 */
135 public static Color getColour(final String hex) {
/*
P/P * Method: Color getColour(String)
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
* init'ed(java.awt.Color.WHITE)
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.util.Map:containsKey(...)@136: {0}, {1}
*/
136 if (COLOUR_CACHE.containsKey(hex)) {
137 return COLOUR_CACHE.get(hex);
138 }
139
140 Color colour = null;
141
142 try {
143 colour = Color.decode("#" + hex);
144 } catch (NumberFormatException ex) {
145 Logger.userError(ErrorLevel.MEDIUM, "Invalid colour #" + hex);
146 return Color.WHITE;
147 }
148
149 COLOUR_CACHE.put(hex, colour);
150 return colour;
151 }
152
153 /**
154 * Returns a Color object that represents the colour associated with the
155 * specified IRC colour code. If the code is not found, a warning is logged
156 * with the client's Logger class, and white is returned.
157 * @param number The IRC colour code to look up
158 * @return The corresponding Color object
159 */
160 public static Color getColour(final int number) {
161
/*
P/P * Method: Color getColour(int)
*
* Preconditions:
* (soft) IRC_COLOURS != null
* (soft) IRC_COLOURS.length - number in {-232+1..-16, 1..+Inf}
* (soft) init'ed(IRC_COLOURS[...])
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
* init'ed(java.awt.Color.WHITE)
*
* Postconditions:
* return_value == One-of{IRC_COLOURS[...], java.awt.Color.WHITE}
* init'ed(return_value)
*
* Test Vectors:
* number: {-231..-1}, {16..232-1}, {0..15}
*/
162 if (number >= 0 && number <= 15) {
163 return IRC_COLOURS[number];
164 } else {
165 Logger.userError(ErrorLevel.MEDIUM, "Invalid colour: " + number);
166 return Color.WHITE;
167 }
168 }
169
170 /**
171 * Retrieves the hex representation of the specified colour.
172 * @param colour The colour to be parsed
173 * @return A 6-digit hex string representing the colour
174 */
175 public static String getHex(final Color colour) {
/*
P/P * Method: String getHex(Color)
*
* Preconditions:
* colour != null
*
* Presumptions:
* java.awt.Color:getBlue(...)@178 in {0..255}
* java.awt.Color:getGreen(...)@177 in {0..255}
* java.awt.Color:getRed(...)@176 in {0..255}
*
* Postconditions:
* java.lang.StringBuilder:toString(...)._tainted == 0
* return_value == &java.lang.StringBuilder:toString(...)
*/
176 final int r = colour.getRed();
177 final int g = colour.getGreen();
178 final int b = colour.getBlue();
179
180 return toHex(r) + toHex(g) + toHex(b);
181 }
182
183 /**
184 * Converts the specified integer (in the range 0-255) into a hex string.
185 * @param value The integer to convert
186 * @return A char digit hex string representing the specified integer
187 */
188 private static String toHex(final int value) {
/*
P/P * Method: String toHex(int)
*
* Preconditions:
* value in {0..255}
*
* Postconditions:
* java.lang.StringBuilder:toString(...)._tainted == 0
* return_value == &java.lang.StringBuilder:toString(...)
*/
189 final char[] chars = {
190 '0', '1', '2', '3', '4', '5', '6', '7',
191 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
192 };
193
194 return ("" + chars[value / 16]) + chars[value % 16];
195 }
196
197 static {
198 IdentityManager.getGlobalConfig().addChangeListener("colour",
/*
P/P * Method: void com.dmdirc.ui.messages.ColourManager$1()
*/
199 new ConfigChangeListener() {
200 /** {@inheritDoc} */
201 @Override
202 public void configChanged(final String domain, final String key) {
/*
P/P * Method: void configChanged(String, String)
*
* Preconditions:
* (soft) init'ed(com.dmdirc.ui.messages.ColourManager__static_init.new Color[](ColourManager__static_init#2)[...])
* (soft) com/dmdirc/ui/messages/ColourManager.IRC_COLOURS != null
* (soft) com/dmdirc/ui/messages/ColourManager.IRC_COLOURS.length >= 16
* (soft) com/dmdirc/ui/messages/ColourManager.IRC_COLOURS[...] != null
*
* Postconditions:
* init'ed(com/dmdirc/ui/messages/ColourManager.IRC_COLOURS[...])
*/
203 initColours();
204 }
205 });
206
207 initColours();
208 }
209
210 }
SofCheck Inspector Build Version : 2.17854
| ColourManager.java |
2009-Jun-25 01:54:24 |
| ColourManager.class |
2009-Sep-02 17:04:17 |
| ColourManager$1.class |
2009-Sep-02 17:04:17 |