/*
*
* Copyright 2008,2009 Newcastle University
*
* This file is part of Workcraft.
*
* Workcraft is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Workcraft 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 Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Workcraft.  If not, see <http://www.gnu.org/licenses/>.
*
*/

package org.workcraft.plugins.transform;

import java.util.Collection;

import org.workcraft.TransformationTool;
import org.workcraft.dom.visual.VisualModel;
import org.workcraft.dom.visual.connections.VisualConnection;
import org.workcraft.dom.visual.connections.VisualConnection.ConnectionType;
import org.workcraft.util.Hierarchy;
import org.workcraft.util.WorkspaceUtils;
import org.workcraft.workspace.WorkspaceEntry;

public class StraightenConnectionsTool extends TransformationTool {

    @Override
    public String getDisplayName() {
        return "Straighten connections (selected or all)";
    }

    @Override
    public boolean isApplicableTo(WorkspaceEntry we) {
        return WorkspaceUtils.canHas(we, VisualModel.class);
    }

    @Override
    public Position getPosition() {
        return Position.BOTTOM;
    }

    @Override
    public void run(WorkspaceEntry we) {
        VisualModel visualModel = WorkspaceUtils.getAs(we, VisualModel.class);
        if (visualModel != null) {
            Collection<VisualConnection> connections = Hierarchy.getDescendantsOfType(visualModel.getRoot(), VisualConnection.class);
            if (!visualModel.getSelection().isEmpty()) {
                connections.retainAll(visualModel.getSelection());
            }
            if (!connections.isEmpty()) {
                we.saveMemento();
                for (VisualConnection connection: connections) {
                    connection.setConnectionType(ConnectionType.BEZIER);
                    connection.setConnectionType(ConnectionType.POLYLINE);
                }
            }
        }
    }

}

@@ -781,16 +781,80 @@
 ection;%0A
+import java.util.HashSet;%0A%0Aimport org.workcraft.NodeTransformer;
 %0Aimport 
@@ -916,26 +916,258 @@
 dom.
-visual.VisualModel
+Model;%0Aimport org.workcraft.dom.Node;%0Aimport org.workcraft.dom.visual.VisualModel;%0Aimport org.workcraft.dom.visual.connections.Bezier;%0Aimport org.workcraft.dom.visual.connections.ConnectionGraphic;%0Aimport org.workcraft.dom.visual.connections.Polyline
 ;%0Aim
@@ -1496,16 +1496,43 @@
 ionTool 
+implements NodeTransformer 
 %7B%0A%0A    @
@@ -1809,662 +1809,1764 @@
 lic 
-Position getPosition() %7B%0A        return Position.BOTTOM;%0A    %7D%0A%0A    @Override%0A    public void run(WorkspaceEntry we) %7B%0A        VisualModel visualModel = WorkspaceUtils.getAs(we, VisualModel.class);%0A        if (visualModel != null) %7B%0A            Collection%3CVisualConnection%3E connections = Hierarchy.getDescendantsOfType(visualModel.getRoot(), VisualConnection.class);%0A            if (!visualModel.getSelection().isEmpty()) %7B%0A                connections.retainAll(visualModel.getSelection());%0A            %7D%0A            if (!connections.isEmpty()) %7B%0A                we.saveMemento();%0A                for (VisualConnection connection: connections) %7B%0A        
+boolean isApplicableTo(Node node) %7B%0A        boolean result = false;%0A        if (node instanceof VisualConnection) %7B%0A            VisualConnection connection = (VisualConnection) node;%0A            ConnectionGraphic graphic = connection.getGraphic();%0A            if (graphic instanceof Bezier) %7B%0A                result = true;%0A            %7D else if (graphic instanceof Polyline) %7B%0A                Polyline polyline = (Polyline) graphic;%0A                result = polyline.getControlPointCount() %3E 0;%0A            %7D%0A        %7D%0A        return result;%0A    %7D%0A%0A    @Override%0A    public Position getPosition() %7B%0A        return Position.BOTTOM;%0A    %7D%0A%0A    @Override%0A    public void run(WorkspaceEntry we) %7B%0A        VisualModel visualModel = WorkspaceUtils.getAs(we, VisualModel.class);%0A        if (visualModel != null) %7B%0A            Collection%3CVisualConnection%3E connections = Hierarchy.getDescendantsOfType(visualModel.getRoot(), VisualConnection.class);%0A            Collection%3CNode%3E selection = visualModel.getSelection();%0A            if (!selection.isEmpty()) %7B%0A                HashSet%3CNode%3E selectedConnections = new HashSet%3C%3E(selection);%0A                selectedConnections.retainAll(connections);%0A                if (!selectedConnections.isEmpty()) %7B%0A                    connections.retainAll(selection);%0A                %7D%0A            %7D%0A            if (!connections.isEmpty()) %7B%0A                we.saveMemento();%0A                for (VisualConnection connection: connections) %7B%0A                    transform(visualModel, connection);%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void transform(Model model, Node node) %7B%0A        if (node instanceof VisualConnection) %7B%0A            VisualConnection connection = (VisualConnection) node;%0A
     
@@ -3634,32 +3634,24 @@
             
-    
-    
 connection.s
@@ -3697,40 +3697,8 @@
 E);%0A
-                %7D%0A            %7D%0A
     

Add straighten connection tool into popup menu