Source for org.jfree.ui.StrokeChooserPanel

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2009, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it
  10:  * under the terms of the GNU Lesser General Public License as published by
  11:  * the Free Software Foundation; either version 2.1 of the License, or
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
  22:  * USA.
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25:  * in the United States and other countries.]
  26:  *
  27:  * -----------------------
  28:  * StrokeChooserPanel.java
  29:  * -----------------------
  30:  * (C) Copyright 2000-2009, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   Dirk Zeitz;
  34:  *
  35:  * $Id: StrokeChooserPanel.java,v 1.8 2009/02/27 13:58:41 mungady Exp $
  36:  *
  37:  * Changes (from 26-Oct-2001)
  38:  * --------------------------
  39:  * 26-Oct-2001 : Changed package to com.jrefinery.ui.*;
  40:  * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41:  * 16-Mar-2004 : Fix for focus problems (DZ);
  42:  * 27-Feb-2009 : Fixed bug 2612649, NullPointerException (DG);
  43:  *
  44:  */
  45: 
  46: package org.jfree.ui;
  47: 
  48: import java.awt.BorderLayout;
  49: import java.awt.Stroke;
  50: import java.awt.event.ActionEvent;
  51: import java.awt.event.ActionListener;
  52: 
  53: import javax.swing.DefaultComboBoxModel;
  54: import javax.swing.JComboBox;
  55: import javax.swing.JPanel;
  56: 
  57: /**
  58:  * A component for choosing a stroke from a list of available strokes.  This
  59:  * class needs work.
  60:  *
  61:  * @author David Gilbert
  62:  */
  63: public class StrokeChooserPanel extends JPanel {
  64: 
  65:     /** A combo for selecting the stroke. */
  66:     private JComboBox selector;
  67: 
  68:     /**
  69:      * Creates a panel containing a combo-box that allows the user to select
  70:      * one stroke from a list of available strokes.
  71:      *
  72:      * @param current  the current stroke sample.
  73:      * @param available  an array of 'available' stroke samples.
  74:      */
  75:     public StrokeChooserPanel(StrokeSample current, StrokeSample[] available) {
  76:         setLayout(new BorderLayout());
  77:         // we've changed the behaviour here to populate the combo box
  78:         // with Stroke objects directly - ideally we'd change the signature
  79:         // of the constructor too...maybe later.
  80:         DefaultComboBoxModel model = new DefaultComboBoxModel();
  81:         for (int i = 0; i < available.length; i++) {
  82:             model.addElement(available[i].getStroke());
  83:         }
  84:         this.selector = new JComboBox(model);
  85:         this.selector.setSelectedItem(current.getStroke());
  86:         this.selector.setRenderer(new StrokeSample(null));
  87:         add(this.selector);
  88:         // Changes due to focus problems!! DZ
  89:         this.selector.addActionListener(new ActionListener() {
  90:             public void actionPerformed(final ActionEvent evt) {
  91:                 getSelector().transferFocus();
  92:             }
  93:         });
  94:     }
  95: 
  96: 
  97:     /**
  98:      * Returns the selector component.
  99:      *
 100:      * @return Returns the selector.
 101:      */
 102:     protected final JComboBox getSelector() {
 103:         return this.selector;
 104:     }
 105: 
 106:     /**
 107:      * Returns the selected stroke.
 108:      *
 109:      * @return The selected stroke (possibly <code>null</code>).
 110:      */
 111:     public Stroke getSelectedStroke() {
 112:         return (Stroke) this.selector.getSelectedItem();
 113:     }
 114: 
 115: }