blob: 5591b380fa4d5c7a7c1e866068bd64629de8e227 [file] [log] [blame]
Francesco Furfariec7e1752006-10-02 13:37:04 +00001/*
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 Furfari91af5da2006-04-04 23:44:23 +00009 *
Francesco Furfariec7e1752006-10-02 13:37:04 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Francesco Furfari91af5da2006-04-04 23:44:23 +000011 *
Francesco Furfariec7e1752006-10-02 13:37:04 +000012 * 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 Furfari91af5da2006-04-04 23:44:23 +000018 */
19
20package org.apache.felix.upnp.tester.gui;
21
22
23
24import java.awt.event.ActionEvent;
25import java.util.Dictionary;
26import java.util.Enumeration;
27
28import javax.swing.AbstractAction;
29import javax.swing.JButton;
30import javax.swing.JPanel;
31import javax.swing.JTree;
32import javax.swing.SwingUtilities;
33
34import org.apache.felix.upnp.tester.Activator;
35import org.apache.felix.upnp.tester.Mediator;
36import org.apache.felix.upnp.tester.UPnPSubscriber;
37import org.apache.felix.upnp.tester.discovery.DeviceNode;
38import org.osgi.service.upnp.UPnPDevice;
39import org.osgi.service.upnp.UPnPEventListener;
40import org.osgi.service.upnp.UPnPService;
Francesco Furfarif2a67912006-07-17 17:08:02 +000041/*
Karl Paulsd312acc2007-06-18 20:38:33 +000042* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
Francesco Furfarif2a67912006-07-17 17:08:02 +000043*/
Francesco Furfari91af5da2006-04-04 23:44:23 +000044public 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