Source for gnu.java.awt.peer.swing.SwingListPeer

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