⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sparkplug_dev_guide.html.svn-base

📁 开源项目openfire的完整源程序
💻 SVN-BASE
📖 第 1 页 / 共 3 页
字号:
</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 {
            serviceNames = MultiUserChat.getServiceNames(SparkManager.getConnection());
        }
        catch (XMPPException e) {
            e.printStackTrace();
        }

        // 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 Plugin.
  <li>Create a class that implements Preference.
  <li>Create a UI to associate with the Preference.
  <li>Register your new Preference with the PreferenceManager.
</ol>

<fieldset>

    <legend>Creating a Preference</legend>
<ul>
<li>Create a class that implements Preference. 
<pre class="java">
/**
 * $RCSfile: ,v $
 * $Revision: $
 * $Date:  $
 *
 * Copyright (C) 1999-2005 Jive Software. All rights reserved.
 *
 * This software is the proprietary information of Jive Software.
 * Use is subject to license terms.
 */
package com.jivesoftware.spark.examples.preferences;

import com.jivesoftware.spark.preference.Preference;
import com.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 com.jivesoftware.spark.examples.preferences;

import com.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 Preference class with Preference Manager.
<pre class="java">
  public void addPreference(){
     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.
    
     ChatContainer chatContainer = ChatManager.getChatContainer();
     
     // Get the room you wish to be notified.
     ChatRoom chatRoom = chatContainer.getActiveChatRoom();
     chatContainer.startFlashing(chatRoom);
</pre>

</fieldset>

</body>
</html>


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -