Stephane Frenot | 641ddbc | 2006-09-19 14:42:16 +0000 | [diff] [blame] | 1 | /* |
Stephane Frenot | ff3b257 | 2007-04-24 15:28:01 +0000 | [diff] [blame^] | 2 | * |
Stephane Frenot | 641ddbc | 2006-09-19 14:42:16 +0000 | [diff] [blame] | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
| 4 | * or more contributor license agreements. See the NOTICE file |
| 5 | * distributed with this work for additional information |
| 6 | * regarding copyright ownership. The ASF licenses this file |
| 7 | * to you under the Apache License, Version 2.0 (the |
| 8 | * "License"); you may not use this file except in compliance |
| 9 | * with the License. You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, |
| 14 | * software distributed under the License is distributed on an |
| 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | * KIND, either express or implied. See the License for the |
| 17 | * specific language governing permissions and limitations |
| 18 | * under the License. |
| 19 | */ |
Stephane Frenot | 4cdf3c6 | 2006-07-21 11:01:24 +0000 | [diff] [blame] | 20 | package org.apache.felix.mosgi.console.gui; |
| 21 | |
| 22 | import java.awt.Dimension; |
| 23 | import java.awt.Toolkit; |
| 24 | import java.beans.PropertyChangeEvent; |
| 25 | import java.beans.PropertyChangeListener; |
| 26 | import java.util.ArrayList; |
| 27 | import javax.swing.JFrame; |
| 28 | import javax.swing.JRootPane; |
| 29 | import javax.swing.JSplitPane; |
| 30 | import javax.swing.event.EventListenerList; |
| 31 | |
| 32 | import org.apache.felix.mosgi.console.ifc.Plugin; |
| 33 | import org.apache.felix.mosgi.console.ifc.CommonPlugin; |
| 34 | |
| 35 | import org.osgi.framework.BundleActivator; |
| 36 | import org.osgi.framework.BundleContext; |
| 37 | import org.osgi.framework.ServiceListener; |
| 38 | import org.osgi.framework.ServiceEvent; |
| 39 | import org.osgi.framework.ServiceReference; |
| 40 | import org.osgi.framework.InvalidSyntaxException; |
| 41 | |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 42 | import java.awt.event.WindowAdapter; |
| 43 | import java.awt.event.WindowEvent; |
| 44 | |
Stephane Frenot | 4cdf3c6 | 2006-07-21 11:01:24 +0000 | [diff] [blame] | 45 | public class Activator implements BundleActivator { |
| 46 | protected BundleContext m_context = null; |
Stephane Frenot | e2b7440 | 2006-10-12 11:54:37 +0000 | [diff] [blame] | 47 | |
Stephane Frenot | 4cdf3c6 | 2006-07-21 11:01:24 +0000 | [diff] [blame] | 48 | protected ArrayList m_pluginList = null; |
Stephane Frenot | 7ac10e3 | 2006-11-24 10:21:58 +0000 | [diff] [blame] | 49 | protected ArrayList m_commonpluginList = null; //TODO Do I need this table ? |
Stephane Frenot | 4cdf3c6 | 2006-07-21 11:01:24 +0000 | [diff] [blame] | 50 | private EventListenerList m_listenerList = null; |
Stephane Frenot | e2b7440 | 2006-10-12 11:54:37 +0000 | [diff] [blame] | 51 | |
Stephane Frenot | 4cdf3c6 | 2006-07-21 11:01:24 +0000 | [diff] [blame] | 52 | private JFrame m_frame = null; |
| 53 | private NodesTree nodesTree=null; |
| 54 | |
| 55 | public Activator() { |
| 56 | m_pluginList = new ArrayList(); |
| 57 | m_commonpluginList = new ArrayList(); |
| 58 | m_listenerList = new EventListenerList(); |
| 59 | } |
| 60 | |
Stephane Frenot | 7ac10e3 | 2006-11-24 10:21:58 +0000 | [diff] [blame] | 61 | /////////////////////////////////////// |
| 62 | // BundleActivator // |
| 63 | /////////////////////////////////////// |
Stephane Frenot | 4cdf3c6 | 2006-07-21 11:01:24 +0000 | [diff] [blame] | 64 | public void start(BundleContext context) { |
| 65 | m_context = context; |
| 66 | |
| 67 | // Listen for factory service events. |
| 68 | ServiceListener sl = new ServiceListener() { |
| 69 | public void serviceChanged(ServiceEvent event) { |
| 70 | ServiceReference ref = event.getServiceReference(); |
| 71 | Object svcObj = m_context.getService(ref); |
| 72 | if (event.getType() == ServiceEvent.REGISTERED) { |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 73 | synchronized (Activator.this) { |
| 74 | // !!!!!!!!!! ORDER MATTERS (Inheritance pb) |
| 75 | if (!m_pluginList.contains(svcObj)) { |
| 76 | if(svcObj instanceof CommonPlugin){ |
| 77 | m_commonpluginList.add(svcObj); |
| 78 | firePropertyChangedEvent(CommonPlugin.COMMON_PLUGIN_ADDED, null, svcObj); |
| 79 | }else if (svcObj instanceof Plugin){ |
| 80 | m_pluginList.add(svcObj); |
| 81 | firePropertyChangedEvent(Plugin.PLUGIN_ADDED, null, svcObj); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } else if (event.getType() == ServiceEvent.UNREGISTERING) { |
| 86 | synchronized (Activator.this) { |
| 87 | removePropertyChangeListener((PropertyChangeListener)svcObj); |
| 88 | if(svcObj instanceof CommonPlugin){ |
| 89 | m_commonpluginList.remove(svcObj); |
| 90 | firePropertyChangedEvent(CommonPlugin.COMMON_PLUGIN_REMOVED, null, svcObj); |
| 91 | }else if (svcObj instanceof Plugin){ |
| 92 | m_pluginList.remove(svcObj); |
| 93 | firePropertyChangedEvent(Plugin.PLUGIN_REMOVED, null, svcObj); |
| 94 | } |
| 95 | } |
| 96 | } else { |
| 97 | m_context.ungetService(ref); |
Stephane Frenot | 4cdf3c6 | 2006-07-21 11:01:24 +0000 | [diff] [blame] | 98 | } |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 99 | } |
| 100 | }; |
| 101 | try { |
| 102 | m_context.addServiceListener(sl, |
| 103 | "(|(objectClass=" |
| 104 | + Plugin.class.getName() |
| 105 | + ")(objectClass=" |
| 106 | + CommonPlugin.class.getName()+"))"); |
| 107 | } |
| 108 | catch (InvalidSyntaxException ex) { |
| 109 | System.err.println("ShellGuiActivator: Cannot add service listener."); |
| 110 | System.err.println("ShellGuiActivator: " + ex); |
Stephane Frenot | 4cdf3c6 | 2006-07-21 11:01:24 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 113 | // Create and display the frame. |
| 114 | if (m_frame == null) { |
| 115 | m_frame=new JFrame("OSGi GUI Remote Manager"); |
| 116 | m_frame.setUndecorated(true); |
| 117 | m_frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME); |
| 118 | m_frame.setIconImage(Toolkit.getDefaultToolkit().getImage(m_context.getBundle().getResource("images/logo.gif"))); |
| 119 | //m_frame.setResizable(false); |
| 120 | //m_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
Stephane Frenot | 7ac10e3 | 2006-11-24 10:21:58 +0000 | [diff] [blame] | 121 | // TODO : add a windowListener and use a Preferences service to save screen size |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 122 | m_frame.addWindowListener(new WindowAdapter() { |
| 123 | public void windowClosing(WindowEvent we) { |
| 124 | JFrame jf=(JFrame) we.getWindow(); |
| 125 | System.out.println(" Console.gui : window closing ("+jf.getSize().height+"*"+jf.getSize().width+")"); |
| 126 | } |
| 127 | public void windowClosed(WindowEvent we) { |
| 128 | JFrame jf=(JFrame) we.getWindow(); |
| 129 | System.out.println(" Console.gui : window closed ("+jf.getSize().height+"*"+jf.getSize().width+")"); |
| 130 | } |
| 131 | }); |
| 132 | |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 133 | Dimension maxdim = m_frame.getToolkit().getScreenSize(); |
| 134 | int m_width=maxdim.width-100; |
| 135 | int m_height=maxdim.height-100; |
| 136 | m_frame.setBounds( (int) ((maxdim.width-m_width)/2), 20, m_width, m_height); |
| 137 | |
| 138 | //Right panel |
| 139 | JSplitPane rightSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new NodePanel(this, context) , new CommonPanel(this)); |
| 140 | rightSplitPane.setOneTouchExpandable(true); |
| 141 | rightSplitPane.setDividerLocation((int) (m_height*2/3)); |
| 142 | |
| 143 | //General Panel |
| 144 | this.nodesTree = new NodesTree(this, context); |
| 145 | JSplitPane gSplitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, this.nodesTree , rightSplitPane); |
| 146 | gSplitPane.setOneTouchExpandable(true); |
| 147 | gSplitPane.setDividerLocation((int) (m_width/4)); |
| 148 | |
| 149 | m_frame.getContentPane().add(gSplitPane); |
| 150 | } |
| 151 | |
| 152 | // Now try to manually initialize the plugin list |
| 153 | // since some might already be available. |
Stephane Frenot | 7ac10e3 | 2006-11-24 10:21:58 +0000 | [diff] [blame] | 154 | // initializePlugins(); |
| 155 | |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 156 | m_frame.setVisible(true); |
Stephane Frenot | 7ac10e3 | 2006-11-24 10:21:58 +0000 | [diff] [blame] | 157 | this.nodesTree.runDiscovery(); |
| 158 | } |
| 159 | |
| 160 | public void stop(BundleContext context) { |
| 161 | if (m_frame != null) { |
| 162 | this.nodesTree.stop(); |
| 163 | m_frame.setVisible(false); |
| 164 | m_frame.dispose(); |
| 165 | m_frame = null; |
| 166 | } |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Stephane Frenot | 7ac10e3 | 2006-11-24 10:21:58 +0000 | [diff] [blame] | 169 | //////////////////////////////////// |
| 170 | // |
| 171 | //////////////////////////////////// |
| 172 | /*private synchronized void initializePlugins() { // Never used ? |
Stephane Frenot | e2b7440 | 2006-10-12 11:54:37 +0000 | [diff] [blame] | 173 | System.out.println("??? private synchronized void initializePlugins() ???"); |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 174 | try { |
| 175 | // Get all model services. |
| 176 | Object svcObj=null; |
| 177 | ServiceReference refs[] = m_context.getServiceReferences(Plugin.class.getName(), null); |
| 178 | if (refs != null) { |
| 179 | // Add model services to list, ignore duplicates. |
| 180 | for (int i = 0; i < refs.length; i++) { |
| 181 | svcObj = m_context.getService(refs[i]); |
| 182 | if (!m_pluginList.contains(svcObj)) { |
| 183 | m_pluginList.add(svcObj); |
| 184 | firePropertyChangedEvent(Plugin.PLUGIN_ADDED, null, (Plugin)svcObj); |
Stephane Frenot | 4cdf3c6 | 2006-07-21 11:01:24 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 187 | } |
| 188 | // Get all common plugins |
| 189 | refs = m_context.getServiceReferences(CommonPlugin.class.getName(), null); |
| 190 | if (refs != null) { |
| 191 | for (int i = 0; i < refs.length; i++) { |
| 192 | svcObj = m_context.getService(refs[i]); |
| 193 | if (!m_commonpluginList.contains(svcObj)) { |
| 194 | m_commonpluginList.add(svcObj); |
| 195 | firePropertyChangedEvent(CommonPlugin.COMMON_PLUGIN_ADDED, null, (CommonPlugin)svcObj); |
Stephane Frenot | 4cdf3c6 | 2006-07-21 11:01:24 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 198 | } |
| 199 | } catch (Exception ex) { |
| 200 | System.err.println("ShellGuiActivator: Error initializing model list."); |
| 201 | System.err.println("ShellGuiActivator: " + ex); |
| 202 | ex.printStackTrace(); |
| 203 | } |
Stephane Frenot | 7ac10e3 | 2006-11-24 10:21:58 +0000 | [diff] [blame] | 204 | }*/ |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 205 | |
Stephane Frenot | e2b7440 | 2006-10-12 11:54:37 +0000 | [diff] [blame] | 206 | ////////////////////////////// |
| 207 | // Event methods. // |
| 208 | ////////////////////////////// |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 209 | public void addPropertyChangeListener(PropertyChangeListener l) { |
| 210 | m_listenerList.add(PropertyChangeListener.class, l); |
| 211 | } |
| 212 | |
| 213 | public void removePropertyChangeListener(PropertyChangeListener l) { |
| 214 | m_listenerList.remove(PropertyChangeListener.class, l); |
| 215 | } |
| 216 | |
| 217 | public void firePropertyChangedEvent(String name, Object oldValue, Object newValue) { |
Stephane Frenot | 7ac10e3 | 2006-11-24 10:21:58 +0000 | [diff] [blame] | 218 | //System.out.println("[Gui Activator] fire PCE("+name+","+oldValue+","+newValue+")"); |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 219 | PropertyChangeEvent event = null; |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 220 | // Guaranteed to return a non-null array |
| 221 | Object[] listeners = m_listenerList.getListenerList(); |
| 222 | |
| 223 | // Process the listeners last to first, notifying |
Stephane Frenot | e2b7440 | 2006-10-12 11:54:37 +0000 | [diff] [blame] | 224 | // those that are interested in this event |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 225 | for (int i = listeners.length - 2; i >= 0; i -= 2) { |
| 226 | if (listeners[i] == PropertyChangeListener.class) { |
| 227 | // Lazily create the event: |
| 228 | if (event == null) { |
| 229 | event = new PropertyChangeEvent(this, name, oldValue, newValue); |
| 230 | } |
| 231 | ((PropertyChangeListener) listeners[i + 1]).propertyChange(event); |
Stephane Frenot | 4cdf3c6 | 2006-07-21 11:01:24 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
Stephane Frenot | 45c7f6a | 2006-09-21 10:55:28 +0000 | [diff] [blame] | 234 | } |
Stephane Frenot | 4cdf3c6 | 2006-07-21 11:01:24 +0000 | [diff] [blame] | 235 | |
Stephane Frenot | 4cdf3c6 | 2006-07-21 11:01:24 +0000 | [diff] [blame] | 236 | } |