You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cl...@apache.org on 2008/10/07 14:42:05 UTC

svn commit: r702457 - in /felix/sandbox/clement/ipojo-tutorials/shell-gui: ./ shell.gui/ shell.gui/src/ shell.gui/src/main/ shell.gui/src/main/java/ shell.gui/src/main/java/org/ shell.gui/src/main/java/org/apache/ shell.gui/src/main/java/org/apache/fel...

Author: clement
Date: Tue Oct  7 05:42:04 2008
New Revision: 702457

URL: http://svn.apache.org/viewvc?rev=702457&view=rev
Log:
Initial commit of an iPOJO version of the Felix Shell Gui (host). This version uses the same service interface than the Felix shell gui plugin and so can use those plugins.

Added:
    felix/sandbox/clement/ipojo-tutorials/shell-gui/
    felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/   (with props)
    felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/metadata.xml
    felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/pom.xml
    felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/
    felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/
    felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/
    felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/
    felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/
    felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/
    felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/
    felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/
    felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/Plugin.java
    felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/impl/
    felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/impl/Host.java
    felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/impl/ShellPanel.java

Propchange: felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Tue Oct  7 05:42:04 2008
@@ -0,0 +1,6 @@
+.classpath
+.externalToolBuilders
+.project
+.settings
+target
+maven-eclipse.xml

Added: felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/metadata.xml
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/metadata.xml?rev=702457&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/metadata.xml (added)
+++ felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/metadata.xml Tue Oct  7 05:42:04 2008
@@ -0,0 +1,12 @@
+<ipojo>
+<component classname="org.apache.felix.shell.gui.impl.Host"
+    public="false">
+	<requires aggregate="true">
+		<callback type="bind" method="bindPlugin"/>
+		<callback type="unbind" method="unbindPlugin"/>
+	</requires>
+	<callback transition="validate" method="start"/>
+	<callback transition="invalidate" method="stop"/>
+</component>
+<instance component="org.apache.felix.shell.gui.impl.Host"/>
+</ipojo>
\ No newline at end of file

Added: felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/pom.xml
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/pom.xml?rev=702457&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/pom.xml (added)
+++ felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/pom.xml Tue Oct  7 05:42:04 2008
@@ -0,0 +1,69 @@
+
+	<!--
+		Licensed to the Apache Software Foundation (ASF) under one or more
+		contributor license agreements. See the NOTICE file distributed with
+		this work for additional information regarding copyright ownership.
+		The ASF licenses this file to you under the Apache License, Version
+		2.0 (the "License"); you may not use this file except in compliance
+		with the License. You may obtain a copy of the License at
+		http://www.apache.org/licenses/LICENSE-2.0 Unless required by
+		applicable law or agreed to in writing, software distributed under the
+		License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+		CONDITIONS OF ANY KIND, either express or implied. See the License for
+		the specific language governing permissions and limitations under the
+		License.
+	-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+	<groupId>org.apache.felix</groupId>
+	<modelVersion>4.0.0</modelVersion>
+	<packaging>bundle</packaging>
+	<name>Apache Felix iPOJO Shell GUI</name>
+	<artifactId>org.apache.felix.ipojo.shell.gui</artifactId>
+	<version>0.9.0-SNAPSHOT</version>
+	<dependencies>
+		<dependency>
+			<groupId>${pom.groupId}</groupId>
+			<artifactId>org.osgi.core</artifactId>
+			<version>1.1.0-SNAPSHOT</version>
+			<scope>provided</scope>
+		</dependency>
+	</dependencies>
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.felix</groupId>
+				<artifactId>maven-bundle-plugin</artifactId>
+				<version>1.4.0</version>
+				<extensions>true</extensions>
+				<configuration>
+					<instructions>
+						<Bundle-Name>ShellGUI</Bundle-Name>
+						<Bundle-Description>A simple plugin-oriented GUI shell powered by
+							iPOJO.</Bundle-Description>
+						<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
+						<Bundle-Vendor>The Apache Software Foundation</Bundle-Vendor>
+						<Export-Package>org.apache.felix.shell.gui;
+							specification-version="1.0.0"</Export-Package>
+						<Private-Package>org.apache.felix.shell.gui.impl</Private-Package>
+					</instructions>
+				</configuration>
+			</plugin>
+			<plugin>
+				<groupId>org.apache.felix</groupId>
+				<artifactId>maven-ipojo-plugin</artifactId>
+				<version>0.9.0-SNAPSHOT</version>
+				<executions>
+					<execution>
+						<goals>
+							<goal>ipojo-bundle</goal>
+						</goals>
+						<configuration>
+							<ignoreAnnotations>true</ignoreAnnotations>
+						</configuration>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
+	</build>
+</project>
\ No newline at end of file

Added: felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/Plugin.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/Plugin.java?rev=702457&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/Plugin.java (added)
+++ felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/Plugin.java Tue Oct  7 05:42:04 2008
@@ -0,0 +1,40 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.shell.gui;
+
+import java.awt.Component;
+
+/**
+ * A simple plugin interface for the GUI shell bundle.
+**/
+public interface Plugin
+{
+    /**
+     * Returns the name of the plugin.
+     * @return the name of the plugin.
+    **/
+    public String getName();
+    
+    /**
+     * Returns the GUI associated with the plugin; this method should
+     * always return the same GUI instance when it is invoked.
+     * @return the GUI associated with the plugin.
+    **/
+    public Component getGUI();
+}
\ No newline at end of file

Added: felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/impl/Host.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/impl/Host.java?rev=702457&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/impl/Host.java (added)
+++ felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/impl/Host.java Tue Oct  7 05:42:04 2008
@@ -0,0 +1,222 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.shell.gui.impl;
+
+import java.awt.BorderLayout;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.swing.JFrame;
+import javax.swing.event.EventListenerList;
+
+import org.apache.felix.shell.gui.Plugin;
+
+/**
+ * iPOJO Shell Gui Host.
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class Host {
+    
+    /**
+     * The list of plug-ins.
+     * Accessed must be synchronized.
+     */
+    private List m_plugins = new ArrayList();
+    
+    /**
+     * The event listener list.
+     */
+    private EventListenerList m_listenerList = new EventListenerList();
+    
+    /**
+     * The host frame
+     */
+    private JFrame m_frame = null;
+    
+    public static final String PLUGIN_LIST_PROPERTY = "plugin.list";
+    
+    /**
+     * Stop method called when the instance becomes invalid.
+     * This method is called by the iPOJO container.
+     */
+    public void stop() {
+        if (m_frame != null) {
+            m_frame.setVisible(false);
+            m_frame.dispose();
+            m_frame = null;
+        }
+        synchronized(this) {
+            m_plugins.clear();
+        }
+    }
+    
+    /**
+     * Start method called when the instance becomes valid.
+     * This method is called by the iPOJO container.
+     */
+    public void start() {
+        if (m_frame == null) {
+            ShellPanel panel = new ShellPanel(this);
+            m_frame = new JFrame("Felix GUI Shell powered by iPOJO");
+            m_frame.getContentPane().setLayout(new BorderLayout());
+            m_frame.getContentPane().add(panel);
+            m_frame.pack();
+            m_frame.setSize(700, 400);
+            m_frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
+            m_frame.addWindowListener(new WindowAdapter() {
+                public void windowClosing(WindowEvent event)
+                {
+                    System.out.println("Shell Gui closed");
+                }
+            });
+        }
+        m_frame.setVisible(true);
+    }
+    
+    
+    /**
+     * Gets the number of plugged plug-ins.
+     * @return the plug-in count
+     */
+    public synchronized int getPluginCount()
+    {
+        if (m_plugins == null)
+        {
+            return 0;
+        }
+        return m_plugins.size();
+    }
+
+    /**
+     * Gets the plug-in stored at the index <code>i</code>
+     * in the {@link Host#m_plugins} list.
+     * @param i the index
+     * @return the plug-in located at the index
+     * <code>i</code> in the  {@link Host#m_plugins} list.
+     */
+    public synchronized Plugin getPlugin(int i)
+    {
+        if ((i < 0) || (i >= getPluginCount()))
+        {
+            return null;
+        }
+        return (Plugin) m_plugins.get(i);
+    }
+
+    /**
+     * Checks if the specified plug-in is already plugged.
+     * @param plugin the plug-in
+     * @return <code>true</code> if the plug-in object is
+     * already content in the {@link Host#m_plugins} list,
+     * <code>false</code> otherwise.
+     */
+    public synchronized boolean pluginExists(Plugin plugin)
+    {
+        for (int i = 0; i < m_plugins.size(); i++)
+        {
+            if (m_plugins.get(i) == plugin)
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+    
+    /**
+     * A new plug-in appears.
+     * This method is called by the iPOJO container when a new 
+     * plug-in service arrives. The new plug-in is added to the
+     * {@link Host#m_plugins} list, and the {@link Host#m_frame}
+     * is notified.
+     * This method must be synchronized as it accesses to the
+     * {@link Host#m_plugins} list.
+     * @param plugin the new plug-in
+     */
+    public synchronized void bindPlugin(Plugin plugin) {
+        if (!m_plugins.contains(plugin))
+        {
+            m_plugins.add(plugin);
+            firePropertyChangedEvent(
+                PLUGIN_LIST_PROPERTY, null, null);
+        }
+    }
+    
+   /**
+    * A used plug-in disappears.
+    * This method is called by the iPOJO container when a used 
+    * plug-in service leaves. The plug-in is removed from the
+    * {@link Host#m_plugins} list, and the {@link Host#m_frame}
+    * is notified.
+    * This method must be synchronized as it accesses to the
+    * {@link Host#m_plugins} list.
+    */
+    public synchronized void unbindPlugin(Plugin plugin) {
+        m_plugins.remove(plugin);
+        firePropertyChangedEvent(
+            PLUGIN_LIST_PROPERTY, null, null);
+    }
+    
+    /**
+     * Adds a property change listener.
+     * This method is used by the frame to be notified of
+     * plug-in arrivals and departures.
+     * @param l the listener to add
+     */
+    public void addPropertyChangeListener(PropertyChangeListener l)
+    {
+        m_listenerList.add(PropertyChangeListener.class, l);
+    }
+
+    /**
+     * Dispatches a plug-in arrival or departures to listeners from
+     * {@link Host#m_listenerList}.
+     * @param name impacted property
+     * @param oldValue <code>null</code>
+     * @param newValue <code>null</code>
+     */
+    protected void firePropertyChangedEvent(String name, Object oldValue, Object newValue)
+    {
+        PropertyChangeEvent event = null;
+
+        // Guaranteed to return a non-null array
+        Object[] listeners = m_listenerList.getListenerList();
+
+        // Process the listeners last to first, notifying
+        // those that are interested in this event
+        for (int i = listeners.length - 2; i >= 0; i -= 2)
+        {
+            if (listeners[i] == PropertyChangeListener.class)
+            {
+                // Lazily create the event:
+                if (event == null)
+                {
+                    event = new PropertyChangeEvent(this, name, oldValue, newValue);
+                }
+                ((PropertyChangeListener) listeners[i + 1]).propertyChange(event);
+            }
+        }
+    }
+    
+    
+
+}

Added: felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/impl/ShellPanel.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/impl/ShellPanel.java?rev=702457&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/impl/ShellPanel.java (added)
+++ felix/sandbox/clement/ipojo-tutorials/shell-gui/shell.gui/src/main/java/org/apache/felix/shell/gui/impl/ShellPanel.java Tue Oct  7 05:42:04 2008
@@ -0,0 +1,170 @@
+/* 
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.shell.gui.impl;
+
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+
+import javax.swing.*;
+import javax.swing.event.*;
+
+import org.apache.felix.shell.gui.Plugin;
+
+public class ShellPanel extends JPanel implements PropertyChangeListener
+{
+    private Host m_host = null;
+    private JPanel m_emptyPanel = null;
+    private JList m_pluginList = null;
+    private Plugin m_selectedPlugin = null;
+    private Runnable m_runnable = null;
+
+    public ShellPanel(Host host)
+    {
+        m_host = host;
+        m_host.addPropertyChangeListener(this);
+
+        setLayout(new BorderLayout());
+        JScrollPane scroll = null;
+        add(scroll = new JScrollPane(m_pluginList = new JList(new SimpleListModel())), BorderLayout.WEST);
+        scroll.setPreferredSize(new Dimension(150, scroll.getPreferredSize().height));
+        add(m_emptyPanel = new JPanel(), BorderLayout.CENTER);
+
+        createEventListeners();
+    }
+
+    public void propertyChange(PropertyChangeEvent event)
+    {
+        if (event.getPropertyName().equals(Host.PLUGIN_LIST_PROPERTY))
+        {
+            if (m_runnable == null)
+            {
+                m_runnable = new PropertyChangeRunnable();
+            }
+            SwingUtilities.invokeLater(m_runnable);
+        }
+    }
+
+    private void createEventListeners()
+    {
+        m_pluginList.addListSelectionListener(new ListSelectionListener() {
+            public void valueChanged(ListSelectionEvent event)
+            {
+                if (!event.getValueIsAdjusting())
+                {
+                    if (m_pluginList.getSelectedIndex() >= 0)
+                    {
+                        // Remove the current GUI.
+                        if (m_selectedPlugin != null)
+                        {
+                            remove(m_selectedPlugin.getGUI());
+                        }
+                        else
+                        {
+                            remove(m_emptyPanel);
+                        }
+
+                        // Get the selected plugin GUI.
+                        m_selectedPlugin =
+                            m_host.getPlugin(m_pluginList.getSelectedIndex());
+                        if (m_selectedPlugin != null)
+                        {
+                            // Display the selected plugin GUI.
+                            add(m_selectedPlugin.getGUI(), BorderLayout.CENTER);
+                        }
+                        else
+                        {
+                            // Display the empty panel.
+                            add(m_emptyPanel, BorderLayout.CENTER);
+                        }
+
+                        revalidate();
+                        repaint();
+                    }
+                }
+            }
+        });
+    }
+
+    private class SimpleListModel extends AbstractListModel
+        implements ListDataListener
+    {
+        private SimpleListModel()
+        {
+        }
+
+        public int getSize()
+        {
+            return m_host.getPluginCount();
+        }
+
+        public Object getElementAt(int index)
+        {
+            return m_host.getPlugin(index).getName();
+        }
+
+        public void intervalAdded(ListDataEvent event)
+        {
+            fireIntervalAdded(this, event.getIndex0(), event.getIndex1());
+        }
+
+        public void intervalRemoved(ListDataEvent event)
+        {
+            fireIntervalRemoved(this, event.getIndex0(), event.getIndex1());
+        }
+
+        public void contentsChanged(ListDataEvent event)
+        {
+            fireContentsChanged(this, event.getIndex0(), event.getIndex1());
+        }
+
+        public void update()
+        {
+            fireContentsChanged(this, 0, -1);
+        }
+    }
+
+    private class PropertyChangeRunnable implements Runnable
+    {
+        public void run()
+        {
+            ((SimpleListModel) m_pluginList.getModel()).update();
+
+            // Check to see if the selected component has been
+            // removed, if so, then reset the selected component
+            // to be an empty panel.
+            if ((m_selectedPlugin != null) &&
+                !m_host.pluginExists(m_selectedPlugin))
+            {
+                m_pluginList.clearSelection();
+                remove(m_selectedPlugin.getGUI());
+                m_selectedPlugin = null;
+                add(m_emptyPanel, BorderLayout.CENTER);
+                revalidate();
+                repaint();
+            }
+
+            if ((m_selectedPlugin == null) && (m_host.getPluginCount() > 0))
+            {
+                m_pluginList.setSelectedIndex(0);
+            }
+        }
+    }
+}
\ No newline at end of file