[jsword-svn] bibledesktop/java/limbo/org/crosswire/bibledesktop/passage s

jswordcvs at crosswire.org jswordcvs at crosswire.org
Sun May 8 18:28:32 MST 2005


Update of /cvs/jsword/bibledesktop/java/limbo/org/crosswire/bibledesktop/passage
In directory www.crosswire.org:/tmp/cvs-serv5786/java/limbo/org/crosswire/bibledesktop/passage

Added Files:
	VerseTreeNode.java PassageTreeNode.java 
	KeyTreeCellRenderer.java PassageListCellRenderer.java 
	PassageTreeModel.java VerseRangeTreeNode.java 
	KeyTreeModel.java BookTreeNode.java BibleTreeNode.java 
	KeyTreeNode.java ChapterTreeNode.java 
Log Message:
Moved unused code to limbo.
Upgraded support-tools: checkstyle, pmd and findbugs to most recent.
Addressed over 100 issues reported by findbugs and checkstyle.
Resulted in major refactoring of GBFFilter.
Net result is that code size is significantly smaller.

--- NEW FILE: KeyTreeCellRenderer.java ---
package org.crosswire.bibledesktop.passage;

import java.awt.Component;

import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

import org.crosswire.common.util.Logger;
import org.crosswire.jsword.passage.Key;

/**
 * A specialization of DefaultTreeCellRenderer that knows how to get names from
 * Keys.
 * 
 * <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: KeyTreeCellRenderer.java,v 1.1 2005/05/09 01:28:30 dmsmith Exp $
 */
public class KeyTreeCellRenderer extends DefaultTreeCellRenderer
{
    /* (non-Javadoc)
     * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree, java.lang.Object, boolean, boolean, boolean, int, boolean)
     */
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean isselected, boolean expanded, boolean leaf, int row, boolean focus)
    {
        super.getTreeCellRendererComponent(tree, value, isselected, expanded, leaf, row, focus);

        if (value instanceof KeyTreeNode)
        {
            KeyTreeNode keytn = (KeyTreeNode) value;
            Key key = keytn.getKey();
            setText(key.getName());
        }
        else
        {
            log.warn("value is not a key: " + value.getClass().getName()); //$NON-NLS-1$
        }

        return this;
    }

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

    /**
     * Serialization ID
     */
    private static final long serialVersionUID = 3545232531516765241L;
}

--- NEW FILE: PassageTreeNode.java ---

package org.crosswire.bibledesktop.passage;

import java.util.Enumeration;
import java.util.Iterator;

import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

import org.crosswire.common.util.IteratorEnumeration;
import org.crosswire.jsword.passage.Passage;
import org.crosswire.jsword.passage.PassageEvent;
import org.crosswire.jsword.passage.PassageListener;
import org.crosswire.jsword.passage.RestrictionType;

/**
 * A PassageTreeNode extends TreeNode to Model a Passage.
 * 
 * <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: PassageTreeNode.java,v 1.1 2005/05/09 01:28:30 dmsmith Exp $
 */
public class PassageTreeNode implements TreeNode, PassageListener
{
    /**
     * Simple ctor
     */
    public PassageTreeNode(Passage ref, JTree tree)
    {
        this.ref = ref;
        this.tree = tree;
        ref.addPassageListener(this);
    }

    /**
     * Returns the child <code>TreeNode</code> at index i
     */
    public TreeNode getChildAt(int index)
    {
        return new VerseRangeTreeNode(ref.getRangeAt(index, RestrictionType.CHAPTER));
    }

    /**
     * Returns the number of children <code>TreeNode</code>s the receiver
     * contains.
     */
    public int getChildCount()
    {
        return ref.countRanges(RestrictionType.CHAPTER);
    }

    /**
     * Returns the parent <code>TreeNode</code> of the receiver.
     */
    public TreeNode getParent()
    {
        return this;
    }

    /**
     * Returns the index of <code>node</code> in the receivers children.
     * If the receiver does not contain <code>node</code>, -1 will be
     * returned.
     */
    public int getIndex(TreeNode node)
    {
        int count = 0;
        Iterator it = ref.rangeIterator(RestrictionType.NONE);

        while (it.hasNext())
        {
            if (it.next() == node)
            {
                return count;
            }

            count++;
        }

        return -1;
    }

    /**
     * Returns true if the receiver allows children.
     */
    public boolean getAllowsChildren()
    {
        return true;
    }

    /**
     * Returns true if the receiver is a leaf.
     */
    public boolean isLeaf()
    {
        return false;
    }

    /**
     * Sent after stuff has been added to the Passage.
     * More info about what and where can be had from the Event
     * @param ev a PassageEvent encapuslating the event information
     */
    public void versesAdded(PassageEvent ev)
    {
    }

    /**
     * Sent after stuff has been removed from the Passage.
     * More info about what and where can be had from the Event
     * @param ev a PassageEvent encapuslating the event information
     */
    public void versesRemoved(PassageEvent ev)
    {
    }

    /**
     * Sent after verses have been symultaneously added and removed from the Passage.
     * More info about what and where can be had from the Event
     * @param ev a PassageEvent encapuslating the event information
     */
    public void versesChanged(PassageEvent ev)
    {
        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
        model.nodeStructureChanged(this);
    }

    /**
     * Returns the children of the reciever as an Enumeration.
     */
    public Enumeration children()
    {
        return new IteratorEnumeration(ref.rangeIterator(RestrictionType.NONE));
    }

    /**
     * Returns the children of the reciever as an Enumeration.
     */
    public String toString()
    {
        return ref.getOverview();
    }

    /**
     * The Passage to be displayed
     */
    protected Passage ref;

    /**
     * The Passage to be displayed
     */
    protected JTree tree;
}

--- NEW FILE: ChapterTreeNode.java ---
package org.crosswire.bibledesktop.passage;

import java.util.Iterator;

import javax.swing.tree.TreeNode;

import org.crosswire.jsword.passage.BibleInfo;
import org.crosswire.jsword.passage.NoSuchVerseException;
import org.crosswire.jsword.passage.Passage;
import org.crosswire.jsword.passage.Verse;

/**
 * PassageTableModel.
 *
 * <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: ChapterTreeNode.java,v 1.1 2005/05/09 01:28:30 dmsmith Exp $
 */
public class ChapterTreeNode extends BookTreeNode
{
    /**
     * This constructor is for when we are really a BookTreeNode
     */
    protected ChapterTreeNode(TreeNode parent, int book, int chapter) throws NoSuchVerseException
    {
        super(parent, book);
        this.chapter = chapter;

        kids = new VerseTreeNode[BibleInfo.versesInChapter(book, chapter)];
    }

    /**
     * This constructor is for when we are really a BookTreeNode
     */
    public void setPassage(Passage ref, boolean filter)
    {
        this.ref = ref;

        if (filter)
        {
            try
            {
                kids = new VerseTreeNode[ref.versesInPassage(book, chapter)];

                int verse_count = 0;

                Iterator it = ref.iterator();
                while (it.hasNext())
                {
                    Verse verse = (Verse) it.next();

                    if ((book == 0 || verse.getBook() == book)
                        && (chapter == 0 || verse.getChapter() == chapter))
                    {
                        VerseTreeNode node = new VerseTreeNode(this, book, chapter, verse.getVerse());
                        node.setPassage(ref, true);
                        kids[verse_count++] = node;
                    }
                }
            }
            catch (NoSuchVerseException ex)
            {
                assert false : ex;
            }
        }
    }

    /**
     * Returns the child <code>TreeNode</code> at index i
     */
    public TreeNode getChildAt(int i)
    {
        try
        {
            if (kids[i] != null)
            {
                return kids[i];
            }

            kids[i] = new VerseTreeNode(this, book, chapter, i + 1);
            return kids[i];
        }
        catch (NoSuchVerseException ex)
        {
            assert false : ex;
            return null;
        }
    }

    /**
     * Returns the index of <code>node</code> in the receivers children.
     * If the receiver does not contain <code>node</code>, -1 will be
     * returned.
     */
    public int getIndex(TreeNode node)
    {
        if (!(node instanceof VerseTreeNode))
        {
            return -1;
        }

        VerseTreeNode verse = (VerseTreeNode) node;
        return verse.getVerse();
    }

    /**
     * How we appear in the Tree
     */
    public String toString()
    {
        try
        {
            String chapNum = Integer.toString(chapter);
            if (ref == null)
            {
                return chapNum;
            }

            int verses = ref.versesInPassage(book, chapter);
            if (verses == 0)
            {
                return chapNum;
            }

            return chapNum + " (" + verses + ')';  //$NON-NLS-1$
        }
        catch (NoSuchVerseException ex)
        {
            assert false : ex;
            return "!Error!"; //$NON-NLS-1$
        }
    }

    /**
     * The current Passage number
     */
    public int getChapter()
    {
        return chapter;
    }

    /**
     * The Book that this node referrs to
     */
    protected int chapter;
}


--- NEW FILE: BibleTreeNode.java ---
package org.crosswire.bibledesktop.passage;

import java.util.Enumeration;
import java.util.Iterator;

import javax.swing.tree.TreeNode;

import org.crosswire.jsword.passage.BibleInfo;
import org.crosswire.jsword.passage.NoSuchVerseException;
import org.crosswire.jsword.passage.Passage;
import org.crosswire.jsword.passage.Verse;

/**
 * BibleTreeNode.
 *
 * <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: BibleTreeNode.java,v 1.1 2005/05/09 01:28:30 dmsmith Exp $
 */
public class BibleTreeNode implements TreeNode
{
    /**
     *
     */
    public BibleTreeNode()
    {
        kids = new BookTreeNode[BibleInfo.booksInBible()];
    }

    /**
     *
     */
    public void setPassage(Passage ref, boolean filter)
    {
        this.ref = ref;

        try
        {
            if (filter)
            {
                kids = new BookTreeNode[ref.booksInPassage()];

                int currentBook = 0;
                int bookCount = 0;

                Iterator it = ref.iterator();
                while (it.hasNext())
                {
                    Verse verse = (Verse) it.next();
                    if (currentBook != verse.getBook())
                    {
                        currentBook = verse.getBook();
                        BookTreeNode node = new BookTreeNode(this, currentBook);
                        node.setPassage(ref, true);
                        kids[bookCount++] = node;
                    }
                }
            }
        }
        catch (NoSuchVerseException ex)
        {
            assert false : ex;
        }
    }

    /**
     * Returns the child <code>TreeNode</code> at index i
     */
    public TreeNode getChildAt(int i)
    {
        try
        {
            if (kids[i] != null)
            {
                return kids[i];
            }

            BookTreeNode node = new BookTreeNode(this, i + 1);
            node.setPassage(ref, false);
            kids[i] = node;

            return kids[i];
        }
        catch (NoSuchVerseException ex)
        {
            assert false : ex;
            return null;
        }
    }

    /**
     * Returns the number of children <code>TreeNode</code>s the receiver
     * contains.
     */
    public int getChildCount()
    {
        return kids.length;
    }

    /**
     * Returns the parent <code>TreeNode</code> of the receiver.
     */
    public TreeNode getParent()
    {
        return this;
    }

    /**
     * Returns the index of <code>node</code> in the receivers children.
     * If the receiver does not contain <code>node</code>, -1 will be
     * returned.
     */
    public int getIndex(TreeNode node)
    {
        if (!(node instanceof BookTreeNode))
        {
            return -1;
        }

        BookTreeNode book = (BookTreeNode) node;
        return book.getBook();
    }

    /**
     * Returns true if the receiver allows children.
     */
    public boolean getAllowsChildren()
    {
        return true;
    }

    /**
     * Returns true if the receiver is a leaf.
     */
    public boolean isLeaf()
    {
        return false;
    }

    /**
     * Returns the children of the reciever as an Enumeration.
     */
    public Enumeration children()
    {
        return new NodeEnumeration();
    }

    /**
     * Returns the children of the reciever as an Enumeration.
     */
    public String toString()
    {
        if (ref == null)
        {
            return Msg.WHOLE_BIBLE.toString();
        }

        return Msg.PART_BIBLE.toString(ref.getOverview());
    }

    /**
     * The Enumerate over an array
     */
    public class NodeEnumeration implements Enumeration
    {
        public boolean hasMoreElements()
        {
            return index < kids.length;
        }

        public Object nextElement()
        {
            return kids[index++];
        }

        private int index;
    }

    /**
     * If we are only displaying some of the verses
     */
    protected Passage ref;

    /**
     * The ChapterTreeNodes that we have created
     */
    protected TreeNode[] kids;
}

--- NEW FILE: PassageListCellRenderer.java ---
package org.crosswire.bibledesktop.passage;

import java.awt.Component;
import java.io.Serializable;
import java.util.Map;
import java.util.HashMap;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

import org.crosswire.common.swing.GuiUtil;
import org.crosswire.common.util.Reporter;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.BookData;
import org.crosswire.jsword.passage.VerseRange;

/**
 * Renders a Passage in a JList.
 *
 * <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: PassageListCellRenderer.java,v 1.1 2005/05/09 01:28:30 dmsmith Exp $
 */
public class PassageListCellRenderer implements ListCellRenderer, Serializable
{
    /**
     * Constructs a default renderer object for an item in a list.
     */
    public PassageListCellRenderer(Book bible)
    {
        this.bible = bible;

        border = new EmptyBorder(1, 1, 1, 1);

        label.setBorder(border);
        label.setOpaque(true);
        label.setIcon(GuiUtil.getIcon("images/Passage16.gif")); //$NON-NLS-1$
    }

    /**
     * Customize something to display the Passage component
     * @return The customized component
     */
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean selected, boolean focus)
    {
        if (selected)
        {
            label.setBackground(list.getSelectionBackground());
            label.setForeground(list.getSelectionForeground());
        }
        else
        {
            label.setBackground(list.getBackground());
            label.setForeground(list.getForeground());
        }

        if (value instanceof VerseRange)
        {
            try
            {
                VerseRange range = (VerseRange) value;
                String text = (String) hash.get(range);

                if (text == null)
                {
                    BookData bdata = bible.getData(range);
                    String simple = bdata.getVerseText();
                    text = "<html><b>" + range.getName() + "</b> " + simple; //$NON-NLS-1$ //$NON-NLS-2$
                    hash.put(range, text);
                }

                label.setText(text);
            }
            catch (Exception ex)
            {
                Reporter.informUser(this, ex);
                label.setText(Msg.ERROR.toString());
            }
        }
        else
        {
            label.setText((value == null) ? "" : value.toString()); //$NON-NLS-1$
        }

        label.setEnabled(list.isEnabled());
        label.setFont(list.getFont());
        label.setBorder(focus ? UIManager.getBorder("List.focusCellHighlightBorder") : border); //$NON-NLS-1$

        return label;
    }

    /**
     * The Bible in which to look up verses
     */
    private Book bible;

    /**
     * The label to display if the item is not selected
     */
    private JLabel label = new JLabel();

    /**
     * The border if the label is selected
     */
    private Border border;

    /**
     * A cache of Bible texts
     */
    private Map hash = new HashMap();

    /**
     * Serialization ID
     */
    private static final long serialVersionUID = 3978423624430270256L;
}

--- NEW FILE: VerseTreeNode.java ---

package org.crosswire.bibledesktop.passage;

import javax.swing.tree.TreeNode;

import org.crosswire.jsword.passage.NoSuchVerseException;
import org.crosswire.jsword.passage.Passage;

/**
 * PassageTableModel.
 *
 * <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: VerseTreeNode.java,v 1.1 2005/05/09 01:28:30 dmsmith Exp $
 */
public class VerseTreeNode extends ChapterTreeNode
{
    /**
     * This constructor is for when we are really a BookTreeNode
     */
    protected VerseTreeNode(TreeNode parent, int book, int passage, int verse) throws NoSuchVerseException
    {
        super(parent, book, passage);
        this.verse = verse;
    }

    /**
     * This constructor is for when we are really a BookTreeNode
     */
    public void setPassage(Passage ref, boolean filter)
    {
        this.ref = ref;
    }

    /**
     * Returns the child <code>TreeNode</code> at index i
     */
    public TreeNode getChildAt(int i)
    {
        return null; // VerseDisplay thing
    }

    /**
     * Returns the number of children <code>TreeNode</code>s the receiver
     * contains.
     */
    public int getChildCount()
    {
        return 0;
    }

    /**
     * Returns the index of <code>node</code> in the receivers children. If the
     * receiver does not contain <code>node</code>, -1 will be returned.
     */
    public int getIndex(TreeNode node)
    {
        return -1;
    }

    /**
     * How we appear in the Tree
     */
    public String toString()
    {
        return Integer.toString(verse);
    }

    /**
     * The current Passage number
     */
    public int getVerse()
    {
        return verse;
    }

    /**
     * The Verse that this node referrs to
     */
    protected int verse;
}


--- NEW FILE: BookTreeNode.java ---
package org.crosswire.bibledesktop.passage;

import java.util.Iterator;

import javax.swing.tree.TreeNode;

import org.crosswire.jsword.passage.BibleInfo;
import org.crosswire.jsword.passage.NoSuchVerseException;
import org.crosswire.jsword.passage.Passage;
import org.crosswire.jsword.passage.Verse;

/**
 * PassageTableModel.
 *
 * <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: BookTreeNode.java,v 1.1 2005/05/09 01:28:30 dmsmith Exp $
 */
public class BookTreeNode extends BibleTreeNode
{
    /**
     * This constructor is for when we are really a BookTreeNode
     */
    protected BookTreeNode(TreeNode parent, int book) throws NoSuchVerseException
    {
        this.parent = parent;
        this.book = book;

        kids = new ChapterTreeNode[BibleInfo.chaptersInBook(book)];
    }

    /**
     * This constructor is for when we are really a BookTreeNode
     */
    public void setPassage(Passage ref, boolean filter)
    {
        this.ref = ref;

        if (filter)
        {
            try
            {
                kids = new ChapterTreeNode[ref.chaptersInPassage(book)];

                int currentRef = 0;
                int count = 0;

                Iterator it = ref.iterator();
                while (it.hasNext())
                {
                    Verse verse = (Verse) it.next();

                    if ((book == 0 || verse.getBook() == book)
                        && currentRef != verse.getChapter())
                    {
                        currentRef = verse.getChapter();

                        ChapterTreeNode node = new ChapterTreeNode(this, book, currentRef);
                        node.setPassage(ref, true);
                        kids[count++] = node;
                    }
                }
            }
            catch (NoSuchVerseException ex)
            {
                assert false : ex;
            }
        }
    }

    /**
     * Returns the child <code>TreeNode</code> at index i
     */
    public TreeNode getChildAt(int i)
    {
        try
        {
            if (kids[i] != null)
            {
                return kids[i];
            }

            ChapterTreeNode node = new ChapterTreeNode(this, book, i + 1);
            node.setPassage(ref, false);
            kids[i] = node;

            return kids[i];
        }
        catch (NoSuchVerseException ex)
        {
            assert false : ex;
            return null;
        }
    }

    /**
     * Returns the parent <code>TreeNode</code> of the receiver.
     */
    public TreeNode getParent()
    {
        return parent;
    }

    /**
     * Returns the index of <code>node</code> in the receivers children.
     * If the receiver does not contain <code>node</code>, -1 will be
     * returned.
     */
    public int getIndex(TreeNode node)
    {
        if (!(node instanceof ChapterTreeNode))
        {
            return -1;
        }

        ChapterTreeNode chap = (ChapterTreeNode) node;
        return chap.getChapter();
    }

    /**
     * How we appear in the Tree
     */
    public String toString()
    {
        try
        {
            String bookName = BibleInfo.getLongBookName(book);
            if (ref == null)
            {
                return bookName;
            }

            int chapters = ref.chaptersInPassage(book);
            if (chapters == 0)
            {
                return bookName;
            }

            return bookName + " (" + chapters + ')';  //$NON-NLS-1$ //$NON-NLS-2$
        }
        catch (NoSuchVerseException ex)
        {
            assert false : ex;
            return "!Error!"; //$NON-NLS-1$
        }
    }

    /**
     * The current book number (Genesis=1)
     */
    public int getBook()
    {
        return book;
    }

    /**
     * The Book that this node referrs to
     */
    protected int book;

    /** The base of this tree */
    protected TreeNode parent;
}


--- NEW FILE: VerseRangeTreeNode.java ---

package org.crosswire.bibledesktop.passage;

import java.util.Enumeration;

import javax.swing.tree.TreeNode;

import org.crosswire.jsword.passage.VerseRange;

/**
 * BibleTreeNode.
 * 
 * <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: VerseRangeTreeNode.java,v 1.1 2005/05/09 01:28:30 dmsmith Exp $
 */
public class VerseRangeTreeNode implements TreeNode
{
    /**
     *
     */
    public VerseRangeTreeNode(VerseRange range)
    {
        this.range = range;
    }

    /**
     * Returns the child <code>TreeNode</code> at index i
     */
    public TreeNode getChildAt(int index)
    {
        return null;
    }

    /**
     * Returns the number of children <code>TreeNode</code>s the receiver
     * contains.
     */
    public int getChildCount()
    {
        return 0;
    }

    /**
     * Returns the parent <code>TreeNode</code> of the receiver.
     */
    public TreeNode getParent()
    {
        return this;
    }

    /**
     * Returns the index of <code>node</code> in the receivers children.
     * If the receiver does not contain <code>node</code>, -1 will be
     * returned.
     */
    public int getIndex(TreeNode node)
    {
        return -1;
    }

    /**
     * Returns true if the receiver allows children.
     */
    public boolean getAllowsChildren()
    {
        return false;
    }

    /**
     * Returns true if the receiver is a leaf.
     */
    public boolean isLeaf()
    {
        return true;
    }

    /**
     * Returns the children of the reciever as an Enumeration.
     */
    public Enumeration children()
    {
        return null;
    }

    /**
     * Returns the children of the reciever as an Enumeration.
     */
    public String toString()
    {
        return range.getName();
    }

    /** The range that we are displaying */
    private VerseRange range;
}


--- NEW FILE: KeyTreeNode.java ---
package org.crosswire.bibledesktop.passage;

import java.util.Enumeration;

import javax.swing.tree.TreeNode;

import org.crosswire.common.util.IteratorEnumeration;
import org.crosswire.jsword.passage.Key;

/**
 * An implementation of TreeNode that reads from Keys and KeyLists.
 * 
 * <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: KeyTreeNode.java,v 1.1 2005/05/09 01:28:30 dmsmith Exp $
 */
public class KeyTreeNode implements TreeNode
{
    /**
     * Simple ctor
     */
    public KeyTreeNode(Key key, TreeNode parent)
    {
        this.key = key;
        this.parent = parent;
    }

    /* (non-Javadoc)
     * @see javax.swing.tree.TreeNode#getChildCount()
     */
    public int getChildCount()
    {
        return key.getChildCount();
    }

    /* (non-Javadoc)
     * @see javax.swing.tree.TreeNode#getAllowsChildren()
     */
    public boolean getAllowsChildren()
    {
        return key.canHaveChildren();
    }

    /* (non-Javadoc)
     * @see javax.swing.tree.TreeNode#isLeaf()
     */
    public boolean isLeaf()
    {
        return key.isEmpty();
    }

    /* (non-Javadoc)
     * @see javax.swing.tree.TreeNode#children()
     */
    public Enumeration children()
    {
        return new IteratorEnumeration(key.iterator());
    }

    /* (non-Javadoc)
     * @see javax.swing.tree.TreeNode#getParent()
     */
    public TreeNode getParent()
    {
        return parent;
    }

    /* (non-Javadoc)
     * @see javax.swing.tree.TreeNode#getChildAt(int)
     */
    public TreeNode getChildAt(int index)
    {
        Key child = key.get(index);
        return new KeyTreeNode(child, this);
    }

    /* (non-Javadoc)
     * @see javax.swing.tree.TreeNode#getIndex(javax.swing.tree.TreeNode)
     */
    public int getIndex(TreeNode node)
    {
        if (node instanceof KeyTreeNode)
        {
            KeyTreeNode keynode = (KeyTreeNode) node;
            Key that = keynode.getKey();

            return key.indexOf(that);
        }
        return -1;
    }

    /**
     * Accessor for the key
     */
    public Key getKey()
    {
        return key;
    }

    private Key key;
    private TreeNode parent;
}

--- NEW FILE: PassageTreeModel.java ---
package org.crosswire.bibledesktop.passage;

import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;

import org.crosswire.jsword.passage.Passage;

/**
 * The PassageTreeModel class implements TreeModel using various custom
 * TreeNodes, and simply extending DefaultTreeModel.
 * 
 * <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: PassageTreeModel.java,v 1.1 2005/05/09 01:28:30 dmsmith Exp $
 * @see DefaultTreeModel
 * @see PassageTreeNode
 */
public class PassageTreeModel extends DefaultTreeModel
{
    /**
     * Basic constructor.
     * At some stage when editing this and PassageTreeNode we should be able to
     * remove the JTree param.
     */
    public PassageTreeModel(Passage ref, JTree tree)
    {
        super(new PassageTreeNode(ref, tree));
    }

    /**
     * Serialization ID
     */
    private static final long serialVersionUID = 3546081345886434357L;
}

--- NEW FILE: KeyTreeModel.java ---
package org.crosswire.bibledesktop.passage;

import javax.swing.tree.DefaultTreeModel;

import org.crosswire.jsword.passage.Key;

/**
 * A TreeModel that helps with working with Keys.
 * 
 * <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: KeyTreeModel.java,v 1.1 2005/05/09 01:28:30 dmsmith Exp $
 */
public class KeyTreeModel extends DefaultTreeModel
{
    /**
     * Simple ctor
     * @param key The root TreeNode
     */
    public KeyTreeModel(Key key)
    {
        super(new KeyTreeNode(key, null));
        this.key = key;
    }

    /**
     * What key is this tree editing
     * @return Returns the key.
     */
    public Key getKey()
    {
        return key;
    }

    /**
     * Sets the key is this tree editing
     */
    public void setKey(Key key)
    {
        this.key = key;
        setRoot(new KeyTreeNode(key, null));
    }

    /**
     * The key that this tree is displaying.
     */
    private Key key;

    /**
     * Serialization ID
     */
    private static final long serialVersionUID = 3977303235050353714L;
}



More information about the jsword-svn mailing list