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: IndexResolver.java 2125 2011-03-14 21:37:06Z dmsmith $
21   */
22  package org.crosswire.bibledesktop.book.install;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Component;
26  import java.io.IOException;
27  import java.util.Map;
28  
29  import javax.swing.JComboBox;
30  import javax.swing.JLabel;
31  import javax.swing.JOptionPane;
32  import javax.swing.JPanel;
33  
34  import org.crosswire.bibledesktop.BDMsg;
35  import org.crosswire.common.swing.CWOptionPane;
36  import org.crosswire.common.util.Logger;
37  import org.crosswire.jsword.book.Book;
38  import org.crosswire.jsword.book.BookException;
39  import org.crosswire.jsword.book.install.InstallException;
40  import org.crosswire.jsword.book.install.InstallManager;
41  import org.crosswire.jsword.book.install.Installer;
42  import org.crosswire.jsword.index.IndexManagerFactory;
43  import org.crosswire.jsword.util.IndexDownloader;
44  
45  /**
46   * A class to prompt the user to download or create a search index and to do
47   * carry out the users wishes.
48   * 
49   * @see gnu.gpl.License for license details.<br>
50   *      The copyright to this program is held by it's authors.
51   * @author Joe Walker [joe at eireneh dot com]
52   */
53  public final class IndexResolver {
54      /**
55       * Prevent instantiation
56       */
57      private IndexResolver() {
58      }
59  
60      /**
61       * @param parent
62       * 
63       */
64      public static void scheduleIndex(Book book, Component parent) {
65          int choice = 1;
66  
67          switch (choice) {
68          case 0: // download
69              Installer installer = selectInstaller(parent);
70              if (installer != null) {
71                  Exception ex = null;
72                  try {
73                      IndexDownloader.downloadIndex(book, installer);
74                  } catch (InstallException e) {
75                      ex = e;
76  
77                  } catch (BookException e) {
78                      ex = e;
79                  } catch (IOException e) {
80                      ex = e;
81                  }
82  
83                  if (ex != null) {
84                      log.error("index download failed: ", ex);
85                      // TRANSLATOR: Title to a dialog that asks whether the user wants to generate an index.
86                      // Currently unused.
87                      String gtitle = BDMsg.gettext("Download or generate?");
88                      StringBuffer gmsg = new StringBuffer(200);
89                      // TRANSLATOR: The download failed for one reason or another.
90                      // Currently unused.
91                      gmsg.append(BDMsg.gettext("Downloading failed."));
92                      gmsg.append("\n");
93                      // TRANSLATOR: Since the download failed, ask the user whether the index should be generated.
94                      // Currently unused.
95                      gmsg.append(BDMsg.gettext("Do you wish to generate an index anyway?"));
96                      int yn = CWOptionPane.showConfirmDialog(parent, gmsg.toString(), gtitle, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
97  
98                      if (yn == JOptionPane.YES_OPTION) {
99                          IndexManagerFactory.getIndexManager().scheduleIndexCreation(book);
100                     }
101                 }
102             }
103             break;
104 
105         case 1: // generate
106             IndexManagerFactory.getIndexManager().scheduleIndexCreation(book);
107             break;
108 
109         default: // cancel
110             break;
111         }
112     }
113 
114     /**
115      * Pick an installer
116      * 
117      * @param parent
118      *            A component to tie dialogs to
119      * @return The chosen installer or null if the user cancelled.
120      */
121     private static Installer selectInstaller(Component parent) {
122         // Pick an installer
123         InstallManager insman = new InstallManager();
124         Map<String, Installer> installers = insman.getInstallers();
125         Installer installer = null;
126         if (installers.size() == 1) {
127             for (Installer anInstaller : installers.values()) {
128                 installer = anInstaller;
129                 break;
130             }
131             assert installer != null;
132         } else {
133             JComboBox choice = new JComboBox(new InstallManagerComboBoxModel(insman));
134             // TRANSLATOR: Label for a list of index download sites.
135             // Currently unused.
136             JLabel label = new JLabel(BDMsg.gettext("Which download site do you wish to use?"));
137             JPanel panel = new JPanel(new BorderLayout());
138             panel.add(label, BorderLayout.NORTH);
139             panel.add(choice, BorderLayout.CENTER);
140 
141             // TRANSLATOR: Title for a dialog that asks whether the user should download the index.
142             // Currently unused.
143             String title = BDMsg.gettext("Download an index?");
144 
145             int yn = CWOptionPane.showConfirmDialog(parent, panel, title, JOptionPane.YES_OPTION);
146             if (yn == JOptionPane.YES_OPTION) {
147                 installer = (Installer) choice.getSelectedItem();
148             }
149         }
150 
151         return installer;
152     }
153 
154     /**
155      * The log stream
156      */
157     private static final Logger log = Logger.getLogger(IndexResolver.class);
158 }
159