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.constraints;
025    
026    import java.util.ArrayList;
027    import java.util.HashSet;
028    import java.util.List;
029    
030    import se.liu.ida.critiquer.mics.Comparer;
031    import se.liu.ida.critiquer.mics.Utils;
032    
033    /**
034     * 
035     * <p>A map of visual constraints, with flags indicating if they are active or not
036     * </p> 
037     * 
038     * <p>We actually only care about the ViewRenderingListener constraints here,
039     * the other ones can do their job without knowing about the view rendering
040     * </p>  
041     *  
042     *  */
043    
044    public class VisualConstraints {
045    
046            private static HashSet<VisualConstraint> constraints = new HashSet<VisualConstraint>();
047            
048            /**
049             * Toggles constraints on and off, though they are still active in the sense that they are
050             * updated with information from the activities
051             * */
052            public static void setConstraintEnabled(Class constraintClass,boolean enabled){
053                    for (VisualConstraint c : constraints) {
054                            if (constraintClass.isInstance(c)) {
055                                    c.setActive(enabled);
056                            }
057                    }
058                    
059            }
060            /**
061             * Retrieve a singleton constraint given its class
062             * 
063             * @param c the class of the constraint
064             * @return the singleton constraint
065             */
066            public static VisualConstraint getConstraintByClass(final Class c) {
067                    return Utils.findFirst(constraints, new Comparer<VisualConstraint>() {
068    
069                            public boolean check(VisualConstraint constraint) {
070                                    return c.isInstance(constraint);
071                            }
072                            
073                    });
074            }
075            
076            public static void addConstraint(VisualConstraint c){
077                    constraints.add(c);     
078            }
079            
080            public static void removeConstraint(VisualConstraint c) {
081                    constraints.remove(c);
082            }
083            /**
084             * Get a list of those constraints that are currently displaying visual information.
085             * 
086             * @return a list of active constraints
087             */
088            public static List<VisualConstraint> getActiveConstraints(){
089                    ArrayList<VisualConstraint> activeConstraints = new ArrayList<VisualConstraint>();
090                    for (VisualConstraint constraint : constraints) {
091                            if (constraint.isActive()) {
092                                    activeConstraints.add(constraint);
093                            }
094                    }
095                    return activeConstraints;
096            }
097    
098            
099    }