001    /**
002     * planningtool - A Planning Tool with Critiquing Support.
003     * 
004     * Copyright (C) 2006 olale
005    
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU General Public License
008     * as published by the Free Software Foundation; either version 2
009     * of the License, or (at your option) any later version.
010    
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015    
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019    
020     * Contact information: 
021     * E-mail: olale@ida.liu.se
022     *         olale@lysator.liu.se
023     */
024    package se.liu.ida.critiquer.mics;
025    
026    import java.awt.Point;
027    import java.awt.Rectangle;
028    import java.awt.Shape;
029    import java.awt.geom.Rectangle2D;
030    import java.util.ArrayList;
031    import java.util.Arrays;
032    import java.util.Collection;
033    import java.util.List;
034    import java.util.Vector;
035    
036    public class Utils<T> {
037    
038        /**
039         * Returns a point based on (x, y) but constrained to be within the bounds
040         * of the given rectangle.  Borrowed from ChartPanel in JFreeChart.
041         * 
042         * @param x  the x-coordinate.
043         * @param y  the y-coordinate.
044         * @param area  the rectangle (<code>null</code> not permitted).
045         * 
046         * @return A point within the rectangle.
047         */
048        public Point getPointInRectangle(int x, int y, Rectangle2D area) {
049            x = (int) Math.max(
050                Math.ceil(area.getMinX()), Math.min(x, Math.floor(area.getMaxX()))
051            );   
052            y = (int) Math.max(
053                Math.ceil(area.getMinY()), Math.min(y, Math.floor(area.getMaxY()))
054            );
055            return new Point(x, y);
056        }
057    
058            
059            public static Point getCenter(Shape shape){
060                    Point p = new Point();
061                    Rectangle rect = shape.getBounds();
062                    p.x=(int) rect.getCenterX();
063                    p.y=(int) rect.getCenterY();
064                    return p;
065            }
066    
067            public static List<Class> getAllInterfaces(Class constraintClass) {
068                    List<Class> l = new ArrayList<Class>();
069                    Class c = constraintClass;
070                    while (c != null) {
071                            List<Class> interfaceList = Arrays.asList(c.getInterfaces());
072                            l.addAll(interfaceList);
073                            c=c.getSuperclass();
074                    }
075                    return l;
076            }
077            
078            public static <T> Vector<T> toVector(Collection<T> c) {
079                    Vector<T> vector = new Vector<T>();
080                    for (T element : c) {
081                            vector.add(element);
082                    }
083                    return vector;
084            }
085    
086            public static <T> Vector<T> toVector(T[] c) {
087                    Vector<T> vector = new Vector<T>();
088                    for (int i = 0; i < c.length; i++) {
089                            T element = c[i];                       
090                            vector.add(element);
091                    }
092                    
093                    return vector;
094            }
095    
096            
097            public static <T> T findFirst(Collection<T> c,Comparer<T> comp) {
098                    for (T elt : c) {
099                            if (comp.check(elt)) {
100                                    return elt;
101                            }
102                    }
103                    return null;
104            }
105            
106            public static <T> ArrayList<T> findAll(Collection<T> c,Comparer<T> comp) {
107                    ArrayList<T> list = new ArrayList<T>();
108                    for (T elt : c) {
109                            if (comp.check(elt)) {
110                                    list.add(elt);
111                            }
112                    }
113                    return list;
114            }
115            
116    }