]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/gui/pixx/PixxConfiguration.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / gui / pixx / PixxConfiguration.java
1 /*****************************************************/
2 /*          This java file is a part of the          */
3 /*                                                   */
4 /*           -  Plouf's Java IRC Client  -           */
5 /*                                                   */
6 /*   Copyright (C)  2002 - 2005 Philippe Detournay   */
7 /*                                                   */
8 /*         All contacts : theplouf@yahoo.com         */
9 /*                                                   */
10 /*  PJIRC is free software; you can redistribute     */
11 /*  it and/or modify it under the terms of the GNU   */
12 /*  General Public License as published by the       */
13 /*  Free Software Foundation; version 2 or later of  */
14 /*  the License.                                     */
15 /*                                                   */
16 /*  PJIRC is distributed in the hope that it will    */
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */
18 /*  even the implied warranty of MERCHANTABILITY or  */
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */
20 /*  General Public License for more details.         */
21 /*                                                   */
22 /*  You should have received a copy of the GNU       */
23 /*  General Public License along with PJIRC; if      */
24 /*  not, write to the Free Software Foundation,      */
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */
26 /*  MA  02111-1307  USA                              */
27 /*                                                   */
28 /*****************************************************/
29
30 package irc.gui.pixx;
31
32 import irc.IRCConfiguration;
33 import irc.RuleList;
34 import irc.TextProvider;
35
36 import java.awt.Color;
37 import java.awt.event.InputEvent;
38 import java.util.Enumeration;
39 import java.util.Hashtable;
40 import java.util.Vector;
41
42 /**
43  * NullItem.
44  */
45 class NullItem {
46 }
47
48 /**
49  * MouseEventConfiguration.
50  */
51 class MouseEventConfiguration {
52         /**
53          * Create a new MouseEventConfiguration
54          * 
55          * @param m
56          *          mouse button mack.
57          * @param c
58          *          click count.
59          */
60         public MouseEventConfiguration(int m, int c) {
61                 Mask = m;
62                 ClickCount = c;
63         }
64
65         /**
66          * Mouse button mask.
67          */
68         public int Mask;
69         /**
70          * Click count.
71          */
72         public int ClickCount;
73 }
74
75 /**
76  * PixxConfiguration.
77  */
78 public class PixxConfiguration {
79         private IRCConfiguration _config;
80         private PixxColorModel _colorModel = new PixxColorModel();
81         private TextProvider _textProvider;
82
83         private RuleList _dockingRules;
84         private Hashtable _htable;
85         private Hashtable _mouseConfig;
86         private Vector _highLightWords;
87         private Vector _nickMenuVector;
88
89         private NullItem NULL_ITEM = new NullItem();
90
91         /**
92          * Create a new PixxConfiguration
93          * 
94          * @param config
95          *          original global configuration.
96          */
97         public PixxConfiguration(IRCConfiguration config) {
98                 _config = config;
99                 _htable = new Hashtable();
100                 _mouseConfig = new Hashtable();
101                 _dockingRules = new RuleList();
102                 _dockingRules.setDefaultValue(new Boolean(false));
103                 _highLightWords = new Vector();
104                 _nickMenuVector = new Vector();
105         }
106
107         /**
108          * Get the nick menu configuration vector.
109          * 
110          * @return the nick menu vector.
111          */
112         public synchronized Vector getNickMenuVector() {
113                 return _nickMenuVector;
114         }
115
116         /**
117          * Set the text provider to be used.
118          * 
119          * @param txt
120          *          text provider.
121          */
122         public void setTextProvider(TextProvider txt) {
123                 _textProvider = txt;
124         }
125
126         /**
127          * Configure the given mouse event name.
128          * 
129          * @param eventName
130          *          event name.
131          * @param button
132          *          mouse button index.
133          * @param count
134          *          mouse click count.
135          */
136         public synchronized void setMouseConfiguration(String eventName, int button, int count) {
137                 int mask;
138                 switch (button) {
139                 case 1:
140                         mask = InputEvent.BUTTON1_MASK;
141                         break;
142                 case 2:
143                         mask = InputEvent.BUTTON2_MASK;
144                         break;
145                 case 3:
146                         mask = InputEvent.BUTTON3_MASK;
147                         break;
148                 default:
149                         mask = InputEvent.BUTTON1_MASK;
150                         break;
151                 }
152                 _mouseConfig.put(eventName.toLowerCase(java.util.Locale.ENGLISH), new MouseEventConfiguration(mask, count));
153         }
154
155         /**
156          * Check whether the given event name and the given mouse event match.
157          * 
158          * @param eventName
159          *          mouse event name.
160          * @param event
161          *          mouse event.
162          * @return true if events match, false otherwise.
163          */
164         public synchronized boolean matchMouseConfiguration(String eventName, java.awt.event.MouseEvent event) {
165                 if (getB("ignoreallmouseevents"))
166                         return false;
167                 MouseEventConfiguration config = (MouseEventConfiguration) _mouseConfig.get(eventName
168                                 .toLowerCase(java.util.Locale.ENGLISH));
169                 if (config == null)
170                         throw new Error(eventName + " : unknown mouse event name");
171                 if (config.ClickCount != event.getClickCount())
172                         return false;
173                 return (event.getModifiers() & config.Mask) != 0;
174         }
175
176         /**
177          * Set the given property to the given value. This value may be null.
178          * 
179          * @param key
180          *          property name.
181          * @param obj
182          *          property value.
183          */
184         public synchronized void set(String key, Object obj) {
185                 if (obj == null)
186                         obj = NULL_ITEM;
187                 _htable.put(key.toLowerCase(java.util.Locale.ENGLISH), obj);
188         }
189
190         /**
191          * Set the given property to the given int value.
192          * 
193          * @param key
194          *          property name.
195          * @param val
196          *          property value.
197          */
198         public synchronized void set(String key, int val) {
199                 set(key, new Integer(val));
200         }
201
202         /**
203          * Set the given property to the given boolean value.
204          * 
205          * @param key
206          *          property name.
207          * @param val
208          *          property value.
209          */
210         public synchronized void set(String key, boolean val) {
211                 set(key, new Boolean(val));
212         }
213
214         /**
215          * Get the given property value.
216          * 
217          * @param key
218          *          property name.
219          * @return the property value.
220          * @throws RuntimeException
221          *           if the property is unknown.
222          */
223         public synchronized Object get(String key) {
224                 Object v = _htable.get(key.toLowerCase(java.util.Locale.ENGLISH));
225                 if (v == null)
226                         throw new RuntimeException("Unknown configuration property " + key);
227                 if (v == NULL_ITEM)
228                         v = null;
229                 return v;
230         }
231
232         /**
233          * Get the given property value as an int value.
234          * 
235          * @param key
236          *          property name.
237          * @return the property value.
238          * @throws RuntimeException
239          *           if the property is unknown.
240          */
241         public synchronized int getI(String key) {
242                 Integer i = (Integer) get(key);
243                 return i.intValue();
244         }
245
246         /**
247          * Get the given property value as a boolean value.
248          * 
249          * @param key
250          *          property name.
251          * @return the property value.
252          * @throws RuntimeException
253          *           if the property is unknown.
254          */
255         public synchronized boolean getB(String key) {
256                 Boolean b = (Boolean) get(key);
257                 return b.booleanValue();
258         }
259
260         /**
261          * Get the given property value as String value.
262          * 
263          * @param key
264          *          property name.
265          * @return the property value.
266          * @throws RuntimeException
267          *           if the property is unknown.
268          */
269         public synchronized String getS(String key) {
270                 return (String) get(key);
271         }
272
273         /**
274          * Set highlight configuration.
275          * 
276          * @param color
277          *          hightlight color.
278          * @param nick
279          *          true if nick highlight enabled.
280          * @param words
281          *          hightlight words set.
282          */
283         public synchronized void setHighLightConfig(int color, boolean nick, Vector words) {
284                 set("highlightcolor", color);
285                 set("highlightnick", nick);
286                 _highLightWords = new Vector();
287                 for (int i = 0; i < words.size(); i++)
288                         _highLightWords.insertElementAt(words.elementAt(i), _highLightWords.size());
289         }
290
291         /**
292          * Get nick highlight flag.
293          * 
294          * @return nick highlight flag.
295          */
296         public synchronized boolean highLightNick() {
297                 return getB("highlightnick") && getB("highlight");
298         }
299
300         /**
301          * Get highlight words.
302          * 
303          * @return enumeration of String.
304          */
305         public synchronized Enumeration getHighLightWords() {
306                 if (!getB("highlight"))
307                         return new Vector().elements();
308                 return _highLightWords.elements();
309         }
310
311         /**
312          * Add a word into the highlight word list.
313          * 
314          * @param word
315          *          new word to add.
316          */
317         public synchronized void addHighLightWord(String word) {
318                 _highLightWords.insertElementAt(word, _highLightWords.size());
319         }
320
321         /**
322          * Remove a word from the highlight word list.
323          * 
324          * @param word
325          *          the word to remove.
326          */
327         public synchronized void removeHighLightWord(String word) {
328                 for (int i = 0; i < _highLightWords.size(); i++) {
329                         if (((String) _highLightWords.elementAt(i)).equals(word)) {
330                                 _highLightWords.removeElementAt(i);
331                                 i--;
332                         }
333                 }
334         }
335
336         /**
337          * Set the color model to be used.
338          * 
339          * @param model
340          *          the color model.
341          */
342         public void setColorModel(PixxColorModel model) {
343                 _colorModel = model;
344         }
345
346         /**
347          * Get the original IRC configuration.
348          * 
349          * @return original IRCConfiguration.
350          */
351         public IRCConfiguration getIRCConfiguration() {
352                 return _config;
353         }
354
355         /**
356          * Get color model in use.
357          * 
358          * @return color model.
359          */
360         public PixxColorModel getColorModel() {
361                 return _colorModel;
362         }
363
364         /**
365          * Get the color at given index.
366          * 
367          * @param i
368          *          index.
369          * @return the i'th color.
370          */
371         public Color getColor(int i) {
372                 return _colorModel.getColor(i);
373         }
374
375         /**
376          * Set the docking policy associated with the given source type and name.
377          * 
378          * @param type
379          *          source type.
380          * @param name
381          *          source name.
382          * @param action
383          *          true if source should be undocked, false otherwise.
384          */
385         public synchronized void setDockingPolicy(String type, String name, boolean action) {
386                 _dockingRules.addRule(new String[] { type, name }, new Boolean(action));
387         }
388
389         /**
390          * Get the docking policy for the given source type and name.
391          * 
392          * @param type
393          *          source type.
394          * @param name
395          *          source name.
396          * @return true if the source should be undocked, false otherwise.
397          */
398         public synchronized boolean getDockingPolicy(String type, String name) {
399                 return ((Boolean) (_dockingRules.findValue(new String[] { type, name }))).booleanValue();
400         }
401
402         /**
403          * Get formatted text associated with the given text code, with no parameter.
404          * 
405          * @param code
406          *          text code.
407          * @return formatted text.
408          */
409         public synchronized String getText(int code) {
410                 if (code < TextProvider.USER_BASE)
411                         return _config.getText(code);
412                 return _textProvider.getString(code);
413         }
414
415         /**
416          * Get formatted text associated with the given text code, with one parameter.
417          * 
418          * @param code
419          *          text code.
420          * @param p1
421          *          first parameter.
422          * @return formatted text.
423          */
424         public synchronized String getText(int code, String p1) {
425                 if (code < TextProvider.USER_BASE)
426                         return _config.getText(code, p1);
427                 return _textProvider.getString(code, p1);
428         }
429
430         /**
431          * Get formatted text associated with the given text code, with two
432          * parameters.
433          * 
434          * @param code
435          *          text code.
436          * @param p1
437          *          first parameter.
438          * @param p2
439          *          second parameter.
440          * @return formatted text.
441          */
442         public synchronized String getText(int code, String p1, String p2) {
443                 if (code < TextProvider.USER_BASE)
444                         return _config.getText(code, p1, p2);
445                 return _textProvider.getString(code, p1, p2);
446         }
447
448         /**
449          * Get formatted text associated with the given text code, with three
450          * parameters.
451          * 
452          * @param code
453          *          text code.
454          * @param p1
455          *          first parameter.
456          * @param p2
457          *          second parameter.
458          * @param p3
459          *          third parameter.
460          * @return formatted text.
461          */
462         public synchronized String getText(int code, String p1, String p2, String p3) {
463                 if (code < TextProvider.USER_BASE)
464                         return _config.getText(code, p1, p2, p3);
465                 return _textProvider.getString(code, p1, p2, p3);
466         }
467
468 }