You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by mj...@apache.org on 2009/08/15 22:46:47 UTC
svn commit: r804531 -
/mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/
Author: mjakl
Date: Sat Aug 15 20:46:47 2009
New Revision: 804531
URL: http://svn.apache.org/viewvc?rev=804531&view=rev
Log:
Publishing and receiving messages is now possible.
Added:
mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubEventListener.java
mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubOpenButtonListener.java
mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubPublishButtonListener.java
Modified:
mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubClientGUI.java
mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubClientModel.java
mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubTableModel.java
Modified: mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubClientGUI.java
URL: http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubClientGUI.java?rev=804531&r1=804530&r2=804531&view=diff
==============================================================================
--- mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubClientGUI.java (original)
+++ mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubClientGUI.java Sat Aug 15 20:46:47 2009
@@ -47,6 +47,7 @@
public class PubsubClientGUI implements Runnable, ListSelectionListener {
private JFrame frame;
private JButton delete;
+ private JButton open;
private PubsubClientModel pcm = new PubsubClientModel();
private void createAndShowGUI() {
@@ -68,11 +69,16 @@
lsm.addListSelectionListener(this);
tableModel.addTableModelListener(new PubsubTableModelListener(pcm));
- JButton create = new JButton("Create");
+ JButton create = new JButton("Create node");
create.setActionCommand("create");
create.addActionListener(new PubsubCreateButtonListener(frame, pcm));
- delete = new JButton("Delete");
+ open = new JButton("Open node");
+ open.setActionCommand("open");
+ open.addActionListener(new PubsubOpenButtonListener(pcm));
+ disableOpenButton();
+
+ delete = new JButton("Delete node");
delete.setActionCommand("delete");
delete.addActionListener(new PubsubDeleteButtonListener(frame, pcm));
delete.setEnabled(false);
@@ -83,6 +89,7 @@
JPanel buttons = new JPanel();
buttons.add(create);
+ buttons.add(open);
buttons.add(delete);
buttons.add(refresh);
@@ -182,27 +189,46 @@
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if(e.getValueIsAdjusting()) {
if(lsm.isSelectionEmpty()) {
- // disable delete button
- delete.setEnabled(false);
- pcm.deselectNode();
+ disableOpenButton();
+ disableDeleteButton();
} else {
- // store the node and enable delete button
- PubsubTableModel tableModel = pcm.getTableModel();
-
- int idx = getNewSelectionIndex(e, tableModel);
-
- String selectedNode = (String)tableModel.getValueAt(idx, 0);
- pcm.selectNode(selectedNode);
- Boolean owner = (Boolean)tableModel.getValueAt(idx, 2);
- if(owner !=null && owner == Boolean.TRUE ) { //owner
- delete.setEnabled(true);
- } else {
- delete.setEnabled(false);
- }
+ changeOpenButton();
+ changeDeleteButton(e);
}
}
}
+ private void disableOpenButton() {
+ // disable open button
+ open.setEnabled(false);
+ }
+
+ private void disableDeleteButton() {
+ // disable delete button
+ delete.setEnabled(false);
+ pcm.deselectNode();
+ }
+
+ private void changeOpenButton() {
+ open.setEnabled(true);
+ }
+
+ private void changeDeleteButton(ListSelectionEvent e) {
+ // store the node and enable delete button
+ PubsubTableModel tableModel = pcm.getTableModel();
+
+ int idx = getNewSelectionIndex(e, tableModel);
+
+ String selectedNode = (String)tableModel.getValueAt(idx, 0);
+ pcm.selectNode(selectedNode);
+ Boolean owner = (Boolean)tableModel.getValueAt(idx, 2);
+ if(owner !=null && owner == Boolean.TRUE ) { //owner
+ delete.setEnabled(true);
+ } else {
+ delete.setEnabled(false);
+ }
+ }
+
private int getNewSelectionIndex(ListSelectionEvent e, PubsubTableModel tableModel) {
int idx = e.getFirstIndex(); // check which one is the right index (the new one)
String selectedNode = (String)tableModel.getValueAt(idx, 0);
Modified: mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubClientModel.java
URL: http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubClientModel.java?rev=804531&r1=804530&r2=804531&view=diff
==============================================================================
--- mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubClientModel.java (original)
+++ mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubClientModel.java Sat Aug 15 20:46:47 2009
@@ -23,6 +23,9 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.TreeMap;
+
+import javax.swing.DefaultListModel;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
@@ -33,7 +36,9 @@
import org.jivesoftware.smackx.pubsub.Subscription;
public class PubsubClientModel {
+ private Map<String, DefaultListModel> nodeMessages = new TreeMap<String, DefaultListModel>();
private PubsubTableModel tableModel = new PubsubTableModel();
+ private PubsubEventListener pel = new PubsubEventListener(this);
private XMPPConnection connection;
private PubSubManager pubsubMgr;
@@ -133,6 +138,12 @@
tableModel.startBulkAdd();
for(PubsubNode n : lookup.values()) {
tableModel.bulkAddRow(n);
+
+ try {
+ pubsubMgr.getNode(n.getNode()).addItemEventListener(pel); // TODO maybe we add one listener more than one times?
+ } catch (XMPPException e) {
+ e.printStackTrace();
+ }
}
tableModel.endBulkAdd();
}
@@ -170,4 +181,16 @@
public String getSelectedNode() {
return this.selectedNode;
}
+
+ public DefaultListModel getListModel(String nodeID) {
+ if(!nodeMessages.containsKey(nodeID)) {
+ DefaultListModel dlm = new DefaultListModel();
+ nodeMessages.put(nodeID, dlm);
+ }
+ return nodeMessages.get(nodeID);
+ }
+
+ public boolean isOwner(String nodeID) {
+ return tableModel.isOwner(nodeID);
+ }
}
Added: mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubEventListener.java
URL: http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubEventListener.java?rev=804531&view=auto
==============================================================================
--- mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubEventListener.java (added)
+++ mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubEventListener.java Sat Aug 15 20:46:47 2009
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.vysper.demo.pubsub.client;
+
+import javax.swing.DefaultListModel;
+
+import org.jivesoftware.smackx.pubsub.Item;
+import org.jivesoftware.smackx.pubsub.ItemPublishEvent;
+import org.jivesoftware.smackx.pubsub.SimplePayload;
+import org.jivesoftware.smackx.pubsub.listener.ItemEventListener;
+
+public class PubsubEventListener implements ItemEventListener {
+ private PubsubClientModel parent;
+
+ public PubsubEventListener(PubsubClientModel parent) {
+ this.parent = parent;
+ }
+
+ public void handlePublishedItems(ItemPublishEvent e) {
+ DefaultListModel lm = parent.getListModel(e.getNodeId());
+ System.out.println("Got something from " + e.getNodeId());
+ for(Item<SimplePayload> i : e.getItems()) {
+ lm.add(0,i.getPayload().toXML()); //alwasy add to the top
+ }
+ }
+
+}
Added: mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubOpenButtonListener.java
URL: http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubOpenButtonListener.java?rev=804531&view=auto
==============================================================================
--- mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubOpenButtonListener.java (added)
+++ mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubOpenButtonListener.java Sat Aug 15 20:46:47 2009
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.vysper.demo.pubsub.client;
+
+import java.awt.BorderLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JList;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTextField;
+import javax.swing.ListModel;
+
+public class PubsubOpenButtonListener implements ActionListener {
+
+ private PubsubClientModel parent;
+
+ public PubsubOpenButtonListener(PubsubClientModel parent) {
+ this.parent = parent;
+ }
+
+ public void actionPerformed(ActionEvent e) {
+ String nodeID = parent.getSelectedNode();
+ if(nodeID != null) {
+ createAndShowGUI(nodeID);
+ }
+ }
+
+ private void createAndShowGUI(String nodeID) {
+ ListModel lm = parent.getListModel(nodeID);
+
+ JFrame frame = new JFrame("Events from "+nodeID);
+ frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+
+ JPanel panel = new JPanel();
+ panel.setLayout(new BorderLayout());
+
+ JList eventList = new JList(lm);
+ JScrollPane scrollPane = new JScrollPane(eventList);
+ frame.add(panel);
+
+ panel.add(scrollPane, BorderLayout.NORTH);
+
+ JTextField messageTxt = createOwnerControls(nodeID, panel);
+
+ frame.pack();
+ frame.setVisible(true);
+
+ setFocus(messageTxt);
+ }
+
+ private void setFocus(JTextField messageTxt) {
+ if(messageTxt != null) {
+ messageTxt.requestFocusInWindow();
+ }
+ }
+
+ private JTextField createOwnerControls(String nodeID, JPanel panel) {
+ boolean owner = parent.isOwner(nodeID);
+ JTextField messageTxt = null;
+ if(owner) {
+ messageTxt = new JTextField(20);
+
+ JButton publish = new JButton("Publish");
+ publish.setActionCommand("publish");
+ publish.addActionListener(new PubsubPublishButtonListener(nodeID, messageTxt, parent));
+
+ JPanel buttons = new JPanel();
+ buttons.add(messageTxt);
+ buttons.add(publish);
+
+ panel.add(buttons, BorderLayout.SOUTH);
+ }
+ return messageTxt;
+ }
+}
\ No newline at end of file
Added: mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubPublishButtonListener.java
URL: http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubPublishButtonListener.java?rev=804531&view=auto
==============================================================================
--- mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubPublishButtonListener.java (added)
+++ mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubPublishButtonListener.java Sat Aug 15 20:46:47 2009
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.vysper.demo.pubsub.client;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.JTextField;
+
+import org.jivesoftware.smack.XMPPException;
+import org.jivesoftware.smackx.pubsub.Item;
+import org.jivesoftware.smackx.pubsub.Node;
+import org.jivesoftware.smackx.pubsub.PubSubManager;
+import org.jivesoftware.smackx.pubsub.SimplePayload;
+
+public class PubsubPublishButtonListener implements ActionListener {
+
+ private String nodeID;
+ private PubsubClientModel parent;
+ private JTextField messageTxt;
+ private static final String ELEMENT = "message";
+ private static final String NAMESPACE = "http://mina.apache.org/vysper/demo";
+
+ public PubsubPublishButtonListener(String nodeID, JTextField messageTxt, PubsubClientModel parent) {
+ this.nodeID = nodeID;
+ this.parent = parent;
+ this.messageTxt = messageTxt;
+ }
+
+ public void actionPerformed(ActionEvent e) {
+ PubSubManager pubsubMgr = parent.getPubsubMgr();
+
+ Node node = getNode(pubsubMgr);
+ if(node == null) return;
+
+ String message = getMessage();
+
+ Item<SimplePayload> item = createItem(message);
+
+ sendItem(node, item);
+ }
+
+ private String getMessage() {
+ String message = messageTxt.getText();
+ cleanup();
+ return message;
+ }
+
+ private void sendItem(Node node, Item<SimplePayload> item) {
+ try {
+ node.send(item);
+ } catch (XMPPException e1) {
+ System.err.println("Couldn't send an item to " + nodeID);
+ e1.printStackTrace();
+ }
+ }
+
+ private Item<SimplePayload> createItem(String message) {
+ String itemId = "demoID"+System.currentTimeMillis();
+ Item<SimplePayload> item = new Item<SimplePayload>(itemId, new SimplePayload(ELEMENT, NAMESPACE, message));
+ return item;
+ }
+
+ private Node getNode(PubSubManager pubsubMgr) {
+ Node node = null;
+ try {
+ node = pubsubMgr.getNode(nodeID);
+ } catch (XMPPException e1) {
+ System.err.println("Couldn't get the node object for " + nodeID);
+ e1.printStackTrace();
+ }
+ return node;
+ }
+
+ private void cleanup() {
+ messageTxt.setText("");
+ messageTxt.requestFocusInWindow();
+ }
+}
Modified: mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubTableModel.java
URL: http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubTableModel.java?rev=804531&r1=804530&r2=804531&view=diff
==============================================================================
--- mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubTableModel.java (original)
+++ mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubTableModel.java Sat Aug 15 20:46:47 2009
@@ -112,4 +112,13 @@
Collections.sort(this.nodes);
fireTableRowsInserted(0, this.getRowCount()-1);
}
+
+ public boolean isOwner(String nodeID) {
+ for(PubsubNode n : nodes) {
+ if(n.getNode().equals(nodeID)) {
+ return n.getOwnership();
+ }
+ }
+ return false;
+ }
}