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:SplitBookDataDisplay.java 1369 2007-06-01 13:35:27Z dmsmith $
21   */
22  package org.crosswire.bibledesktop.display.basic;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Component;
26  import java.io.IOException;
27  import java.io.ObjectInputStream;
28  import java.util.Arrays;
29  
30  import javax.swing.JPanel;
31  import javax.swing.JSplitPane;
32  import javax.swing.event.EventListenerList;
33  
34  import org.crosswire.bibledesktop.display.BookDataDisplay;
35  import org.crosswire.bibledesktop.passage.KeyChangeListener;
36  import org.crosswire.bibledesktop.passage.KeySidebar;
37  import org.crosswire.common.swing.FixedSplitPane;
38  import org.crosswire.common.util.Logger;
39  import org.crosswire.jsword.book.Book;
40  import org.crosswire.jsword.book.BookProvider;
41  import org.crosswire.jsword.passage.Key;
42  
43  /**
44   * A SplitBookDataDisplay consists of a KeySidebar and a BookDataDisplay in a
45   * SplitPane.
46   * 
47   * @see gnu.gpl.License for license details.<br>
48   *      The copyright to this program is held by it's authors.
49   * @author Joe Walker [joe at eireneh dot com]
50   * @author DM Smith [dmsmith555 at yahoo dot com]
51   */
52  public class SplitBookDataDisplay extends JPanel implements BookProvider {
53      /**
54       * Initialize the SplitBookDataDisplay
55       */
56      public SplitBookDataDisplay(KeySidebar sidebar, BookDataDisplay child) {
57          this.child = child;
58          this.sidebar = sidebar;
59          listenerList = new EventListenerList();
60  
61          split = new FixedSplitPane();
62          split.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
63          split.setLeftComponent(sidebar);
64          split.setRightComponent(child.getComponent());
65          split.setOneTouchExpandable(true);
66          split.setDividerLocation(0.0D);
67          split.setBorder(null);
68          split.setDividerSize(8);
69  
70          setLayout(new BorderLayout());
71          add(split, BorderLayout.CENTER);
72      }
73  
74      /**
75       * @return Returns the sidebar.
76       */
77      public KeySidebar getSidebar() {
78          return sidebar;
79      }
80  
81      /**
82       * @return Returns the display area.
83       */
84      public BookDataDisplay getBookDataDisplay() {
85          return child;
86      }
87  
88      /**
89       * Set the books and/or key to display.
90       * 
91       * @param books
92       * @param key
93       */
94      public void setBookData(Book[] books, Key key) {
95          boolean keyChanged = child.getKey() == null || !child.getKey().equals(key);
96          boolean bookChanged = child.getBooks() == null || !Arrays.equals(child.getBooks(), books);
97  
98          // Only set the passage if it has changed
99          if (keyChanged) {
100             log.debug("new passage chosen: " + key.getName());
101         }
102 
103         if (bookChanged) {
104             log.debug("new book(s) chosen: " + Arrays.toString(books));
105         }
106 
107         if (bookChanged || keyChanged) {
108             child.setBookData(books, key);
109         }
110     }
111 
112     /**
113      * Show or hide the passage sidebar.
114      * 
115      * @param show
116      *            boolean
117      */
118     public void showSidebar(boolean show) {
119         Component childComponent = child.getComponent();
120         if (show) {
121             remove(childComponent);
122             split.add(childComponent, JSplitPane.RIGHT);
123             add(split);
124         } else {
125             remove(split);
126             split.remove(childComponent);
127             add(childComponent);
128         }
129 
130         // Force it to layout again.
131         validate();
132     }
133 
134     /**
135      * @return the key
136      */
137     public Key getKey() {
138         return child.getKey();
139     }
140 
141     /**
142      * @return the book
143      */
144     public Book[] getBooks() {
145         return child.getBooks();
146     }
147 
148     /**
149      * Get the first book being displayed
150      */
151     public Book getFirstBook() {
152         return child.getFirstBook();
153     }
154 
155     /**
156      * copy the child
157      */
158     public void copy() {
159         child.copy();
160     }
161 
162     /**
163      * Add a listener for changes in the Key.
164      * 
165      * @param listener
166      *            the listener to add
167      */
168     public synchronized void addKeyChangeListener(KeyChangeListener listener) {
169         child.addKeyChangeListener(listener);
170     }
171 
172     /**
173      * Remove a listener for changes in the Key.
174      * 
175      * @param listener
176      *            the listener to remove
177      */
178     public synchronized void removeKeyChangeListener(KeyChangeListener listener) {
179         child.removeKeyChangeListener(listener);
180     }
181 
182     /**
183      * Serialization support.
184      * 
185      * @param is
186      * @throws IOException
187      * @throws ClassNotFoundException
188      */
189     private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
190         // Broken but we don't serialize views
191         child = null;
192         is.defaultReadObject();
193     }
194 
195     /**
196      * The log stream
197      */
198     private static final Logger log = Logger.getLogger(SplitBookDataDisplay.class);
199 
200     /*
201      * GUI Components
202      */
203     private KeySidebar sidebar;
204     private JSplitPane split;
205     private transient BookDataDisplay child;
206 
207     /**
208      * Serialization ID
209      */
210     private static final long serialVersionUID = 3257283643176202806L;
211 }
212