1 /* SwingListPeer.java -- A Swing based peer for AWT lists
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.java.awt.peer.swing;
40 import java.awt.Color;
41 import java.awt.Component;
42 import java.awt.Container;
43 import java.awt.Dimension;
44 import java.awt.Graphics;
45 import java.awt.Image;
47 import java.awt.Point;
48 import java.awt.Rectangle;
49 import java.awt.event.KeyEvent;
50 import java.awt.event.MouseEvent;
51 import java.awt.peer.ListPeer;
53 import javax.swing.DefaultListModel;
54 import javax.swing.JComponent;
55 import javax.swing.JList;
56 import javax.swing.JScrollPane;
57 import javax.swing.ListSelectionModel;
59 public class SwingListPeer
60 extends SwingComponentPeer
65 * A spezialized Swing scroller used to hold the list.
67 * @author Roman Kennke (kennke@aicas.com)
69 private class SwingList
71 implements SwingComponent
74 SwingList(Component comp)
76 super(comp, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
77 JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
83 * @return <code>this</code>
85 public JComponent getJComponent()
91 * Handles mouse events by forwarding it to
92 * <code>processMouseEvent()</code>.
94 * @param ev the mouse event
96 public void handleMouseEvent(MouseEvent ev)
103 * Force lightweight mouse dispatching.
105 public boolean isLightweight()
111 * Handles mouse motion events by forwarding it to
112 * <code>processMouseMotionEvent()</code>.
114 * @param ev the mouse motion event
116 public void handleMouseMotionEvent(MouseEvent ev)
118 processMouseMotionEvent(ev);
122 * Handles key events by forwarding it to <code>processKeyEvent()</code>.
124 * @param ev the mouse event
126 public void handleKeyEvent(KeyEvent ev)
132 * Overridden so that this method returns the correct value even without a
135 * @return the screen location of the button
137 public Point getLocationOnScreen()
139 return SwingListPeer.this.getLocationOnScreen();
143 * Overridden so that the isShowing method returns the correct value for the
144 * swing button, even if it has no peer on its own.
146 * @return <code>true</code> if the button is currently showing,
147 * <code>false</code> otherwise
149 public boolean isShowing()
151 boolean retVal = false;
152 if (SwingListPeer.this.awtComponent != null)
153 retVal = SwingListPeer.this.awtComponent.isShowing();
158 * Overridden, so that the Swing button can create an Image without its
161 * @param w the width of the image
162 * @param h the height of the image
166 public Image createImage(int w, int h)
168 return SwingListPeer.this.createImage(w, h);
171 public Graphics getGraphics()
173 return SwingListPeer.this.getGraphics();
176 public Container getParent()
178 Container par = null;
179 if (SwingListPeer.this.awtComponent != null)
180 par = SwingListPeer.this.awtComponent.getParent();
186 * The actual Swing JList.
190 private DefaultListModel listModel;
192 public SwingListPeer(List list)
195 listModel = new DefaultListModel();
196 jList = new JList(listModel);
197 SwingList swingList = new SwingList(jList);
198 init(list, swingList);
200 // Pull over the items from the list.
201 String[] items = list.getItems();
202 for (int i = 0 ; i < items.length; i++)
203 addItem(items[i], i);
206 public void add(String item, int index)
208 if (listModel != null)
209 listModel.add(index, item);
212 public void addItem(String item, int index)
214 if (listModel != null)
215 listModel.add(index, item);
220 if (listModel != null)
224 public void delItems(int startIndex, int endIndex)
226 if (listModel != null)
227 listModel.removeRange(startIndex, endIndex);
230 public void deselect(int index)
234 jList.getSelectionModel().removeSelectionInterval(index, index);
238 public Dimension getMinimumSize(int s)
243 d = jList.getComponent(s).getMinimumSize();
248 public Dimension getPreferredSize(int s)
253 d = jList.getComponent(s).getPreferredSize();
258 public int[] getSelectedIndexes()
263 sel = jList.getSelectedIndices();
268 public void makeVisible(int index)
272 Component comp = jList.getComponent(index);
273 jList.scrollRectToVisible(comp.getBounds());
277 public Dimension minimumSize(int s)
282 d = jList.getComponent(s).getMinimumSize();
287 public Dimension preferredSize(int s)
292 d = jList.getComponent(s).getPreferredSize();
297 public void removeAll()
305 public void select(int index)
309 jList.setSelectedIndex(index);
313 public void setMultipleMode(boolean multi)
317 jList.setSelectionMode(multi
318 ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
319 : ListSelectionModel.SINGLE_SELECTION);
323 public void setMultipleSelections(boolean multi)
327 jList.setSelectionMode(multi
328 ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
329 : ListSelectionModel.SINGLE_SELECTION);
333 public void reshape(int x, int y, int width, int height)
335 if (swingComponent != null)
337 swingComponent.getJComponent().setBounds(x, y, width, height);
338 swingComponent.getJComponent().validate();
342 protected void peerPaint(Graphics g, boolean update)
344 super.peerPaint(g, update);
348 Rectangle r = getBounds();
349 g.setColor(Color.RED);
350 g.drawRect(r.x, r.y, r.width, r.height);