Spark研究笔记2:重要的工厂类Workspace(原创)

前言:上一节我们参观了工厂的中控指挥中心,接下来需要下到各个生产车间去看看了。

 

Workspace是Spark的内置单例容器类,用于放置自定义的插件,比如新建一个Tab页:

  Workspace workspace = SparkManager.getWorkspace();
  JButton button = new JButton("HELLO SPARK USERS");
  workspace.getWorkspacePane().addTab("MyPlugin", button);

它的私有域内置了Workspace、Tab容器TabbedPane、状态栏StatusBar、联系人列表ContactList、配置服务ConferenceServices,另外还内置了一些重要的插件,比如GatewayPlugin、BookmarkPlugin、ChatTranscriptPlugin、BroadcastPlugin。

布局说明:

重要方法:

     /**
     * 获取一个单例实例Workspace
     */
    public static Workspace getInstance() 

 

/**
     * 构造函数,创建SupportChatWorkspace
     */

private Workspace() {
        final MainWindow mainWindow = SparkManager.getMainWindow();

            // 添加MainWindow监听
            mainWindow.addMainWindowListener(new MainWindowListener() {
                public void shutdown() {
                    final ChatContainer container = SparkManager.getChatManager().getChatContainer();
                    // 关闭(离开)所有聊天室.
                    for (ChatRoom chatRoom : container.getChatRooms()) {
                        // 离开聊天室
                        container.leaveChatRoom(chatRoom);
                    }
          //关闭插件
                    conferences.shutdown();
                    gatewayPlugin.shutdown();
                    bookmarkPlugin.shutdown();
                    broadcastPlugin.shutdown();
                }
    
                public void mainWindowActivated() {
    
                }
    
                public void mainWindowDeactivated() {
    
                }
            });
       

        // 初始化workspace面板, 默认放置在最下方,BOTTOM哦,

   //JTabbedPane是Swing的扩展控件,定义是: public class JTabbedPane extends JComponent implements Serializable, Accessible, SwingConstants
        workspacePane = new SparkTabbedPane(JTabbedPane.BOTTOM);
        workspacePane.setBorder(BorderFactory.createEmptyBorder());
        // 添加面板
        cardLayout = new CardLayout();
        cardPanel = new JPanel(cardLayout);
        cardPanel.setOpaque(false);
        cardPanel.add(WORKSPACE_PANE, this);
        //状态栏
        statusBox = new StatusBar();
        
        // 建立默认workspacePane和状态栏
        this.setLayout(new GridBagLayout());

    //GridBagConstraints 是awt的GridBag布局类型,具体可看看这里:http://blog.sina.com.cn/s/blog_4412ae250100062n.html
        add(workspacePane, new GridBagConstraints(0, 9, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
        add(statusBox, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));

   //在Jcomponent中,定义了registerKeyboardAction方法,使用这个方法来将需要处理的键盘事件以及处理事件的行为绑定在一 起。Text类中具有keymap对象,同Jcomponent中的处理方法类似,这个对象保存//着需要处理的键盘事件和对应的行为。F12可以调出调试窗口,对程序员比较有用
        this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("F12"), "showDebugger");
        this.getActionMap().put("showDebugger", new AbstractAction("showDebugger") {
            private static final long serialVersionUID = 4066886679016416923L;

            public void actionPerformed(ActionEvent evt) {
                EnhancedDebuggerWindow window = EnhancedDebuggerWindow.getInstance();
                window.setVisible(true);
            }
        });

        // 背景设置,个人感觉Spark的这种Hardcode做法还是有很大改进余地
        setBackground(new Color(235, 239, 254));
    }

 

 

  /**
     * Load 插件
     */
    public void loadPlugins() {
        // 这个方法比较复杂,将来开新专题讨论

 

/**

* 处理消息包,Packet引用自org.jivesoftware.smack.packet,smack对XMPP消息处理的封装,让客户端调用很方便

*/

private void handleIncomingPacket(Packet packet) {
        // We only handle message packets here.
        if (packet instanceof Message) {
            final Message message = (Message)packet;
            boolean isGroupChat = message.getType() == Message.Type.groupchat;

            // 如果是会议邀请,就不再这里处理,就return了,在哪里处理?我也不清楚,待研究
            if (message.getExtension("x", "jabber:x:conference") != null) {
                return;
            }

            final String body = message.getBody();
       //是否广播消息
            boolean broadcast = message.getProperty("broadcast") != null;

            // 处理离线消息.
            DelayInformation offlineInformation = (DelayInformation)message.getExtension("x", "jabber:x:delay");
            if (offlineInformation != null && (Message.Type.chat == message.getType() ||
                Message.Type.normal == message.getType())) {
                handleOfflineMessage(message);
            }

            if (body == null ||
                isGroupChat ||
                broadcast ||
                message.getType() == Message.Type.normal ||
                message.getType() == Message.Type.headline ||
                message.getType() == Message.Type.error) {
                return;
            }

            // 验证消息来源
            final String from = packet.getFrom();
            final String host = SparkManager.getSessionManager().getServerAddress();

            // Don't allow workgroup notifications to come through here.
            final String bareJID = StringUtils.parseBareAddress(from);
            if (host.equalsIgnoreCase(from) || from == null) {
                return;
            }

       //通过验证则创建ChatRoom ,ChatRoom 位于org/jivesoftware/spark/ui
            ChatRoom room = null;
            try {
                room = SparkManager.getChatManager().getChatContainer().getChatRoom(bareJID);
            }
            catch (ChatRoomNotFoundException e) {
                // Ignore
            }

            // 如果room为空,则新建
            if (room == null) {
                createOneToOneRoom(bareJID, message);
            }
        }
    }

 

     /**
     * Workspace TabbedPane. 可以定义插件来动态添加控件
     * 使用方法: addTab( name, icon, component ) .
     */
    public SparkTabbedPane getWorkspacePane() {
        return workspacePane;
    }

 

 

posted on 2013-04-27 11:49  CVT-wangxin  阅读(492)  评论(0编辑  收藏  举报

导航