]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/gui/pixx/NickMenuHandler.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 / NickMenuHandler.java
1 /*****************************************************/\r
2 /*          This java file is a part of the          */\r
3 /*                                                   */\r
4 /*           -  Plouf's Java IRC Client  -           */\r
5 /*                                                   */\r
6 /*   Copyright (C)  2002 - 2004 Philippe Detournay   */\r
7 /*                                                   */\r
8 /*         All contacts : theplouf@yahoo.com         */\r
9 /*                                                   */\r
10 /*  PJIRC is free software; you can redistribute     */\r
11 /*  it and/or modify it under the terms of the GNU   */\r
12 /*  General Public License as published by the       */\r
13 /*  Free Software Foundation; version 2 or later of  */\r
14 /*  the License.                                     */\r
15 /*                                                   */\r
16 /*  PJIRC is distributed in the hope that it will    */\r
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */\r
18 /*  even the implied warranty of MERCHANTABILITY or  */\r
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */\r
20 /*  General Public License for more details.         */\r
21 /*                                                   */\r
22 /*  You should have received a copy of the GNU       */\r
23 /*  General Public License along with PJIRC; if      */\r
24 /*  not, write to the Free Software Foundation,      */\r
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */\r
26 /*  MA  02111-1307  USA                              */\r
27 /*                                                   */\r
28 /*****************************************************/\r
29 \r
30 package irc.gui.pixx;\r
31 \r
32 import irc.EventDispatcher;\r
33 import irc.Source;\r
34 \r
35 import java.awt.Component;\r
36 import java.awt.MenuItem;\r
37 import java.awt.PopupMenu;\r
38 import java.awt.event.ActionEvent;\r
39 import java.awt.event.ActionListener;\r
40 import java.util.Enumeration;\r
41 \r
42 /**\r
43  * Popupmenu handling for nicknames.\r
44  */\r
45 public class NickMenuHandler implements ActionListener {\r
46         private String _selectedNick;\r
47         private String _whoisNick;\r
48         private PopupMenu _menu;\r
49         private Source _source;\r
50         private Component _parent;\r
51         private PixxConfiguration _pixxConfiguration;\r
52 \r
53         /**\r
54          * Create a new NickMenuHandler.\r
55          * \r
56          * @param config\r
57          *          the pixx configuration.\r
58          * @param parent\r
59          *          a parent component for the popup menu.\r
60          * @param source\r
61          *          the source associated with this popup menu.\r
62          */\r
63         public NickMenuHandler(PixxConfiguration config, Component parent, Source source) {\r
64                 _pixxConfiguration = config;\r
65                 _parent = parent;\r
66                 _source = source;\r
67                 _menu = new PopupMenu();\r
68                 _menu.addActionListener(this);\r
69                 _parent.add(_menu);\r
70         }\r
71 \r
72         /**\r
73          * Release this instance.\r
74          */\r
75         public void release() {\r
76                 _menu.removeActionListener(this);\r
77                 _parent.remove(_menu);\r
78                 _menu = null;\r
79                 _parent = null;\r
80                 _source = null;\r
81                 _pixxConfiguration = null;\r
82         }\r
83 \r
84         private String parameters(String on, String[] params) {\r
85                 for (int i = 0; i < on.length() - 1; i++) {\r
86                         if (on.charAt(i) == '%') {\r
87                                 char next = on.charAt(i + 1);\r
88                                 if (next == '%') {\r
89                                         String before = on.substring(0, i);\r
90                                         String after = on.substring(i + 2);\r
91                                         on = before + "%" + after;\r
92                                 } else if ((next >= '1') && (next <= '9')) {\r
93                                         int c = next - '1';\r
94                                         if (c < params.length) {\r
95                                                 String before = on.substring(0, i);\r
96                                                 String after = on.substring(i + 2);\r
97                                                 on = before + params[c] + after;\r
98                                         }\r
99                                 }\r
100                         }\r
101                 }\r
102                 return on;\r
103         }\r
104 \r
105         @Override\r
106         public void actionPerformed(ActionEvent e) {\r
107                 EventDispatcher.dispatchEventAsync(this, "actionPerformedEff", new Object[] { e });\r
108         }\r
109 \r
110         /**\r
111          * Internally used.\r
112          * \r
113          * @param e\r
114          */\r
115         public void actionPerformedEff(ActionEvent e) {\r
116                 String cmd = e.getActionCommand();\r
117                 String[] params = new String[] { _selectedNick, _source.getName(),\r
118                                 _pixxConfiguration.getIRCConfiguration().formatASL(_whoisNick), _whoisNick };\r
119                 for (int j = 0; j < _pixxConfiguration.getNickMenuVector().size(); j++) {\r
120                         String[] cmds = (String[]) _pixxConfiguration.getNickMenuVector().elementAt(j);\r
121                         if (cmds[0].equals(cmd)) {\r
122                                 for (int i = 1; i < cmds.length; i++)\r
123                                         _source.sendString(parameters(cmds[i], params));\r
124                         }\r
125                 }\r
126         }\r
127 \r
128         /**\r
129          * Open and handle a popup menu for a nickname.\r
130          * \r
131          * @param nick\r
132          *          the nickname.\r
133          * @param whois\r
134          *          the whois or asl nickname information.\r
135          * @param c\r
136          *          the component where the menu is to be displayed. This component\r
137          *          must be a child of the parent component for this menu.\r
138          * @param x\r
139          *          x-position relative to c.\r
140          * @param y\r
141          *          y-position relative to c.\r
142          */\r
143         public void popup(String nick, String whois, Component c, int x, int y) {\r
144                 if (_pixxConfiguration.getNickMenuVector().size() == 0)\r
145                         return;\r
146                 _selectedNick = nick;\r
147                 _whoisNick = whois;\r
148 \r
149                 _menu.removeAll();\r
150 \r
151                 Enumeration keys = _pixxConfiguration.getNickMenuVector().elements();\r
152                 while (keys.hasMoreElements()) {\r
153                         String[] v = (String[]) keys.nextElement();\r
154                         if (v[0].equals("--"))\r
155                                 _menu.addSeparator();\r
156                         else\r
157                                 _menu.add(new MenuItem(v[0]));\r
158                 }\r
159 \r
160                 _menu.show(c, x, y);\r
161         }\r
162 \r
163 }\r