blob: 7e771e79016053df06afed5444619f214cc54172 [file] [log] [blame]
Richard S. Hall46754b52007-07-16 18:36:11 +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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
18 */
19package org.apache.felix.example.extenderbased.host;
20
21import java.awt.event.WindowAdapter;
22import java.awt.event.WindowEvent;
Richard S. Hall0db88872012-03-07 16:24:47 +000023import javax.swing.JFrame;
24import javax.swing.SwingUtilities;
Richard S. Hall0db88872012-03-07 16:24:47 +000025import org.osgi.framework.BundleActivator;
26import org.osgi.framework.BundleContext;
27import org.osgi.framework.BundleException;
Richard S. Hall46754b52007-07-16 18:36:11 +000028
29/**
30 * The activator of the host application bundle. The activator creates the
31 * main application <tt>JFrame</tt> and starts tracking <tt>SimpleShape</tt>
32 * extensions. All activity is performed on the Swing event thread to avoid
33 * synchronization and repainting issues. Closing the application window
34 * will result in <tt>Bundle.stop()</tt> being called on the system bundle,
35 * which will cause the framework to shutdown and the JVM to exit.
Richard S. Hall46754b52007-07-16 18:36:11 +000036**/
Richard S. Hall259e83c2009-06-10 19:54:15 +000037public class Activator implements BundleActivator
Richard S. Hall46754b52007-07-16 18:36:11 +000038{
Richard S. Hall46754b52007-07-16 18:36:11 +000039 private DrawingFrame m_frame = null;
Richard S. Hallf48d7dc2012-03-26 15:23:41 +000040 private ShapeBundleTracker m_shapetracker = null;
Richard S. Hall46754b52007-07-16 18:36:11 +000041
42 /**
43 * Displays the applications window and starts extension tracking;
44 * everything is done on the Swing event thread to avoid synchronization
45 * and repainting issues.
46 * @param context The context of the bundle.
47 **/
Richard S. Hallc9e91a22012-03-08 17:09:16 +000048 @Override
Richard S. Hall259e83c2009-06-10 19:54:15 +000049 public void start(final BundleContext context)
Richard S. Hall46754b52007-07-16 18:36:11 +000050 {
Richard S. Hall0db88872012-03-07 16:24:47 +000051 SwingUtilities.invokeLater(new Runnable() {
Richard S. Hall259e83c2009-06-10 19:54:15 +000052 // This creates of the application window.
Richard S. Hallc9e91a22012-03-08 17:09:16 +000053 @Override
Richard S. Hall259e83c2009-06-10 19:54:15 +000054 public void run()
Richard S. Hall46754b52007-07-16 18:36:11 +000055 {
Richard S. Hall259e83c2009-06-10 19:54:15 +000056 m_frame = new DrawingFrame();
57 m_frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Richard S. Hall0db88872012-03-07 16:24:47 +000058 m_frame.addWindowListener(new WindowAdapter()
59 {
60 @Override
Richard S. Hall259e83c2009-06-10 19:54:15 +000061 public void windowClosing(WindowEvent evt)
62 {
63 try
64 {
65 context.getBundle(0).stop();
66 }
67 catch (BundleException ex)
68 {
69 ex.printStackTrace();
70 }
71 }
72 });
73
74 m_frame.setVisible(true);
75
Richard S. Hallf48d7dc2012-03-26 15:23:41 +000076 m_shapetracker = new ShapeBundleTracker(context, m_frame);
Richard S. Hall259e83c2009-06-10 19:54:15 +000077 m_shapetracker.open();
Richard S. Hall46754b52007-07-16 18:36:11 +000078 }
Richard S. Hall259e83c2009-06-10 19:54:15 +000079 });
Richard S. Hall46754b52007-07-16 18:36:11 +000080 }
81
82 /**
83 * Stops extension tracking and disposes of the application window.
84 * @param context The context of the bundle.
85 **/
Richard S. Hallc9e91a22012-03-08 17:09:16 +000086 @Override
Richard S. Hall46754b52007-07-16 18:36:11 +000087 public void stop(BundleContext context)
88 {
Richard S. Hall259e83c2009-06-10 19:54:15 +000089 Runnable runner = new Runnable() {
90 // This disposes of the application window.
Richard S. Hallc9e91a22012-03-08 17:09:16 +000091 @Override
Richard S. Hall259e83c2009-06-10 19:54:15 +000092 public void run()
Richard S. Hall46754b52007-07-16 18:36:11 +000093 {
Richard S. Hall259e83c2009-06-10 19:54:15 +000094 m_shapetracker.close();
95 m_frame.setVisible(false);
96 m_frame.dispose();
Richard S. Hall46754b52007-07-16 18:36:11 +000097 }
Richard S. Hall259e83c2009-06-10 19:54:15 +000098 };
Richard S. Hall46754b52007-07-16 18:36:11 +000099
Richard S. Hall259e83c2009-06-10 19:54:15 +0000100 if (SwingUtilities.isEventDispatchThread())
101 {
102 runner.run();
103 }
104 else
105 {
106 try
107 {
108 javax.swing.SwingUtilities.invokeAndWait(runner);
109 }
110 catch (Exception ex)
111 {
112 ex.printStackTrace();
113 }
114 }
Richard S. Hall46754b52007-07-16 18:36:11 +0000115 }
Richard S. Hall259e83c2009-06-10 19:54:15 +0000116}