Skip to content

Commit

Permalink
Add canvas parameter to AnnotatationTree methods. Some cleanup. Refs
Browse files Browse the repository at this point in the history
#4314
  • Loading branch information
mikekucera committed Sep 13, 2018
1 parent a62aaa6 commit 689b4e4
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 139 deletions.
Expand Up @@ -74,16 +74,16 @@ public String toString() {
}

void removeEmptyGroups() {
for(AnnotationNode n : children) {
n.removeEmptyGroups();
}
Iterator<AnnotationNode> iter = children.iterator();
while(iter.hasNext()) {
AnnotationNode n = iter.next();
if(n.getAnnotation() instanceof GroupAnnotation && n.getChildCount() == 0) {
iter.remove();
}
}
for(AnnotationNode n : children) {
n.removeEmptyGroups();
}
}


Expand Down
Expand Up @@ -8,6 +8,7 @@
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -57,33 +58,36 @@ public AnnotationNode getRoot(String canvas) {
}
}

public AnnotationNode get(Annotation a) {
AnnotationNode node = foregroundLookup.get(a);
if(node != null)
return node;
return backgroundLookup.get(a);
}
public AnnotationNode get(String canvas, Annotation a) {
switch(canvas) {
case Annotation.FOREGROUND: return foregroundLookup.get(a);
case Annotation.BACKGROUND: return backgroundLookup.get(a);
default: return null;
}
}


public void shift(int direction, Collection<? extends Annotation> annotations) {
groupByParent(annotations).forEach((parent, childrenToShift) -> parent.shift(direction, childrenToShift));
public void shift(int direction, String canvas, Collection<? extends Annotation> annotations) {
groupByParent(canvas, annotations).forEach((parent, childrenToShift) -> parent.shift(direction, childrenToShift));
}

public boolean shiftAllowed(int direction, Collection<? extends Annotation> annotations) {
public boolean shiftAllowed(int direction, String canvas, Collection<? extends Annotation> annotations) {
if(annotations.isEmpty())
return false;

for(Map.Entry<AnnotationNode,List<AnnotationNode>> entry : groupByParent(annotations).entrySet()) {
if(!entry.getKey().shiftAllowed(direction, entry.getValue())) {
for(Map.Entry<AnnotationNode,List<AnnotationNode>> entry : groupByParent(canvas, annotations).entrySet()) {
AnnotationNode parent = entry.getKey();
List<AnnotationNode> childrenToShift = entry.getValue();
if(!parent.shiftAllowed(direction, childrenToShift)) {
return false;
}
}
return true;
}

private Map<AnnotationNode,List<AnnotationNode>> groupByParent(Collection<? extends Annotation> annotations) {
private Map<AnnotationNode,List<AnnotationNode>> groupByParent(String canvas, Collection<? extends Annotation> annotations) {
return annotations.stream()
.map(this::get)
.map(a -> this.get(canvas, a))
.filter(a -> a != null)
.collect(groupingBy(AnnotationNode::getParent)); // doesn't matter which tree they are already separate
}
Expand Down Expand Up @@ -242,6 +246,27 @@ private static boolean containsCycle(DingAnnotation a, Collection<DingAnnotation
}
return false;
}


public static boolean hasSameParent(Collection<? extends Annotation> annotations) {
if(annotations.isEmpty())
return false;

Iterator<? extends Annotation> iter = annotations.iterator();
GroupAnnotation parent = null;
if(iter.hasNext()) {
DingAnnotation a = (DingAnnotation)iter.next();
parent = a.getGroupParent();
}
while(iter.hasNext()) {
DingAnnotation a = (DingAnnotation)iter.next();
if(parent != a.getGroupParent()) {
return false;
}
}

return true;
}


private static void sortAnnotations(List<Annotation> annotations) {
Expand Down
Expand Up @@ -4,6 +4,7 @@
import java.util.Collections;

import org.cytoscape.ding.impl.DGraphView;
import org.cytoscape.ding.impl.cyannotator.AnnotationTree;
import org.cytoscape.ding.impl.cyannotator.CyAnnotator;
import org.cytoscape.ding.impl.cyannotator.annotations.DingAnnotation;
import org.cytoscape.ding.impl.cyannotator.annotations.GroupAnnotationImpl;
Expand Down Expand Up @@ -59,6 +60,9 @@ public GroupAnnotationsTask(CyNetworkView view, Collection<DingAnnotation> annot

@Override
public void run(TaskMonitor tm) throws Exception {
if(annotations.isEmpty() || !AnnotationTree.hasSameParent(annotations))
return;

if (view instanceof DGraphView) {
DGraphView dView = (DGraphView) view;

Expand All @@ -72,6 +76,8 @@ public void run(TaskMonitor tm) throws Exception {

cyAnnotator.markUndoEdit("Group Annotations");

GroupAnnotation parent = annotations.iterator().next().getGroupParent(); // may be null

// remove the annotations from any existing groups
for(DingAnnotation a : selectedAnnotations) {
GroupAnnotation group = a.getGroupParent();
Expand All @@ -80,21 +86,24 @@ public void run(TaskMonitor tm) throws Exception {
}
}

GroupAnnotationImpl group = new GroupAnnotationImpl(dView, Collections.emptyMap());
group.addComponent(null); // Need to add this first so we can update things appropriately
GroupAnnotationImpl newGroup = new GroupAnnotationImpl(dView, Collections.emptyMap());
newGroup.addComponent(null); // Need to add this first so we can update things appropriately

// Now, add all of the children--do not iterate AnnotationSelection directly or that can throw
// ConcurrentModifcationExceptions
for(DingAnnotation a : selectedAnnotations) {
group.addMember(a);
newGroup.addMember(a);
a.setSelected(false);
};

cyAnnotator.addAnnotation(group);
if(parent != null)
parent.addMember(newGroup);

cyAnnotator.addAnnotation(newGroup);

// Finally, set ourselves to be the selected component
group.setSelected(true);
group.update();
// Finally, set ourselves to be the selected components
newGroup.setSelected(true);
newGroup.update();

cyAnnotator.postUndoEdit();
}
Expand Down
@@ -1,41 +1,12 @@
package org.cytoscape.ding.impl.cyannotator.tasks;

/*
* #%L
* Cytoscape Ding View/Presentation Impl (ding-presentation-impl)
* $Id:$
* $HeadURL:$
* %%
* Copyright (C) 2006 - 2013 The Cytoscape Consortium
* %%
* This program 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 (at your option) any later version.
*
* This program 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. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/



import java.awt.geom.Point2D;

import org.cytoscape.ding.impl.DGraphView;
import org.cytoscape.ding.impl.cyannotator.AnnotationTree;
import org.cytoscape.ding.impl.cyannotator.CyAnnotator;
import org.cytoscape.task.NetworkViewTaskFactory;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.work.TaskIterator;

import org.cytoscape.ding.impl.DGraphView;
import org.cytoscape.ding.impl.cyannotator.CyAnnotator;
import org.cytoscape.ding.impl.cyannotator.annotations.DingAnnotation;

public class GroupAnnotationsTaskFactory implements NetworkViewTaskFactory {

@Override
Expand All @@ -47,9 +18,6 @@ public TaskIterator createTaskIterator(CyNetworkView networkView) {
@Override
public boolean isReady(CyNetworkView networkView) {
CyAnnotator cyAnnotator = ((DGraphView)networkView).getCyAnnotator();
// Get all of the selected annotations
if (cyAnnotator.getAnnotationSelection().count() > 1)
return true;
return false;
return AnnotationTree.hasSameParent(cyAnnotator.getAnnotationSelection().getSelectedAnnotations());
}
}
Expand Up @@ -4,13 +4,17 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.cytoscape.ding.impl.DGraphView;
import org.cytoscape.ding.impl.cyannotator.AnnotationTree;
import org.cytoscape.ding.impl.cyannotator.CyAnnotator;
import org.cytoscape.ding.impl.cyannotator.annotations.DingAnnotation;
import org.cytoscape.task.AbstractNetworkViewTask;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.presentation.annotations.Annotation;
import org.cytoscape.view.presentation.annotations.GroupAnnotation;
import org.cytoscape.work.TaskMonitor;

/*
Expand Down Expand Up @@ -79,7 +83,11 @@ public void run(TaskMonitor tm) throws Exception {

private void changeCanvas() {
for (int i = annotations.size() - 1; i >= 0; i--) {
annotations.get(i).changeCanvas(canvasName);
DingAnnotation da = annotations.get(i);
// group annotations must stay on the foreground canvas
if(!(da instanceof GroupAnnotation && Annotation.BACKGROUND.equals(canvasName))) {
da.changeCanvas(canvasName);
}
}

// need to rebuild the tree AFTER changing the canvas
Expand All @@ -93,7 +101,17 @@ private void reorder(Integer offset) {
CyAnnotator cyAnnotator = ((DGraphView)view).getCyAnnotator();
AnnotationTree tree = cyAnnotator.getAnnotationTree();

tree.shift(offset, annotations);
Map<String,List<DingAnnotation>> byCanvas =
annotations.stream().collect(Collectors.groupingBy(DingAnnotation::getCanvasName));

List<DingAnnotation> fga = byCanvas.get(Annotation.FOREGROUND);
if(fga != null && !fga.isEmpty())
tree.shift(offset, Annotation.FOREGROUND, fga);

List<DingAnnotation> bga = byCanvas.get(Annotation.BACKGROUND);
if(bga != null && !bga.isEmpty())
tree.shift(offset, Annotation.BACKGROUND, bga);

tree.resetZOrder();
}

Expand Down
Expand Up @@ -8,6 +8,7 @@
import org.cytoscape.ding.impl.cyannotator.annotations.DingAnnotation;
import org.cytoscape.task.NetworkViewTaskFactory;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.presentation.annotations.Annotation;
import org.cytoscape.work.TaskIterator;

/*
Expand Down Expand Up @@ -75,7 +76,9 @@ public boolean isReady(CyNetworkView view) {


if (offset != null) {
return cyAnnotator.getAnnotationTree().shiftAllowed(offset, annotations);
boolean fg = cyAnnotator.getAnnotationTree().shiftAllowed(offset, Annotation.FOREGROUND, annotations);
boolean bg = cyAnnotator.getAnnotationTree().shiftAllowed(offset, Annotation.BACKGROUND, annotations);
return fg || bg;
}

if (canvasName != null) {
Expand Down
Expand Up @@ -3,6 +3,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.cytoscape.ding.impl.DGraphView;
Expand Down Expand Up @@ -55,19 +56,29 @@ public UngroupAnnotationsTask(CyNetworkView view, Collection<GroupAnnotation> an
}

@Override
public void run(TaskMonitor tm) throws Exception {
public void run(TaskMonitor tm) {
if (view instanceof DGraphView) {

CyAnnotator annotator = ((DGraphView)view).getCyAnnotator();
annotator.markUndoEdit("Ungroup Annotations");

for(GroupAnnotation ga : groups) {
for(Annotation a : ga.getMembers()) {
GroupAnnotation parent = ((DingAnnotation)ga).getGroupParent();
List<Annotation> members = ga.getMembers();

for(Annotation a : members) {
ga.removeMember(a);
a.setSelected(true);
}

ga.removeAnnotation();
// move the annotations into the parent
if(parent != null) {
for(Annotation a : members) {
parent.addMember(a);
}
}

ga.removeAnnotation(); // this fires an event so it must go at the end
}

annotator.postUndoEdit();
Expand Down

0 comments on commit 689b4e4

Please sign in to comment.