Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 1 | /*
|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one
|
| 3 | * or more contributor license agreements. See the NOTICE file
|
| 4 | * distributed with this work for additional information
|
| 5 | * regarding copyright ownership. The ASF licenses this file
|
| 6 | * to you under the Apache License, Version 2.0 (the
|
| 7 | * "License"); you may not use this file except in compliance
|
| 8 | * with the License. You may obtain a copy of the License at
|
Francesco Furfari | 91af5da | 2006-04-04 23:44:23 +0000 | [diff] [blame] | 9 | *
|
Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0
|
Francesco Furfari | 91af5da | 2006-04-04 23:44:23 +0000 | [diff] [blame] | 11 | *
|
Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 12 | * Unless required by applicable law or agreed to in writing,
|
| 13 | * software distributed under the License is distributed on an
|
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
| 15 | * KIND, either express or implied. See the License for the
|
| 16 | * specific language governing permissions and limitations
|
| 17 | * under the License.
|
Francesco Furfari | 91af5da | 2006-04-04 23:44:23 +0000 | [diff] [blame] | 18 | */
|
| 19 |
|
| 20 | package org.apache.felix.upnp.tester.gui;
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 | import java.awt.event.ActionEvent;
|
| 25 | import java.util.Dictionary;
|
| 26 | import java.util.Enumeration;
|
| 27 |
|
| 28 | import javax.swing.AbstractAction;
|
| 29 | import javax.swing.JButton;
|
| 30 | import javax.swing.JPanel;
|
| 31 | import javax.swing.JTree;
|
| 32 | import javax.swing.SwingUtilities;
|
| 33 |
|
| 34 | import org.apache.felix.upnp.tester.Activator;
|
| 35 | import org.apache.felix.upnp.tester.Mediator;
|
| 36 | import org.apache.felix.upnp.tester.UPnPSubscriber;
|
| 37 | import org.apache.felix.upnp.tester.discovery.DeviceNode;
|
| 38 | import org.osgi.service.upnp.UPnPDevice;
|
| 39 | import org.osgi.service.upnp.UPnPEventListener;
|
| 40 | import org.osgi.service.upnp.UPnPService;
|
Francesco Furfari | f2a6791 | 2006-07-17 17:08:02 +0000 | [diff] [blame] | 41 | /*
|
Karl Pauls | d312acc | 2007-06-18 20:38:33 +0000 | [diff] [blame^] | 42 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
|
Francesco Furfari | f2a6791 | 2006-07-17 17:08:02 +0000 | [diff] [blame] | 43 | */
|
Francesco Furfari | 91af5da | 2006-04-04 23:44:23 +0000 | [diff] [blame] | 44 | public class SubscriptionPanel extends JPanel implements UPnPEventListener{
|
| 45 | public SubscriptionPanel() {
|
| 46 | super();
|
| 47 | buildButtonPanel();
|
| 48 | }
|
| 49 |
|
| 50 | private void buildButtonPanel(){
|
| 51 | JButton subscribeBtn = new JButton("Subscribe");
|
| 52 | subscribeBtn.addActionListener(new AbstractAction(){
|
| 53 | public void actionPerformed(ActionEvent e) {
|
| 54 | //System.out.println("subscribing ...");
|
| 55 | doSubscribe();
|
| 56 | }
|
| 57 | });
|
| 58 | JButton unsubscribeBtn = new JButton("Unsubscribe");
|
| 59 | unsubscribeBtn.addActionListener(new AbstractAction(){
|
| 60 | public void actionPerformed(ActionEvent e) {
|
| 61 | //System.out.println("unsubscribing ...");
|
| 62 | doUnsubscribe();
|
| 63 | }
|
| 64 | });
|
| 65 | add(subscribeBtn);
|
| 66 | add(unsubscribeBtn);
|
| 67 | }
|
| 68 |
|
| 69 | UPnPSubscriber subscriber;
|
| 70 | public void doSubscribe()
|
| 71 | {
|
| 72 | if (subscriber == null)
|
| 73 | subscriber = new UPnPSubscriber(Activator.context,this);
|
| 74 |
|
| 75 | UPnPDeviceTreeNode selectedNode = getSelectedNode();
|
| 76 | String serviceId = getServiceId(selectedNode);
|
| 77 | String parentId = getDeviceId(selectedNode);
|
| 78 | LogPanel.log("subscribing ... "+ "ServiceId ["+serviceId+"] of DeviceId ["+parentId +"]");
|
| 79 | subscriber.subscribe(parentId,serviceId);
|
| 80 | setSubscribedVariableOf(selectedNode);
|
| 81 | }
|
| 82 |
|
| 83 | public void doUnsubscribe(){
|
| 84 | UPnPDeviceTreeNode selectedNode = getSelectedNode();
|
| 85 | String serviceId = getServiceId(selectedNode);
|
| 86 | String parentId = getDeviceId(selectedNode);
|
| 87 | LogPanel.log("unsubscribing ... "+ "ServiceId ["+serviceId+"] of DeviceId ["+parentId +"]");
|
| 88 | subscriber.unsubscribe(parentId,serviceId);
|
| 89 | setUnubscribedVariableOf(selectedNode);
|
| 90 | }
|
| 91 |
|
| 92 | public void notifyUPnPEvent(final String deviceId, final String serviceId, final Dictionary events) {
|
| 93 | // UPnP base driver notify are synchronous !!
|
| 94 | Runnable doShowMsg = new Runnable() {
|
| 95 | public void run() {
|
| 96 | LogPanel.log("notifyUPnPEvent::[DeviceID "+deviceId+"][ServiceId "+serviceId+"]");
|
| 97 | Enumeration elements = events.keys();
|
| 98 | while (elements.hasMoreElements()){
|
| 99 | Object key = elements.nextElement();
|
| 100 | Object value = events.get(key);
|
| 101 | LogPanel.log("["+key+"][value "+value+"]");
|
| 102 | }
|
| 103 | }
|
| 104 | };
|
| 105 | SwingUtilities.invokeLater(doShowMsg);
|
| 106 |
|
| 107 | }
|
| 108 |
|
| 109 | private void setSubscribedVariableOf(UPnPDeviceTreeNode selectedNode){
|
| 110 | Enumeration list = selectedNode.children();
|
| 111 | while (list.hasMoreElements()){
|
| 112 | UPnPDeviceTreeNode node = (UPnPDeviceTreeNode) list.nextElement();
|
| 113 | if (node.category == UPnPDeviceTreeNode.EVENTED_STATE)
|
| 114 | node.category = UPnPDeviceTreeNode.SUBSCRIBED_STATE;
|
| 115 | }
|
| 116 | JTree tree = Mediator.getUPnPDeviceTree();
|
| 117 | tree.validate();
|
| 118 | tree.repaint();
|
| 119 | }
|
| 120 |
|
| 121 | private void setUnubscribedVariableOf(UPnPDeviceTreeNode selectedNode){
|
| 122 | Enumeration list = selectedNode.children();
|
| 123 | while (list.hasMoreElements()){
|
| 124 | UPnPDeviceTreeNode node = (UPnPDeviceTreeNode) list.nextElement();
|
| 125 | if (node.category == UPnPDeviceTreeNode.SUBSCRIBED_STATE)
|
| 126 | node.category = UPnPDeviceTreeNode.EVENTED_STATE;
|
| 127 | }
|
| 128 | JTree tree = Mediator.getUPnPDeviceTree();
|
| 129 | tree.validate();
|
| 130 | tree.repaint();
|
| 131 | }
|
| 132 |
|
| 133 | private UPnPDeviceTreeNode getSelectedNode(){
|
| 134 | JTree tree = Mediator.getUPnPDeviceTree();
|
| 135 | UPnPDeviceTreeNode selectedNode = (UPnPDeviceTreeNode)tree.getLastSelectedPathComponent();
|
| 136 | return selectedNode;
|
| 137 | }
|
| 138 | private String getServiceId (UPnPDeviceTreeNode selectedNode){
|
| 139 | Object userObj = selectedNode.getUserObject();
|
| 140 | String serviceId = ((UPnPService) userObj).getId();
|
| 141 | return serviceId;
|
| 142 | }
|
| 143 | private String getDeviceId (UPnPDeviceTreeNode selectedNode){
|
| 144 | UPnPDeviceTreeNode parent = (UPnPDeviceTreeNode)selectedNode.getParent();
|
| 145 | DeviceNode node =(DeviceNode)parent.getUserObject();
|
| 146 | String parentId = (String) node.getReference().getProperty(UPnPDevice.ID);
|
| 147 | return parentId;
|
| 148 | }
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 | }
|
| 153 |
|
| 154 |
|