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.resources; 025 026 import java.awt.BorderLayout; 027 import java.awt.event.ItemEvent; 028 import java.awt.event.ItemListener; 029 import java.io.Serializable; 030 import java.util.Vector; 031 032 import javax.swing.JComboBox; 033 import javax.swing.JLabel; 034 import javax.swing.JPanel; 035 036 import se.liu.ida.critiquer.scenarios.standard.AgentParameterName; 037 038 /** 039 * This class 040 */ 041 042 public class SingleSelectionParameter<T> implements Serializable,ParameterInAgent<T> { 043 044 private static final long serialVersionUID = 1L; 045 private T value; 046 private transient JPanel component; 047 private transient JComboBox selectionList; 048 private AgentParameterName name; 049 private transient JLabel nameLabel; 050 private Vector<T> range; 051 private Agent agent; 052 053 private ParameterInAgent.Type type = ParameterInAgent.Type.EXTERNAL; 054 055 public SingleSelectionParameter(AgentParameterName name,final Agent agent,Vector<T> range,T defaultObject) { 056 this.agent=agent; 057 this.name=name; 058 value = defaultObject; 059 060 if (! range.contains(defaultObject)) { 061 range.add(0, defaultObject); 062 } 063 this.range=range; 064 } 065 066 067 private void initComponents(String name, final Agent agent) { 068 component = new JPanel(new BorderLayout()); 069 nameLabel = new JLabel(name); 070 selectionList = new JComboBox(range); 071 component.add(nameLabel,BorderLayout.WEST); 072 component.add(selectionList,BorderLayout.CENTER); 073 selectionList.setSelectedItem(value); 074 selectionList.addItemListener(new ItemListener(){ 075 076 public void itemStateChanged(ItemEvent e) { 077 Object obj = selectionList.getSelectedItem(); 078 value = (T) obj; 079 agent.notifyAgentChangedListeners(); 080 } 081 082 }); 083 } 084 085 086 public void editEnabled(boolean enabled) { 087 selectionList.setEditable(enabled); 088 } 089 090 /** 091 * @return Returns the component. 092 */ 093 public JPanel getComponent() { 094 if (component==null) { 095 initComponents(getName().toString(), agent); 096 } 097 return component; 098 } 099 100 /** 101 * @return Returns the value. 102 */ 103 public T getValue() { 104 return value; 105 } 106 107 public AgentParameterName getName() { 108 return name; 109 } 110 111 /* (non-Javadoc) 112 * @see java.lang.Object#equals(java.lang.Object) 113 */ 114 @Override 115 public boolean equals(Object obj) { 116 boolean eq = false; 117 118 if (obj instanceof SingleSelectionParameter) { 119 SingleSelectionParameter param = (SingleSelectionParameter) obj; 120 if (param.getName().equals(getName())) { 121 eq=true; 122 } 123 } 124 125 return eq; 126 } 127 128 129 /** 130 * @return Returns the type. 131 */ 132 public ParameterInAgent.Type getType() { 133 return type; 134 } 135 136 137 /** 138 * @param type The type to set. 139 */ 140 public void setType(ParameterInAgent.Type type) { 141 this.type = type; 142 } 143 144 145 146 }