📄 sparkplug_dev_guide.html.svn-base
字号:
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 Plugin.
<li>Add a ChatRoomListener to the ChatManager.
<li>Get either the TranscriptWindow or ChatInputEditor from the ChatRoom.
<li>Add a ContactMenuListener to the ChatArea.
</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) {
final TranscriptWindow chatWindow = (TranscriptWindow)object;
Action clearAction = new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
try {
chatWindow.insert("My own text :)");
}
catch (BadLocationException e) {
e.printStackTrace();
}
}
};
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. Note: I will use a ChatRoomListenerAdapter for brevity.
chatManager.addChatRoomListener(new ChatRoomListenerAdapter() {
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.
final 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.
final ChatRoomButton button = new ChatRoomButton("Push Me");
// Add to a new ChatRoom when the ChatRoom opens.
chatManager.addChatRoomListener(new ChatRoomListenerAdapter() {
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 com.jivesoftware.spark.examples;
import com.jivesoftware.spark.search.Searchable;
import com.jivesoftware.spark.SparkManager;
import com.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);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -