File Source: NickColourPlugin.java
/*
P/P * Method: com.dmdirc.addons.nickcolours.NickColourPlugin__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.addons.nickcolours;
24
25 import com.dmdirc.Channel;
26 import com.dmdirc.ChannelClientProperty;
27 import com.dmdirc.actions.ActionManager;
28 import com.dmdirc.actions.interfaces.ActionType;
29 import com.dmdirc.actions.CoreActionType;
30 import com.dmdirc.config.IdentityManager;
31 import com.dmdirc.config.prefs.PreferencesCategory;
32 import com.dmdirc.config.prefs.PreferencesManager;
33 import com.dmdirc.config.prefs.PreferencesSetting;
34 import com.dmdirc.config.prefs.PreferencesType;
35 import com.dmdirc.interfaces.ActionListener;
36 import com.dmdirc.parser.irc.ChannelClientInfo;
37 import com.dmdirc.parser.irc.ChannelInfo;
38 import com.dmdirc.parser.irc.ClientInfo;
39 import com.dmdirc.plugins.Plugin;
40 import com.dmdirc.ui.messages.ColourManager;
41
42 import java.awt.Color;
43 import java.util.ArrayList;
44 import java.util.List;
45 import java.util.Map;
46
47 /**
48 * Provides various features related to nickname colouring.
49 *
50 * @author chris
51 */
52 public final class NickColourPlugin extends Plugin implements ActionListener {
53
54 /** "Random" colours to use to colour nicknames. */
55 private String[] randColours = new String[] {
56 "E90E7F", "8E55E9", "B30E0E", "18B33C",
57 "58ADB3", "9E54B3", "B39875", "3176B3",
58 };
59
60 /** Creates a new instance of NickColourPlugin. */
61 public NickColourPlugin() {
/*
P/P * Method: void com.dmdirc.addons.nickcolours.NickColourPlugin()
*
* Postconditions:
* this.randColours == &new String[](NickColourPlugin#1)
* new String[](NickColourPlugin#1) num objects == 1
* this.randColours.length == 8
* this.randColours[0] == &"E90E7F"
* this.randColours[1] == &"8E55E9"
* this.randColours[2] == &"B30E0E"
* this.randColours[3] == &"18B33C"
* this.randColours[4] == &"58ADB3"
* this.randColours[5] == &"9E54B3"
* this.randColours[6] == &"B39875"
* ...
*/
62 super();
63 }
64
65 /** {@inheritDoc} */
66 @Override
67 public void processEvent(final ActionType type, final StringBuffer format,
68 final Object... arguments) {
/*
P/P * Method: void processEvent(ActionType, StringBuffer, Object[])
*
* Preconditions:
* type != null
* (soft) arguments != null
* (soft) arguments.length >= 2
* (soft) arguments[0] != null
* (soft) arguments[1] != null
* (soft) this.randColours != null
* (soft) this.randColours.length >= 1
* (soft) init'ed(this.randColours[...])
*
* Presumptions:
* com.dmdirc.Channel:getChannelInfo(...)@70 != null
* com.dmdirc.Channel:getServer(...)@71 != null
* com.dmdirc.Channel:getServer(...)@77 != null
* init'ed(com.dmdirc.actions.CoreActionType.CHANNEL_GOTNAMES)
* init'ed(com.dmdirc.actions.CoreActionType.CHANNEL_JOIN)
* ...
*
* Test Vectors:
* java.lang.Object:equals(...)@69: {0}, {1}
* java.lang.Object:equals(...)@76: {0}, {1}
*/
69 if (type.equals(CoreActionType.CHANNEL_GOTNAMES)) {
70 final ChannelInfo chanInfo = ((Channel) arguments[0]).getChannelInfo();
71 final String network = ((Channel) arguments[0]).getServer().getNetwork();
72
73 for (ChannelClientInfo client : chanInfo.getChannelClients()) {
74 colourClient(network, client);
75 }
76 } else if (type.equals(CoreActionType.CHANNEL_JOIN)) {
77 final String network = ((Channel) arguments[0]).getServer().getNetwork();
78
79 colourClient(network, (ChannelClientInfo) arguments[1]);
80 }
81 }
82
83 /**
84 * Colours the specified client according to the user's config.
85 *
86 * @param network The network to use for the colouring
87 * @param client The client to be coloured
88 */
89 private void colourClient(final String network, final ChannelClientInfo client) {
/*
P/P * Method: void colourClient(String, ChannelClientInfo)
*
* Preconditions:
* client != null
* (soft) this.randColours != null
* (soft) this.randColours.length >= 1
* (soft) init'ed(this.randColours[...])
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@102 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@108 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@110 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@97 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@99 != null
* ...
*
* Test Vectors:
* com.dmdirc.config.ConfigManager:getOptionBool(...)@102: {0}, {1}
* com.dmdirc.config.ConfigManager:getOptionBool(...)@97: {0}, {1}
* com.dmdirc.config.ConfigManager:hasOptionString(...)@108: {0}, {1}
* com.dmdirc.config.ConfigManager:hasOptionString(...)@110: {0}, {1}
* java.lang.Object:equals(...)@97: {0}, {1}
*/
90 final Map map = client.getMap();
91 final ClientInfo myself = client.getClient().getParser().getMyself();
92 final String nickOption1 = "color:"
93 + client.getClient().getParser().getIRCStringConverter().toLowerCase(network + ":" + client.getNickname());
94 final String nickOption2 = "color:"
95 + client.getClient().getParser().getIRCStringConverter().toLowerCase("*:" + client.getNickname());
96
97 if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "useowncolour")
98 && client.getClient().equals(myself)) {
99 final Color color = ColourManager.parseColour(
100 IdentityManager.getGlobalConfig().getOption(getDomain(), "owncolour"));
101 putColour(map, color, color);
102 } else if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "userandomcolour")) {
103 putColour(map, getColour(client.getNickname()), getColour(client.getNickname()));
104 }
105
106 String[] parts = null;
107
108 if (IdentityManager.getGlobalConfig().hasOptionString(getDomain(), nickOption1)) {
109 parts = getParts(nickOption1);
110 } else if (IdentityManager.getGlobalConfig().hasOptionString(getDomain(), nickOption2)) {
111 parts = getParts(nickOption2);
112 }
113
114 if (parts != null) {
115 Color textColor = null;
116 Color nickColor = null;
117
118 if (parts[0] != null) {
119 textColor = ColourManager.parseColour(parts[0], null);
120 }
121 if (parts[1] != null) {
122 nickColor = ColourManager.parseColour(parts[1], null);
123 }
124
125 putColour(map, textColor, nickColor);
126 }
127 }
128
129 /**
130 * Puts the specified colour into the given map. The keys are determined
131 * by config settings.
132 *
133 * @param map The map to use
134 * @param textColour Text colour to be inserted
135 * @param nickColour Nick colour to be inserted
136 */
137 @SuppressWarnings("unchecked")
138 private void putColour(final Map map, final Color textColour, final Color nickColour) {
/*
P/P * Method: void putColour(Map, Color, Color)
*
* Preconditions:
* (soft) map != null
*
* Presumptions:
* init'ed(com.dmdirc.ChannelClientProperty.NICKLIST_FOREGROUND)
* init'ed(com.dmdirc.ChannelClientProperty.TEXT_FOREGROUND)
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@139 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@144 != null
*
* Test Vectors:
* nickColour: Addr_Set{null}, Inverse{null}
* textColour: Addr_Set{null}, Inverse{null}
* com.dmdirc.config.ConfigManager:getOptionBool(...)@139: {0}, {1}
* com.dmdirc.config.ConfigManager:getOptionBool(...)@144: {0}, {1}
*/
139 if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "settext")
140 && textColour != null) {
141 map.put(ChannelClientProperty.TEXT_FOREGROUND, textColour);
142 }
143
144 if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "setnicklist")
145 && nickColour != null) {
146 map.put(ChannelClientProperty.NICKLIST_FOREGROUND, nickColour);
147 }
148 }
149
150 /**
151 * Retrieves a pseudo-random colour for the specified nickname.
152 *
153 * @param nick The nickname of the client whose colour we're determining
154 * @return Colour of the specified nickname
155 */
156 private Color getColour(final String nick) {
/*
P/P * Method: Color getColour(String)
*
* Preconditions:
* nick != null
* this.randColours != null
* this.randColours.length >= 1
* (soft) init'ed(this.randColours[...])
*
* Postconditions:
* init'ed(return_value)
*/
157 int count = 0;
158
159 for (int i = 0; i < nick.length(); i++) {
160 count += nick.charAt(i);
161 }
162
163 count = count % randColours.length;
164
165 return ColourManager.parseColour(randColours[count]);
166 }
167
168 /**
169 * Reads the nick colour data from the config.
170 *
171 * @return A multi-dimensional array of nick colour info.
172 */
173 public Object[][] getData() {
/*
P/P * Method: Object[][] getData()
*
* Presumptions:
* com.dmdirc.config.ConfigManager:getOptions(...)@176 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@176 != null
* java.lang.String:indexOf(...)@179 <= 232-2
* java.util.Iterator:next(...)@176 != null
* java.util.List:size(...)@187 >= 1
* ...
*
* Postconditions:
* return_value == &new Object[][](getData#3)
* new Object[](getData#3) num objects in {1..232-1}
* new Object[](getData#3).length == 4
* new Object[][](getData#3) num objects == 1
* return_value.length in {1..232-1}
* possibly_updated(return_value[...])
* new Object[](getData#3) num objects == java.util.List:size(...)@187
*
* Test Vectors:
* java.lang.String:startsWith(...)@177: {0}, {1}
* java.util.Iterator:hasNext(...)@176: {0}, {1}
* java.util.Iterator:hasNext(...)@190: {0}, {1}
*/
174 final List<Object[]> data = new ArrayList<Object[]>();
175
176 for (String key : IdentityManager.getGlobalConfig().getOptions(getDomain()).keySet()) {
177 if (key.startsWith("color:")) {
178 final String network = key.substring(6, key.indexOf(':', 6));
179 final String user = key.substring(1 + key.indexOf(':', 6));
180 final String[] parts = getParts(key);
181
182
183 data.add(new Object[]{network, user, parts[0], parts[1]});
184 }
185 }
186
187 final Object[][] res = new Object[data.size()][4];
188
189 int i = 0;
190 for (Object[] row : data) {
191 res[i] = row;
192
193 i++;
194 }
195
196 return res;
197 }
198
199 /**
200 * Retrieves the config option with the specified key, and returns an
201 * array of the colours that should be used for it.
202 *
203 * @param key The config key to look up
204 * @return The colours specified by the given key
205 */
206 private String[] getParts(final String key) {
/*
P/P * Method: String[] getParts(String)
*
* Presumptions:
* com.dmdirc.config.ConfigManager:getOption(...)@207 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@207 != null
*
* Postconditions:
* java.lang.String:split(...)._tainted == 0
* java.lang.String:split(...).length == undefined
* java.lang.String:split(...).length == 0, if init'ed
* java.lang.String:split(...)[0] == undefined
* java.lang.String:split(...)[0] == null
* return_value == One-of{&new String[](getParts#1), &java.lang.String:split(...), &new String[](getParts#2)}
* return_value in Addr_Set{&java.lang.String:split(...),&new String[](getParts#1)}
* new String[](getParts#1) num objects <= 1
* new String[](getParts#1).length == 2
* new String[](getParts#1)[0] == null
* ...
*/
207 String[] parts = IdentityManager.getGlobalConfig().getOption(getDomain(), key).split(":");
208
209 if (parts.length == 0) {
210 parts = new String[]{null, null};
211 } else if (parts.length == 1) {
212 parts = new String[]{parts[0], null};
213 }
214
215 return parts;
216 }
217
218 /** {@inheritDoc} */
219 @Override
220 public void onLoad() {
/*
P/P * Method: void onLoad()
*
* Presumptions:
* init'ed(com.dmdirc.actions.CoreActionType.CHANNEL_GOTNAMES)
* init'ed(com.dmdirc.actions.CoreActionType.CHANNEL_JOIN)
* com.dmdirc.config.ConfigManager:getOptionList(...)@222 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@221 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@222 != null
*
* Postconditions:
* possibly_updated(this.randColours)
*
* Test Vectors:
* com.dmdirc.config.ConfigManager:hasOptionString(...)@221: {0}, {1}
*/
221 if (IdentityManager.getGlobalConfig().hasOptionString(getDomain(), "randomcolours")) {
222 randColours = IdentityManager.getGlobalConfig().getOptionList(getDomain(), "randomcolours").toArray(new String[0]);
223 }
224
225 ActionManager.addListener(this, CoreActionType.CHANNEL_GOTNAMES,
226 CoreActionType.CHANNEL_JOIN);
227 }
228
229 /** {@inheritDoc} */
230 @Override
231 public void onUnload() {
/*
P/P * Method: void onUnload()
*/
232 ActionManager.removeListener(this);
233 }
234
235 /** {@inheritDoc} */
236 @Override
237 public void showConfig(final PreferencesManager manager) {
/*
P/P * Method: void showConfig(PreferencesManager)
*
* Preconditions:
* manager != null
*
* Presumptions:
* com.dmdirc.config.prefs.PreferencesManager:getCategory(...)@266 != null
* init'ed(com.dmdirc.config.prefs.PreferencesType.BOOLEAN)
* init'ed(com.dmdirc.config.prefs.PreferencesType.COLOUR)
*/
238 final PreferencesCategory general = new PreferencesCategory("Nick Colours",
239 "General configuration for NickColour plugin.");
240 final PreferencesCategory colours = new PreferencesCategory("Colours",
241 "Set colours for specific nicknames.", new NickColourPanel(this));
242
243 general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
244 "ui", "shownickcoloursintext", "Show colours in text" +
245 "area", "Colour nicknames in main text area?"));
246 general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
247 "ui", "shownickcoloursinnicklist", "Show colours in" +
248 " nick list", "Colour nicknames in channel nick lists?"));
249 general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
250 getDomain(), "settext", "Set colours in textarea",
251 "Should the plugin set the textarea colour of nicks?"));
252 general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
253 getDomain(), "setnicklist", "Set colours in nick list",
254 "Should the plugin set the nick list colour of nicks?"));
255 general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
256 getDomain(), "userandomcolour", "Use random colour",
257 "Use a pseudo-random colour for each person?"));
258 general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
259 getDomain(), "useowncolour", "Use colour for own nick",
260 "Always use the same colour for our own nickname?"));
261 general.addSetting(new PreferencesSetting(PreferencesType.COLOUR,
262 getDomain(), "owncolour", "Colour to use for own nick",
263 "Colour used for our own nickname, if above setting is enabled."));
264
265 general.addSubCategory(colours);
266 manager.getCategory("Plugins").addSubCategory(general);
267 }
268
269 }
SofCheck Inspector Build Version : 2.17854
| NickColourPlugin.java |
2009-Jun-25 01:54:24 |
| NickColourPlugin.class |
2009-Sep-02 17:04:15 |