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.BorderLayout; 027 import java.awt.event.ActionEvent; 028 import java.awt.event.ActionListener; 029 030 import javax.swing.BorderFactory; 031 import javax.swing.DefaultComboBoxModel; 032 import javax.swing.JCheckBox; 033 import javax.swing.JComboBox; 034 import javax.swing.JPanel; 035 036 import se.liu.ida.critiquer.activities.Activity; 037 import se.liu.ida.critiquer.mics.ReferenceHolder; 038 /** 039 * 040 * This parameter determines a commander who is responsible for carrying out one or more activities using one or more resources. 041 * All activities share a global list of commanders that is updated whenever new names are typed in the editable combo box. 042 * 043 * @author olale 044 * 045 */ 046 public class CommanderParameter extends AbstractTaskViewParameter<JPanel, Commander> implements CommanderAddedListener { 047 048 private transient JComboBox commanderList = null; 049 private boolean enabled; 050 051 public CommanderParameter(String name, Activity activity) { 052 super(name, activity); 053 initComponent(); 054 } 055 056 public boolean isEnabled() { 057 return enabled && hasValue(); 058 } 059 060 /** 061 * 062 */ 063 private static final long serialVersionUID = 1L; 064 private void setCommanderEnabled(boolean enabled) { 065 this.enabled = enabled; 066 commanderList.setEnabled(enabled); 067 } 068 069 @Override 070 public void initComponent() { 071 component = new JPanel(new BorderLayout()); 072 component.setBorder(BorderFactory.createTitledBorder(getName())); 073 initCommanderList(); 074 final JCheckBox agentAssignableChooser = new JCheckBox("Assign commander"); 075 agentAssignableChooser.setSelected(enabled); 076 agentAssignableChooser.addActionListener(new ActionListener() { 077 078 public void actionPerformed(ActionEvent e) { 079 if (agentAssignableChooser.isSelected()) { 080 setCommanderEnabled(true); 081 } else { 082 setCommanderEnabled(false); 083 } 084 valueChanged(); 085 } 086 087 088 089 }); 090 component.add(agentAssignableChooser,BorderLayout.WEST); 091 component.add(commanderList,BorderLayout.CENTER); 092 093 } 094 /** 095 * 096 * Create the list component used to select commanders for activities. 097 * 098 */ 099 private void initCommanderList() { 100 commanderList = new JComboBox(ReferenceHolder.commanders.getCommanders().keySet().toArray(new String[] {})); 101 commanderList.setEnabled(enabled); 102 commanderList.setEditable(true); 103 if (hasValue()) { 104 commanderList.setSelectedItem(getValue().getName()); 105 } 106 commanderList.addActionListener(new ActionListener() { 107 108 public void actionPerformed(ActionEvent e) { 109 String commanderName = (String) commanderList.getSelectedItem(); 110 if (! ReferenceHolder.commanders.getCommanders().containsKey(commanderName)) { 111 ReferenceHolder.commanders.addCommander(new Commander(commanderName)); 112 113 } 114 if (hasValue()) { 115 getValue().removeActivity(getActivity()); 116 } 117 Commander commander = ReferenceHolder.commanders.getCommanders().get(commanderName); 118 setValue(commander); 119 commander.addActivity(getActivity()); 120 } 121 122 }); 123 } 124 125 /** 126 * @return Returns the commanderList. 127 */ 128 public JComboBox getCommanderList() { 129 if (commanderList==null) { 130 initCommanderList(); 131 } 132 return commanderList; 133 } 134 135 /** 136 * 137 * When the user has typed a new unique name of a commander, update the model used by the list component. 138 * 139 * @see se.liu.ida.critiquer.activities.parameters.CommanderAddedListener#commanderAdded(se.liu.ida.critiquer.activities.parameters.Commander) 140 */ 141 public void commanderAdded(Commander commander) { 142 commanderList.setModel(new DefaultComboBoxModel(ReferenceHolder.commanders.getCommanders().keySet().toArray(new String[] {}))); 143 } 144 145 146 }