OSDN Git Service

Merged gcj-eclipse branch to trunk.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / java / awt / peer / swing / SwingListPeer.java
1 /* SwingListPeer.java -- A Swing based peer for AWT lists
2    Copyright (C)  2006  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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)
9 any later version.
10
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.
15
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
19 02110-1301 USA.
20
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
24 combination.
25
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. */
37
38 package gnu.java.awt.peer.swing;
39
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;
46 import java.awt.List;
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;
52
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;
58
59 public class SwingListPeer
60     extends SwingComponentPeer
61     implements ListPeer
62 {
63
64   /**
65    * A spezialized Swing scroller used to hold the list. 
66    *
67    * @author Roman Kennke (kennke@aicas.com)
68    */
69   private class SwingList
70     extends JScrollPane
71     implements SwingComponent
72   {
73
74     SwingList(Component comp)
75     {
76       super(comp, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
77             JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
78     }
79
80     /**
81      * Returns this label.
82      *
83      * @return <code>this</code>
84      */
85     public JComponent getJComponent()
86     {
87       return this;
88     }
89
90     /**
91      * Handles mouse events by forwarding it to
92      * <code>processMouseEvent()</code>.
93      *
94      * @param ev the mouse event
95      */
96     public void handleMouseEvent(MouseEvent ev)
97     {
98       ev.setSource(this);
99       dispatchEvent(ev);
100     }
101
102     /**
103      * Force lightweight mouse dispatching.
104      */
105     public boolean isLightweight()
106     {
107       return false;
108     }
109
110     /**
111      * Handles mouse motion events by forwarding it to
112      * <code>processMouseMotionEvent()</code>.
113      *
114      * @param ev the mouse motion event
115      */
116     public void handleMouseMotionEvent(MouseEvent ev)
117     {
118       processMouseMotionEvent(ev);
119     }
120
121     /**
122      * Handles key events by forwarding it to <code>processKeyEvent()</code>.
123      *
124      * @param ev the mouse event
125      */
126     public void handleKeyEvent(KeyEvent ev)
127     {
128       processKeyEvent(ev);
129     }
130
131     /**
132      * Overridden so that this method returns the correct value even without a
133      * peer.
134      *
135      * @return the screen location of the button
136      */
137     public Point getLocationOnScreen()
138     {
139       return SwingListPeer.this.getLocationOnScreen();
140     }
141
142     /**
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.
145      *
146      * @return <code>true</code> if the button is currently showing,
147      *         <code>false</code> otherwise
148      */
149     public boolean isShowing()
150     {
151       boolean retVal = false;
152       if (SwingListPeer.this.awtComponent != null)
153         retVal = SwingListPeer.this.awtComponent.isShowing();
154       return retVal;
155     }
156
157     /**
158      * Overridden, so that the Swing button can create an Image without its
159      * own peer.
160      *
161      * @param w the width of the image
162      * @param h the height of the image
163      *
164      * @return an image
165      */
166     public Image createImage(int w, int h)
167     {
168       return SwingListPeer.this.createImage(w, h);
169     }
170
171     public Graphics getGraphics()
172     {
173       return SwingListPeer.this.getGraphics();
174     }
175
176     public Container getParent()
177     {
178       Container par = null;
179       if (SwingListPeer.this.awtComponent != null)
180         par = SwingListPeer.this.awtComponent.getParent();
181       return par;
182     }
183   }
184
185   /**
186    * The actual Swing JList.
187    */
188   private JList jList;
189
190   private DefaultListModel listModel;
191
192   public SwingListPeer(List list)
193   {
194     super();
195     listModel = new DefaultListModel();
196     jList = new JList(listModel);
197     SwingList swingList = new SwingList(jList);
198     init(list, swingList);
199
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);
204   }
205
206   public void add(String item, int index)
207   {
208     if (listModel != null)
209       listModel.add(index, item);
210   }
211
212   public void addItem(String item, int index)
213   {
214     if (listModel != null)
215       listModel.add(index, item);
216   }
217
218   public void clear()
219   {
220     if (listModel != null)
221       listModel.clear();
222   }
223
224   public void delItems(int startIndex, int endIndex)
225   {
226     if (listModel != null)
227       listModel.removeRange(startIndex, endIndex);
228   }
229
230   public void deselect(int index)
231   {
232     if (jList != null)
233       {
234         jList.getSelectionModel().removeSelectionInterval(index, index);
235       }
236   }
237
238   public Dimension getMinimumSize(int s)
239   {
240     Dimension d = null;
241     if (jList != null)
242       {
243         d = jList.getComponent(s).getMinimumSize();
244       }
245     return d;
246   }
247
248   public Dimension getPreferredSize(int s)
249   {
250     Dimension d = null;
251     if (jList != null)
252       {
253         d = jList.getComponent(s).getPreferredSize();
254       }
255     return d;
256   }
257
258   public int[] getSelectedIndexes()
259   {
260     int[] sel = null;
261     if (jList != null)
262       {
263         sel = jList.getSelectedIndices();
264       }
265     return sel;
266   }
267
268   public void makeVisible(int index)
269   {
270     if (jList != null)
271       {
272         Component comp = jList.getComponent(index);
273         jList.scrollRectToVisible(comp.getBounds());
274       }
275   }
276
277   public Dimension minimumSize(int s)
278   {
279     Dimension d = null;
280     if (jList != null)
281       {
282         d = jList.getComponent(s).getMinimumSize();
283       }
284     return d;
285   }
286
287   public Dimension preferredSize(int s)
288   {
289     Dimension d = null;
290     if (jList != null)
291       {
292         d = jList.getComponent(s).getPreferredSize();
293       }
294     return d;
295   }
296
297   public void removeAll()
298   {
299     if (jList != null)
300       {
301         jList.removeAll();
302       }
303   }
304
305   public void select(int index)
306   {
307     if (jList != null)
308       {
309         jList.setSelectedIndex(index);
310       }
311   }
312
313   public void setMultipleMode(boolean multi)
314   {
315     if (jList != null)
316       {
317         jList.setSelectionMode(multi
318                                ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
319                                : ListSelectionModel.SINGLE_SELECTION);
320       }
321   }
322
323   public void setMultipleSelections(boolean multi)
324   {
325     if (jList != null)
326       {
327         jList.setSelectionMode(multi
328                                ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
329                                : ListSelectionModel.SINGLE_SELECTION);
330       }
331   }
332
333   public void reshape(int x, int y, int width, int height)
334   {
335     if (swingComponent != null)
336       {
337         swingComponent.getJComponent().setBounds(x, y, width, height);
338         swingComponent.getJComponent().validate();
339       }
340   }
341
342   protected void peerPaint(Graphics g, boolean update)
343   {
344     super.peerPaint(g, update);
345     jList.doLayout();
346     jList.list();
347     
348     Rectangle r = getBounds();
349     g.setColor(Color.RED);
350     g.drawRect(r.x, r.y, r.width, r.height);
351   }
352 }