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