Author: kono
Date: 2011-07-12 12:05:35 -0700 (Tue, 12 Jul 2011)
New Revision: 26153

Added:
   
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/util/RecentlyOpenedTrackerImpl.java
Modified:
   
core3/io-impl/trunk/src/main/resources/META-INF/spring/bundle-context-osgi.xml
   core3/io-impl/trunk/src/main/resources/META-INF/spring/bundle-context.xml
Log:
refs #112 RecentOpenedTracker implementation had been added and exported as an 
OSGi service.

Added: 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/util/RecentlyOpenedTrackerImpl.java
===================================================================
--- 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/util/RecentlyOpenedTrackerImpl.java
                             (rev 0)
+++ 
core3/io-impl/trunk/src/main/java/org/cytoscape/io/internal/util/RecentlyOpenedTrackerImpl.java
     2011-07-12 19:05:35 UTC (rev 26153)
@@ -0,0 +1,94 @@
+package org.cytoscape.io.internal.util;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.net.URL;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.cytoscape.io.util.RecentlyOpenedTracker;
+import org.cytoscape.property.CyProperty;
+
+
+public class RecentlyOpenedTrackerImpl implements RecentlyOpenedTracker {
+       
+       private static final int MAX_TRACK_COUNT = 5;
+       private static final String USER_HOME_DIR = 
System.getProperty("user.home");
+       
+       private final String trackerFileName;
+       private final LinkedList<URL> trackerURLs;
+       
+       // TODO: Where should we manage this property directory?
+       private final String PROP_DIR = USER_HOME_DIR + 
System.getProperty("file.separator") + CyProperty.DEFAULT_CONFIG_DIR;
+
+       /**
+        * Creates a "recently opened" file tracker.
+        * 
+        * @param trackerFileName
+        *            the name of the file in the Cytoscape config directory to 
read
+        *            saved file names from.
+        */
+       public RecentlyOpenedTrackerImpl(final String trackerFileName) throws 
IOException {
+               this.trackerFileName = trackerFileName;
+               trackerURLs = new LinkedList<URL>();
+
+               final File input = new File(PROP_DIR, trackerFileName);
+               if (!input.exists())
+                       input.createNewFile();
+
+               final BufferedReader reader = new BufferedReader(new 
FileReader(input));
+               String line;
+               while ((line = reader.readLine()) != null && trackerURLs.size() 
< MAX_TRACK_COUNT) {
+                       final String newURL = line.trim();
+                       if (newURL.length() > 0)
+                               trackerURLs.addLast(new URL(newURL));
+               }
+       }
+
+       /* (non-Javadoc)
+        * @see 
org.cytoscape.task.internal.session.RecentlyOpenedTracker#getRecentlyOpenedURLs()
+        */
+       @Override
+       public synchronized List<URL> getRecentlyOpenedURLs() {
+               return Collections.unmodifiableList(trackerURLs);
+       }
+
+
+       /* (non-Javadoc)
+        * @see 
org.cytoscape.task.internal.session.RecentlyOpenedTracker#add(java.net.URL)
+        */
+       @Override
+       public synchronized void add(final URL newURL) {
+               trackerURLs.remove(newURL);
+               if (trackerURLs.size() == MAX_TRACK_COUNT)
+                       trackerURLs.removeLast();
+               trackerURLs.addFirst(newURL);
+       }
+
+       /* (non-Javadoc)
+        * @see 
org.cytoscape.task.internal.session.RecentlyOpenedTracker#writeOut()
+        */
+       @Override
+       public void writeOut() throws FileNotFoundException {
+               final PrintWriter writer = new PrintWriter(new File(PROP_DIR, 
trackerFileName));
+               for (final URL trackerURL : trackerURLs)
+                       writer.println(trackerURL.toString());
+               writer.close();
+       }
+
+       /* (non-Javadoc)
+        * @see 
org.cytoscape.task.internal.session.RecentlyOpenedTracker#getMostRecentAddition()
+        */
+       @Override
+       public synchronized URL getMostRecentlyOpenedURL() {
+               if (trackerURLs.isEmpty())
+                       return null;
+               else
+                       return trackerURLs.getFirst();
+       }
+}

Modified: 
core3/io-impl/trunk/src/main/resources/META-INF/spring/bundle-context-osgi.xml
===================================================================
--- 
core3/io-impl/trunk/src/main/resources/META-INF/spring/bundle-context-osgi.xml  
    2011-07-12 18:42:35 UTC (rev 26152)
+++ 
core3/io-impl/trunk/src/main/resources/META-INF/spring/bundle-context-osgi.xml  
    2011-07-12 19:05:35 UTC (rev 26153)
@@ -370,8 +370,15 @@
                <osgi:listener bind-method="addCyWriterFactory"
                        unbind-method="removeCyWriterFactory" 
ref="vizmapWriterManager" />
        </osgi:set>
+
+       <!-- Export listeners -->
+       <osgi:service id="unrecognizedVisualPropertyManagerService"
+               ref="unrecognizedVisualPropertyManager"
+               
interface="org.cytoscape.view.model.events.NetworkViewAboutToBeDestroyedListener"
 />
        
-       <!-- Export listeners -->
-       <osgi:service id="unrecognizedVisualPropertyManagerService" 
ref="unrecognizedVisualPropertyManager"
-        
interface="org.cytoscape.view.model.events.NetworkViewAboutToBeDestroyedListener"
 />
+       <!-- Export Session Tracker as a service -->
+       <osgi:service id="recentlyOpenedTrackerService" 
ref="recentlyOpenedTracker"
+               interface="org.cytoscape.io.util.RecentlyOpenedTracker">
+       </osgi:service>
+       
 </beans>

Modified: 
core3/io-impl/trunk/src/main/resources/META-INF/spring/bundle-context.xml
===================================================================
--- core3/io-impl/trunk/src/main/resources/META-INF/spring/bundle-context.xml   
2011-07-12 18:42:35 UTC (rev 26152)
+++ core3/io-impl/trunk/src/main/resources/META-INF/spring/bundle-context.xml   
2011-07-12 19:05:35 UTC (rev 26153)
@@ -622,17 +622,22 @@
        <!-- Other beans -->
        
        <bean id="calculatorConverterFactory"
-        
class="org.cytoscape.io.internal.util.vizmap.CalculatorConverterFactory" />
-        
-    <bean id="visualStyleSerializer"
-        class="org.cytoscape.io.internal.util.vizmap.VisualStyleSerializer">
-        <constructor-arg ref="calculatorConverterFactory" />
-        <constructor-arg ref="visualStyleFactoryServiceRef" />
-        <constructor-arg ref="visualMappingManagerServiceRef" />
-        <constructor-arg ref="renderingEngineManagerServiceRef" />
-        <constructor-arg ref="discreteMappingFactoryServiceRef" />
-        <constructor-arg ref="continuousMappingFactoryServiceRef" />
-        <constructor-arg ref="passthroughMappingFactoryServiceRef" />
-    </bean>
+       
class="org.cytoscape.io.internal.util.vizmap.CalculatorConverterFactory" />
 
+       <bean id="visualStyleSerializer"
+               
class="org.cytoscape.io.internal.util.vizmap.VisualStyleSerializer">
+               <constructor-arg ref="calculatorConverterFactory" />
+               <constructor-arg ref="visualStyleFactoryServiceRef" />
+               <constructor-arg ref="visualMappingManagerServiceRef" />
+               <constructor-arg ref="renderingEngineManagerServiceRef" />
+               <constructor-arg ref="discreteMappingFactoryServiceRef" />
+               <constructor-arg ref="continuousMappingFactoryServiceRef" />
+               <constructor-arg ref="passthroughMappingFactoryServiceRef" />
+       </bean>
+       
+       <!-- For recent session files -->
+       <bean id="recentlyOpenedTracker" 
class="org.cytoscape.io.internal.util.RecentlyOpenedTrackerImpl" 
scope="singleton">
+               <constructor-arg value="tracker.recent.sessions" />
+       </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