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.util.Dictionary; |
| 22 | import javax.swing.*; |
| 23 | import org.apache.felix.example.extenderbased.host.extension.SimpleShape; |
| 24 | import org.osgi.framework.Bundle; |
| 25 | import org.osgi.framework.BundleContext; |
| 26 | |
| 27 | /** |
| 28 | * Extends the <tt>BundleTracker</tt> to create a tracker for |
| 29 | * <tt>SimpleShape</tt> extensions. The tracker is responsible for |
Richard S. Hall | 8828735 | 2007-07-16 19:22:36 +0000 | [diff] [blame] | 30 | * listening for <tt>SimpleShape</tt> extensions and informing the |
| 31 | * application about the availability of shapes. This tracker forces |
| 32 | * all notifications to be processed on the Swing event thread to |
| 33 | * avoid synchronization and redraw issues. |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 34 | **/ |
| 35 | public class ShapeTracker extends BundleTracker |
| 36 | { |
| 37 | // Flag indicating an added shape. |
| 38 | private static final int ADDED = 1; |
| 39 | // Flag indicating a removed shape. |
| 40 | private static final int REMOVED = 2; |
Richard S. Hall | d115071 | 2007-07-25 17:51:29 +0000 | [diff] [blame] | 41 | // The bundle context used for tracking. |
| 42 | private BundleContext m_context; |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 43 | // The application object to notify. |
| 44 | private DrawingFrame m_frame; |
| 45 | |
| 46 | /** |
| 47 | * Constructs a tracker that uses the specified bundle context to |
| 48 | * track extensions and notifies the specified application object about |
| 49 | * changes. |
| 50 | * @param context The bundle context to be used by the tracker. |
| 51 | * @param frame The application object to notify about extension changes. |
| 52 | **/ |
| 53 | public ShapeTracker(BundleContext context, DrawingFrame frame) |
| 54 | { |
Richard S. Hall | 8828735 | 2007-07-16 19:22:36 +0000 | [diff] [blame] | 55 | super(context); |
Richard S. Hall | d115071 | 2007-07-25 17:51:29 +0000 | [diff] [blame] | 56 | m_context = context; |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 57 | m_frame = frame; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Overrides the <tt>BundleTracker</tt> functionality to inform |
| 62 | * the application object about the added extensions. |
| 63 | * @param bundle The activated bundle. |
| 64 | **/ |
Richard S. Hall | 8828735 | 2007-07-16 19:22:36 +0000 | [diff] [blame] | 65 | protected void addedBundle(Bundle bundle) |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 66 | { |
| 67 | processBundleOnEventThread(ADDED, bundle); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Overrides the <tt>BundleTracker</tt> functionality to inform |
| 72 | * the application object about removed extensions. |
| 73 | * @param bundle The inactivated bundle. |
| 74 | **/ |
Richard S. Hall | 8828735 | 2007-07-16 19:22:36 +0000 | [diff] [blame] | 75 | protected void removedBundle(Bundle bundle) |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 76 | { |
| 77 | processBundleOnEventThread(REMOVED, bundle); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Processes a received bundle notification from the <tt>BundleTracker</tt>, |
| 82 | * forcing the processing of the notification onto the Swing event thread |
| 83 | * if it is not already on it. |
| 84 | * @param action The type of action associated with the notification. |
| 85 | * @param bundle The bundle of the corresponding extension. |
| 86 | **/ |
| 87 | private void processBundleOnEventThread(int action, Bundle bundle) |
| 88 | { |
| 89 | try |
| 90 | { |
| 91 | if (SwingUtilities.isEventDispatchThread()) |
| 92 | { |
| 93 | processBundle(action, bundle); |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | SwingUtilities.invokeAndWait(new BundleRunnable(action, bundle)); |
| 98 | } |
| 99 | } |
| 100 | catch (Exception ex) |
| 101 | { |
| 102 | ex.printStackTrace(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Actually performs the processing of the bundle notification. Invokes |
| 108 | * the appropriate callback method on the application object depending on |
| 109 | * the action type of the notification. |
| 110 | * @param action The type of action associated with the notification. |
| 111 | * @param bundle The bundle of the corresponding extension. |
| 112 | **/ |
| 113 | private void processBundle(int action, Bundle bundle) |
| 114 | { |
| 115 | Dictionary dict = bundle.getHeaders(); |
| 116 | |
| 117 | // Try to get the name of the extension. |
| 118 | String name = (String) dict.get(SimpleShape.NAME_PROPERTY); |
| 119 | // Return immediately if the bundle is not an extension. |
| 120 | if (name == null) |
| 121 | { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | switch (action) |
| 126 | { |
| 127 | case ADDED: |
| 128 | // Get the icon resource of the extension. |
| 129 | String iconPath = (String) dict.get(SimpleShape.ICON_PROPERTY); |
| 130 | Icon icon = new ImageIcon(bundle.getResource(iconPath)); |
| 131 | // Get the class of the extension. |
Richard S. Hall | d115071 | 2007-07-25 17:51:29 +0000 | [diff] [blame] | 132 | String className = (String) dict.get(SimpleShape.CLASS_PROPERTY); |
| 133 | m_frame.addShape( |
| 134 | name, |
| 135 | icon, |
| 136 | new DefaultShape(m_context, bundle.getBundleId(), className)); |
Richard S. Hall | 46754b5 | 2007-07-16 18:36:11 +0000 | [diff] [blame] | 137 | break; |
| 138 | |
| 139 | case REMOVED: |
| 140 | m_frame.removeShape(name); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Simple class used to process bundle notification handling on the |
| 147 | * Swing event thread. |
| 148 | **/ |
| 149 | private class BundleRunnable implements Runnable |
| 150 | { |
| 151 | private int m_action; |
| 152 | private Bundle m_bundle; |
| 153 | |
| 154 | /** |
| 155 | * Constructs an object with the specified action and bundle |
| 156 | * object for processing on the Swing event thread. |
| 157 | * @param action The type of action associated with the notification. |
| 158 | * @param bundle The bundle of the corresponding extension. |
| 159 | **/ |
| 160 | public BundleRunnable(int action, Bundle bundle) |
| 161 | { |
| 162 | m_action = action; |
| 163 | m_bundle = bundle; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Calls the <tt>processBundle()</tt> method. |
| 168 | **/ |
| 169 | public void run() |
| 170 | { |
| 171 | processBundle(m_action, m_bundle); |
| 172 | } |
| 173 | } |
| 174 | } |