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 2091 2011-03-07 04:15:31Z 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.bibledesktop.BDMsg;
30  import org.crosswire.common.swing.GuiUtil;
31  import org.crosswire.jsword.versification.BookName;
32  
33  /**
34   * Render a list of Bible Book names with their full name as a tooltip
35   * 
36   * @see gnu.gpl.License for license details.<br>
37   *      The copyright to this program is held by it's authors.
38   * @author DM Smith [dmsmith555 at yahoo dot com]
39   */
40  public class BibleNameCellRenderer extends DefaultListCellRenderer {
41      /**
42       * Constructs a default renderer object for an item in a list, using full
43       * names.
44       */
45      public BibleNameCellRenderer() {
46          this(false);
47      }
48  
49      /**
50       * Constructs a renderer object for an item in a list, using abbreviated
51       * names if desired.
52       * 
53       * @param abbreviated
54       *            use the initials in the list.
55       */
56      public BibleNameCellRenderer(boolean abbreviated) {
57          super();
58          this.abbreviated = abbreviated;
59          GuiUtil.applyDefaultOrientation(this);
60      }
61  
62      /**
63       * @return the abbreviated
64       */
65      public boolean isAbbreviated() {
66          return abbreviated;
67      }
68  
69      /**
70       * @param newAbbreviated
71       *            the abbreviated to set
72       */
73      public void setAbbreviated(boolean newAbbreviated) {
74          this.abbreviated = newAbbreviated;
75      }
76  
77      /* (non-Javadoc)
78       * @see javax.swing.DefaultListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
79       */
80      @Override
81      public Component getListCellRendererComponent(JList list, Object value, int index, boolean selected, boolean focus) {
82          // Do the default rendering
83          Component comp = super.getListCellRendererComponent(list, value, index, selected, focus);
84  
85          // Do our rendering
86          setToolTipText(null);
87  
88          if (value == null) {
89              // TRANSLATOR: This is the replacement text for a blank book name in a list.
90              setText(BDMsg.gettext("None"));
91              setEnabled(false);
92          }
93  
94          // Hack to allow us to use PROTOTYPE_BOOK_NAME as a prototype value
95          if (value instanceof BookName) {
96              BookName bookName = (BookName) value;
97              String name = bookName.getLongName();
98  
99              setText(abbreviated ? bookName.getShortName() : name);
100             setToolTipText(name);
101         }
102 
103         return comp;
104     }
105 
106     /**
107      * If true then the initials of a book are shown, otherwise the full name.
108      */
109     private boolean abbreviated;
110 
111     /**
112      * Make sure that book names are not too wide
113      */
114     public static final String PROTOTYPE_BOOK_NAME = "0123456789";
115 
116     /**
117      * Serialization ID
118      */
119     private static final long serialVersionUID = 3978138859576308017L;
120 }
121