All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jlibs.nblr.editor.layout.RuleLayout Maven / Gradle / Ivy

/**
 * Copyright 2015 Santhosh Kumar Tekuri
 *
 * The JLibs authors license this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package jlibs.nblr.editor.layout;

/**
 * @author Santhosh Kumar T
 */

import jlibs.core.lang.NotImplementedException;
import jlibs.nblr.editor.RuleScene;
import jlibs.nblr.rules.Edge;
import jlibs.nblr.rules.Node;
import org.netbeans.api.visual.graph.layout.GraphLayout;
import org.netbeans.api.visual.graph.layout.UniversalGraph;
import org.netbeans.api.visual.widget.LabelWidget;
import org.netbeans.api.visual.widget.Widget;

import java.awt.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * @author Santhosh Kumar T
 */
public class RuleLayout extends GraphLayout{
    private boolean animated;
    public static final int originX = 100;
    public static final int originY = 50;
    public static final int horizontalGap = 100;
    public static final int verticalGap = 25;

    public RuleLayout(boolean animated){
        this.animated = animated;
    }

    private List> coordinates;

    @Override
    protected void performGraphLayout(UniversalGraph graph){
        RuleScene scene = (RuleScene)graph.getScene();
        if(scene.getRule()==null)
            return;

        coordinates = scene.getRule().node.coordinates();

        List columns = new ArrayList();
        columns.add(originX);
        for(List nodes: coordinates){
            for(int col=1; col0){
                for(Node node: coordinates.get(row-1)){
                    if(node!=null){
                        bottom = Math.max(bottom, node.conLeftBottom);
                        bottom = Math.max(bottom, node.conRightBottom);
                    }
                }
            }

            int height = top+bottom;
            rows[row] = 50*height;
            if(row>0)
                rows[row] += rows[row-1];
            if(height==0)
                rows[row] += originY;
            else
                rows[row] += verticalGap;
        }

        for(Node node: graph.getNodes()){
            Widget widget = scene.findWidget(node);
            Point location = new Point(columns.get(node.col), rows[node.row]/*originY + node.row * verticalGap*/);
            if(animated)
                scene.getSceneAnimator().animatePreferredLocation(widget, location);
            else
                widget.setPreferredLocation(location);
        }
    }

    private void analyzeEdges(UniversalGraph graph, int colCount){
        for(Node node: graph.getNodes()){
            node.conLeft        = false;
            node.conRight       = false;
            node.conTop         = false;
            node.conBottom      = false;
            node.conLeftTop     = 0;
            node.conLeftBottom  = 0;
            node.conRightTop    = 0;
            node.conRightBottom = 0;
        }

        int rowCount = coordinates.size();
        ArrayList edgesList = new ArrayList(graph.getEdges());
        for(int row=0; row edges = edgesList.iterator();
                while(edges.hasNext()){
                    Edge edge = edges.next();
                    if(edge.forward() && edge.sameRow(row) && edge.jump()==jump){
                        analyzeEdge(row, jump, edge);
                        edges.remove();
                    }
                }
                edges = edgesList.iterator();
                while(edges.hasNext()){
                    Edge edge = edges.next();
                    if(edge.backward() && edge.sameRow(row) && edge.jump()==jump){
                        analyzeEdge(row, jump, edge);
                        edges.remove();
                    }
                }
            }
        }
    }

    private void analyzeEdge(int row, int jump, Edge edge){
        Node minNode = edge.min();
        Node maxNode = edge.max();
        if(jump==0 && !minNode.conRight && !maxNode.conLeft){
            edge.con = 0;
            minNode.conRight = maxNode.conLeft = true;
        }else{
            boolean topPossible = !minNode.conTop && !maxNode.conTop;
            if(topPossible){
                int topHeight = Math.max(minNode.conRightTop, maxNode.conLeftTop);
                List nodes = coordinates.get(row);
                for(int i=minNode.col+1; i nodes = coordinates.get(row);
                for(int i=minNode.col+1; i neUniversalGraph, Collection ns){
        throw new NotImplementedException();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy