view src/MPipeline/src/mpipeline/Utils.java @ 27:29276498ebdb database-connection-manager

Remove LTPDARepositoryManager implementation * * * Remove GUI helper
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents f0afece42f48
children
line wrap: on
line source

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mpipeline;

import java.awt.Frame;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;

/**
 * A utility class of static methods.
 * 
 * @author hewitson
 */
public class Utils implements Serializable {



    public final static String  removeFileName(String   path)
     {

         // Find the last path seperator

         int pos = path.lastIndexOf(File.pathSeparator);
         if (pos != -1)
             return path.substring(0, pos);

         // Return an empty string, no path seperators

         return "";
     }
    /**
     * 
     * @return
     */
    public static MainWindow getMainWindow() {

        Frame[] frames = Frame.getFrames();
        for (int k = 0; k < frames.length; k++) {
            Frame fr = frames[k];
            if (fr instanceof MainWindow) {
                return (MainWindow) fr;
            }
        }

        return null;
    }

    /**
     * Get the extension of a file.
     * @param f
     * @return
     */
    public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 && i < s.length() - 1) {
            ext = s.substring(i + 1).toLowerCase();
        }
        return ext;
    }


    /**
     *
     * @param s1
     * @param s2
     * @return
     */
    public static String makeDiagramTitle(String s1, String s2){
        if (s1==null || s1.equals("")){
            return s2;
        } else {
            return s1 + " / " + s2;
        }
    }

    /**
     * Return a set of control points suitable for drawing a pipe
     * between the source and destination nodes. The 'midoffset' parameter
     * puts an x-offset on the center portion of the pipe. The 'offset'
     * parameter specifys the overshoot for pipes that need to go around
     * blocks.
     * 
     * @param src
     * @param dst
     * @param midoffset
     * @param offset
     * @return
     */
    public static ArrayList<Point> getControlPointsFromNodes(MNode src, MNode dst, int midoffset, int offset) {
        ArrayList<Point> points = new ArrayList<Point>();

        int R = dst.getRadius();
        int w = 0;
        int h = 0;
        int sx = src.getX();
        int sy = src.getY();
        int dsx = dst.getX();
        int dsy = dst.getY();
        
        if (dsx > sx) {
            // case 1 or 2
            if (dsy > sy) {
                // case 1  
                w = dsx + dst.getWidth() - sx;
                h = dsy + dst.getHeight() - sy;
                // set control nodes
                points.clear();
                points.add(new Point(R, R));
                points.add(new Point(Math.round(w/2) + midoffset, R));
                points.add(new Point(Math.round(w/2) + midoffset, h - R));
                points.add(new Point(w - R, h - R));
            } else {
                // case 2 
                w = dsx - sx + 2 * R;
                h = sy - dsy + 2 * R;
                // set control nodes
                points.clear();
                points.add(new Point(R, h - R));
                points.add(new Point(Math.round(w/2) + midoffset, h - R));
                points.add(new Point(Math.round(w/2) + midoffset, R));
                points.add(new Point(w - R, R));
            }

        } else {
            // case 3
            if (dsy > sy) {
                w = sx + 2*R + 2*offset - dsx;
                h = dsy + 2*R - sy;
                // set control points
                points.clear();
                points.add(new Point(w - offset - R, R));
                points.add(new Point(w - R + midoffset, R));
                points.add(new Point(w - R + midoffset, midoffset + Math.round(h/2)));
                points.add(new Point(R - midoffset, midoffset + Math.round(h/2)));
                points.add(new Point(R - midoffset, h - R));
                points.add(new Point(offset + R, h - R));

            } else {
                w = sx + 2 * R + 2*offset - dsx;
                h = sy + 2 * R - dsy;
                // set control nodes
                points.clear();
                points.add(new Point(w - offset - R, h - R));
                points.add(new Point(w - R + midoffset, h - R));
                points.add(new Point(w - R + midoffset, midoffset + Math.round(h/2)));
                points.add(new Point(R - midoffset, midoffset + Math.round(h/2)));
                points.add(new Point(R - midoffset, R));
                points.add(new Point(R + offset, R));
            }
        }

        return points;
    }

    /**
     * Return a set of control points suitable for drawing a pipe
     * between the source and destination points. The 'midoffset' parameter
     * puts an x-offset on the center portion of the pipe. The 'offset'
     * parameter specifys the overshoot for pipes that need to go around
     * blocks.
     * 
     * @param src
     * @param dst
     * @param midoffset
     * @param offset
     * @return
     */
    public static ArrayList<Point> getControlPointsFromPoints(Point src, Point dst, int midoffset, int offset) {
        ArrayList<Point> points = new ArrayList<Point>();

        int w = 0;
        int h = 0;
        int sx = src.x;
        int sy = src.y;
        int dx = dst.x;
        int dy = dst.y;
        if (dx > sx) {
            w = dx - sx;
            h = dy - sy;
            // set control nodes
            points.clear();
            points.add(new Point(sx, sy));
            points.add(new Point(sx + Math.round(w/2) + midoffset, sy));
            points.add(new Point(sx + Math.round(w/2) + midoffset, dy));
            points.add(new Point(dx, dy));
        } else {
            h = (dy - sy);
            // set control points
            points.clear();
            points.add(new Point(sx, sy));
            points.add(new Point(sx + offset + midoffset, sy));
            points.add(new Point(sx + offset + midoffset, sy + Math.round(h/2)));
            points.add(new Point(dx - offset - midoffset, sy + Math.round(h/2)));
            points.add(new Point(dx - offset - midoffset, dy));
            points.add(new Point(dx, dy));
        }

        return points;
    }

    /**
     * Return a rectangle that bounds a pipe between two nodes. The 'offset'
     * parameter specifys the overshoot for pipes that need to go around
     * blocks.
     * 
     * @param src
     * @param dst
     * @param offset 
     * @return
     */
    public static Rectangle getBoundsFromNodes(MNode src, MNode dst, int offset) {

        int R = dst.getRadius();
        int x = 0;
        int y = 0;
        int w = 0;
        int h = 0;
        int sx = src.getX();
        int sy = src.getY();
        int dsx = dst.getX();
        int dsy = dst.getY();
        
        if (dsx > sx) {
            // case 1 or 2
            if (dsy > sy) {
                // case 1  
                x = sx;
                y = sy;
                w = dsx + dst.getWidth() - sx;
                h = dsy + dst.getHeight() - sy;
            } else {
                // case 2 
                x = sx;
                y = dsy;
                w = dsx - sx + 2 * R;
                h = sy - dsy + 2 * R;
            }

        } else {
            // case 3
            if (dsy > sy) {
                x = dsx - offset;
                y = sy;
                w = sx + offset + 2 * R - x;
                h = dsy + 2 * R - y;
            } else {
                x = dsx - offset;
                y = dsy;
                w = sx + 2 * R + offset - x;
                h = sy + 2 * R - y;
            }
        }

        return new Rectangle(x, y, w, h);
    }

    /**
     *
     * @param input
     * @return
     */
    public static boolean isInteger(String input) {

//        System.out.println("Checking if " + input + " is an integer");

        try {
            Integer.parseInt(input);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     *
     * @param bname
     * @return
     */
    public static String incrementName(String bname) {


        // look for signature _[0-9]+

        int idx = bname.lastIndexOf("_");
        int sval = -1;
        if (isInteger(bname.substring(idx+1))){
            sval = Integer.parseInt(bname.substring(idx+1));
        }

//        System.out.println("string has end value: " + sval);

        if (sval > 0){
            sval++;
            bname = bname.substring(0, idx) + "_" + sval;
        }

//        System.out.println("updated string to " + bname);


        return bname;
    }

    /**
     *
     * @param name
     * @return
     */
    public static String checkName(String name) {
        String myName = name.replaceAll("[^\\sa-zA-Z0-9]+", "_");
        myName = myName.replaceAll("^_+", ""); // remove _ from beginning of string
        myName = myName.replaceAll("_+$", "");// remove _ from end of string
        if (!myName.equals(name)) {
            System.err.println("Some not allowed characters were replaced by _");
        }
        return myName;
    }
}