001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: ListEditor.java,v $ 023 Revision 1.2 2004/05/31 07:47:07 markl 024 completed implementation 025 026 Revision 1.1 2004/03/16 06:42:09 markl 027 new class 028 ---------------------------------------------------------------------------- 029*/ 030 031package kiwi.ui; 032 033import java.awt.*; 034import java.awt.event.*; 035import javax.swing.*; 036 037import kiwi.ui.model.*; 038import kiwi.util.*; 039 040/** A simple editor for entering a list of text items. The editor consists of 041 * an input field for entry of new items and a scrollable list for the display 042 * of currently entered items. Pressing the <i>Return</i> key in the text field 043 * accepts the input in the input field and adds it to the list. Selecting 044 * items in the list and pressing the <i>Delete</i> or <i>Backspace</i> key 045 * removes those items from the list. 046 * 047 * @author Mark Lindner 048 * @since Kiwi 2.0 049 */ 050 051public class ListEditor extends KPanel implements ActionListener 052 { 053 protected JList list; 054 protected DataField t_item; 055 private OrderedListModel _model; 056 private KButton b_add, b_remove; 057 058 /** Construct a new <code>ListEditor</code>. 059 * 060 * @param fieldWidth The width for the input field. 061 * @param maxItemLength The maximum input length, in characters. 062 * @param model The data model for the editor. 063 */ 064 065 public ListEditor(int fieldWidth, int maxItemLength, OrderedListModel model) 066 { 067 _model = model; 068 069 setLayout(new GridBagLayout()); 070 GridBagConstraints gbc = new GridBagConstraints(); 071 072 ResourceManager resmgr = KiwiUtils.getResourceManager(); 073 LocaleData loc = LocaleManager.getDefault().getLocaleData("KiwiDialogs"); 074 075 gbc.insets = KiwiUtils.firstInsets; 076 gbc.gridwidth = 1; 077 gbc.fill = gbc.HORIZONTAL; 078 gbc.anchor = gbc.NORTHWEST; 079 080 t_item = new DataField(fieldWidth); 081 t_item.setMaximumLength(maxItemLength); 082 t_item.addActionListener(this); 083 add(t_item, gbc); 084 085 b_add = new KButton(resmgr.getIcon("plus.gif")); 086 b_add.setToolTipText(loc.getMessage("kiwi.tooltip.add_item")); 087 b_add.addActionListener(this); 088 089 gbc.gridwidth = gbc.REMAINDER; 090 gbc.fill = gbc.NONE; 091 gbc.insets = KiwiUtils.lastInsets; 092 add(b_add, gbc); 093 094 list = new JList(_model); 095 096 list.addKeyListener(new KeyAdapter() 097 { 098 public void keyTyped(KeyEvent evt) 099 { 100 char code = evt.getKeyChar(); 101 if((code == KeyEvent.VK_DELETE) || (code == KeyEvent.VK_BACK_SPACE)) 102 removeItems(); 103 } 104 }); 105 106 KScrollPane sp = new KScrollPane(list); 107 sp.setOpaque(true); 108 sp.setBackground(Color.white); 109 Dimension sz = new Dimension(10, 125); 110 sp.setSize(sz); 111 sp.setPreferredSize(sz); 112 113 gbc.gridwidth = 1; 114 gbc.fill = gbc.BOTH; 115 gbc.insets = KiwiUtils.firstBottomInsets; 116 add(sp, gbc); 117 118 b_remove = new KButton(resmgr.getIcon("minus.gif")); 119 b_remove.setToolTipText(loc.getMessage("kiwi.tooltip.remove_sel_items")); 120 b_remove.addActionListener(this); 121 122 gbc.gridwidth = gbc.REMAINDER; 123 gbc.fill = gbc.NONE; 124 gbc.insets = KiwiUtils.lastBottomInsets; 125 add(b_remove, gbc); 126 } 127 128 /* 129 */ 130 131 private void addItem() 132 { 133 String item = t_item.getText().trim(); 134 if(! item.equals("")) 135 { 136 _model.addElement(item); 137 t_item.setText(null); 138 } 139 } 140 141 /* 142 */ 143 144 private void removeItems() 145 { 146 int sel[] = list.getSelectedIndices(); 147 for(int i = sel.length - 1; i >= 0; i--) 148 _model.removeElementAt(sel[i]); 149 } 150 151 /** Set the text in the input field. */ 152 153 public void setText(String text) 154 { 155 t_item.setText(text); 156 } 157 158 /** Get the text in the input field. */ 159 160 public String getText() 161 { 162 return(t_item.getText()); 163 } 164 165 /** Set the cell renderer for the list. 166 * 167 * @param renderer The new cell renderer. 168 */ 169 170 public void setCellRenderer(ListCellRenderer renderer) 171 { 172 list.setCellRenderer(renderer); 173 } 174 175 /** Get the <code>JList</code> component for this <code>ListEditor</code>. 176 */ 177 178 public JList getJList() 179 { 180 return(list); 181 } 182 183 /** 184 */ 185 186 public void actionPerformed(ActionEvent evt) 187 { 188 Object o = evt.getSource(); 189 190 if((o == t_item) || (o == b_add)) 191 addItem(); 192 else if(o == b_remove) 193 removeItems(); 194 } 195 196 } 197 198/* end of source file */