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: UIElementChooser.java,v $ 023 Revision 1.6 2004/05/12 18:53:25 markl 024 comment block updates 025 026 Revision 1.5 2003/01/19 09:46:48 markl 027 replaced JScrollPane instances with KScrollPane. 028 029 Revision 1.4 2001/03/12 09:28:01 markl 030 Source code and Javadoc cleanup. 031 032 Revision 1.3 1999/07/06 09:17:31 markl 033 Added requestFocus() method. 034 035 Revision 1.2 1999/07/05 08:20:15 markl 036 Added methods to set/get the selection. 037 038 Revision 1.1 1999/05/10 09:12:18 markl 039 Initial revision 040 ---------------------------------------------------------------------------- 041*/ 042 043package kiwi.ui; 044 045import java.awt.*; 046import java.util.*; 047import javax.swing.*; 048import javax.swing.border.*; 049import javax.swing.event.*; 050 051/** A chooser component for <code>UIElement</code>s. The component consists of 052 * a scrollable list box to the right of a display area which is used to 053 * display the currently-selected element. 054 * 055 * <p><center> 056 * <img src="snapshot/UIElementChooser.gif"><br> 057 * <i>An example UIElementChooser (using a TextureViewer).</i> 058 * </center> 059 * 060 * @author Mark Lindner 061 */ 062 063public class UIElementChooser extends KPanel 064 { 065 private UIElementViewer elementViewer = null; 066 private DefaultListModel model; 067 private JList jlist; 068 private _ListSelectionListener selListener; 069 private KPanel viewpane; 070 private static final BevelBorder border 071 = new BevelBorder(BevelBorder.LOWERED); 072 073 /** Construct a new <code>UIElementChooser</code> with the specified viewer 074 * and element list. 075 * 076 * @param viewer A viewer component for displaying the element. 077 * @param list A Vector of elements that can be picked from. 078 */ 079 080 public UIElementChooser(UIElementViewer viewer, Vector list) 081 { 082 this(); 083 084 setElementViewer(viewer); 085 setElementList(list); 086 } 087 088 /** Construct a new <code>UIElementChooser</code>. 089 */ 090 091 public UIElementChooser() 092 { 093 setLayout(new BorderLayout(5, 5)); 094 095 jlist = new JList(); 096 jlist.setModel(model = new DefaultListModel()); 097 jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 098 add("East", new KScrollPane(jlist)); 099 100 selListener = new _ListSelectionListener(); 101 jlist.addListSelectionListener(selListener); 102 103 viewpane = new KPanel(); 104 viewpane.setLayout(new GridLayout(1, 0)); 105 viewpane.setBorder(border); 106 add("Center", viewpane); 107 } 108 109 /** Set the viewer component to be used to display elements. 110 * 111 * @param viewer The new viewer. 112 */ 113 114 public void setElementViewer(UIElementViewer viewer) 115 { 116 if(elementViewer != null) 117 viewpane.remove(elementViewer.getViewerComponent()); 118 119 JComponent c = viewer.getViewerComponent(); 120 viewpane.add(c); 121 elementViewer = viewer; 122 c.invalidate(); 123 validate(); 124 } 125 126 /** Set the list of elements to allow selection from. 127 * 128 * @param list The list of elements. 129 */ 130 131 public void setElementList(Vector list) 132 { 133 model.removeAllElements(); 134 135 Enumeration e = list.elements(); 136 while(e.hasMoreElements()) 137 model.addElement(e.nextElement()); 138 139 if(model.getSize() > 0) 140 jlist.setSelectedIndex(0); 141 142 if(elementViewer != null) 143 { 144 Object elem = null; 145 if(model.getSize() > 0) 146 elem = model.getElementAt(0); 147 } 148 } 149 150 /** Get the currently selected item. 151 * 152 * @return The currently selected <code>UIElement</code>, or 153 * <code>null</code> if there is no selection. 154 */ 155 156 public UIElement getSelectedItem() 157 { 158 return((UIElement)jlist.getSelectedValue()); 159 } 160 161 /** Set the currently selected item. 162 * 163 * @param element The <code>UIElement</code> to select. 164 */ 165 166 public void setSelectedItem(UIElement element) 167 { 168 jlist.setSelectedValue(element, true); 169 } 170 171 /** Set the currently selected item by name. 172 * 173 * @param name The name of the element to select; the name of each 174 * <code>UIElement</code> in the list is compared to the specified name; if 175 * the names match, the corresponding item in the list is selected. 176 * 177 * @see kiwi.ui.UIElement#getName 178 */ 179 180 public void setSelectedItem(String name) 181 { 182 for(int i = 0; i < model.getSize(); i++) 183 { 184 UIElement e = (UIElement)model.getElementAt(i); 185 if(e.getName().equals(name)) 186 jlist.setSelectedIndex(i); 187 } 188 } 189 190 /** Request focus for this component. 191 */ 192 193 public void requestFocus() 194 { 195 jlist.requestFocus(); 196 } 197 198 /** ListSelectionListener */ 199 200 private class _ListSelectionListener implements ListSelectionListener 201 { 202 public void valueChanged(ListSelectionEvent evt) 203 { 204 Object elem = jlist.getSelectedValue(); 205 if((elem != null) && (elementViewer != null)) 206 elementViewer.showElement((UIElement)elem); 207 } 208 } 209 210 } 211 212/* end of source file */