001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.x.form.support;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Component;
017: import java.awt.Dimension;
018: import java.awt.event.MouseAdapter;
019: import java.awt.event.MouseEvent;
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.List;
023:
024: import javax.swing.BorderFactory;
025: import javax.swing.DefaultListModel;
026: import javax.swing.JCheckBox;
027: import javax.swing.JList;
028: import javax.swing.JPanel;
029: import javax.swing.JScrollPane;
030: import javax.swing.ListCellRenderer;
031: import javax.swing.ListSelectionModel;
032:
033: import com.eviware.x.form.XFormOptionsField;
034: import com.eviware.x.impl.swing.AbstractSwingXFormField;
035:
036: /**
037: * Swing-Specific multi-select list
038: *
039: * @author ole.matzura
040: */
041:
042: public class XFormMultiSelectList extends
043: AbstractSwingXFormField<JPanel> implements XFormOptionsField {
044: private JList list;
045: private DefaultListModel listModel;
046: private List<Boolean> selected = new ArrayList<Boolean>();
047:
048: public XFormMultiSelectList(String[] values) {
049: super (new JPanel(new BorderLayout()));
050:
051: listModel = new DefaultListModel();
052: for (String value : values) {
053: selected.add(false);
054: listModel.addElement(value);
055: }
056:
057: list = new JList(listModel);
058: list.setCellRenderer(new CheckListCellRenderer());
059: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
060: list.addMouseListener(new MouseAdapter() {
061: public void mousePressed(MouseEvent e) {
062: int index = list.locationToIndex(e.getPoint());
063:
064: if (index != -1) {
065: selected.set(index, !selected.get(index));
066: list.repaint();
067: }
068: }
069: });
070:
071: getComponent().add(new JScrollPane(list), BorderLayout.CENTER);
072: getComponent().setSize(new Dimension(400, 120));
073: getComponent().setMaximumSize(new Dimension(400, 120));
074: getComponent().setPreferredSize(new Dimension(400, 120));
075: getComponent().setMinimumSize(new Dimension(400, 120));
076: }
077:
078: public String getValue() {
079: return (String) list.getSelectedValue();
080: }
081:
082: public void setValue(String value) {
083: int index = listModel.indexOf(value);
084: selected.set(index, true);
085: list.setSelectedIndex(index);
086: }
087:
088: public void addItem(String value) {
089: listModel.addElement(value);
090: selected.add(false);
091: }
092:
093: public String[] getOptions() {
094: String[] options = new String[listModel.size()];
095: for (int c = 0; c < options.length; c++)
096: options[c] = (String) listModel.get(c);
097: return options;
098: }
099:
100: public String[] getSelectedOptions() {
101: List<String> result = new ArrayList<String>();
102:
103: for (int c = 0; c < selected.size(); c++) {
104: if (selected.get(c))
105: result.add((String) listModel.get(c));
106: }
107:
108: return result.toArray(new String[result.size()]);
109: }
110:
111: public void setOptions(Object[] values) {
112: listModel.clear();
113: selected.clear();
114: for (Object value : values) {
115: selected.add(false);
116: listModel.addElement(value);
117: }
118: }
119:
120: public class CheckListCellRenderer extends JCheckBox implements
121: ListCellRenderer {
122: public CheckListCellRenderer() {
123: setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
124: }
125:
126: public Component getListCellRendererComponent(JList list,
127: Object value, int index, boolean isSelected,
128: boolean cellHasFocus) {
129: setText(value.toString());
130: setSelected(selected.get(index));
131:
132: if (isSelected) {
133: setBackground(list.getSelectionBackground());
134: setForeground(list.getSelectionForeground());
135: } else {
136: setBackground(list.getBackground());
137: setForeground(list.getForeground());
138: }
139:
140: return this ;
141: }
142: }
143:
144: public void setSelectedOptions(String[] options) {
145: List<String> asList = Arrays.asList(options);
146:
147: for (int c = 0; c < selected.size(); c++) {
148: selected.set(c, asList.contains(listModel.get(c)));
149: }
150:
151: list.repaint();
152: }
153: }
|