KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > gui > script > SessionOperators


1 package rero.gui.script;
2
3 import rero.gui.*;
4 import rero.client.*;
5
6 import java.awt.*;
7 import javax.swing.*;
8
9 import sleep.runtime.*;
10 import sleep.interfaces.*;
11 import sleep.bridges.*;
12
13 import java.util.*;
14
15 import rero.util.ClientUtils;
16
17 public class SessionOperators implements Loadable
18 {
19    protected IRCSession session;
20
21    public SessionOperators(IRCSession _session)
22    {
23       session = _session;
24    }
25
26    private Capabilities getCapabilities()
27    {
28       return session.getCapabilities();
29    }
30
31    public boolean scriptLoaded(ScriptInstance script)
32    {
33       Hashtable env = script.getScriptEnvironment().getEnvironment();
34
35       SessionUI manipulate = new SessionUI();
36
37       env.put("&getSessionId", new getSessionId());
38       env.put("&getActiveSessionId", new getActiveSessionId());
39       env.put("&activateSession", manipulate);
40       env.put("&getTotalSessions", new getTotalSessions());
41       env.put("&setSessionColor", manipulate);
42       env.put("&getSessionColor", new getSessionColor());
43       env.put("&getSessionText", new getSessionText());
44       env.put("&setSessionText", manipulate);
45
46       env.put("&callInSession", new callCommandInSession());
47
48       env.put("&getServerHost", new getServerName());
49       env.put("&getServerPort", new getServerPort());
50       env.put("&getServerNetwork", new getServerNetwork());
51       env.put("&getServerPassword", new getServerPassword());
52       env.put("&isServerConnected", new isServerConnected());
53       env.put("&isServerSecure", new isServerSecure());
54
55       env.put("&getLogFile", new getLogFile());
56       env.put("&logMessage", new logMessage());
57       
58       return true;
59    }
60
61    private class getLogFile implements Function
62    {
63       public Scalar evaluate(String JavaDoc f, ScriptInstance si, Stack locals)
64       {
65           return SleepUtils.getScalar(BuiltInLogger.getLogFileName(getCapabilities().getSocketConnection().getSocketInformation(), BridgeUtilities.getString(locals, "%STATUS%")));
66       }
67    }
68
69    private class logMessage implements Function
70    {
71       public Scalar evaluate(String JavaDoc f, ScriptInstance si, Stack locals)
72       {
73           getCapabilities().getUserInterface().logMessage(BridgeUtilities.getString(locals, "%STATUS%"), BridgeUtilities.getString(locals, ""));
74           return SleepUtils.getEmptyScalar();
75       }
76    }
77
78
79    private class getServerName implements Function
80    {
81       public Scalar evaluate(String JavaDoc f, ScriptInstance si, Stack locals)
82       {
83           return SleepUtils.getScalar(getCapabilities().getSocketConnection().getSocketInformation().hostname);
84       }
85    }
86
87    private class getServerPort implements Function
88    {
89       public Scalar evaluate(String JavaDoc f, ScriptInstance si, Stack locals)
90       {
91           return SleepUtils.getScalar(getCapabilities().getSocketConnection().getSocketInformation().port);
92       }
93    }
94
95    private class getServerNetwork implements Function
96    {
97       public Scalar evaluate(String JavaDoc f, ScriptInstance si, Stack locals)
98       {
99           return SleepUtils.getScalar(getCapabilities().getSocketConnection().getSocketInformation().network);
100       }
101    }
102
103    private class getServerPassword implements Function
104    {
105       public Scalar evaluate(String JavaDoc f, ScriptInstance si, Stack locals)
106       {
107           return SleepUtils.getScalar(getCapabilities().getSocketConnection().getSocketInformation().password);
108       }
109    }
110
111    private class isServerConnected implements Function
112    {
113       public Scalar evaluate(String JavaDoc f, ScriptInstance si, Stack locals)
114       {
115           if (getCapabilities().getSocketConnection().getSocketInformation().isConnected)
116              return SleepUtils.getScalar("true");
117
118           return SleepUtils.getEmptyScalar();
119       }
120    }
121
122    private class isServerSecure implements Function
123    {
124       public Scalar evaluate(String JavaDoc f, ScriptInstance si, Stack locals)
125       {
126           if (getCapabilities().getSocketConnection().getSocketInformation().isSecure)
127              return SleepUtils.getScalar("true");
128
129           return SleepUtils.getEmptyScalar();
130       }
131    }
132
133    public boolean scriptUnloaded(ScriptInstance script)
134    {
135       return true;
136    }
137
138    private class getSessionText implements Function
139    {
140       public Scalar evaluate(String JavaDoc f, ScriptInstance si, Stack locals)
141       {
142          GlobalCapabilities gc = getCapabilities().getGlobalCapabilities();
143          SessionManager sm = gc.getSessionManager();
144
145          return SleepUtils.getScalar( sm.getTitleAt(BridgeUtilities.getInt(locals, sm.getIndexFor(getCapabilities()))) );
146       }
147    }
148
149    private class callCommandInSession implements Function
150    {
151       public Scalar evaluate(String JavaDoc f, ScriptInstance si, Stack locals)
152       {
153          GlobalCapabilities gc = getCapabilities().getGlobalCapabilities();
154          SessionManager sm = gc.getSessionManager();
155
156          sm.getSpecificSession(BridgeUtilities.getInt(locals, 0)).executeCommand(locals.pop().toString());
157          return SleepUtils.getEmptyScalar();
158       }
159    }
160
161    private class getSessionColor implements Function
162    {
163       public Scalar evaluate(String JavaDoc f, ScriptInstance si, Stack locals)
164       {
165          GlobalCapabilities gc = getCapabilities().getGlobalCapabilities();
166          SessionManager sm = gc.getSessionManager();
167
168          return SleepUtils.getScalar(sm.getForegroundAt(BridgeUtilities.getInt(locals, sm.getIndexFor(getCapabilities()))).getRGB() );
169       }
170    }
171
172    private class getTotalSessions implements Function
173    {
174       public Scalar evaluate(String JavaDoc f, ScriptInstance si, Stack locals)
175       {
176          GlobalCapabilities gc = getCapabilities().getGlobalCapabilities();
177          SessionManager sm = gc.getSessionManager();
178
179          return SleepUtils.getScalar(sm.getTabCount());
180       }
181    }
182
183    private class getSessionId implements Function
184    {
185       public Scalar evaluate(String JavaDoc f, ScriptInstance si, Stack locals)
186       {
187          GlobalCapabilities gc = getCapabilities().getGlobalCapabilities();
188          SessionManager sm = gc.getSessionManager();
189
190          return SleepUtils.getScalar(sm.getIndexFor(getCapabilities()));
191       }
192    }
193
194    private class getActiveSessionId implements Function
195    {
196       public Scalar evaluate(String JavaDoc f, ScriptInstance si, Stack locals)
197       {
198          GlobalCapabilities gc = getCapabilities().getGlobalCapabilities();
199          SessionManager sm = gc.getSessionManager();
200
201          return SleepUtils.getScalar(sm.getSelectedIndex());
202       }
203    }
204
205    private class SessionUI implements Function
206    {
207       public Scalar evaluate(final String JavaDoc f, ScriptInstance si, Stack locals)
208       {
209          Scalar value = (Scalar)locals.pop();
210          final int asInt = value.intValue();
211          final String JavaDoc asString = value.toString();
212
213          ClientUtils.invokeLater(new Runnable JavaDoc()
214          {
215             public void run()
216             {
217                GlobalCapabilities gc = getCapabilities().getGlobalCapabilities();
218                SessionManager sm = gc.getSessionManager();
219
220                if (f.equals("&activateSession"))
221                   sm.setSelectedIndex(asInt);
222
223                if (f.equals("&setSessionColor"))
224                   sm.setForegroundAt(sm.getIndexFor(getCapabilities()), Color.decode(asString));
225
226                if (f.equals("&setSessionText"))
227                   sm.setTitleAt(sm.getIndexFor(getCapabilities()), asString);
228             }
229          });
230
231          return SleepUtils.getEmptyScalar();
232       }
233    }
234 }
235
Popular Tags