1   /**
2    * Distribution License:
3    * BibleDesktop is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU General Public License, version 2 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/gpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: BibleNameCellRenderer.java 1508 2007-07-21 23:28:34Z dmsmith $
21   */
22  package org.crosswire.bibledesktop.book;
23  
24  import java.awt.Component;
25  
26  import javax.swing.DefaultListCellRenderer;
27  import javax.swing.JList;
28  
29  import org.crosswire.common.swing.GuiUtil;
30  import org.crosswire.jsword.versification.BookName;
31  
32  /**
33   * Render a list of Bible Book names with their full name as a tooltip
34   *
35   * @see gnu.gpl.License for license details.
36   *      The copyright to this program is held by it's authors.
37   * @author DM Smith [dmsmith555 at yahoo dot com]
38   */
39  public class BibleNameCellRenderer extends DefaultListCellRenderer
40  {
41      /**
42       * Constructs a default renderer object for an item in a list,
43       * using full names.
44       */
45      public BibleNameCellRenderer()
46      {
47          this(false);
48      }
49  
50      /**
51       * Constructs a renderer object for an item in a list,
52       * using abbreviated names if desired.
53       * 
54       * @param abbreviated use the initials in the list.
55       */
56      public BibleNameCellRenderer(boolean abbreviated)
57      {
58          super();
59          this.abbreviated = abbreviated;
60          GuiUtil.applyDefaultOrientation(this);
61      }
62  
63      /**
64       * @return the abbreviated
65       */
66      public boolean isAbbreviated()
67      {
68          return abbreviated;
69      }
70  
71      /**
72       * @param newAbbreviated the abbreviated to set
73       */
74      public void setAbbreviated(boolean newAbbreviated)
75      {
76          this.abbreviated = newAbbreviated;
77      }
78  
79      /* (non-Javadoc)
80       * @see javax.swing.DefaultListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
81       */
82      public Component getListCellRendererComponent(JList list, Object value, int index, boolean selected, boolean focus)
83      {
84          // Do the default rendering
85          Component comp = super.getListCellRendererComponent(list, value, index, selected, focus);
86  
87          // Do our rendering
88          setToolTipText(null);
89  
90          if (value == null)
91          {
92              setText(Msg.NONE.toString());
93              setEnabled(false);
94          }
95  
96          // Hack to allow us to use PROTOTYPE_BOOK_NAME as a prototype value
97          if (value instanceof BookName)
98          {
99              BookName bookName = (BookName) value;
100             String name = bookName.getLongName();
101 
102             setText(abbreviated ? bookName.getShortName() : name);
103             setToolTipText(name);
104         }
105 
106         return comp;
107     }
108 
109     /**
110      * If true then the initials of a book are shown, otherwise the full name.
111      */
112     private boolean abbreviated;
113 
114     /**
115      * Make sure that book names are not too wide
116      */
117     public static final String PROTOTYPE_BOOK_NAME = "0123456789"; //$NON-NLS-1$
118 
119     /**
120      * Serialization ID
121      */
122     private static final long serialVersionUID = 3978138859576308017L;
123 }
124