Author: scooter
Date: 2012-07-30 17:28:17 -0700 (Mon, 30 Jul 2012)
New Revision: 30047

Added:
   
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/GroupEdit.java
Modified:
   
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/CyActivator.java
   
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/CollapseGroupTask.java
   
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/GroupNodeContextTaskFactoryImpl.java
   
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/GroupNodesTask.java
   
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/GroupNodesTaskFactoryImpl.java
   
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/UnGroupNodesTask.java
   
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/UnGroupNodesTaskFactoryImpl.java
Log:
Add undo support for grouping and ungrouping nodes


Modified: 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/CyActivator.java
===================================================================
--- 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/CyActivator.java
  2012-07-30 23:45:09 UTC (rev 30046)
+++ 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/CyActivator.java
  2012-07-31 00:28:17 UTC (rev 30047)
@@ -971,7 +971,7 @@
                                connectSelectedNodesTaskFactoryProps);
 
                GroupNodesTaskFactoryImpl groupNodesTaskFactory = 
-                       new GroupNodesTaskFactoryImpl(cyGroupManager, 
cyGroupFactory);
+                       new GroupNodesTaskFactoryImpl(cyGroupManager, 
cyGroupFactory, undoSupportServiceRef);
                Properties groupNodesTaskFactoryProps = new Properties();
                
groupNodesTaskFactoryProps.setProperty(PREFERRED_MENU,NETWORK_GROUP_MENU);
                groupNodesTaskFactoryProps.setProperty(TITLE,"Group Selected 
Nodes");
@@ -998,7 +998,7 @@
                
registerService(bc,groupNodesTaskFactory,NodeViewTaskFactory.class, 
groupNodeViewTaskFactoryProps);
 
                UnGroupNodesTaskFactoryImpl unGroupTaskFactory = 
-                       new UnGroupNodesTaskFactoryImpl(cyGroupManager);
+                       new UnGroupNodesTaskFactoryImpl(cyGroupManager, 
cyGroupFactory, undoSupportServiceRef);
                Properties unGroupNodesTaskFactoryProps = new Properties();
                
unGroupNodesTaskFactoryProps.setProperty(PREFERRED_MENU,NETWORK_GROUP_MENU);
                unGroupNodesTaskFactoryProps.setProperty(TITLE,"Ungroup 
Selected Nodes");

Modified: 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/CollapseGroupTask.java
===================================================================
--- 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/CollapseGroupTask.java
      2012-07-30 23:45:09 UTC (rev 30046)
+++ 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/CollapseGroupTask.java
      2012-07-31 00:28:17 UTC (rev 30047)
@@ -1,7 +1,7 @@
 /*
- File: SelectFirstNeighborsNodeViewTask.java
+ File: CollapseGroupTask.java
 
- Copyright (c) 2006, 2010, The Cytoscape Consortium (www.cytoscape.org)
+ Copyright (c) 2012, The Cytoscape Consortium (www.cytoscape.org)
 
  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

Added: 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/GroupEdit.java
===================================================================
--- 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/GroupEdit.java
                              (rev 0)
+++ 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/GroupEdit.java
      2012-07-31 00:28:17 UTC (rev 30047)
@@ -0,0 +1,164 @@
+/*
+ File: GroupEdit.java
+
+ Copyright (c) 2012, The Cytoscape Consortium (www.cytoscape.org)
+
+ 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.task.internal.group;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.cytoscape.group.CyGroup;
+import org.cytoscape.group.CyGroupFactory;
+import org.cytoscape.group.CyGroupManager;
+import org.cytoscape.model.CyEdge;
+import org.cytoscape.model.CyNetwork;
+import org.cytoscape.model.CyNode;
+import org.cytoscape.view.model.CyNetworkView;
+import org.cytoscape.work.undo.AbstractCyEdit;
+
+public class GroupEdit extends AbstractCyEdit {
+       private CyNetwork net;
+       private CyGroupManager mgr;
+       private CyGroupFactory factory;
+       private CyGroup group;
+       private Set<CyGroup> groupSet;
+
+       private Map<CyGroup, List<CyNode>> nodeMap;
+       private Map<CyGroup, List<CyEdge>> edgesMap;
+       private Map<CyGroup, List<CyNetwork>> networkMap;
+       private Map<CyGroup, List<CyNetwork>> collapseMap;
+
+       public GroupEdit(CyNetwork net, CyGroupManager mgr, CyGroupFactory 
factory, CyGroup group) {
+               super("Group Nodes");
+               this.net = net;
+               this.mgr = mgr;
+               this.factory = factory;
+               this.group = group;
+               this.groupSet = null;
+               initMaps();
+               createShadowGroup(group);
+       }
+
+       public GroupEdit(CyNetwork net, CyGroupManager mgr, CyGroupFactory 
factory, Set <CyGroup> groupSet) {
+               super("Ungroup Nodes");
+               this.net = net;
+               this.mgr = mgr;
+               this.factory = factory;
+               this.group = null;
+               this.groupSet = groupSet;
+               initMaps();
+               for (CyGroup group: groupSet)
+                       createShadowGroup(group);
+       }
+
+       public void undo() {
+               if (group != null) {
+                       // Undo group nodes
+                       mgr.destroyGroup(group);
+               } else if (groupSet != null) {
+                       Set<CyGroup>newGroups = new HashSet<CyGroup>();
+
+                       // Undo ungroup nodes
+                       for (CyGroup group: groupSet) {
+                               newGroups.add(reGroup(group));
+                       }
+
+                       // Now recreate our maps in case we redo
+                       initMaps();
+                       groupSet = newGroups;
+                       for (CyGroup group: groupSet)
+                               createShadowGroup(group);
+               }
+       }
+
+       public void redo() {
+               if (group != null) {
+                       // This is sort of a pain.  We need to reconfigure the
+                       // group from scratch...
+                       this.group = reGroup(group);
+                       initMaps();
+                       createShadowGroup(group);
+               } else if (groupSet != null) {
+                       for (CyGroup group: groupSet) {
+                               mgr.destroyGroup(group);
+                       }
+               }
+       }
+
+       private CyGroup reGroup(CyGroup group) {
+               List<CyNetwork> netList = networkMap.get(group);
+
+               // network 0 is always the root network
+               CyNetwork firstNet = netList.get(1);
+               CyGroup newGroup = factory.createGroup(firstNet, 
nodeMap.get(group), edgesMap.get(group), true);
+
+               // It should be at least 2 (rootNetwork + initial network)
+               if (netList.size() > 2) {
+                       for (int i = 2; i < netList.size(); i++) {
+                               newGroup.addGroupToNetwork(netList.get(i));
+                       }
+               }
+
+               for (CyNetwork net: collapseMap.get(group)) {
+                       newGroup.collapse(net);
+               }
+               return newGroup;
+       }
+
+       private void initMaps() {
+               nodeMap = new HashMap<CyGroup, List<CyNode>>();
+               edgesMap = new HashMap<CyGroup, List<CyEdge>>();
+               networkMap = new HashMap<CyGroup, List<CyNetwork>>();
+               collapseMap = new HashMap<CyGroup, List<CyNetwork>>();
+       }
+
+       private void createShadowGroup(CyGroup group) {
+               // Get the nodes, edges, and external edges
+               nodeMap.put(group, group.getNodeList());
+               List<CyEdge> edgesList = new 
ArrayList<CyEdge>(group.getInternalEdgeList());
+               edgesList.addAll(group.getExternalEdgeList());
+               edgesMap.put(group, edgesList);
+
+               // Get the networks
+               networkMap.put(group, new 
ArrayList<CyNetwork>(group.getNetworkSet()));
+
+               // Get the collapse set
+               List<CyNetwork> collapseList = new ArrayList<CyNetwork>();
+               for (CyNetwork net: group.getNetworkSet()) {
+                       if (group.isCollapsed(net)) {
+                               collapseList.add(net);
+                       }
+               }
+               collapseMap.put(group, collapseList);
+       }
+
+}

Modified: 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/GroupNodeContextTaskFactoryImpl.java
===================================================================
--- 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/GroupNodeContextTaskFactoryImpl.java
        2012-07-30 23:45:09 UTC (rev 30046)
+++ 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/GroupNodeContextTaskFactoryImpl.java
        2012-07-31 00:28:17 UTC (rev 30047)
@@ -1,7 +1,7 @@
 /*
- File: SelectFirstNeighborsNodeViewTaskFactory.java
+ File: GroupNodeContextTaskFactoryImpl.java
 
- Copyright (c) 2006, 2010, The Cytoscape Consortium (www.cytoscape.org)
+ Copyright (c) 2012, The Cytoscape Consortium (www.cytoscape.org)
 
  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

Modified: 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/GroupNodesTask.java
===================================================================
--- 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/GroupNodesTask.java
 2012-07-30 23:45:09 UTC (rev 30046)
+++ 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/GroupNodesTask.java
 2012-07-31 00:28:17 UTC (rev 30047)
@@ -1,7 +1,7 @@
 /*
- File: SelectFirstNeighborsNodeViewTask.java
+ File: GroupNodesTask.java
 
- Copyright (c) 2006, 2010, The Cytoscape Consortium (www.cytoscape.org)
+ Copyright (c) 2012, The Cytoscape Consortium (www.cytoscape.org)
 
  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
@@ -43,18 +43,21 @@
 
 import org.cytoscape.work.AbstractTask;
 import org.cytoscape.work.TaskMonitor;
+import org.cytoscape.work.undo.UndoSupport;
 
 public class GroupNodesTask extends AbstractTask {
        private CyNetwork net;
        private CyGroupManager mgr;
        private CyGroupFactory factory;
+       private UndoSupport undoSupport;
 
-       public GroupNodesTask(CyNetworkView netView, CyGroupManager mgr, 
CyGroupFactory factory) {
+       public GroupNodesTask(UndoSupport undoSupport, CyNetworkView netView, 
CyGroupManager mgr, CyGroupFactory factory) {
                if (netView == null)
                        throw new NullPointerException("network view is null");
                this.net = netView.getModel();
                this.mgr = mgr;
                this.factory = factory;
+               this.undoSupport = undoSupport;
        }
 
        public void run(TaskMonitor tm) throws Exception {
@@ -66,6 +69,7 @@
                // At some point, we'll want to seriously think about only 
adding 
                // those edges that are also selected, but for now....
                CyGroup group = factory.createGroup(net, selNodes, null, true);
+               undoSupport.postEdit(new GroupEdit(net, mgr, factory, group));
                // mgr.addGroup(group);
                tm.setProgress(1.0d);
        }

Modified: 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/GroupNodesTaskFactoryImpl.java
===================================================================
--- 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/GroupNodesTaskFactoryImpl.java
      2012-07-30 23:45:09 UTC (rev 30046)
+++ 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/GroupNodesTaskFactoryImpl.java
      2012-07-31 00:28:17 UTC (rev 30047)
@@ -1,7 +1,7 @@
 /*
- File: SelectFirstNeighborsNodeViewTaskFactory.java
+ File: GroupNodesTaskFactoryImpl.java
 
- Copyright (c) 2006, 2010, The Cytoscape Consortium (www.cytoscape.org)
+ Copyright (c) 2012, The Cytoscape Consortium (www.cytoscape.org)
 
  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
@@ -42,24 +42,27 @@
 import org.cytoscape.view.model.CyNetworkView;
 import org.cytoscape.view.model.View;
 import org.cytoscape.work.TaskIterator;
+import org.cytoscape.work.undo.UndoSupport;
 
 public class GroupNodesTaskFactoryImpl extends AbstractNetworkViewTaskFactory 
                                        implements NodeViewTaskFactory, 
GroupNodesTaskFactory {
        private CyGroupManager mgr;
        private CyGroupFactory groupFactory;
+       private UndoSupport undoSupport;
 
-       public GroupNodesTaskFactoryImpl(CyGroupManager mgr, CyGroupFactory 
groupFactory) {
+       public GroupNodesTaskFactoryImpl(CyGroupManager mgr, CyGroupFactory 
groupFactory, UndoSupport undoSupport) {
                super();
                this.mgr = mgr;
                this.groupFactory = groupFactory;
+               this.undoSupport = undoSupport;
        }
 
        public TaskIterator createTaskIterator(CyNetworkView view) {
-               return new TaskIterator(new GroupNodesTask(view, mgr, 
groupFactory));
+               return new TaskIterator(new GroupNodesTask(undoSupport, view, 
mgr, groupFactory));
        }
 
        public TaskIterator createTaskIterator(View<CyNode> nodeView, 
CyNetworkView view) {
-               return new TaskIterator(new GroupNodesTask(view, mgr, 
groupFactory));
+               return new TaskIterator(new GroupNodesTask(undoSupport, view, 
mgr, groupFactory));
        }
 
        public boolean isReady(CyNetworkView netView) {

Modified: 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/UnGroupNodesTask.java
===================================================================
--- 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/UnGroupNodesTask.java
       2012-07-30 23:45:09 UTC (rev 30046)
+++ 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/UnGroupNodesTask.java
       2012-07-31 00:28:17 UTC (rev 30047)
@@ -1,7 +1,7 @@
 /*
- File: SelectFirstNeighborsNodeViewTask.java
+ File: UnGroupNodesTask.java
 
- Copyright (c) 2006, 2010, The Cytoscape Consortium (www.cytoscape.org)
+ Copyright (c) 2012, The Cytoscape Consortium (www.cytoscape.org)
 
  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
@@ -33,6 +33,7 @@
 import java.util.Set;
 
 import org.cytoscape.group.CyGroup;
+import org.cytoscape.group.CyGroupFactory;
 import org.cytoscape.group.CyGroupManager;
 
 import org.cytoscape.model.CyNetwork;
@@ -40,27 +41,35 @@
 
 import org.cytoscape.work.AbstractTask;
 import org.cytoscape.work.TaskMonitor;
+import org.cytoscape.work.undo.UndoSupport;
 
 public class UnGroupNodesTask extends AbstractTask {
        private CyNetwork net;
        private CyGroupManager mgr;
+       private CyGroupFactory factory;
        private Set<CyGroup>groupSet = null;
+       private UndoSupport undoSupport;
 
-       public UnGroupNodesTask(CyNetwork net, Set<CyGroup>groups, 
CyGroupManager mgr) {
+       public UnGroupNodesTask(UndoSupport undoSupport, CyNetwork net, 
CyGroupFactory factory,
+                               Set<CyGroup>groups, CyGroupManager mgr) {
                if (net == null)
                        throw new NullPointerException("network is null");
                this.net = net;
                this.mgr = mgr;
+               this.factory = factory;
                this.groupSet = groups;
+               this.undoSupport = undoSupport;
        }
 
        public void run(TaskMonitor tm) throws Exception {
                tm.setProgress(0.0);
 
+               GroupEdit edit = new GroupEdit(net, mgr, factory, groupSet);
                for (CyGroup group: groupSet) {
                        mgr.destroyGroup(group);
                        tm.setProgress(1.0d/(double)groupSet.size());
                }
+               undoSupport.postEdit(edit);
                tm.setProgress(1.0d);
        }
 }

Modified: 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/UnGroupNodesTaskFactoryImpl.java
===================================================================
--- 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/UnGroupNodesTaskFactoryImpl.java
    2012-07-30 23:45:09 UTC (rev 30046)
+++ 
core3/impl/trunk/core-task-impl/src/main/java/org/cytoscape/task/internal/group/UnGroupNodesTaskFactoryImpl.java
    2012-07-31 00:28:17 UTC (rev 30047)
@@ -1,7 +1,7 @@
 /*
- File: SelectFirstNeighborsNodeViewTaskFactory.java
+ File: UnGroupNodesTaskFactoryImpl.java
 
- Copyright (c) 2006, 2010, The Cytoscape Consortium (www.cytoscape.org)
+ Copyright (c) 2012, The Cytoscape Consortium (www.cytoscape.org)
 
  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
@@ -35,6 +35,7 @@
 import java.util.Set;
 
 import org.cytoscape.group.CyGroup;
+import org.cytoscape.group.CyGroupFactory;
 import org.cytoscape.group.CyGroupManager;
 import org.cytoscape.model.CyNetwork;
 import org.cytoscape.model.CyNode;
@@ -46,14 +47,19 @@
 import org.cytoscape.view.model.CyNetworkView;
 import org.cytoscape.view.model.View;
 import org.cytoscape.work.TaskIterator;
+import org.cytoscape.work.undo.UndoSupport;
 
 public class UnGroupNodesTaskFactoryImpl implements NetworkViewTaskFactory, 
                                                     UnGroupTaskFactory, 
UnGroupNodesTaskFactory {
        
+       private CyGroupFactory factory;
        private CyGroupManager mgr;
+       private UndoSupport undoSupport;
 
-       public UnGroupNodesTaskFactoryImpl(CyGroupManager mgr) {
+       public UnGroupNodesTaskFactoryImpl(CyGroupManager mgr, CyGroupFactory 
factory, UndoSupport undoSupport) {
                this.mgr = mgr;
+               this.undoSupport = undoSupport;
+               this.factory = factory;
        }
 
        public boolean isReady(View<CyNode> nodeView, CyNetworkView netView) {
@@ -91,14 +97,14 @@
                CyNetwork net = netView.getModel();
                Set<CyGroup> groups = getGroups(net, nodeList);
 
-               return new TaskIterator(new UnGroupNodesTask(net, groups, mgr));
+               return new TaskIterator(new UnGroupNodesTask(undoSupport, net, 
factory, groups, mgr));
        }
 
        public TaskIterator createTaskIterator(CyNetworkView netView) {
                CyNetwork net = netView.getModel();
                final List<CyNode> selNodes = CyTableUtil.getNodesInState(net, 
CyNetwork.SELECTED, true);
                Set<CyGroup> groups = getGroups(net, selNodes);
-               return new TaskIterator(new 
UnGroupNodesTask(netView.getModel(), groups, mgr));
+               return new TaskIterator(new UnGroupNodesTask(undoSupport, 
netView.getModel(), factory, groups, mgr));
        }
 
        private Set<CyGroup>getGroups(CyNetwork net, List<CyNode>nodeList) {

-- 
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