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