[jsword-svn] bibledesktop/java/main/org/crosswire/bibledesktop/book s

jswordcvs at crosswire.org jswordcvs at crosswire.org
Sun Oct 10 15:12:13 MST 2004


Update of /cvs/jsword/bibledesktop/java/main/org/crosswire/bibledesktop/book
In directory www.crosswire.org:/tmp/cvs-serv25280/java/main/org/crosswire/bibledesktop/book

Modified Files:
	Msg.properties DisplaySelectPane.java Msg.java 
	EditSitePane.java SitePane.java 
Added Files:
	InstallManagerComboBoxModel.java IndexResolver.java 
Removed Files:
	InstallManagerListModel.java 
Log Message:
basics of downloadable indexers

Index: SitePane.java
===================================================================
RCS file: /cvs/jsword/bibledesktop/java/main/org/crosswire/bibledesktop/book/SitePane.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** SitePane.java	2 Oct 2004 14:01:38 -0000	1.11
--- SitePane.java	10 Oct 2004 22:12:11 -0000	1.12
***************
*** 212,216 ****
              try
              {
!                 installer.reloadIndex();
  
                  treAvailable.setModel(new BooksTreeModel(installer));
--- 212,216 ----
              try
              {
!                 installer.reloadBookList();
  
                  treAvailable.setModel(new BooksTreeModel(installer));

Index: Msg.java
===================================================================
RCS file: /cvs/jsword/bibledesktop/java/main/org/crosswire/bibledesktop/book/Msg.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** Msg.java	9 Oct 2004 21:45:24 -0000	1.11
--- Msg.java	10 Oct 2004 22:12:11 -0000	1.12
***************
*** 73,76 ****
--- 73,86 ----
      static final Msg ADVANCED_SELECT_TITLE = new Msg("AdvancedSearchPane.SelectPassageTitle"); //$NON-NLS-1$
  
+     static final Msg HOW_MESSAGE_TITLE = new Msg("IndexResolver.HowMessageTitle"); //$NON-NLS-1$
+     static final Msg HOW_MESSAGE = new Msg("IndexResolver.HowMessage"); //$NON-NLS-1$
+     static final Msg HOW_GENERATE_TITLE = new Msg("IndexResolver.HowGenerateTitle"); //$NON-NLS-1$
+     static final Msg HOW_GENERATE = new Msg("IndexResolver.HowGenerate"); //$NON-NLS-1$
+     static final Msg HOW_SITE_TITLE = new Msg("IndexResolver.HowSiteTitle"); //$NON-NLS-1$
+     static final Msg HOW_SITE = new Msg("IndexResolver.HowSite"); //$NON-NLS-1$
+     static final Msg OPTION_DOWNLOAD = new Msg("IndexResolver.OptionDownload"); //$NON-NLS-1$
+     static final Msg OPTION_GENERATE = new Msg("IndexResolver.OptionGenerate"); //$NON-NLS-1$
+     static final Msg OPTION_CANCEL = new Msg("IndexResolver.OptionCancel"); //$NON-NLS-1$
+ 
      /**
       * Passthrough ctor

Index: DisplaySelectPane.java
===================================================================
RCS file: /cvs/jsword/bibledesktop/java/main/org/crosswire/bibledesktop/book/DisplaySelectPane.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -d -r1.16 -r1.17
*** DisplaySelectPane.java	9 Oct 2004 21:45:24 -0000	1.16
--- DisplaySelectPane.java	10 Oct 2004 22:12:11 -0000	1.17
***************
*** 37,41 ****
  import org.crosswire.jsword.book.BookMetaData;
  import org.crosswire.jsword.book.IndexStatus;
- import org.crosswire.jsword.book.search.IndexManagerFactory;
  import org.crosswire.jsword.book.search.parse.IndexSearcher;
  import org.crosswire.jsword.book.search.parse.PhraseParamWord;
--- 37,40 ----
***************
*** 170,174 ****
          this.add(btnIndex, new GridBagConstraints(2, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
          this.add(txtSearch, new GridBagConstraints(2, 2, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
!         this.add(chkMatch, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
          this.add(btnAdvanced, new GridBagConstraints(4, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
          this.add(btnSearch, new GridBagConstraints(5, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 2, 2, 2), 0, 0));
--- 169,173 ----
          this.add(btnIndex, new GridBagConstraints(2, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
          this.add(txtSearch, new GridBagConstraints(2, 2, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
!         this.add(chkMatch, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
          this.add(btnAdvanced, new GridBagConstraints(4, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
          this.add(btnSearch, new GridBagConstraints(5, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 2, 2, 2), 0, 0));
***************
*** 323,328 ****
      public void doIndex()
      {
!         BookMetaData bmd = mdlBible.getSelectedBookMetaData();
!         if (bmd == null)
          {
              noBookInstalled();
--- 322,326 ----
      public void doIndex()
      {
!         if (selected == null)
          {
              noBookInstalled();
***************
*** 330,334 ****
          }
  
!         IndexManagerFactory.getIndexManager().scheduleIndexCreation(bmd.getBook());
          enableComponents();
      }
--- 328,332 ----
          }
  
!         IndexResolver.scheduleIndex(selected, this);
          enableComponents();
      }

--- NEW FILE: InstallManagerComboBoxModel.java ---
package org.crosswire.bibledesktop.book;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;

import org.crosswire.jsword.book.install.InstallManager;
import org.crosswire.jsword.book.install.InstallerEvent;
import org.crosswire.jsword.book.install.InstallerListener;

/**
 * A ListModel for a JList that uses the list of Installers given by the
 * InstallManager.
 *
 * <p><table border='1' cellPadding='3' cellSpacing='0'>
 * <tr><td bgColor='white' class='TableRowColor'><font size='-7'>
 *
 * Distribution Licence:<br />
 * JSword is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public License,
 * version 2 as published by the Free Software Foundation.<br />
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.<br />
 * The License is available on the internet
 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, or by writing to:
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA<br />
 * The copyright to this program is held by it's authors.
 * </font></td></tr></table>
 * @see gnu.gpl.Licence
 * @author Joe Walker [joe at eireneh dot com]
 * @version $Id: InstallManagerComboBoxModel.java,v 1.1 2004/10/10 22:12:11 joe Exp $
 */
public class InstallManagerComboBoxModel extends AbstractListModel implements ComboBoxModel
{
    /**
     * Simple ctor
     */
    public InstallManagerComboBoxModel(InstallManager imanager)
    {
        this.imanager = imanager;

        update(null);
        selection = getElementAt(0);

        imanager.addInstallerListener(new CustomInstallerListener());
    }

    /* (non-Javadoc)
     * @see javax.swing.ComboBoxModel#getSelectedItem()
     */
    public Object getSelectedItem()
    {
        return selection;
    }

    /* (non-Javadoc)
     * @see javax.swing.ComboBoxModel#setSelectedItem(java.lang.Object)
     */
    public void setSelectedItem(Object selection)
    {
        this.selection = selection;
    }

    /* (non-Javadoc)
     * @see javax.swing.ListModel#getSize()
     */
    public int getSize()
    {
        return names.size();
    }

    /* (non-Javadoc)
     * @see javax.swing.ListModel#getElementAt(int)
     */
    public Object getElementAt(int index)
    {
        return names.get(index);
    }

    /**
     * Listens to the InstallManager for Installer changes
     */
    private class CustomInstallerListener implements InstallerListener
    {
        /* (non-Javadoc)
         * @see org.crosswire.jsword.book.install.InstallerListener#installerAdded(org.crosswire.jsword.book.install.InstallerEvent)
         */
        public void installerAdded(InstallerEvent ev)
        {
            update(ev);
        }

        /* (non-Javadoc)
         * @see org.crosswire.jsword.book.install.InstallerListener#installerRemoved(org.crosswire.jsword.book.install.InstallerEvent)
         */
        public void installerRemoved(InstallerEvent ev)
        {
            update(ev);
        }
    }

    /**
     * Simple way to avoid eclipse private/protected warning
     */
    protected void update(InstallerEvent ev)
    {
        int oldmax = names.size();

        names.clear();
        names.addAll(imanager.getInstallers().keySet());
        Collections.sort(names);

        if (ev != null)
        {
            fireContentsChanged(ev.getSource(), 0, oldmax);
        }
    }

    /**
     * The currently selected object
     */
    private Object selection;

    /**
     * A cache of the names in the Install Manager
     */
    private List names = new ArrayList();

    /**
     * The install manager that we are representing
     */
    private InstallManager imanager;
}

--- NEW FILE: IndexResolver.java ---
package org.crosswire.bibledesktop.book;

import java.awt.BorderLayout;
import java.awt.Component;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.crosswire.common.util.Logger;
import org.crosswire.common.util.NetUtil;
import org.crosswire.jsword.book.BookException;
import org.crosswire.jsword.book.BookMetaData;
import org.crosswire.jsword.book.install.InstallException;
import org.crosswire.jsword.book.install.InstallManager;
import org.crosswire.jsword.book.install.Installer;
import org.crosswire.jsword.book.search.IndexManager;
import org.crosswire.jsword.book.search.IndexManagerFactory;

/**
 * A class to prompt the user to download or create a search index and to do
 * carry out the users wishes.
 * 
 * <p><table border='1' cellPadding='3' cellSpacing='0'>
 * <tr><td bgColor='white' class='TableRowColor'><font size='-7'>
 *
 * Distribution Licence:<br />
 * JSword is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public License,
 * version 2 as published by the Free Software Foundation.<br />
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.<br />
 * The License is available on the internet
 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, or by writing to:
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA<br />
 * The copyright to this program is held by it's authors.
 * </font></td></tr></table>
 * @see gnu.gpl.Licence
 * @author Joe Walker [joe at eireneh dot com]
 * @version $Id: IndexResolver.java,v 1.1 2004/10/10 22:12:11 joe Exp $
 */
public class IndexResolver
{
    /**
     * Prevent instansiation
     */
    private IndexResolver()
    {
    }

    /**
     * The options that we show to the user for how to get a search index
     */
    private static Object[] options = new Object[]
    {
        Msg.OPTION_DOWNLOAD,
        Msg.OPTION_GENERATE,
        Msg.OPTION_CANCEL,
    };

    /**
     * @param parent
     * 
     */
    public static void scheduleIndex(BookMetaData bmd, Component parent)
    {
        String title = Msg.HOW_MESSAGE_TITLE.toString();
        Msg msg = Msg.HOW_MESSAGE;
        int choice = JOptionPane.showOptionDialog(parent, msg, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

        switch (choice)
        {
            case 0: // download
                Installer installer = selectInstaller(parent);
                if (installer != null)
                {
                    try
                    {
                        downloadIndex(bmd, installer);
                    }
                    catch (Exception ex)
                    {
                        log.error("index download failed: ", ex); //$NON-NLS-1$
                        //Reporter.informUser(parent, ex);

                        String gtitle = Msg.HOW_GENERATE_TITLE.toString();
                        Msg gmsg = Msg.HOW_GENERATE;
                        int yn = JOptionPane.showConfirmDialog(parent, gmsg, gtitle, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

                        if (yn == JOptionPane.YES_OPTION)
                        {
                            IndexManagerFactory.getIndexManager().scheduleIndexCreation(bmd.getBook());
                        }
                    }
                }
                break;

            case 1: // generate
                IndexManagerFactory.getIndexManager().scheduleIndexCreation(bmd.getBook());
                break;

            default: // cancel
                break;
        }
    }
    
    /**
     * Download and install a search index
     * @param bmd The book to get an index for
     */
    private static void downloadIndex(BookMetaData bmd, Installer installer) throws IOException, InstallException, BookException
    {
        // Get a temp home
        URL tempDownload = NetUtil.getTemporaryURL(TEMP_PREFIX, TEMP_SUFFIX);

        try
        {
            // Now we know what installer to use, download to the temp file
            installer.downloadSearchIndex(bmd, tempDownload);

            // And install from that file.
            IndexManager idxman = IndexManagerFactory.getIndexManager();
            idxman.installDownloadedIndex(bmd.getBook(), tempDownload);
        }
        finally
        {
            // tidy up after ourselves
            if (tempDownload != null)
            {
                NetUtil.delete(tempDownload);
            }
        }
    }

    /**
     * Pick an installer
     * @param parent A component to tie dialogs to
     * @return The chosen installer or null if the user cancelled.
     */
    private static Installer selectInstaller(Component parent)
    {
        // Pick an installer
        InstallManager insman = new InstallManager();
        Map installers = insman.getInstallers();
        Installer installer = null;
        if (installers.size() == 1)
        {
            Iterator it = installers.values().iterator();
            assert it.hasNext();
            installer = (Installer) it.next();
        }
        else
        {
            JComboBox choice = new JComboBox(new InstallManagerComboBoxModel(insman));
            JLabel label = new JLabel(Msg.HOW_SITE.toString());
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(label, BorderLayout.NORTH);
            panel.add(choice, BorderLayout.CENTER);

            String title = Msg.HOW_SITE_TITLE.toString();

            int yn = JOptionPane.showConfirmDialog(parent, panel, title, JOptionPane.YES_OPTION);
            if (yn == JOptionPane.YES_OPTION)
            {
                installer = (Installer) choice.getSelectedItem();
            }
        }

        return installer;
    }

    /**
     * The log stream
     */
    private static final Logger log = Logger.getLogger(IndexResolver.class);

    private static final String TEMP_SUFFIX = "dat"; //$NON-NLS-1$
    private static final String TEMP_PREFIX = "jsword-index"; //$NON-NLS-1$
}

Index: Msg.properties
===================================================================
RCS file: /cvs/jsword/bibledesktop/java/main/org/crosswire/bibledesktop/book/Msg.properties,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** Msg.properties	9 Oct 2004 21:45:24 -0000	1.9
--- Msg.properties	10 Oct 2004 22:12:11 -0000	1.10
***************
*** 69,70 ****
--- 69,84 ----
  
  AdvancedSearchPane.SelectPassageTitle=Select Passages to Restrict Search to
+ 
+ IndexResolver.HowMessageTitle=Download or generate?
+ IndexResolver.HowMessage=Do you wish to try to download and index or generate one for yourself?\n\
+ 	Downloading (2-3Mb) will take around 10 mins on a modem or 1 min on broadband.\n\
+ 	Generating an index will take around 5 mins on an average computer.\n\
+ 	Not all books have indexes so download may not be possible.
+ IndexResolver.HowSiteTitle=Which download site?
+ IndexResolver.HowSite=Which download site do you wish to use?
+ IndexResolver.HowGenerateTitle=Generate an index anyway?
+ IndexResolver.HowGenerate=Downloading failed.\n\
+ 	Do you wish to generate an index anyway?
+ IndexResolver.OptionDownload=Download
+ IndexResolver.OptionGenerate=Generate
+ IndexResolver.OptionCancel=Cancel

--- InstallManagerListModel.java DELETED ---

Index: EditSitePane.java
===================================================================
RCS file: /cvs/jsword/bibledesktop/java/main/org/crosswire/bibledesktop/book/EditSitePane.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** EditSitePane.java	30 Aug 2004 09:26:13 -0000	1.8
--- EditSitePane.java	10 Oct 2004 22:12:11 -0000	1.9
***************
*** 91,95 ****
          actions = new ActionFactory(EditSitePane.class, this);
  
!         lstSite = new JList(new InstallManagerListModel(imanager));
          JScrollPane scrSite = new JScrollPane();
          scrSite.add(lstSite, null);
--- 91,95 ----
          actions = new ActionFactory(EditSitePane.class, this);
  
!         lstSite = new JList(new InstallManagerComboBoxModel(imanager));
          JScrollPane scrSite = new JScrollPane();
          scrSite.add(lstSite, null);



More information about the jsword-svn mailing list