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