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 11:33:59 UTC

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

Author: mjakl
Date: Sat Aug 15 09:33:40 2009
New Revision: 804445

URL: http://svn.apache.org/viewvc?rev=804445&view=rev
Log:
It is now possible to delete a node (if the user is the owner).
Split up the GUI and model (little refactoring).

Added:
    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/PubsubDeleteButtonListener.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/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/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=804445&r1=804444&r2=804445&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 09:33:40 2009
@@ -20,12 +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;
-import java.util.List;
-import java.util.Map;
 
 import javax.swing.JButton;
 import javax.swing.JFrame;
@@ -39,14 +34,8 @@
 import javax.swing.ListSelectionModel;
 import javax.swing.SwingUtilities;
 import javax.swing.UIManager;
-
-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.PubSubManager;
-import org.jivesoftware.smackx.pubsub.Subscription;
+import javax.swing.event.ListSelectionEvent;
+import javax.swing.event.ListSelectionListener;
 
 /**
  * A simple demo application for the pubsub module of Vysper. It allows to lookup
@@ -55,19 +44,16 @@
  *
  * @author The Apache MINA Project (http://mina.apache.org)
  */
-public class PubsubClientGUI implements Runnable {
+public class PubsubClientGUI implements Runnable, ListSelectionListener {
     private JFrame frame;
-    private PubsubTableModel tableModel = new PubsubTableModel();
-    private XMPPConnection connection;
-    private PubSubManager pubsubMgr;
-    private String username;
-    private String hostname;
-    private String password;
-    private String jid;
+    private JButton delete;
+    private PubsubClientModel pcm = new PubsubClientModel();
 
     private void createAndShowGUI() {
         setUpLookAndFeel();
 
+        PubsubTableModel tableModel = pcm.getTableModel();
+        
         frame = new JFrame("Vysper Publish/Subscribe Client");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
@@ -78,13 +64,18 @@
         JScrollPane scrollPane = new JScrollPane(nodeTable);
         nodeTable.setFillsViewportHeight(true);
         nodeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
-        tableModel.addTableModelListener(new PubsubTableModelListener(this));
+        ListSelectionModel lsm = nodeTable.getSelectionModel();
+        lsm.addListSelectionListener(this);
+        tableModel.addTableModelListener(new PubsubTableModelListener(pcm));
 
         JButton create = new JButton("Create");
         create.setActionCommand("create");
-        create.addActionListener(new PubsubCreateButtonListener(this));
+        create.addActionListener(new PubsubCreateButtonListener(frame, pcm));
 
-        JButton delete = new JButton("Delete");
+        delete = new JButton("Delete");
+        delete.setActionCommand("delete");
+        delete.addActionListener(new PubsubDeleteButtonListener(frame, pcm));
+        delete.setEnabled(false);
         
         JPanel buttons = new JPanel();
         buttons.add(create);
@@ -112,103 +103,29 @@
         SwingUtilities.invokeLater(ex1);
     }
 
-    private void logout() {
-        if(connection != null && connection.isConnected()) {
-            connection.disconnect();
-        }
-    }
-
     public void run() {
         createAndShowGUI();
         registerShutDownHook();
 
         login();
-        refresh();
+        pcm.refresh();
+    }
+    
+    public void login() {
+        do {
+            askForCredentials();
+        } while(pcm.login() == false);
     }
 
     private void registerShutDownHook() {
         Runtime.getRuntime().addShutdownHook(new Thread() {
             @Override
             public void run() {
-                logout();
+                pcm.logout();
             }
         });
     }
 
-    public void refresh() {
-        Map<String, PubsubNode> lookup = new HashMap<String, PubsubNode>();
-
-        try {
-            discoverNodes(lookup);
-            discoverSubscriptions(lookup);
-            discoverAffiliations(lookup);
-        } catch (XMPPException e) {
-            e.printStackTrace();
-        }
-
-        tableModel.clear();
-        for(PubsubNode n : lookup.values()) {
-            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.getType());
-            System.out.print(" of ");
-            System.out.println(affiliation.getNodeId());
-
-            PubsubNode n = lookup.get(affiliation.getNodeId());
-            n.setOwnership(affiliation.getType().toString().equals("owner"));
-        }
-    }
-
-    private void discoverSubscriptions(Map<String, PubsubNode> lookup) throws XMPPException {
-        List<Subscription> lSubscriptions = pubsubMgr.getSubscriptions();
-        for(Subscription subscription : lSubscriptions) {
-            System.out.print(subscription.getState());
-            System.out.print(" at ");
-            System.out.println(subscription.getNode());
-
-            PubsubNode n = lookup.get(subscription.getNode());
-            if(n != null) {
-                n.setSubscribed(subscription.getState().toString().equals("subscribed"));
-            }
-        }
-    }
-
-    private void discoverNodes(Map<String, PubsubNode> lookup) throws XMPPException {
-        DiscoverItems di = pubsubMgr.discoverNodes();
-        Iterator<Item> iIt = di.getItems();
-        while(iIt.hasNext()) {
-            Item i = iIt.next();
-            System.out.println("Adding " + i.getNode());
-
-            PubsubNode n = new PubsubNode(i.getNode());
-            if(n != null) {
-                lookup.put(i.getNode(), n);
-            }
-        }
-    }
-
-    private void login() {
-        boolean loginOK = false;
-        
-        do {
-            askForCredentials();
-            try {
-                connection = connect(username, password, hostname);
-                loginOK = true;
-            } catch (XMPPException e) {
-                System.err.println("Login failed for user "+username);
-                e.printStackTrace();
-            }
-        } while(loginOK == false);
-        
-        pubsubMgr = new PubSubManager(connection);
-    }
-
     private void askForCredentials() {
         JLabel jidLab = new JLabel("JID");
         JTextField jidTxt = new JTextField("user1@vysper.org");
@@ -250,32 +167,27 @@
             System.exit(0);
         }
         
-        this.username = usernameTxt.getText();
-        this.hostname = hostTxt.getText();
-        this.password = passwordTxt.getText();
-        this.jid = jidTxt.getText();
-    }
-
-    private XMPPConnection connect(String username, String password, String host) throws XMPPException {
-        XMPPConnection connection = new XMPPConnection(host);
-        connection.connect();
-        connection.login(username, password);
-        return connection;
-    }
-
-    public Component getFrame() {
-        return frame;
-    }
-
-    public PubSubManager getPubsubMgr() {
-        return pubsubMgr;
-    }
-
-    public PubsubTableModel getTableModel() {
-        return tableModel;
+        pcm.setUsername(usernameTxt.getText());
+        pcm.setHostname(hostTxt.getText());
+        pcm.setPassword(passwordTxt.getText());
+        pcm.setJID(jidTxt.getText());
+    }
+
+    public void valueChanged(ListSelectionEvent e) {
+        ListSelectionModel lsm = (ListSelectionModel)e.getSource();
+        if(lsm.isSelectionEmpty()) {
+            // disable delete button
+            delete.setEnabled(false);
+            pcm.deselectNode();
+        } else {
+            // store the node and enable delete button
+            PubsubTableModel tableModel = pcm.getTableModel();
+            String selectedNode = (String)tableModel.getValueAt(e.getFirstIndex(), 0);
+            pcm.selectNode(selectedNode);
+            if((Boolean)tableModel.getValueAt(e.getFirstIndex(), 2)) { //owner
+                delete.setEnabled(true);
+            }
+        }
     }
 
-    public String getJID() {
-        return jid;
-    }
 }

Added: 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=804445&view=auto
==============================================================================
--- mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubClientModel.java (added)
+++ mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubClientModel.java Sat Aug 15 09:33:40 2009
@@ -0,0 +1,171 @@
+/*
+ *  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.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+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.PubSubManager;
+import org.jivesoftware.smackx.pubsub.Subscription;
+
+public class PubsubClientModel {
+    private PubsubTableModel tableModel = new PubsubTableModel();
+    
+    private XMPPConnection connection;
+    private PubSubManager pubsubMgr;
+    
+    private String username;
+    private String hostname;
+    private String password;
+    private String jid;
+    
+    private String selectedNode;
+    
+
+    public PubSubManager getPubsubMgr() {
+        return pubsubMgr;
+    }
+
+    public PubsubTableModel getTableModel() {
+        return tableModel;
+    }
+
+    public String getJID() {
+        return jid;
+    }
+
+    private void discoverAffiliations(Map<String, PubsubNode> lookup) throws XMPPException {
+        List<Affiliation> lAffiliations = pubsubMgr.getAffiliations();
+        for(Affiliation affiliation : lAffiliations) {
+            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"));
+        }
+    }
+
+    private void discoverSubscriptions(Map<String, PubsubNode> lookup) throws XMPPException {
+        List<Subscription> lSubscriptions = pubsubMgr.getSubscriptions();
+        for(Subscription subscription : lSubscriptions) {
+            System.out.print(subscription.getState());
+            System.out.print(" at ");
+            System.out.println(subscription.getNode());
+
+            PubsubNode n = lookup.get(subscription.getNode());
+            if(n != null) {
+                n.setSubscribed(subscription.getState().toString().equals("subscribed"));
+            }
+        }
+    }
+
+    private void discoverNodes(Map<String, PubsubNode> lookup) throws XMPPException {
+        DiscoverItems di = pubsubMgr.discoverNodes();
+        Iterator<Item> iIt = di.getItems();
+        while(iIt.hasNext()) {
+            Item i = iIt.next();
+            System.out.println("Adding " + i.getNode());
+
+            PubsubNode n = new PubsubNode(i.getNode());
+            if(n != null) {
+                lookup.put(i.getNode(), n);
+            }
+        }
+    }
+
+    public boolean login() {
+        try {
+            connection = connect(username, password, hostname);
+        } catch (XMPPException e) {
+            System.err.println("Login failed for user "+username);
+            e.printStackTrace();
+            return false;
+        }
+        
+        pubsubMgr = new PubSubManager(connection);
+        return true;
+    }
+
+    private XMPPConnection connect(String username, String password, String host) throws XMPPException {
+        XMPPConnection connection = new XMPPConnection(host);
+        connection.connect();
+        connection.login(username, password);
+        return connection;
+    }
+
+    public void refresh() {
+        Map<String, PubsubNode> lookup = new HashMap<String, PubsubNode>();
+
+        try {
+            discoverNodes(lookup);
+            discoverSubscriptions(lookup);
+            discoverAffiliations(lookup);
+        } catch (XMPPException e) {
+            e.printStackTrace();
+        }
+
+        tableModel.clear();
+        for(PubsubNode n : lookup.values()) {
+            tableModel.addRow(n);
+        }
+    }
+
+    public void logout() {
+        if(connection != null && connection.isConnected()) {
+            connection.disconnect();
+        }
+    }
+
+    public void setHostname(String hostname) {
+        this.hostname = hostname;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public void setJID(String jid) {
+        this.jid = jid;
+    }
+
+    public void deselectNode() {
+        this.selectedNode = null;
+    }
+
+    public void selectNode(String selectedNode) {
+        this.selectedNode = selectedNode;
+    }
+    
+    public String getSelectedNode() {
+        return this.selectedNode;
+    }
+}

Modified: 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=804445&r1=804444&r2=804445&view=diff
==============================================================================
--- mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubCreateButtonListener.java (original)
+++ mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubCreateButtonListener.java Sat Aug 15 09:33:40 2009
@@ -23,6 +23,7 @@
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 
+import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
@@ -33,10 +34,12 @@
 public class PubsubCreateButtonListener implements ActionListener {
 
     private String nodeID;
-    private PubsubClientGUI parent;
+    private PubsubClientModel parent;
+    private JFrame frame;
     
-    public PubsubCreateButtonListener(PubsubClientGUI parent) {
+    public PubsubCreateButtonListener(JFrame frame, PubsubClientModel parent) {
         this.parent = parent;
+        this.frame = frame;
     }
     
     public void actionPerformed(ActionEvent e) {
@@ -62,14 +65,14 @@
         panel.add(nodeLab);
         panel.add(nodeTxt);
         
-        int answer = JOptionPane.showOptionDialog(parent.getFrame(),
+        int answer = JOptionPane.showOptionDialog(frame,
                 panel,
                 "Create new node",
                 JOptionPane.OK_CANCEL_OPTION,
                 JOptionPane.QUESTION_MESSAGE,
                 null,
                 new String[] {"OK", "Cancel"},
-                "OK");
+                null);
 
         if(answer != 0) {
             return false;

Added: mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubDeleteButtonListener.java
URL: http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubDeleteButtonListener.java?rev=804445&view=auto
==============================================================================
--- mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubDeleteButtonListener.java (added)
+++ mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubDeleteButtonListener.java Sat Aug 15 09:33:40 2009
@@ -0,0 +1,73 @@
+/*
+ *  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.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JTextField;
+
+import org.jivesoftware.smack.XMPPException;
+
+public class PubsubDeleteButtonListener implements ActionListener {
+
+    private PubsubClientModel parent;
+    private JFrame frame;
+    
+    public PubsubDeleteButtonListener(JFrame frame, PubsubClientModel parent) {
+        this.parent = parent;
+        this.frame = frame;
+    }
+    
+    public void actionPerformed(ActionEvent e) {
+        String nodeID = parent.getSelectedNode();
+        if(nodeID != null && askForSure()) {
+            try {
+                parent.getPubsubMgr().deleteNode(nodeID);
+                System.out.println("Node deleted: " + nodeID);
+                parent.refresh();
+            } catch (XMPPException e1) {
+                System.err.println("Couldn't delete node "+nodeID);
+                e1.printStackTrace();
+            }
+        }
+    }
+    
+    private boolean askForSure() {
+        JLabel nodeLab = new JLabel("Node ID");
+        JTextField nodeTxt = new JTextField();
+        nodeLab.setLabelFor(nodeTxt);
+        
+        int answer = JOptionPane.showConfirmDialog(frame,
+                "The node and all associated data will be lost!",
+                "Delete node?",
+                JOptionPane.OK_CANCEL_OPTION,
+                JOptionPane.WARNING_MESSAGE);
+
+        if(answer != JOptionPane.OK_OPTION) {
+            return false;
+        }
+        return true;
+    }
+
+}

Modified: 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=804445&r1=804444&r2=804445&view=diff
==============================================================================
--- mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubTableModelListener.java (original)
+++ mina/sandbox/vysper/trunk/demo/pubsub/client/src/main/java/org/apache/vysper/demo/pubsub/client/PubsubTableModelListener.java Sat Aug 15 09:33:40 2009
@@ -26,9 +26,9 @@
 import org.jivesoftware.smackx.pubsub.PubSubManager;
 
 public class PubsubTableModelListener implements TableModelListener {
-    private PubsubClientGUI parent;
+    private PubsubClientModel parent;
 
-    public PubsubTableModelListener(PubsubClientGUI parent) {
+    public PubsubTableModelListener(PubsubClientModel parent) {
         this.parent = parent;
     }