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 01:39:12 UTC

svn commit: r804409 - in /mina/sandbox/vysper/trunk/demo/pubsub/client: ./ src/main/java/org/apache/vysper/demo/pubsub/client/

Author: mjakl
Date: Fri Aug 14 23:39:11 2009
New Revision: 804409

URL: http://svn.apache.org/viewvc?rev=804409&view=rev
Log:
Added the possibility to create new nodes.
Extracted the table model as a separate class.

Added:
    mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubCreateButtonListener.java
    mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubTableModelListener.java
Modified:
    mina/sandbox/vysper/trunk/demo/pubsub/client/   (props changed)
    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/PubsubTableModel.java

Propchange: mina/sandbox/vysper/trunk/demo/pubsub/client/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Fri Aug 14 23:39:11 2009
@@ -0,0 +1,4 @@
+.settings
+target
+.classpath
+.project

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=804409&r1=804408&r2=804409&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 Fri Aug 14 23:39:11 2009
@@ -20,6 +20,7 @@
 package org.apache.vysper.demo.pubsub.client;
 
 import java.awt.BorderLayout;
+import java.awt.Component;
 import java.awt.GridLayout;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -35,17 +36,15 @@
 import javax.swing.JScrollPane;
 import javax.swing.JTable;
 import javax.swing.JTextField;
+import javax.swing.ListSelectionModel;
 import javax.swing.SwingUtilities;
 import javax.swing.UIManager;
-import javax.swing.event.TableModelEvent;
-import javax.swing.event.TableModelListener;
 
 import org.jivesoftware.smack.XMPPConnection;
 import org.jivesoftware.smack.XMPPException;
 import org.jivesoftware.smackx.packet.DiscoverItems;
 import org.jivesoftware.smackx.packet.DiscoverItems.Item;
 import org.jivesoftware.smackx.pubsub.Affiliation;
-import org.jivesoftware.smackx.pubsub.Node;
 import org.jivesoftware.smackx.pubsub.PubSubManager;
 import org.jivesoftware.smackx.pubsub.Subscription;
 
@@ -56,9 +55,9 @@
  *
  * @author The Apache MINA Project (http://mina.apache.org)
  */
-public class PubsubClientGUI implements Runnable, TableModelListener {
+public class PubsubClientGUI implements Runnable {
     private JFrame frame;
-    private PubsubTableModel dtm = new PubsubTableModel();
+    private PubsubTableModel tableModel = new PubsubTableModel();
     private XMPPConnection connection;
     private PubSubManager pubsubMgr;
     private String username;
@@ -75,12 +74,15 @@
         JPanel panel = new JPanel();
         panel.setLayout(new BorderLayout());
 
-        JTable nodeTable = new JTable(dtm);
+        JTable nodeTable = new JTable(tableModel);
         JScrollPane scrollPane = new JScrollPane(nodeTable);
         nodeTable.setFillsViewportHeight(true);
-        dtm.addTableModelListener(this);
+        nodeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+        tableModel.addTableModelListener(new PubsubTableModelListener(this));
 
         JButton create = new JButton("Create");
+        create.setActionCommand("create");
+        create.addActionListener(new PubsubCreateButtonListener(this));
 
         JButton delete = new JButton("Delete");
         
@@ -133,7 +135,7 @@
         });
     }
 
-    private void refresh() {
+    public void refresh() {
         Map<String, PubsubNode> lookup = new HashMap<String, PubsubNode>();
 
         try {
@@ -144,17 +146,18 @@
             e.printStackTrace();
         }
 
+        tableModel.clear();
         for(PubsubNode n : lookup.values()) {
-            dtm.addRow(n);
+            tableModel.addRow(n);
         }
     }
 
     private void discoverAffiliations(Map<String, PubsubNode> lookup) throws XMPPException {
         List<Affiliation> lAffiliations = pubsubMgr.getAffiliations();
         for(Affiliation affiliation : lAffiliations) {
-            System.out.print(affiliation.getNodeId());
-            System.out.print(": ");
-            System.out.println(affiliation.getType());
+            System.out.print(affiliation.getType());
+            System.out.print(" of ");
+            System.out.println(affiliation.getNodeId());
 
             PubsubNode n = lookup.get(affiliation.getNodeId());
             n.setOwnership(affiliation.getType().toString().equals("owner"));
@@ -164,9 +167,9 @@
     private void discoverSubscriptions(Map<String, PubsubNode> lookup) throws XMPPException {
         List<Subscription> lSubscriptions = pubsubMgr.getSubscriptions();
         for(Subscription subscription : lSubscriptions) {
-            System.out.print(subscription.getNode());
-            System.out.print(": ");
-            System.out.println(subscription.getState());
+            System.out.print(subscription.getState());
+            System.out.print(" at ");
+            System.out.println(subscription.getNode());
 
             PubsubNode n = lookup.get(subscription.getNode());
             if(n != null) {
@@ -198,6 +201,7 @@
                 connection = connect(username, password, hostname);
                 loginOK = true;
             } catch (XMPPException e) {
+                System.err.println("Login failed for user "+username);
                 e.printStackTrace();
             }
         } while(loginOK == false);
@@ -259,20 +263,19 @@
         return connection;
     }
 
-    public void tableChanged(TableModelEvent event) {
-        try {
-            Boolean sub = (Boolean)dtm.getValueAt(event.getFirstRow(), event.getColumn());
-            String nodeName = (String)dtm.getValueAt(event.getFirstRow(), 0);
+    public Component getFrame() {
+        return frame;
+    }
 
-            Node node = pubsubMgr.getNode(nodeName);
+    public PubSubManager getPubsubMgr() {
+        return pubsubMgr;
+    }
 
-            if(sub.booleanValue()) { // contains the new value (soll)
-                node.subscribe(jid);
-            } else {
-                node.unsubscribe(jid);
-            }
-        } catch(Exception ex) {
-            ex.printStackTrace();
-        }
+    public PubsubTableModel getTableModel() {
+        return tableModel;
+    }
+
+    public String getJID() {
+        return jid;
     }
 }

Added: mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubCreateButtonListener.java
URL: http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubCreateButtonListener.java?rev=804409&view=auto
==============================================================================
--- mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubCreateButtonListener.java (added)
+++ mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubCreateButtonListener.java Fri Aug 14 23:39:11 2009
@@ -0,0 +1,82 @@
+/*
+ *  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.GridLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JTextField;
+
+import org.jivesoftware.smack.XMPPException;
+
+public class PubsubCreateButtonListener implements ActionListener {
+
+    private String nodeID;
+    private PubsubClientGUI parent;
+    
+    public PubsubCreateButtonListener(PubsubClientGUI parent) {
+        this.parent = parent;
+    }
+    
+    public void actionPerformed(ActionEvent e) {
+        if(askForNodeName()) {
+            try {
+                parent.getPubsubMgr().createNode(nodeID);
+                System.out.println("Node created " + nodeID);
+                parent.refresh();
+            } catch (XMPPException e1) {
+                System.err.println("Couldn't create node "+nodeID);
+                e1.printStackTrace();
+            }
+        }
+    }
+    
+    private boolean askForNodeName() {
+        JLabel nodeLab = new JLabel("Node ID");
+        JTextField nodeTxt = new JTextField();
+        nodeLab.setLabelFor(nodeTxt);
+                
+        JPanel panel = new JPanel();
+        panel.setLayout(new GridLayout(2,1));
+        panel.add(nodeLab);
+        panel.add(nodeTxt);
+        
+        int answer = JOptionPane.showOptionDialog(parent.getFrame(),
+                panel,
+                "Create new node",
+                JOptionPane.OK_CANCEL_OPTION,
+                JOptionPane.QUESTION_MESSAGE,
+                null,
+                new String[] {"OK", "Cancel"},
+                "OK");
+
+        if(answer != 0) {
+            return false;
+        }
+        
+        this.nodeID = nodeTxt.getText();
+        return true;
+    }
+
+}

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=804409&r1=804408&r2=804409&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 Fri Aug 14 23:39:11 2009
@@ -76,16 +76,24 @@
     
     public void addRow(PubsubNode node) {
         this.nodes.add(node);
+        fireTableRowsInserted(getRowCount()-1, getColumnCount()-1);
     }
     
     public void deleteRow(int rowIndex) {
         this.nodes.remove(rowIndex);
+        fireTableRowsDeleted(rowIndex, rowIndex);
     }
     
     @Override
     public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
         PubsubNode n = this.nodes.get(rowIndex);
         if(columnIndex == 1) n.setSubscribed((Boolean)aValue);
-        super.fireTableCellUpdated(rowIndex, columnIndex);
+        fireTableCellUpdated(rowIndex, columnIndex);
+    }
+
+    public void clear() {
+        int rowCount = getRowCount();
+        nodes.clear();
+        fireTableRowsDeleted(0, rowCount);
     }
 }

Added: mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubTableModelListener.java
URL: http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubTableModelListener.java?rev=804409&view=auto
==============================================================================
--- mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubTableModelListener.java (added)
+++ mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubTableModelListener.java Fri Aug 14 23:39:11 2009
@@ -0,0 +1,60 @@
+/*
+ *  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.event.TableModelEvent;
+import javax.swing.event.TableModelListener;
+
+import org.jivesoftware.smackx.pubsub.Node;
+import org.jivesoftware.smackx.pubsub.PubSubManager;
+
+public class PubsubTableModelListener implements TableModelListener {
+    private PubsubClientGUI parent;
+
+    public PubsubTableModelListener(PubsubClientGUI parent) {
+        this.parent = parent;
+    }
+
+    public void tableChanged(TableModelEvent event) {
+        PubsubTableModel dtm = parent.getTableModel();
+        PubSubManager pubsubMgr = parent.getPubsubMgr();
+        String jid = parent.getJID();
+        
+        if(event.getType() == TableModelEvent.UPDATE) {
+            try {
+                Boolean sub = (Boolean)dtm.getValueAt(event.getFirstRow(), event.getColumn());
+                String nodeName = (String)dtm.getValueAt(event.getFirstRow(), 0);
+    
+                Node node = pubsubMgr.getNode(nodeName);
+    
+                if(sub.booleanValue()) { // contains the new value (soll)
+                    node.subscribe(jid);
+                    System.out.println(jid + " subscribed to " + node.getId());
+                } else {
+                    node.unsubscribe(jid);
+                    System.out.println(jid + " unsubscribed from " + node.getId());
+                }
+            } catch(Exception ex) {
+                ex.printStackTrace();
+            }
+        }
+    }
+
+}