blob: ac698963df1a3c2c77de7ca6a434796f3c060938 [file] [log] [blame]
Stephane Frenot641ddbc2006-09-19 14:42:16 +00001/*
Stephane Frenotff3b2572007-04-24 15:28:01 +00002 *
Stephane Frenot641ddbc2006-09-19 14:42:16 +00003 * 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 Frenot4cdf3c62006-07-21 11:01:24 +000020package org.apache.felix.mosgi.console.gui;
21
22import java.awt.Dimension;
23import java.awt.Toolkit;
24import java.beans.PropertyChangeEvent;
25import java.beans.PropertyChangeListener;
26import java.util.ArrayList;
27import javax.swing.JFrame;
28import javax.swing.JRootPane;
29import javax.swing.JSplitPane;
30import javax.swing.event.EventListenerList;
31
32import org.apache.felix.mosgi.console.ifc.Plugin;
33import org.apache.felix.mosgi.console.ifc.CommonPlugin;
34
35import org.osgi.framework.BundleActivator;
36import org.osgi.framework.BundleContext;
37import org.osgi.framework.ServiceListener;
38import org.osgi.framework.ServiceEvent;
39import org.osgi.framework.ServiceReference;
40import org.osgi.framework.InvalidSyntaxException;
41
Stephane Frenot45c7f6a2006-09-21 10:55:28 +000042import java.awt.event.WindowAdapter;
43import java.awt.event.WindowEvent;
44
Stephane Frenot4cdf3c62006-07-21 11:01:24 +000045public class Activator implements BundleActivator {
46 protected BundleContext m_context = null;
Stephane Frenote2b74402006-10-12 11:54:37 +000047
Stephane Frenot4cdf3c62006-07-21 11:01:24 +000048 protected ArrayList m_pluginList = null;
Stephane Frenot7ac10e32006-11-24 10:21:58 +000049 protected ArrayList m_commonpluginList = null; //TODO Do I need this table ?
Stephane Frenot4cdf3c62006-07-21 11:01:24 +000050 private EventListenerList m_listenerList = null;
Stephane Frenote2b74402006-10-12 11:54:37 +000051
Stephane Frenot4cdf3c62006-07-21 11:01:24 +000052 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 Frenot7ac10e32006-11-24 10:21:58 +000061 ///////////////////////////////////////
62 // BundleActivator //
63 ///////////////////////////////////////
Stephane Frenot4cdf3c62006-07-21 11:01:24 +000064 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 Frenot45c7f6a2006-09-21 10:55:28 +000073 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 Frenot4cdf3c62006-07-21 11:01:24 +000098 }
Stephane Frenot45c7f6a2006-09-21 10:55:28 +000099 }
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 Frenot4cdf3c62006-07-21 11:01:24 +0000111 }
112
Stephane Frenot45c7f6a2006-09-21 10:55:28 +0000113 // 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 Frenot7ac10e32006-11-24 10:21:58 +0000121 // TODO : add a windowListener and use a Preferences service to save screen size
Stephane Frenot45c7f6a2006-09-21 10:55:28 +0000122 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 Frenot45c7f6a2006-09-21 10:55:28 +0000133 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 Frenot7ac10e32006-11-24 10:21:58 +0000154 // initializePlugins();
155
Stephane Frenot45c7f6a2006-09-21 10:55:28 +0000156 m_frame.setVisible(true);
Stephane Frenot7ac10e32006-11-24 10:21:58 +0000157 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 Frenot45c7f6a2006-09-21 10:55:28 +0000167 }
168
Stephane Frenot7ac10e32006-11-24 10:21:58 +0000169 ////////////////////////////////////
170 //
171 ////////////////////////////////////
172 /*private synchronized void initializePlugins() { // Never used ?
Stephane Frenote2b74402006-10-12 11:54:37 +0000173 System.out.println("??? private synchronized void initializePlugins() ???");
Stephane Frenot45c7f6a2006-09-21 10:55:28 +0000174 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 Frenot4cdf3c62006-07-21 11:01:24 +0000185 }
186 }
Stephane Frenot45c7f6a2006-09-21 10:55:28 +0000187 }
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 Frenot4cdf3c62006-07-21 11:01:24 +0000196 }
197 }
Stephane Frenot45c7f6a2006-09-21 10:55:28 +0000198 }
199 } catch (Exception ex) {
200 System.err.println("ShellGuiActivator: Error initializing model list.");
201 System.err.println("ShellGuiActivator: " + ex);
202 ex.printStackTrace();
203 }
Stephane Frenot7ac10e32006-11-24 10:21:58 +0000204 }*/
Stephane Frenot45c7f6a2006-09-21 10:55:28 +0000205
Stephane Frenote2b74402006-10-12 11:54:37 +0000206 //////////////////////////////
207 // Event methods. //
208 //////////////////////////////
Stephane Frenot45c7f6a2006-09-21 10:55:28 +0000209 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 Frenot7ac10e32006-11-24 10:21:58 +0000218 //System.out.println("[Gui Activator] fire PCE("+name+","+oldValue+","+newValue+")");
Stephane Frenot45c7f6a2006-09-21 10:55:28 +0000219 PropertyChangeEvent event = null;
Stephane Frenot45c7f6a2006-09-21 10:55:28 +0000220 // Guaranteed to return a non-null array
221 Object[] listeners = m_listenerList.getListenerList();
222
223 // Process the listeners last to first, notifying
Stephane Frenote2b74402006-10-12 11:54:37 +0000224 // those that are interested in this event
Stephane Frenot45c7f6a2006-09-21 10:55:28 +0000225 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 Frenot4cdf3c62006-07-21 11:01:24 +0000232 }
233 }
Stephane Frenot45c7f6a2006-09-21 10:55:28 +0000234 }
Stephane Frenot4cdf3c62006-07-21 11:01:24 +0000235
Stephane Frenot4cdf3c62006-07-21 11:01:24 +0000236}