package org.cytoscape.ding.impl.cyannotator.tasks;

import java.util.Collection;

import org.cytoscape.ding.impl.DGraphView;
import org.cytoscape.ding.impl.cyannotator.CyAnnotator;
import org.cytoscape.ding.impl.cyannotator.annotations.DingAnnotation;
import org.cytoscape.ding.impl.cyannotator.annotations.GroupAnnotationImpl;
import org.cytoscape.task.AbstractNetworkViewTask;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.work.TaskMonitor;

/*
 * #%L
 * Cytoscape Ding View/Presentation Impl (ding-presentation-impl)
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2006 - 2018 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%
 */

public class GroupAnnotationsTask extends AbstractNetworkViewTask {

	/**
	 * Group the selected annotations, if any.
	 */
	public GroupAnnotationsTask(CyNetworkView view) {
		super(view);
	}
	
	/**
	 * Group the passed annotations.
	 */
	public GroupAnnotationsTask(CyNetworkView view, Collection<DingAnnotation> annotations) {
		super(view);
	}

	@Override
	public void run(TaskMonitor tm) throws Exception {
		if (view instanceof DGraphView) {
			DGraphView dView = (DGraphView) view;
			CyAnnotator cyAnnotator = dView.getCyAnnotator();
			GroupAnnotationImpl group = new GroupAnnotationImpl(dView, null);
			group.addComponent(null); // Need to add this first so we can update things appropriately
			cyAnnotator.addAnnotation(group);

			// Now, add all of the children--do not iterate AnnotationSelection directly or that can throw
			// ConcurrentModifcationExceptions
			cyAnnotator.getAnnotationSelection().getSelectedAnnotations().forEach(a -> {
				group.addMember(a);
				a.setSelected(false);
			});

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

@@ -415,24 +415,92 @@
 etworkView;%0A
+import org.cytoscape.view.presentation.annotations.GroupAnnotation;%0A
 import org.c
@@ -526,16 +526,16 @@
 onitor;%0A
-
 %0A/*%0A * #
@@ -1463,16 +1463,69 @@
 Task %7B%0A%0A
+%09%0A%09private Collection%3CDingAnnotation%3E annotations;%0A%09%0A
 %09/**%0A%09 *
@@ -1792,17 +1792,53 @@
 view);%0A%09
-%7D
+%09this.annotations = annotations;%0A%09%7D%0A%09
 %0A%0A%09@Over
@@ -1971,16 +1971,20 @@
 ) view;%0A
+%09%09%09%0A
 %09%09%09CyAnn
@@ -2028,16 +2028,457 @@
 ator();%0A
+%09%09%09%0A%09%09%09Collection%3CDingAnnotation%3E selectedAnnotations;%0A%09%09%09if(annotations == null)%0A%09%09%09%09selectedAnnotations = cyAnnotator.getAnnotationSelection().getSelectedAnnotations();%0A%09%09%09else%0A%09%09%09%09selectedAnnotations = annotations;%0A%09%09%09%0A%0A%09%09%09// remove the annotations from any existing groups%0A%09%09%09for(DingAnnotation a : selectedAnnotations) %7B%0A%09%09%09%09GroupAnnotation group = a.getGroupParent();%0A%09%09%09%09if(group != null) %7B%0A%09%09%09%09%09group.removeMember(a);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09%0A
 %09%09%09Group
@@ -2812,49 +2812,32 @@
 %0A%09%09%09
-cyAnnotator.getAnnotationSelection().getS
+for(DingAnnotation a : s
 elec
@@ -2854,23 +2854,9 @@
 ions
-().forEach(a -%3E
+)
  %7B%0A%09
@@ -2908,13 +2908,12 @@
 e);%0A
+
 %09%09%09%7D
-)
 ;%0A%0A%09

Remove annotations from existing groups when adding to a new group. Refs #4287