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.activities.parameters; 025 026 import java.awt.Component; 027 import java.awt.GridLayout; 028 import java.awt.Point; 029 import java.awt.Rectangle; 030 import java.io.Serializable; 031 import java.util.HashSet; 032 import java.util.Vector; 033 034 import javax.swing.JPanel; 035 036 import se.liu.ida.critiquer.activities.Activity; 037 import se.liu.ida.critiquer.gui.AgentLabel; 038 import se.liu.ida.critiquer.mics.ReferenceHolder; 039 import se.liu.ida.critiquer.resources.Agent; 040 041 /** 042 * 043 * One or several agents that are supposed to perform some action. This 044 * parameter is connected to a Commander, which in turn is connected to one or 045 * several activities through CommanderParameters of the activities. 046 * 047 * This parameter is special in that it doesn't contain a value in itself but 048 * relies on a mapping between activities and agents that the commander has. 049 * 050 */ 051 052 public class AgentParameter implements AgentChangedListener, Parameter<HashSet<Agent>>, Serializable { 053 054 /** 055 * 056 */ 057 private static final long serialVersionUID = 1L; 058 059 private transient JPanel pane; 060 061 private Commander commander; 062 063 public JPanel getAgentPanel() { 064 if (pane == null) { 065 pane = new JPanel(new GridLayout(1, 0)); 066 for (Agent agent : getAllAgents()) { 067 AgentLabel label = agent.getLabel(this); 068 pane.add(label); 069 } 070 } 071 return pane; 072 } 073 074 public Point getAgentLocation(Agent agent) { 075 Component c = null; 076 Point center = new Point(); 077 try { 078 Vector<Agent> allAgents = getAllAgents(); 079 c = getAgentPanel().getComponent(allAgents.indexOf(agent)); 080 Rectangle rect = c.getBounds(); 081 center.x = (int) rect.getCenterX(); 082 center.y = (int) rect.getCenterY(); 083 084 } catch (Exception e) { 085 086 System.err.println("No label for agent " + agent); 087 e.printStackTrace(); 088 } 089 return center; 090 } 091 092 /* 093 * 094 * 095 * @see se.liu.ida.critiquer.activities.parameters.ActivityParameter#getValue() 096 */ 097 public HashSet<Agent> getValue() { 098 return null; 099 } 100 101 /* 102 * (non-Javadoc) 103 * 104 * @see se.liu.ida.critiquer.activities.parameters.ActivityParameter#getValue() 105 */ 106 public boolean hasValue() { 107 return !commander.getSubordinates().isEmpty(); 108 } 109 110 /** 111 * 112 * Check if it is ok to add this agent to the list of agents and do so if 113 * possible. 114 * 115 * @param a 116 * @return true iff the agent can be selected. 117 * 118 */ 119 120 public boolean selectAgent(Agent a) { 121 HashSet<Agent> agents = (HashSet<Agent>) commander.getSubordinates().clone(); 122 agents.add(a); 123 boolean valueOk = Activity.isConsistent(null, this, agents); 124 if (valueOk) { 125 // Let the activity know when the agent is changed 126 127 a.addAgentChangedListener(this); 128 agentChanged(a); 129 commander.agentAdded(a); 130 } 131 return valueOk; 132 } 133 134 /** 135 * 136 * Check with the consistency checkers in Activity if it is OK to remove 137 * this agent and remove it in such case. 138 * 139 * @param agent 140 */ 141 public boolean deSelectAgent(Agent agent, Activity activity) { 142 HashSet<Agent> agents = (HashSet<Agent>) commander.getSubordinates().clone(); 143 agents.remove(agent); 144 boolean valueOk = Activity.isConsistent(activity, this, agents); 145 if (valueOk) { 146 147 agent.removeAgentChangedListener(this); 148 agentChanged(agent); 149 commander.agentRemoved(activity,agent); 150 } 151 return valueOk; 152 } 153 154 public AgentParameter(String string, Commander commander) { 155 this.commander = commander; 156 } 157 158 public String toString() { 159 return "Select agents"; 160 } 161 162 private Vector<Agent> getAllAgents() { 163 return ReferenceHolder.organizationModel.getAllAgents(); 164 } 165 166 /** 167 * Whenever parameters that describe the agent change, propagate that 168 * information to the activity containing this agent 169 */ 170 public void agentChanged(Agent agent) { 171 commander.notifyActivities(); 172 } 173 174 /** 175 * @return Returns the commander. 176 */ 177 public Commander getCommander() { 178 return commander; 179 } 180 181 }