KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > client > user > UserHandler


1 package rero.client.user;
2
3 import rero.client.*;
4 import rero.util.*;
5
6 import java.util.*;
7 import java.util.regex.*;
8
9 import rero.ircfw.*;
10
11 import rero.bridges.alias.*;
12
13 import rero.gui.*;
14 import rero.gui.windows.*;
15 import rero.gui.input.*;
16
17 public class UserHandler extends Feature implements rero.gui.input.InputListener
18 {
19    protected static Pattern commandParametersPattern = Pattern.compile("/(\\w*)\\s(.*)"); // alias pattern is: /(\w*)\s(.*)
20

21    protected AliasEnvironment scriptedAliases;
22    protected HashMap commands;
23    protected BuiltInCommands builtInCommands;
24
25    protected InternalDataList ircData;
26
27    public UserHandler()
28    {
29       commands = new HashMap();
30
31       builtInCommands = new BuiltInCommands();
32    }
33
34    public Collection getScriptedAliases()
35    {
36       return scriptedAliases.getAliasList();
37    }
38
39    public void storeDataStructures(WeakHashMap data)
40    {
41       data.put("commands", this);
42    }
43  
44    public void init()
45    {
46       scriptedAliases = (AliasEnvironment) (getCapabilities().getDataStructure("aliasBridge"));
47       ircData = (InternalDataList) (getCapabilities().getDataStructure("clientInformation"));
48
49       LinkedList temp = new LinkedList();
50       getCapabilities().setupFeature(builtInCommands, temp);
51       getCapabilities().finalizeFeatures(temp);
52    }
53
54    public boolean isNickComplete(String JavaDoc text)
55    {
56       int colonIndex = text.indexOf(':');
57  
58       return (colonIndex > -1 && colonIndex == (text.indexOf(' ') - 1) && ClientUtils.isChannel(getQuery()));
59    }
60
61    public void processNickCompletion(String JavaDoc text)
62    {
63       String JavaDoc pnick = text.substring(0, text.indexOf(':'));
64       text = text.substring(pnick.length() + 1, text.length());
65  
66       getCapabilities().getChatCapabilities().sendMessage(getQuery(), ircData.nickComplete(pnick, getQuery()) + ":" + text);
67    }
68
69    public String JavaDoc getQuery()
70    {
71       return getCapabilities().getUserInterface().getQuery();
72    }
73
74    public void onInput(UserInputEvent ev)
75    {
76       if (ircData.getMyUser() != null)
77       {
78          boolean wasIdle = ircData.getMyUser().isIdle();
79
80          ircData.getMyUser().touch();
81
82          if (wasIdle)
83          {
84             getCapabilities().getUserInterface().notifyWindow(getQuery());
85          }
86       }
87
88       if (!ev.isConsumed() && ev.text.length() > 0)
89       {
90          processInput(ev.text);
91       }
92    }
93
94    public void processInput(String JavaDoc text)
95    {
96       if (text.charAt(0) == '/')
97       {
98          processCommand(text);
99       }
100       else if (isNickComplete(text))
101       {
102          processNickCompletion(text);
103       }
104       else
105       {
106          if (getQuery().length() > 0 && (getCapabilities().isConnected() || getQuery().charAt(0) == '='))
107             getCapabilities().getChatCapabilities().sendMessage(getQuery(), text);
108       }
109    }
110
111    /** this is an internal API so there is no effort to remember "prior" commands. It is assumed that the command being
112        registered doesn't exist otherwise */

113    public void registerCommand(String JavaDoc name, ClientCommand command)
114    {
115       commands.put(name, command);
116    }
117
118    public void runAlias(String JavaDoc command, String JavaDoc parameters)
119    {
120       command = command.toUpperCase();
121
122       if (scriptedAliases.isAlias(command))
123       {
124           ScriptAlias runme = scriptedAliases.getAlias(command);
125           runme.runAlias(command, parameters);
126           return;
127       }
128
129       runAliasBuiltIn(command, parameters);
130    }
131
132    public void runAliasBuiltIn(String JavaDoc command, String JavaDoc parameters)
133    {
134       if (commands.get(command) != null)
135       {
136           ClientCommand runme = (ClientCommand)commands.get(command);
137           runme.runAlias(command, parameters);
138           return;
139       }
140
141       builtInCommands.runAlias(command, parameters);
142    }
143
144    public void processCommand(String JavaDoc text)
145    {
146       String JavaDoc command, parms;
147
148       text = text.trim(); // just to be safe, you never know with user input.
149

150       StringParser parser = new StringParser(text, commandParametersPattern);
151
152       if (parser.matches())
153       {
154           command = parser.getParsedString(0);
155           parms = parser.getParsedString(1);
156       }
157       else
158       {
159           command = text.substring(1, text.length());
160           parms = "";
161       }
162
163       runAlias(command, parms);
164    }
165
166    public void processCommandBuiltIn(String JavaDoc text)
167    {
168       String JavaDoc command, parms;
169
170       text = text.trim(); // just to be safe, you never know with user input.
171

172       StringParser parser = new StringParser(text, commandParametersPattern);
173
174       if (parser.matches())
175       {
176           command = parser.getParsedString(0);
177           parms = parser.getParsedString(1);
178       }
179       else
180       {
181           command = text.substring(1, text.length());
182           parms = "";
183       }
184
185       runAliasBuiltIn(command, parms);
186    }
187 }
188
Popular Tags