Author: abeld
Date: 2008-12-20 01:22:09 -0800 (Sat, 20 Dec 2008)
New Revision: 15474

Added:
   
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/java/org/cytoscape/presentation/internal/RendererActivator.java
Modified:
   cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/osgi.bnd
   
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/java/org/cytoscape/presentation/internal/AdjMatrixTextRenderer.java
   
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/java/org/cytoscape/presentation/internal/DiscreteVisualProperty.java
   
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/java/org/cytoscape/presentation/internal/NetworkPresentationFactoryImpl.java
   
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/resources/META-INF/spring/bundle-context.xml
Log:
refactored-viewmodel: improvements in demo presentation:

Make DiscreteVisualProperty extensible via osgi: add getValues()
method which looks up all osgi services implementing the given
interface.  List of values will only be used by UI to allow user to
pick some, so looking it up each time is okay.

(Will have to implement some callbacks so that UI can handle the case
when services are added/removed)


Modified: 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/osgi.bnd
===================================================================
--- 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/osgi.bnd   
    2008-12-19 20:19:10 UTC (rev 15473)
+++ 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/osgi.bnd   
    2008-12-20 09:22:09 UTC (rev 15474)
@@ -3,6 +3,7 @@
 #-----------------------------------------------------------------
 
 Spring-Context: META-INF/spring/*.xml
+Bundle-Activator: org.cytoscape.presentation.internal.RendererActivator
 Private-Package: 
${bundle.symbolicName}.internal,${bundle.symbolicName}.events.internal
 Export-Package: ${bundle.symbolicName},${bundle.symbolicName}.events,
 

Modified: 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/java/org/cytoscape/presentation/internal/AdjMatrixTextRenderer.java
===================================================================
--- 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/java/org/cytoscape/presentation/internal/AdjMatrixTextRenderer.java
   2008-12-19 20:19:10 UTC (rev 15473)
+++ 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/java/org/cytoscape/presentation/internal/AdjMatrixTextRenderer.java
   2008-12-20 09:22:09 UTC (rev 15474)
@@ -10,8 +10,11 @@
 
 import java.util.HashSet;
 import java.util.Set;
+import java.util.List;
 import java.util.Arrays;
 
+import org.osgi.framework.BundleContext;
+
 /**
  * A TextPresentation that shows network as an Adjacency Matrix
  */
@@ -21,17 +24,20 @@
                                       "---[ Adjacency Matrix]---", 
String.class,
                                       VisualProperty.GraphObjectType.NETWORK);
 
+    // FIXME: this should be VisualProperty<>, only temporary hack to print 
registered renderers
+    private final DiscreteVisualProperty<TextNodeRenderer> nodeRenderer;
 
-    private final VisualProperty<TextNodeRenderer> nodeRenderer =
-       new DiscreteVisualProperty<TextNodeRenderer>("TEXT_NODE_RENDERER", 
"node Renderer",
-                                                    TextNodeRenderer.class,
-                                                    
Arrays.asList((TextNodeRenderer)new TextNodeRendererImpl()),
-                                                    
VisualProperty.GraphObjectType.NETWORK);
-
     private CyNetworkView view;
     private Set<VisualProperty> visualProperties = null;
-    public AdjMatrixTextRenderer(CyNetworkView view){
+    public AdjMatrixTextRenderer(CyNetworkView view, BundleContext bc){
        this.view = view;
+       // FIXME: this should be statically intialized, but how do we get the 
BundleContext then?
+       nodeRenderer =
+       new DiscreteVisualProperty<TextNodeRenderer>("TEXT_NODE_RENDERER", 
"node Renderer",
+                                                    TextNodeRenderer.class,
+                                                    new TextNodeRendererImpl(),
+                                                    
VisualProperty.GraphObjectType.NETWORK,
+                                                    bc);
     }
     public String render(){
        StringBuilder sb = new StringBuilder();
@@ -42,6 +48,9 @@
            TextNodeRenderer renderer = 
nodeView.getVisualProperty(nodeRenderer);
            sb.append("\n"+renderer.render());
        }
+       Set<TextNodeRenderer> renderers = nodeRenderer.getValues();
+       System.out.println("available nodeRenderers: "+renderers.size());
+       System.out.println(renderers);
        return sb.toString();
     }
 

Modified: 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/java/org/cytoscape/presentation/internal/DiscreteVisualProperty.java
===================================================================
--- 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/java/org/cytoscape/presentation/internal/DiscreteVisualProperty.java
  2008-12-19 20:19:10 UTC (rev 15473)
+++ 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/java/org/cytoscape/presentation/internal/DiscreteVisualProperty.java
  2008-12-20 09:22:09 UTC (rev 15474)
@@ -1,37 +1,90 @@
 package org.cytoscape.presentation.internal;
 import  org.cytoscape.viewmodel.VisualProperty;
 import  org.cytoscape.viewmodel.DependentVisualPropertyCallback;
-import java.util.List;
-import java.util.ArrayList;
+import java.util.*;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
 /**
- * FIXME
- * Think of it as a column in the viewmodel table.
+ * A VisualProperty whose values are elements of a discrete set, all
+ * implementing the same interface T
+ * 
+ * This VisualProperty is extensible by providing an OSGi service of
+ * interface T. (See demo code at....)
+ * 
+ * Note that defaultValue instance also has to be registered as an
+ * OSGi service.
+ * 
+ * TODO: will need some events so that UI can handle services being
+ * added/removed. (Maybe UI will listen directly to OSGi events, maybe
+ * DiscreteVisualProperty will wrap OSGi events, so that UI can be
+ * OSGi-agnostic.)
+ * 
  */
 public class DiscreteVisualProperty<T> implements VisualProperty<T> {
     private String id;
     private String name;
-    //private T defaultValue;
-    private List<T> values;
+    private T defaultValue;
     private Class<T> dataType;
     private VisualProperty.GraphObjectType objectType;
     private DependentVisualPropertyCallback callback;
-
+    private BundleContext bc;
+    
     public DiscreteVisualProperty(String id, String name, Class<T> dataType,
-                             List<T> initialValues,
-                             VisualProperty.GraphObjectType objectType){
-       this(id, name, dataType, initialValues, objectType, null);
+                                 T defaultValue,
+                                 VisualProperty.GraphObjectType objectType,
+                                 BundleContext bc){
+       this(id, name, dataType, defaultValue, objectType, null, bc);
     }
     public DiscreteVisualProperty(String id, String name, Class<T> dataType,
-                                 List<T> initialValues,
+                                 T defaultValue,
                                  VisualProperty.GraphObjectType objectType,
-                                 DependentVisualPropertyCallback callback){
+                                 DependentVisualPropertyCallback callback,
+                                 BundleContext bc){
        this.id = id;
        this.name = name;
-       this.values = new ArrayList<T>(initialValues);
+       this.defaultValue = defaultValue;
        this.dataType = dataType;
        this.objectType = objectType;
        this.callback = callback;
+       this.bc = bc;
     }
+    /**
+     * Return all known values
+     *
+     * This method is to allow UI to show a list of values so that user can 
pick one.
+     *
+     * This implementation simply queries the OSGi framework for all
+     * services implementing dataType interface.
+     */
+    public Set<T> getValues(){ // copy-paste-modified from CyEventHelperImpl 
in core3/model
+       Set<T> ret = new HashSet<T>();
+       System.out.println("listing values");
+       if (bc == null)
+           return ret;
+       System.out.println("listing values2");
+       try {
+           ServiceReference[] sr = bc.getServiceReferences(dataType.getName(), 
null);
+           
+           if (sr != null){
+               System.out.println("len servicereferences:"+sr.length);
+               for (ServiceReference r : sr) {
+                   System.out.println("listing values3");
+                   T value = (T) bc.getService(r);
+                   
+                   if (value != null)
+                       ret.add(value);
+               }
+           } else {
+               System.out.println("sr is null");
+           }
+       } catch (Exception e) {
+           e.printStackTrace();
+       }
+
+       return ret;
+    }
+
     public VisualProperty.GraphObjectType getObjectType(){
        return objectType;
     }
@@ -56,7 +109,7 @@
         * @return  DOCUMENT ME!
         */
     public T getDefault(){
-       return values.get(0); //FIXME: defensive copy needed? how to do that?
+       return defaultValue; //FIXME: defensive copy needed? how to do that?
     }
 
        /**

Modified: 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/java/org/cytoscape/presentation/internal/NetworkPresentationFactoryImpl.java
===================================================================
--- 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/java/org/cytoscape/presentation/internal/NetworkPresentationFactoryImpl.java
  2008-12-19 20:19:10 UTC (rev 15473)
+++ 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/java/org/cytoscape/presentation/internal/NetworkPresentationFactoryImpl.java
  2008-12-20 09:22:09 UTC (rev 15474)
@@ -6,12 +6,40 @@
 import org.cytoscape.presentation.NetworkPresentationFactory;
 import org.cytoscape.viewmodel.CyNetworkView;
 
+import org.osgi.framework.BundleContext;
+
 /**
  * 
  */
 public class NetworkPresentationFactoryImpl implements 
NetworkPresentationFactory {
+        private BundleContext bundleContext;
+
+       /**
+        * For setter injection (hmm. whats that?)
+        */
+       public NetworkPresentationFactoryImpl() {
+       }
+    public void setBundleContext(BundleContext bundleContext) {
+       this.bundleContext = bundleContext;
+    }
+    public BundleContext getBundleContext() {
+       return bundleContext;
+    }
+
+
+       /**
+        * Creates a new CyNetworkFactoryImpl object.
+        *
+        * @param h  DOCUMENT ME!
+        */
+    public NetworkPresentationFactoryImpl(final BundleContext bundleContext) {
+               if (bundleContext == null)
+                       throw new NullPointerException("bundleContext is null");
+               this.bundleContext = bundleContext;
+       }
+
     public TextPresentation getTextPresentationFor(CyNetworkView view){
-       return new AdjMatrixTextRenderer(view);
+       return new AdjMatrixTextRenderer(view, bundleContext);
     }
     public SwingPresentation getSwingPresentationFor(CyNetworkView view){
        throw new RuntimeException("not implemented");

Added: 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/java/org/cytoscape/presentation/internal/RendererActivator.java
===================================================================
--- 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/java/org/cytoscape/presentation/internal/RendererActivator.java
       2008-12-19 20:19:10 UTC (rev 15473)
+++ 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/java/org/cytoscape/presentation/internal/RendererActivator.java
       2008-12-20 09:22:09 UTC (rev 15474)
@@ -0,0 +1,77 @@
+
+/*
+ Copyright (c) 2006, 2007, The Cytoscape Consortium (www.cytoscape.org)
+
+ The Cytoscape Consortium is:
+ - Institute for Systems Biology
+ - University of California San Diego
+ - Memorial Sloan-Kettering Cancer Center
+ - Institut Pasteur
+ - Agilent Technologies
+
+ This library is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ any later version.
+
+ This library is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
+ MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
+ documentation provided hereunder is on an "as is" basis, and the
+ Institute for Systems Biology and the Whitehead Institute
+ have no obligations to provide maintenance, support,
+ updates, enhancements or modifications.  In no event shall the
+ Institute for Systems Biology and the Whitehead Institute
+ be liable to any party for direct, indirect, special,
+ incidental or consequential damages, including lost profits, arising
+ out of the use of this software and its documentation, even if the
+ Institute for Systems Biology and the Whitehead Institute
+ have been advised of the possibility of such damage.  See
+ the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this library; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+*/
+
+package org.cytoscape.presentation.internal;
+
+import org.cytoscape.presentation.TextPresentation;
+import org.cytoscape.presentation.TextNodeRenderer;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Set;
+
+public class RendererActivator implements BundleActivator {
+
+       private Set<ServiceRegistration> regSet;
+
+       public void start(BundleContext bc) {
+               System.out.println("demo presentation RendererActivator 
start");        
+               // initialize the set of registrations
+               regSet = new HashSet<ServiceRegistration>();
+
+
+               Hashtable props = new Hashtable();
+               //ServiceRegistration reg = 
bc.registerService(TextPresentation.class.getName(),
+               //                                           new 
AdjMatrixTextRenderer(),props);
+               //regSet.add( reg );
+
+               props = new Hashtable();
+               ServiceRegistration reg = 
bc.registerService(TextNodeRenderer.class.getName(),
+                                                            new 
TextNodeRendererImpl(),props);
+               regSet.add( reg );
+
+       }
+
+       public void stop(BundleContext bc) {
+               for ( ServiceRegistration reg : regSet )
+                       reg.unregister();
+       }
+
+}

Modified: 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/resources/META-INF/spring/bundle-context.xml
===================================================================
--- 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/resources/META-INF/spring/bundle-context.xml
  2008-12-19 20:19:10 UTC (rev 15473)
+++ 
cytoscape3/branches/abeld-gsoc/dev/refactored-viewmodel/presentation/src/main/resources/META-INF/spring/bundle-context.xml
  2008-12-20 09:22:09 UTC (rev 15474)
@@ -16,5 +16,6 @@
        <context:annotation-config/>
        
        <bean id="networkPresentationFactory" 
class="org.cytoscape.presentation.internal.NetworkPresentationFactoryImpl">
+         <property name="bundleContext" ref="bundleContext"></property>
        </bean>
 </beans>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to