blob: 263196e3166ef1275d2b5906339d3484ee65a349 [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 */
Francesco Furfariec7e1752006-10-02 13:37:04 +000019
Francesco Furfarid1072b52006-05-03 07:44:48 +000020package org.apache.felix.upnp.sample.clock;
21
22import java.awt.BorderLayout;
23import java.awt.event.WindowAdapter;
24import java.awt.event.WindowEvent;
25import java.net.URL;
26
27import javax.swing.ImageIcon;
28import javax.swing.JFrame;
29
30import org.osgi.framework.BundleContext;
31import org.osgi.framework.BundleException;
32/*
Karl Paulsd312acc2007-06-18 20:38:33 +000033* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
Francesco Furfarid1072b52006-05-03 07:44:48 +000034*/
35
36public class ClockFrame extends JFrame implements Runnable
37{
38 private final static String TITLE = "Felix UPnP Clock";
39 private ClockDevice clockDev;
40 private ClockPane clockPane;
41
42 public ClockFrame(final BundleContext context)
43 {
44 super(TITLE);
45 try {
46 clockDev = new ClockDevice( context);
47 }
48 catch (Exception e) {
49 System.out.println(e);
50 }
51
52 getContentPane().setLayout(new BorderLayout());
53
54 clockPane = new ClockPane();
55 getContentPane().add(clockPane, BorderLayout.CENTER);
56
57 addWindowListener(new WindowAdapter(){
58 public void windowClosing(WindowEvent e)
59 {
60 try {
61 context.getBundle().stop();
62 } catch (BundleException ex) {
63 ex.printStackTrace();
64 }
65 }
66 });
67 try {
68 URL eventIconUrl = ClockFrame.class.getResource("images/logo.gif");
69 ImageIcon icon= new ImageIcon(eventIconUrl,"logo");
70 setIconImage(icon.getImage());
71 }
72 catch (Exception ex){
73 System.out.println("Resource: IMAGES/logo.gif not found : " + ex.toString());
74 }
75
76 pack();
77 setVisible(true);
78 }
79
80 public ClockPane getClockPanel()
81 {
82 return clockPane;
83 }
84
85 public ClockDevice getClockDevice()
86 {
87 return clockDev;
88 }
89
90 ////////////////////////////////////////////////
91 // run
92 ////////////////////////////////////////////////
93
94 private Thread timerThread = null;
95
96 public void run()
97 {
98 Thread thisThread = Thread.currentThread();
99
100 while (timerThread == thisThread) {
101 getClockDevice().update();
102 getClockPanel().repaint();
103 try {
104 Thread.sleep(1000);
105 }
106 catch(InterruptedException e) {}
107 }
108 }
109
110 public void start()
111 {
112 clockDev.start();
113
114 timerThread = new Thread(this);
115 timerThread.start();
116 }
117
118 public void stop()
119 {
120 clockDev.stop();
121 timerThread = null;
122 dispose();
123 }
124
125}
126