blob: 0c28ebd21669b5c4956618d42909804715a92888 [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 Furfarid1072b52006-05-03 07:44:48 +00009 *
Francesco Furfariec7e1752006-10-02 13:37:04 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Francesco Furfarid1072b52006-05-03 07:44:48 +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 Furfarid1072b52006-05-03 07:44:48 +000018 */
19
20package org.apache.felix.upnp.sample.binaryLight;
21
22import java.awt.BorderLayout;
23import java.awt.event.MouseAdapter;
24import java.awt.event.MouseEvent;
25import java.awt.event.WindowAdapter;
26import java.awt.event.WindowEvent;
27import java.beans.PropertyChangeEvent;
28import java.beans.PropertyChangeListener;
29import java.net.URL;
30
31import javax.swing.Icon;
32import javax.swing.ImageIcon;
33import javax.swing.JButton;
34import javax.swing.JFrame;
35import javax.swing.JLabel;
36import javax.swing.JPanel;
37import javax.swing.SwingUtilities;
38
39import org.osgi.framework.BundleException;
40
41/*
Karl Paulsd312acc2007-06-18 20:38:33 +000042* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
Francesco Furfarid1072b52006-05-03 07:44:48 +000043*/
44
45public class LightUI extends JFrame implements PropertyChangeListener {
46 private final static ImageIcon LIGHT_ON = LightUI.loadIcon("LightOn.gif","ON");
47 private final static ImageIcon LIGHT_OFF = LightUI.loadIcon("LightOff.gif","OFF");
48 private final static ImageIcon LIGHT_FAIL = LightUI.loadIcon("LightFail.gif","FAILURE");
49 private final JLabel label = new JLabel();
50 private LightModel model;
51
52 public LightUI(LightModel model) {
53 super("Felix UPnP BinaryLight");
54 this.model = model;
55 setSize(150,150);
56 JPanel panel = new JPanel(new BorderLayout());
57 panel.add(doMainPanel(),BorderLayout.CENTER);
58 panel.add(doControlPanel(),BorderLayout.SOUTH);
59 getContentPane().add(panel);
60 model.addPropertyChangeListener(this);
61
62 addWindowListener(new WindowAdapter(){
63 public void windowClosing(WindowEvent e)
64 {
65 try {
66 Activator.context.getBundle().stop();
67 } catch (BundleException ex) {
68 ex.printStackTrace();
69 }
70 }
71 });
72 try {
73 URL eventIconUrl = LightUI.class.getResource("images/logo.gif");
74 ImageIcon icon= new ImageIcon(eventIconUrl,"logo");
75 setIconImage(icon.getImage());
76 }
77 catch (Exception ex){
78 System.out.println("Resource: IMAGES/logo.gif not found : " + ex.toString());
79 }
80
81 pack();
82 show();
83 }
84
85 private JPanel doMainPanel(){
86 JPanel panel = new JPanel();
87 label.setIcon(LIGHT_OFF);
88 //label.setSize(new Dimension(32,32));
89 label.addMouseListener(new MouseAdapter(){
90 public void mouseClicked(MouseEvent e){
91 if (SwingUtilities.isLeftMouseButton(e)){
92 if (e.getClickCount()==1){
93 Icon icon = label.getIcon();
94 if (icon == LIGHT_ON)
95 model.switchOff();
96 else
97 model.switchOn();
98 }
99 }
100 }
101 });
102 panel.add(label);
103 return panel;
104 }
105
106 private JPanel doControlPanel(){
107 JPanel panel = new JPanel();
108 JButton btnSwitch = new JButton("On");
109 JButton btnFailure = new JButton("Failure");
110 panel.add(btnSwitch);
111 panel.add(btnFailure);
112 return panel;
113 }
114
115 public static ImageIcon loadIcon(String path,String title)
116 {
117 try {
118 URL eventIconUrl = LightUI.class.getResource("images/" + path);
119 return new ImageIcon(eventIconUrl,title);
120 }
121 catch (Exception ex){
122 System.out.println("Resource:" + path + " not found : " + ex.toString());
123 return null;
124 }
125 }
126
127 /* (non-Javadoc)
128 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
129 */
130 public void propertyChange(PropertyChangeEvent evt) {
131 String property = evt.getPropertyName();
132 System.out.println("Light changed property::"+property);
133 boolean value = ((Boolean) evt.getNewValue()).booleanValue();
134 if (property.equals("Status")){
135 if (value)
136 label.setIcon(LIGHT_ON);
137 else
138 label.setIcon(LIGHT_OFF);
139 }
140 else if (property.equals("Failure")){
141 if (value)
142 label.setIcon(LIGHT_FAIL);
143 else
144 label.setIcon(LIGHT_OFF);
145 }
146 getContentPane().validate();
147 repaint();
148
149 }
150
151
152}