Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +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 |
| 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 | */ |
| 19 | package org.apache.felix.example.extenderbased.host; |
| 20 | |
| 21 | import java.awt.event.WindowAdapter; |
| 22 | import java.awt.event.WindowEvent; |
Richard S. Hall | 0db8887 | 2012-03-07 16:24:47 +0000 | [diff] [blame] | 23 | import javax.swing.JFrame; |
| 24 | import javax.swing.SwingUtilities; |
Richard S. Hall | 0db8887 | 2012-03-07 16:24:47 +0000 | [diff] [blame] | 25 | import org.osgi.framework.BundleActivator; |
| 26 | import org.osgi.framework.BundleContext; |
| 27 | import org.osgi.framework.BundleException; |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 28 | |
| 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. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 36 | **/ |
Richard S. Hall | 259e83c | 2009-06-10 19:54:15 +0000 | [diff] [blame] | 37 | public class Activator implements BundleActivator |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 38 | { |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 39 | private DrawingFrame m_frame = null; |
Richard S. Hall | f48d7dc | 2012-03-26 15:23:41 +0000 | [diff] [blame^] | 40 | private ShapeBundleTracker m_shapetracker = null; |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 41 | |
| 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. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 48 | @Override |
Richard S. Hall | 259e83c | 2009-06-10 19:54:15 +0000 | [diff] [blame] | 49 | public void start(final BundleContext context) |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 50 | { |
Richard S. Hall | 0db8887 | 2012-03-07 16:24:47 +0000 | [diff] [blame] | 51 | SwingUtilities.invokeLater(new Runnable() { |
Richard S. Hall | 259e83c | 2009-06-10 19:54:15 +0000 | [diff] [blame] | 52 | // This creates of the application window. |
Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 53 | @Override |
Richard S. Hall | 259e83c | 2009-06-10 19:54:15 +0000 | [diff] [blame] | 54 | public void run() |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 55 | { |
Richard S. Hall | 259e83c | 2009-06-10 19:54:15 +0000 | [diff] [blame] | 56 | m_frame = new DrawingFrame(); |
| 57 | m_frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); |
Richard S. Hall | 0db8887 | 2012-03-07 16:24:47 +0000 | [diff] [blame] | 58 | m_frame.addWindowListener(new WindowAdapter() |
| 59 | { |
| 60 | @Override |
Richard S. Hall | 259e83c | 2009-06-10 19:54:15 +0000 | [diff] [blame] | 61 | 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. Hall | f48d7dc | 2012-03-26 15:23:41 +0000 | [diff] [blame^] | 76 | m_shapetracker = new ShapeBundleTracker(context, m_frame); |
Richard S. Hall | 259e83c | 2009-06-10 19:54:15 +0000 | [diff] [blame] | 77 | m_shapetracker.open(); |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 78 | } |
Richard S. Hall | 259e83c | 2009-06-10 19:54:15 +0000 | [diff] [blame] | 79 | }); |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Stops extension tracking and disposes of the application window. |
| 84 | * @param context The context of the bundle. |
| 85 | **/ |
Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 86 | @Override |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 87 | public void stop(BundleContext context) |
| 88 | { |
Richard S. Hall | 259e83c | 2009-06-10 19:54:15 +0000 | [diff] [blame] | 89 | Runnable runner = new Runnable() { |
| 90 | // This disposes of the application window. |
Richard S. Hall | c9e91a2 | 2012-03-08 17:09:16 +0000 | [diff] [blame] | 91 | @Override |
Richard S. Hall | 259e83c | 2009-06-10 19:54:15 +0000 | [diff] [blame] | 92 | public void run() |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 93 | { |
Richard S. Hall | 259e83c | 2009-06-10 19:54:15 +0000 | [diff] [blame] | 94 | m_shapetracker.close(); |
| 95 | m_frame.setVisible(false); |
| 96 | m_frame.dispose(); |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 97 | } |
Richard S. Hall | 259e83c | 2009-06-10 19:54:15 +0000 | [diff] [blame] | 98 | }; |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 99 | |
Richard S. Hall | 259e83c | 2009-06-10 19:54:15 +0000 | [diff] [blame] | 100 | 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. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 115 | } |
Richard S. Hall | 259e83c | 2009-06-10 19:54:15 +0000 | [diff] [blame] | 116 | } |