-
Notifications
You must be signed in to change notification settings - Fork 446
Expand file tree
/
Copy pathsparkplug_dev_guide.html
More file actions
970 lines (778 loc) · 32.5 KB
/
Copy pathsparkplug_dev_guide.html
File metadata and controls
970 lines (778 loc) · 32.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
<html>
<head>
<title>Plugin Development Guide</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>What are Spark plugins?</h1>
<p>A plugin dynamically extends the features of the Spark instant messaging client.
Use them to customize Spark for your business or organization or to add an innovative twist to instant messaging. The
extensive plugin API allows for complete client flexibility but is still simple and (we hope!) fun to use.</p>
<p>This guide provides a high level overview of the Plugin API and examples of several common client customizations.
Or, jump directly into the <a href="api/index.html">JavaDocs</a>.
</p>
<h2>Contents</h2>
This document contains the following information:
<ul>
<li><a href="#clientOverview">Overview of the Spark Client</a>
<li><a href="#overview">Overview of the Plugin API</a>
<li><a href="api/index.html">Plugin JavaDocs</a>
<li><a href="#plugins">Structure Of A Plugin</a></li>
<li><a href="#gettingStarted">Getting started writing your first plugin</a></li>
<li><a href="#examples">Spark Examples and How-To's. Many examples of the most commonly asked questions when developing with Spark.</a>
</ul>
<h2><a id="clientOverview">Overview of the Spark Client</a></h2>
<p>
The Spark client is designed with the idea that most users find the different aspects of a chat client familiar and easy to use.
All components you see below are either accessible from the Workspace or ChatRoom object and can be manipulated based on your
needs.
</p>
<img src="images/contact-list.png" >
<br/>
<img src="images/chat-room.png">
<h2><a id="overview">Overview of the Spark API</a></h2>
The Spark API provides a framework for adding extensions on top of the protocol and/or UI of the Spark client. For example, you could write your own message filter or add a button to a chat room and send files using the File Transfer API. The Spark API has the following characteristics:
<ul>
<li>Several event listeners to either intercept, be informed of, or execute custom code in response to a particular IM event.
<li>Thorough tie-ins to the UI to allow for customization from simple icon changes, to adding buttons, to adding your own menu items.
<li>Ability to add your own XMPP functions using the SMACK API.
<li>Managers - Managers allow for better (lazy) loading of particular areas within the Spark client as well as providing access points to the system. Some of the more relevant managers are:
<ul>
<li> <a href="api/org/jivesoftware/spark/SparkManager.html">SparkManager</a> -- Acts as the central manager for all of Spark. You use this manager to get instances of ChatManager, SessionManager, SoundManager, and UserManager.
<li> <a href="api/org/jivesoftware/spark/ChatManager.html">ChatManager</a> -- Handles registration of most chat listeners and filters, as well as creation and retrieval of chat rooms. It is also used to retrieve the UI of the ChatFrame.
<li> <a href="api/org/jivesoftware/spark/SessionManager.html">SessionManager</a> -- Contains the information about the current session, such as the server connected to, the handling of connection errors and notification of personal presence changes.
<li> <a href="api/org/jivesoftware/spark/SoundManager.html">SoundManager</a> -- Used to play sounds.
</ul>
<li> Event Handlers -- Spark contains numerous listeners and handlers to allow more pluggability into the Spark client. Some of the more common listeners and handlers are:
<ul>
<li> <a href="api/org/jivesoftware/spark/ui/ChatRoomListener.html">ChatRoomListener</a> -- Allows the plugin to listen for chat rooms being opened, closed and activated. You would generally use this to customize individual chat rooms.
<li> <a href="api/org/jivesoftware/spark/ui/MessageListener.html">MessageListener</a> -- Allows for notification when a message has been received or sent.
<li> <a href="api/org/jivesoftware/spark/ui/ContactGroupListener.html">ContactGroupListener</a> -- Allows for notification of changes to a Contact Group.
<li> <a href="api/org/jivesoftware/spark/ui/ContactListListener.html">ContactListListener</a> -- Allows for notification of changes to the Contact List.
<li> <a href="api/org/jivesoftware/spark/plugin/impl/filetransfer/transfer/TransferListener.html">TransferListener</a> -- Allows you to intercept File transfers.
<li> <a href="api/org/jivesoftware/spark/plugin/ContextMenuListener.html">ContextMenuListener</a> -- Allows for the addition or removal of actions or menu items to right-click (context menu) popups.
<li> <a href="api/org/jivesoftware/spark/ui/PresenceListener.html">PresenceListener</a> -- Allows for notification when Spark presence changes.
<li> <a href="api/org/jivesoftware/spark/ui/ContactItemHandler.html">ContactItemHandler</a> -- Allows the plugin to control the effects of presence changes within a ContactItem and the associated invoke call.
</ul>
<li> Components -- Spark contains many Swing components that will regularly be used during the creation of your plugin. Some of the more commonly used components are :
<ul>
<li> <a href="api/org/jivesoftware/MainWindow.html">MainWindow</a> -- The frame containing the Contact List. You use MainWindow to add new tabs, menu items, or force focus.
<li> <a href="api/org/jivesoftware/spark/ui/ChatRoom.html">ChatRoom</a> -- The base abstract class of all chat rooms within Spark. Known implementations are ChatRoomImpl and GroupChatRoom.
<li> <a href="api/org/jivesoftware/spark/ui/ChatArea.html">ChatArea</a> -- The base chat viewer for both the TranscriptWindow and ChatInputEditor.
<li> <a href="api/org/jivesoftware/spark/ui/ContactList.html">ContactList</a> -- The ContactList UI in Spark.
<li> <a href="api/org/jivesoftware/spark/ui/ChatRoomButton.html">ChatRoomButton</a> -- The button that should be used to conform to the look and feel of other buttons within a ChatRoom.
</ul>
</ul>
<h2><a id="plugins">Structure of a Plugin</a></h2>
<p>Plugins are shipped as compressed JAR (Java Archive) files. The files in a plugin archive are as follows:</p>
<fieldset>
<legend>Plugin Structure</legend>
<pre>myplugin.jar!/
|- plugin.xml <- Plugin definition file
|- libs/ <- Contains all the class archives needed to run this plugin.
</pre>
</fieldset>
<p>
The <tt>plugin.xml</tt> file specifies the main Plugin class. A sample
file might look like the following:
</p>
<fieldset>
<legend>Sample plugin.xml</legend>
<pre class="xml">
<?xml version="1.0" encoding="UTF-8"?>
<span class="comment"><!-- Google Plugin Structure --></span>
<plugin>
<name>Google Desktop Plugin</name>
<class>com.examples.plugins.GooglePlugin</class>
<author>Derek DeMoro</author>
<version>1.0</version>
<description>Enables users to find files and emails relating to users using Google Desktop technology.</description>
<email>ddman@jivesoftware.com</email>
<minSparkVersion>2.6.0</minSparkVersion>
<os>Windows,Linux,Mac</os>
</plugin>
</pre>
</fieldset>
Use Maven and create <code>pom.xml</code> file that extends parent POM
<code>org.igniterealtime.spark.plugins:plugin</code>.
<pre class="xml">
<parent>
<groupId>org.igniterealtime.spark.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.1.0-SNAPSHOT</version>
</parent>
<artifactId>myplugin</artifactId>
<version>0.1.0</version>
<name>My Plugin</name>
</pre>
Then execute Maven command <code>mvn package</code> to create <code>/target/myplugin.jar</code> file.
<h2>Installing your Plugin</h2>
<p>
You only need to drop your newly created <tt>jar file</tt> into the plugins directory of your Spark client install.
</p>
<fieldset>
<legend>Directory Structure</legend>
<pre>Spark/
|- plugins/ <- Put your plugin jar file here
|- lib/ <- The main classes and libraries needed to run the Spark
|- resources/ <- Contains other supportive documents and libraries
|- docs/ <- Help Manuals and the JavaDoc to help you develop your own plugins.
</pre>
</fieldset>
<p>Your plugin class must implement the
<tt><a href="api/org/jivesoftware/spark/plugin/Plugin.html">Plugin</a></tt>
interface from the <a href="api/index.html">Spark Client API</a>. The Plugin interface has
methods for initializing and shutting down the plugin.
</p>
<h2><a id="gettingStarted">Getting started writing a plugin</a></h2>
<p>
In order to build your own plugin, you will need the Spark source code, which can be acquired with Git from
<a href="https://github.com/igniterealtime/Spark.git">https://github.com/igniterealtime/Spark.git</a>
</p>
<h2><a id="examples">Spark How-To's</a></h2>
<ul>
<li><a href="#simplePlugin">How do I create a simple plugin?</a>
<li><a href="#addTab">How do I add my own Tab to the Spark Workspace?</a>
<li><a href="#addContactListContextListener">How do I add a context menu listener to the contact list?</a>
<li><a href="#addChatRoomContextListener">How do I add my own ContextMenu Listener to a ChatRoom?</a>
<li><a href="#addMenu">How do I add my own Menu to Spark?</a>
<li><a href="#addButton">How do I add a button to a Chat Room?</a>
<li><a href="#addSearch">How do I add my own searching feature in Spark like the User Search or Google Search in Firefox?</a>
<li><a href="#interceptFile">How can I intercept a File Transfer request?</a>
<li><a href="#sendFile">How can I send a file to another user?</a>
<li><a href="#contactUI">How can I control the UI and event handling of a ContactItem?</a>
<li><a href="#changePresence">How can I be notified when the Spark user changes their presence?</a>
<li><a href="#messageFilter">How can I add a message filter?</a>
<li><a href="#createChatRoom">How can I create a person-to-person Chat Room?</a>
<li><a href="#createConferenceRoom">How can I create a public Conference room?</a>
<li><a href="#addPreferences">How can I add my own Preferences?</a>
<li><a href="#showAlert">How can I flash the chat frame, like when a new message comes in?</a>
</ul>
<h3 id="simplePlugin">How do I create a simple plugin?</h3>
<ol>
<li>Implement Plugin.
</ol>
<fieldset>
<legend>Simple Plugin</legend>
<pre class="java">
package org.jivesoftware.spark.plugin.example;
import org.jivesoftware.spark.plugin.Plugin;
/**
* Implements the Spark Plugin framework to display the different possibilities using
* Spark.
*/
public class ExamplePlugin implements Plugin {
/**
* Called after Spark is loaded to initialize the new plugin.
*/
public void initialize() {
System.out.println("Welcome To Spark");
}
/**
* Called when Spark is shutting down to allow for persistence of information
* or releasing of resources.
*/
public void shutdown() {
}
/**
* Return true if the Spark can shutdown on users request.
* @return true if Spark can shutdown on users request.
*/
public boolean canShutDown() {
return true;
}
/**
* Is called when a user explicitly asked to uninstall this plugin.
* The plugin owner is responsible to clean up any resources and
* remove any components install in Spark.
*/
public void uninstall(){
// Remove all resources belonging to this plugin.
}
}
</pre>
</fieldset>
<h3 id="addTab">How to add your own Tab to the Spark Workspace?</h3>
<ol>
<li>Implement the <code>Plugin</code> interface.
<li>Retrieve the <code>Workspace</code> which is the UI for Spark.
<li>Retrieve the <code>WorkspacePane</code> which is the Tabbed Pane used by Spark.
<li>Add your own tab.
</ol>
<fieldset>
<legend>Add a Tab to Spark</legend>
<pre class="java">
public class ExamplePlugin implements Plugin {
// ...
/**
* Adds a tab to Spark
*/
private void addTabToSpark(){
// Get Workspace UI from SparkManager
Workspace workspace = SparkManager.getWorkspace();
// Retrieve the Tabbed Pane from the WorkspaceUI.
JTabbedPane tabbedPane = workspace.getWorkspacePane();
// Add own Tab.
tabbedPane.addTab("My Plugin", new JButton("Hello"));
}
// ...
}
</pre>
</fieldset>
<h3 id="addContactListContextListener">How do I add a context menu listener to the contact list?</h3>
<ol>
<li>Implement <code>Plugin</code>.
<li>Retrieve the <code>ContactList</code> which is part of Spark's <code>Workspace</code>.
<li>Add a <code>ContactListListener</code>.
</ol>
<fieldset>
<legend>Add a ContextMenu Listener to ContactList</legend>
<pre class="java">
private void addContactListListener(){
// Get Workspace UI from SparkManager
Workspace workspace = SparkManager.getWorkspace();
// Retrieve the ContactList from the Workspace
ContactList contactList = workspace.getContactList();
// Create an action to add to the Context Menu
final Action sayHelloAction = new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "Welcome to Spark");
}
};
sayHelloAction.putValue(Action.NAME, "Say Hello To Me");
// Add own Tab.
contactList.addContextMenuListener(new ContextMenuListener() {
public void poppingUp(Object object, JPopupMenu popup) {
if(object instanceof ContactItem){
popup.add(sayHelloAction);
}
}
public void poppingDown(JPopupMenu popup) {
}
public boolean handleDefaultAction(MouseEvent e) {
return false;
}
});
}
</pre>
</fieldset>
<h3 id="addChatRoomContextListener">How do I add my own ContextMenu Listener to a ChatRoom</h3>
<ol>
<li>Implement <code>Plugin</code>.
<li>Add a <code>ChatRoomListener</code> to the <code>ChatManager</code>.
<li>Get either the <code>TranscriptWindow</code> or <code>ChatInputEditor</code> from the <code>ChatRoom</code>.
<li>Add a <code>ContactMenuListener</code> to the <code>ChatArea</code>.
</ol>
<fieldset>
<legend>Add a ContextMenuListener to a ChatRoom, TranscriptWindow or ChatInputEditor</legend>
<pre class="java">
private void addContactListenerToChatRoom() {
// Retrieve a ChatManager from SparkManager
ChatManager chatManager = SparkManager.getChatManager();
final ContextMenuListener listener = new ContextMenuListener() {
public void poppingUp(Object object, JPopupMenu popup) {
TranscriptWindow chatWindow = (TranscriptWindow)object;
Action clearAction = new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
try {
chatWindow.insert("My own text :)");
}
catch (BadLocationException e) {
Log.error(e);
}
}
};
clearAction.putValue(Action.NAME, "Insert my own text");
popup.add(clearAction);
}
public void poppingDown(JPopupMenu popup) {
}
public boolean handleDefaultAction(MouseEvent e) {
return false;
}
};
// Add a ChatRoomListener to the ChatManager to allow for notifications
// when a room is being opened.
chatManager.addChatRoomListener(new ChatRoomListener() {
public void chatRoomOpened(ChatRoom room) {
room.getTranscriptWindow().addContextMenuListener(listener);
}
public void chatRoomLeft(ChatRoom room) {
room.getTranscriptWindow().removeContextMenuListener(listener);
}
});
}
</pre>
</fieldset>
<h3 id="addMenu">How do I add my own Menu to Spark?</h3>
<ol>
<li>Implement Plugin.
<li>Retrieve the MainWindow from SparkManager.
<li>Either create a new Menu or add a MenuItem to one of the pre-existing menus.
</ol>
<fieldset>
<legend>Add a Menu To Spark</legend>
<pre class="java">
/**
* Adds a new menu and child menu item to Spark.
*/
private void addMenuToSpark(){
// Retrieve the MainWindow UI from Spark.
MainWindow mainWindow = SparkManager.getMainWindow();
// Create new Menu
JMenu myPluginMenu = new JMenu("My Plugin Menu");
// Create Action to test Menu install.
Action showMessage = new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
JOptionPane.showMessageDialog(mainWindow, "Yeah, It works.");
}
};
// Give the menu item a name.
showMessage.putValue(Action.NAME, "Check if it works");
// Add to Menu
myPluginMenu.add(showMessage);
// Add Menu To Spark
mainWindow.getJMenuBar().add(myPluginMenu);
}
</pre>
</fieldset>
<h3 id="addButton">How do I add a button to a Chat Room?</h3>
<ol>
<li>Implement Plugin.
<li>Add a ChatRoomListener to ChatManager.
<li>When the room is opened, add your ChatRoomButton to the ToolBar of the ChatRoom.
</ol>
<fieldset>
<legend>Add a button to a Chat Room</legend>
<pre class="java">
/**
* Adds a button to each Chat Room that is opened.
*/
private void addChatRoomButton(){
// Retrieve ChatManager from the SparkManager
ChatManager chatManager = SparkManager.getChatManager();
// Create a new ChatRoomButton.
ChatRoomButton button = new ChatRoomButton("Push Me");
// Add to a new ChatRoom when the ChatRoom opens.
chatManager.addChatRoomListener(new ChatRoomListener() {
public void chatRoomOpened(ChatRoom room) {
room.getToolBar().addChatRoomButton(button);
}
public void chatRoomLeft(ChatRoom room) {
room.getToolBar().removeChatRoomButton(button);
}
});
}
</pre>
</fieldset>
<h3 id="addSearch">How do I add my own searching feature in Spark like the User Search or Google Search in Firefox?</h3>
<ol>
<li>Implement Plugin.
<li>Create a searchable object by implementing the Searchable interface.
<li>Add the Searchable implementation to the SearchManager.
</ol>
<fieldset>
<legend>Add a search feature to Spark like User Search or Google Search in Firefox</legend>
<pre class="java">
/**
* Called after Spark is loaded to initialize the new plugin.
*/
public void initialize() {
// Register new Searchable object "SearchMe" with the SearchManager.
SearchManager searchManager = SparkManager.getSearchManager();
searchManager.addSearchService(new SearchMe());
}
</pre>
See the SearchMe code below.
<pre class="java">
package org.jivesoftware.spark.plugin.example;
import org.jivesoftware.spark.search.Searchable;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.resource.LaRes;
import javax.swing.Icon;
import javax.swing.JOptionPane;
/**
* A simple example of how to integrate ones own search into Spark.
*/
public class SearchMe implements Searchable {
/**
* The icon to show in the search box.
* @return the icon.
*/
public Icon getIcon() {
return LaRes.getImageIcon(LaRes.SMALL_AGENT_IMAGE);
}
/**
* Returns the name of this search object that is displayed in the drop down box.
* @return the name.
*/
public String getName() {
return "Searches Nothing Really";
}
/**
* Returns the text that should be displayed in grey when this searchable object
* is initially selected.
* @return the text.
*/
public String getDefaultText() {
return "Click to search me.";
}
/**
* Returns the text to display in the tooltip.
* @return the tooltip text.
*/
public String getToolTip() {
return "Shows an example of integrating ones own search into Spark.";
}
/**
* Is called when a user hits "Enter" key.
* @param query the query the user is searching for.
*/
public void search(String query) {
JOptionPane.showMessageDialog(SparkManager.getMainWindow(), "Nothing Found :(");
}
}
</pre>
</fieldset>
<h3 id="interceptFile">How can I intercept a File Transfer request?</h3>
<ol>
<li>Implement Plugin.
<li>Implement TransferListener.
<li>Register your TransferListener.
</ol>
<fieldset>
<legend>Intercept File Transfer Requests</legend>
<pre class="java">
/**
* Listen for incoming transfer requests and either handle them yourself, or pass them
* off to be handled by the next listener. If no one handles it, then Spark will handle it.
*/
private void addTransferListener(){
SparkTransferManager transferManager = SparkManager.getTransferManager();
transferManager.addTransferListener(new TransferListener() {
public boolean handleTransfer(FileTransferRequest request) {
// If I wanted to handle it, take the request, accept it and get the inputstream.
// Otherwise, return false.
return false;
}
});
}
</pre>
</fieldset>
<h3 id="sendFile">How can I send a file to another user?</h3>
<ol>
<li>Implement Plugin.
<li>Get the full jid of the user via the UserManager.
<li>Get the SparkTransferManager and send the file.
</ol>
<fieldset>
<legend>Send a file to another user</legend>
<pre class="java">
/**
* Sends a file to a user in your ContactList.
*/
private void sendFile(){
// Retrieve SparkTransferManager from the SparkManager.
SparkTransferManager transferManager = SparkManager.getTransferManager();
// In order to send a file to a person, you will need to know their full Jabber
// ID.
// Retrieve the Jabber ID for a user via the UserManager. This can
// return null if the user is not in the ContactList or is offline.
UserManager userManager = SparkManager.getUserManager();
String jid = userManager.getJIDFromNickname("Matt");
if(jid != null){
transferManager.sendFile(new File("MyFile.txt"), jid);
}
}
</pre>
</fieldset>
<h3 id="contactUI">How can I control the UI and event handling of a ContactItem?</h3>
<ol>
<li>Implement Plugin.
<li>Get the ContactList.
<li>Get a ContactItem(s) based on a user's jid.
<li>Add your own ContactItemHandler to the ContactItem.
</ol>
<fieldset>
<legend>Control the UI and event handling of a ContactItem</legend>
<pre class="java">
/**
* Controls the UI of a ContactItem.
*/
private void handleUIAndEventsOfContactItem(){
ContactList contactList = SparkManager.getWorkspace().getContactList();
ContactItem item = contactList.getContactItemByJID("paul@jivesoftware.com/spark");
ContactItemHandler handler = new ContactItemHandler() {
/**
* Called when this users presence changes. You are responsible for changing the
* icon (or not) of this contact item.
* @param presence the users new presence.
*/
public void handlePresence(Presence presence) {
}
/**
* Is called when a user double-clicks the item.
* @return true if you are handling the event.
*/
public boolean handleDoubleClick() {
return false;
}
};
item.setHandler(handler);
}
</pre>
</fieldset>
<h3 id="changePresence">How can I be notified when the Spark user changes their presence?</h3>
<ol>
<li>Implement Plugin.
<li>Get the SessionManager from SparkManager.
<li>Add your own PresenceListener to SessionManager.
</ol>
<fieldset>
<legend>Receive notification when the Spark user changes their presence</legend>
<pre class="java">
/**
* Allows a plugin to be notified when the Spark users changes their
* presence.
*/
private void addPersonalPresenceListener(){
SessionManager sessionManager = SparkManager.getSessionManager();
sessionManager.addPresenceListener(new PresenceListener() {
/**
* Spark user changed their presence.
* @param presence the new presence.
*/
public void presenceChanged(Presence presence) {
}
});
}
</pre>
</fieldset>
<h3 id="messageFilter">How can I add a message filter?</h3>
<ol>
<li>Implement Plugin.
<li>Get the ChatManager from SparkManager.
<li>Create an instance of Message Filter.
<li>Register with the ChatManager.
</ol>
<fieldset>
<legend>Adding own Message Filter</legend>
<pre class="java">
/**
* Installs a new MessageFilter.
*/
private void installMessageFilter() {
// Retrieve the ChatManager from SparkManager
ChatManager chatManager = SparkManager.getChatManager();
MessageFilter messageFilter = new MessageFilter() {
public void filter(Message message) {
String currentBody = message.getBody();
currentBody = currentBody.replaceAll("bad words", "good words");
message.setBody(currentBody);
}
};
chatManager.addMessageFilter(messageFilter);
// Just remember to remove your filter if need be.
}
</pre>
</fieldset>
<h3 id="createChatRoom">How can I create a person-to-person Chat Room</h3>
<ol>
<li>Implement Plugin.
<li>Get the ChatManager from SparkManager.
<li>Create a new ChatRoom using the ChatManager.
<li>Optionally make it the active ChatRoom using the ChatContainer.
</ol>
<fieldset>
<legend>Creating Person-to-Person Chat Room</legend>
<pre class="java">
/**
* Creates a person to person Chat Room and makes it the active chat.
*/
private void createPersonToPersonChatRoom(){
// Get the ChatManager from Sparkmanager
ChatManager chatManager = SparkManager.getChatManager();
// Create the room.
ChatRoom chatRoom = chatManager.createChatRoom("don@jivesoftware.com", "Don The Man", "The Chat Title");
// If you wish to make this the active chat room.
// Get the ChatContainer (This is the container for all Chat Rooms)
ChatContainer chatContainer = chatManager.getChatContainer();
// Ask the ChatContainer to make this chat the active chat.
chatContainer.activateChatRoom(chatRoom);
}
</pre>
</fieldset>
<h3 id="createConferenceRoom">How can I create a public Conference room?</h3>
<ol>
<li>Implement Plugin.
<li>Get the ChatManager from SparkManager.
<li>Create a new conference ChatRoom using the ChatManager.
<li>Optionally make it the active ChatRoom using the ChatContainer.
</ol>
<fieldset>
<legend>Creating a Conference Room</legend>
<pre class="java">
/**
* Creates a person to person Chat Room and makes it the active chat.
*/
private void createConferenceRoom() {
// Get the ChatManager from Sparkmanager
ChatManager chatManager = SparkManager.getChatManager();
Collection serviceNames = null;
// Get the service name you wish to use.
try {
MultiUserChatManager multiUserChatManager = MultiUserChatManager.getInstanceFor( SparkManager.getConnection() );
serviceNames = multiUserChatManager.getServiceNames();
}
catch (Exception e) {
Log.error(e);
}
// Create the room.
ChatRoom chatRoom = chatManager.createConferenceRoom("BusinessChat", (String)serviceNames.toArray()[0]);
// If you wish to make this the active chat room.
// Get the ChatContainer (This is the container for all Chat Rooms)
ChatContainer chatContainer = chatManager.getChatContainer();
// Ask the ChatContainer to make this chat the active chat.
chatContainer.activateChatRoom(chatRoom);
}
}
</pre>
</fieldset>
<h3 id="addPreferences">How can I add my own Preferences?</h3>
<ol>
<li>Implement <code>Plugin</code>.
<li>Create a class that implements <code>Preference</code>.
<li>Create a UI to associate with the <code>Preference</code>.
<li>Register your new Preference with the <code>PreferenceManager</code>.
</ol>
<fieldset>
<legend>Creating a Preference</legend>
<ul>
<li>Create a class that implements <code>Preference</code>.
<pre class="java">
package org.jivesoftware.spark.plugin.example.preferences;
import org.jivesoftware.spark.preference.Preference;
import org.jivesoftware.resource.LaRes;
import javax.swing.Icon;
import javax.swing.JComponent;
public class MyPreferences implements Preference {
private MyPreferenceUI ui;
public MyPreferences(){
ui = new MyPreferenceUI();
}
public String getTitle() {
return "Example Preferences";
}
public Icon getIcon() {
return LaRes.getImageIcon(LaRes.ADD_IMAGE_24x24);
}
public String getTooltip() {
return "Example tooltip in preference dialog";
}
public String getListName() {
return "Examples";
}
public String getNamespace() {
return "EXAMPLE";
}
public JComponent getGUI() {
return ui;
}
public void load() {
// Would load persisted information from file or server and
// set the UI appropriately.
ui.setShowChatHistory(true);
}
public void commit() {
// Would persist the current state of the preferences.
boolean showChatHistory = ui.isChatHistoryShown();
}
public boolean isDataValid() {
return true;
}
public String getErrorMessage() {
return null;
}
public Object getData() {
return null;
}
public void shutdown() {
// Do nothing.
}
}
</pre>
<li>Create a UI class that your Preference will use.
<pre class="java">
package org.jivesoftware.spark.plugin.example.preferences;
import org.jivesoftware.spark.util.ResourceUtils;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import java.awt.FlowLayout;
/**
* Demonstrates a simple panel used to display a UI that can be used as
* the Preference UI in the Preferences Dialog. This panel shows a simple
* UI with accessors for setting the preference values / persistence.
*/
public class MyPreferenceUI extends JPanel {
private JCheckBox showChatHistory;
/**
* Creates the default panel using FlowLayout as the Layout. But
* GridBagLayout is the really only true layout :)
*/
public MyPreferenceUI() {
setLayout(new FlowLayout(FlowLayout.LEFT));
buildUI();
}
private void buildUI() {
showChatHistory = new JCheckBox();
// Use Mnemonics for the CheckBox using ResourceUtils.
ResourceUtils.resButton(showChatHistory, "&Show Chat History in Chat Window");
// Add Button
add(showChatHistory);
}
/**
* Sets the UI based on previous preferences.
* @param show true if Chat History to show up.
*/
public void setShowChatHistory(boolean show){
showChatHistory.setSelected(show);
}
/**
* Returns true if Chat History should be shown.
* @return true if history shown.
*/
public boolean isChatHistoryShown(){
return showChatHistory.isSelected();
}
}
</pre>
<li>Register the <code>Preference</code> class with Preference Manager in your plugin's <code>initialize()</code> method.
<pre class="java">
public void initialize() {
PreferenceManager preferenceManager = SparkManager.getPreferenceManager();
preferenceManager.addPreference(new MyPreferences());
}
</pre>
</ul>
</fieldset>
<h3 id="showAlert">How to show an alert, like when a new message comes in?</h3>
<fieldset><legend>How to show an alert, like when a new message comes in?</legend>
<pre class="java">
// Get the ChatContainer from the ChatManager.
ChatManager chatManager = SparkManager.getChatManager();
ChatContainer chatContainer = chatManager.getChatContainer();
// Get the room you wish to be notified.
ChatRoom chatRoom = chatContainer.getActiveChatRoom();
chatContainer.startFlashing(chatRoom);
</pre>
</fieldset>
<h2>Once I Build It, Then What?</h2>
<p>After you've built your amazingly cool plugin, it's easy to rollout to your users.
Just have them drop it in the plugins directory of their Spark installation.
If your plugin is generally useful, we hope you'll share it with the whole Spark community!
To make your plugin
available via the <a href="https://igniterealtime.org/updater/plugins.jsp">public repository</a>, submit it to
<a href="https://discourse.igniterealtime.org/c/spark/spark-dev/13">on a forum</a>.
</p>
</body>
</html>