]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/IRCApplet.java
GITOLITE.txt
[irc/pjirc-ng.git] / src / main / java / IRCApplet.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 import irc.AppletFileHandler;
31 import irc.AppletImageLoader;
32 import irc.AppletSoundHandler;
33 import irc.AppletURLHandler;
34 import irc.ConfigurationLoader;
35 import irc.EventDispatcher;
36 import irc.FileHandler;
37 import irc.IRCApplication;
38 import irc.IRCConfiguration;
39 import irc.ParameterMixer;
40 import irc.ParameterProvider;
41 import irc.StartupConfiguration;
42 import irc.StreamParameterProvider;
43
44 import java.awt.FlowLayout;
45 import java.awt.Label;
46
47 /**
48  * Root IRCApplet, what is actually displayed in the browser.
49  */
50 public class IRCApplet extends java.applet.Applet implements ParameterProvider {
51         /**
52    * 
53    */
54         private static final long serialVersionUID = 1L;
55         private IRCApplication _application;
56
57         @Override
58         public void init() {
59                 try {
60                         EventDispatcher.disableBadThreadWarning();
61                         EventDispatcher.dispatchEventSyncEx(this, "startEff", new Object[0]);
62                         EventDispatcher.enableBadThreadWarning();
63                 } catch (Throwable ex) {
64                         throw new RuntimeException(ex.toString());
65                 }
66         }
67
68         @Override
69         public void destroy() {
70                 try {
71                         EventDispatcher.disableBadThreadWarning();
72                         EventDispatcher.dispatchEventSyncEx(this, "stopEff", new Object[0]);
73                         EventDispatcher.enableBadThreadWarning();
74                 } catch (Throwable ex) {
75                         throw new RuntimeException(ex.toString());
76                 }
77         }
78
79         /**
80          * Internally used.
81          */
82         public void startEff() {
83                 try {
84                         ParameterProvider provider = this;
85
86                         String useFileParameter = getParameter("fileparameter");
87                         if (useFileParameter == null)
88                                 useFileParameter = "pjirc.cfg";
89
90                         FileHandler file = new AppletFileHandler(this);
91                         provider = new ParameterMixer(provider, new StreamParameterProvider(file.getInputStream(useFileParameter)));
92
93                         ConfigurationLoader loader = new ConfigurationLoader(provider, new AppletURLHandler(getAppletContext()),
94                                         new AppletImageLoader(this), new AppletSoundHandler(this), new AppletFileHandler(this));
95                         IRCConfiguration ircConfiguration = loader.loadIRCConfiguration();
96                         StartupConfiguration startupConfiguration = loader.loadStartupConfiguration();
97
98                         _application = new IRCApplication(ircConfiguration, startupConfiguration, this);
99                         _application.init();
100                         setVisible(false);
101                         setVisible(true);
102                 } catch (Throwable e) {
103                         setLayout(new FlowLayout(FlowLayout.LEFT));
104                         add(new Label("Startup error : " + e));
105                 }
106         }
107
108         /**
109          * Internally used.
110          */
111         public void stopEff() {
112                 if (_application != null)
113                         _application.uninit();
114                 _application = null;
115
116         }
117
118         /** -- javascript support -- **/
119
120         /**
121          * Send the given string to the current source interpretor.
122          * 
123          * @param str
124          *          string to send to the interpretor.
125          */
126         public void sendString(String str) {
127                 if (_application != null)
128                         EventDispatcher.dispatchEventAsync(_application, "sendString", new Object[] { str });
129         }
130
131         /**
132          * Send the given command to the given source interpretor.
133          * 
134          * @param serverName
135          *          the source's server name, or an empty string if no server
136          *          filtering needs to be done.
137          * @param type
138          *          the source type.
139          * @param name
140          *          the source name.
141          * @param cmd
142          *          the command to send.
143          */
144         public void sendString(String serverName, String type, String name, String cmd) {
145                 if (_application != null)
146                         EventDispatcher.dispatchEventAsync(_application, "sendString", new Object[] { serverName, type, name, cmd });
147         }
148
149         /**
150          * Set the current textfield text.
151          * 
152          * @param txt
153          *          new textfield text.
154          */
155         public void setFieldText(String txt) {
156                 if (_application != null)
157                         EventDispatcher.dispatchEventAsync(_application, "setFieldText", new Object[] { txt });
158         }
159
160         /**
161          * Get the current textfield text.
162          * 
163          * @return the current textfield text.
164          */
165         public String getFieldText() {
166                 if (_application != null) {
167                         try {
168                                 return (String) EventDispatcher.dispatchEventAsyncAndWaitEx(_application, "getFieldText", new Object[0]);
169                         } catch (Throwable ex) {
170                                 throw new RuntimeException(ex.toString());
171                         }
172                 }
173                 return "";
174         }
175
176         /**
177          * Validate the current textfield text, as if user pressed return key.
178          */
179         public void validateText() {
180                 if (_application != null)
181                         EventDispatcher.dispatchEventAsync(_application, "validateText", new Object[0]);
182         }
183
184         /**
185          * Request the active source to gain focus.
186          */
187         public void requestSourceFocus() {
188                 if (_application != null)
189                         EventDispatcher.dispatchEventAsync(_application, "requestSourceFocus", new Object[0]);
190         }
191
192         /**
193          * Request the given source to gain focus.
194          * 
195          * @param serverName
196          *          the source's server name, or an empty string if no server
197          *          filtering needs to be done.
198          * @param type
199          *          the source type.
200          * @param name
201          *          the source name.
202          */
203         public void requestSourceFocus(String serverName, String type, String name) {
204                 if (_application != null)
205                         EventDispatcher.dispatchEventAsync(_application, "requestSourceFocus", new Object[] { serverName, type, name });
206         }
207
208         /**
209          * Send the given event value to the given plugin.
210          * 
211          * @param pluginName
212          *          the plugin name.
213          * @param event
214          *          the event value to be sent.
215          */
216         public void sendPluginEvent(String pluginName, Object event) {
217                 if (_application != null)
218                         EventDispatcher.dispatchEventAsync(_application, "sendPluginEvent", new Object[] { pluginName, event });
219         }
220
221         /**
222          * Get the plugin value from the given plugin name.
223          * 
224          * @param pluginName
225          *          the plugin name.
226          * @param valueName
227          *          the value name.
228          * @return the returned plugin value, or null if the plugin is not found.
229          */
230         public Object getPluginValue(String pluginName, Object valueName) {
231                 if (_application != null) {
232                         try {
233                                 return EventDispatcher.dispatchEventAsyncAndWaitEx(_application, "getPluginValue", new Object[] { pluginName,
234                                                 valueName });
235                         } catch (Throwable ex) {
236                                 throw new RuntimeException(ex.toString());
237                         }
238                 }
239                 return null;
240         }
241
242         /**
243          * Get the IRCApplication instance. For advanced use only.
244          * 
245          * @return the IRCApplication instance.
246          */
247         public IRCApplication getIRCApplication() {
248                 return _application;
249         }
250
251 }