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.awt.Color;
027    import java.awt.Container;
028    import java.awt.Graphics2D;
029    import java.awt.Point;
030    import java.awt.geom.AffineTransform;
031    import java.util.ArrayList;
032    import java.util.HashSet;
033    
034    import se.liu.ida.critiquer.activities.Activity;
035    import se.liu.ida.critiquer.activities.ActivityUtils;
036    import se.liu.ida.critiquer.activities.parameters.AgentParameter;
037    import se.liu.ida.critiquer.activities.parameters.Parameter;
038    import se.liu.ida.critiquer.gui.ResourceView;
039    import se.liu.ida.critiquer.gui.View;
040    import se.liu.ida.critiquer.mics.Utils;
041    import se.liu.ida.critiquer.resources.Agent;
042    
043    public class NonSuitableAgent extends StandardConstraint {
044    
045            /**
046             * 
047             */
048            private static final long       serialVersionUID        = 1L;
049            
050            public <T> void paramChanged(Activity activity, Parameter<T> p) {
051                    
052            }
053    
054    
055            /* (non-Javadoc)
056             * @see se.liu.ida.critiquer.constraints.StandardConstraint#initApplicableViews()
057             */
058            @Override
059            protected void initApplicableViews() {
060                    applicableViews.add(ResourceView.class);
061            }
062    
063    
064            public void viewUpdated(View v, Graphics2D g2) {
065                    if (v instanceof ResourceView) {
066                            ResourceView resourceView = (ResourceView) v;
067                            renderCritiqueResourceView(resourceView, g2);
068                    } else {
069                            System.err.println("This critic is inapplicable for view "+v.getClass().getSimpleName());
070                    }
071                    
072            }
073            
074            private void renderCritiqueResourceView(ResourceView resourceView, Graphics2D g2) {
075                    Point resourcePanePosition = resourceView.getResourcePanel()
076                                    .getLocation();
077                    AffineTransform resourcePaneTranslator = AffineTransform
078                                    .getTranslateInstance(resourcePanePosition.x,
079                                                    resourcePanePosition.y);
080    
081                    ArrayList<Activity> selectedActivities = resourceView.getEvaluationActivities();
082                    if (selectedActivities!=null) {
083                            for (Activity selectedActivity : selectedActivities) {
084                                    AgentParameter parameter = ActivityUtils
085                                    .getMainAgentParameter(selectedActivity);
086                                    if (parameter!=null) {
087                                            Point activityLocation = Utils.getCenter(resourceView.getActivityArea(selectedActivity));
088                                            resourcePaneTranslator.transform(activityLocation,activityLocation);
089                                            
090                                            Point agentPanelLocation = parameter.getAgentPanel().getLocation();
091                                            Container panel = parameter.getAgentPanel().getParent();
092                                            AffineTransform agentPanelTranslator = AffineTransform.getTranslateInstance(agentPanelLocation.x,
093                                                                                                                        agentPanelLocation.y);
094                                            /**
095                                             * If the agent panel lies nested within several panels in the resource view, we must work our way up
096                                             */
097                                            while (panel!=resourceView) {
098                                                    panel=panel.getParent();
099                                                    agentPanelTranslator.preConcatenate(AffineTransform.getTranslateInstance(panel.getX(), panel.getY()));
100                                            }
101                                            
102                                            HashSet<Agent> selectedAgents = ActivityUtils.getAgentsInActivity(selectedActivity);
103                                            for (Agent agent : selectedAgents) {
104                                                    Point agentLabelLocation = parameter
105                                                    .getAgentLocation(agent);
106                                                    agentPanelTranslator.transform(agentLabelLocation, agentLabelLocation);
107                                                    Color color = g2.getColor();
108                                                    g2.setColor(Agent.getSuitability(agent, selectedActivity)
109                                                                    .getColor());
110                                                    g2.drawLine(activityLocation.x,
111                                                                    activityLocation.y,
112                                                                    agentLabelLocation.x,
113                                                                    agentLabelLocation.y);
114                                                    g2.setColor(color);
115                                            }
116                                    }       
117                            }
118                    } 
119            }
120            
121    
122    
123    
124    
125            public String getDescription() {
126                    String nl = System.getProperty("line.separator");
127                    return "Draws lines between selected agents and their"+nl+
128                    " respective activities with a color depending"+nl+
129                    "on how well suited they are for the activity."+nl+
130                    "If they're well suited the color is green.";
131            }
132    
133    }