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.util.HashSet; 027 import java.util.Vector; 028 029 import javax.swing.BorderFactory; 030 import javax.swing.JList; 031 import javax.swing.JPanel; 032 import javax.swing.event.ListSelectionEvent; 033 import javax.swing.event.ListSelectionListener; 034 035 import se.liu.ida.critiquer.activities.Activity; 036 037 038 public class SelectionParameter<T> extends ActivityParameter<HashSet<T>> implements TaskViewParameter<JPanel, T> { 039 040 /* 041 * Agentparameter and locationparameter should inherit from this one to have common functionality for 042 * selecting from a set of choices 043 * */ 044 045 /* (non-Javadoc) 046 * @see critiquer.ActivityParameter#getComponent() 047 */ 048 049 /** 050 * 051 */ 052 private static final long serialVersionUID = 1L; 053 054 055 056 public SelectionParameter(String name,Activity activity) { 057 super(name,activity); 058 value = new HashSet<T>(); 059 } 060 061 private Vector<T> listObjects; 062 protected JList list; 063 private JPanel component; 064 065 066 067 protected void createList(final Vector<T> listObjects) { 068 final HashSet<T> selectedObjs = new HashSet<T>(); 069 component = new JPanel(); 070 component.setBorder(BorderFactory.createTitledBorder(toString())); 071 072 list = new JList(listObjects); 073 list.addListSelectionListener(new ListSelectionListener(){ 074 075 public void valueChanged(ListSelectionEvent e) { 076 if (! e.getValueIsAdjusting()) { 077 int[] selected = list.getSelectedIndices(); 078 selectedObjs.clear(); 079 for (int i = 0; i < selected.length; i++) { 080 T elt = listObjects.get(selected[i]); 081 selectedObjs.add(elt); 082 083 } 084 setValue(selectedObjs); 085 } 086 087 } 088 089 }); 090 component.add(list); 091 092 } 093 094 /* (non-Javadoc) 095 * @see se.liu.ida.critiquer.activities.parameters.TaskViewParameter#getComponent() 096 */ 097 public JPanel getComponent() { 098 if (component == null) { 099 initComponent(); 100 } 101 return component; 102 } 103 104 public void initComponent() { 105 createList(listObjects); 106 } 107 108 109 110 @Override 111 public String toString() { 112 String s=""; 113 for (T selectedElement : getValue()) { 114 s+=selectedElement.toString(); 115 } 116 return s; 117 } 118 119 /** 120 * @return Returns the list. 121 */ 122 public JList getList() { 123 return list; 124 } 125 126 }