[sword-cvs] r23 - in trunk/app/src/org/crosswire: common flashcards

Apache apache at crosswire.org
Tue Sep 14 13:36:03 MST 2004


Author: 
Date: 2004-09-14 13:36:02 -0700 (Tue, 14 Sep 2004)
New Revision: 23

Added:
   trunk/app/src/org/crosswire/flashcards/FlashCard.java
   trunk/app/src/org/crosswire/flashcards/Lesson.java
   trunk/app/src/org/crosswire/flashcards/LessonManager.java
   trunk/app/src/org/crosswire/flashcards/LessonSet.java
Modified:
   trunk/app/src/org/crosswire/common/CWClassLoader.java
   trunk/app/src/org/crosswire/flashcards/Debug.java
   trunk/app/src/org/crosswire/flashcards/Editor.java
   trunk/app/src/org/crosswire/flashcards/EditorFrame.java
   trunk/app/src/org/crosswire/flashcards/EditorFrame_AboutBox.java
   trunk/app/src/org/crosswire/flashcards/MainFrame.java
   trunk/app/src/org/crosswire/flashcards/MainFrame_AboutBox.java
   trunk/app/src/org/crosswire/flashcards/MainMenu.java
   trunk/app/src/org/crosswire/flashcards/OpenFile.java
   trunk/app/src/org/crosswire/flashcards/Quiz.java
Log:
Add some new classes and add copyrigt notice

Modified: trunk/app/src/org/crosswire/common/CWClassLoader.java
===================================================================
--- trunk/app/src/org/crosswire/common/CWClassLoader.java	2004-09-11 02:49:16 UTC (rev 22)
+++ trunk/app/src/org/crosswire/common/CWClassLoader.java	2004-09-14 20:36:02 UTC (rev 23)
@@ -265,26 +265,11 @@
     {
         URL reply = null;
         
-        URL homeURL = getHome();;
+        URL override = getHomeResource(search);
 
         // Look at the application's home first to allow overrides
-        if (homeURL != null)
+        if (override != null)
         {
-            // Since home does not end in a '/'
-            // we need to add one to the front of search
-            // if it does not have it.
-            String ssearch = null;
-            if (search.charAt(0) == '/')
-            {
-                ssearch = search;
-            }
-            else
-            {
-                ssearch = '/' + search;
-            }
-
-            URL override = lengthenURL(homeURL, ssearch);
-
             // Make sure the file exists and can be read
             File f = new File(override.getFile());
             if (f.canRead())
@@ -297,6 +282,25 @@
     }
 
     /**
+     * Compute an URL for the resource in the home directory
+     * @param search must be non-null, non-empty
+     */
+    public static URL getHomeResource(String search)
+    {
+        URL reply = null;
+        
+        URL homeURL = getHome();
+
+        // Look at the application's home first to allow overrides
+        if (homeURL != null)
+        {
+            reply = lengthenURL(homeURL, search);
+        }
+
+        return reply;
+    }
+
+    /**
      * Utility to add a string to the end of a URL.
      * @param orig The URL to strip
      * @param extra The text to add to the end of the URL
@@ -306,9 +310,17 @@
     {
         try
         {
+            char firstChar = extra.charAt(extra.length() - 1);
+            if (isSeparator(firstChar))
+            {
+                extra = extra.substring(1);
+            }
+
             if (orig.getProtocol().equals(PROTOCOL_FILE))
             {
-                if (orig.toExternalForm().endsWith(File.separator))
+                String file = orig.toExternalForm();
+                char lastChar = file.charAt(file.length() - 1);
+                if (isSeparator(lastChar))
                 {
                     return new URL(orig.getProtocol(),
                                    orig.getHost(),
@@ -334,11 +346,14 @@
         catch (MalformedURLException ex)
         {
             assert false : ex;
-
             return null;
         }
     }
 
+    private static boolean isSeparator(char c)
+    {
+        return c == '/' || c == '\\';
+    }
 
     /**
      * Constant for the file: protocol

Modified: trunk/app/src/org/crosswire/flashcards/Debug.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/Debug.java	2004-09-11 02:49:16 UTC (rev 22)
+++ trunk/app/src/org/crosswire/flashcards/Debug.java	2004-09-14 20:36:02 UTC (rev 23)
@@ -1,3 +1,23 @@
+/*
+ * Distribution Licence:
+ * FlashCard 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.
+ * 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.
+ * The License is available on the internet at:
+ *     http://www.gnu.org/copyleft/gpl.html,
+ * or by writing to:
+ *     Free Software Foundation, Inc.
+ *     59 Temple Place - Suite 330
+ *     Boston, MA 02111-1307, USA
+ * 
+ * The copyright to this program is held by it's authors
+ * Copyright: 2004
+ */
 ///////////////////////////////////////////////////////////////////////////
 //
 // Debug.java

Modified: trunk/app/src/org/crosswire/flashcards/Editor.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/Editor.java	2004-09-11 02:49:16 UTC (rev 22)
+++ trunk/app/src/org/crosswire/flashcards/Editor.java	2004-09-14 20:36:02 UTC (rev 23)
@@ -1,3 +1,23 @@
+/*
+ * Distribution Licence:
+ * FlashCard 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.
+ * 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.
+ * The License is available on the internet at:
+ *     http://www.gnu.org/copyleft/gpl.html,
+ * or by writing to:
+ *     Free Software Foundation, Inc.
+ *     59 Temple Place - Suite 330
+ *     Boston, MA 02111-1307, USA
+ * 
+ * The copyright to this program is held by it's authors
+ * Copyright: 2004
+ */
 ///////////////////////////////////////////////////////////////////////////
 //
 // Editor.java

Modified: trunk/app/src/org/crosswire/flashcards/EditorFrame.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/EditorFrame.java	2004-09-11 02:49:16 UTC (rev 22)
+++ trunk/app/src/org/crosswire/flashcards/EditorFrame.java	2004-09-14 20:36:02 UTC (rev 23)
@@ -1,3 +1,23 @@
+/*
+ * Distribution Licence:
+ * FlashCard 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.
+ * 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.
+ * The License is available on the internet at:
+ *     http://www.gnu.org/copyleft/gpl.html,
+ * or by writing to:
+ *     Free Software Foundation, Inc.
+ *     59 Temple Place - Suite 330
+ *     Boston, MA 02111-1307, USA
+ * 
+ * The copyright to this program is held by it's authors
+ * Copyright: 2004
+ */
 ///////////////////////////////////////////////////////////////////////////
 //
 // EditorFrame.java

Modified: trunk/app/src/org/crosswire/flashcards/EditorFrame_AboutBox.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/EditorFrame_AboutBox.java	2004-09-11 02:49:16 UTC (rev 22)
+++ trunk/app/src/org/crosswire/flashcards/EditorFrame_AboutBox.java	2004-09-14 20:36:02 UTC (rev 23)
@@ -1,3 +1,23 @@
+/*
+ * Distribution Licence:
+ * FlashCard 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.
+ * 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.
+ * The License is available on the internet at:
+ *     http://www.gnu.org/copyleft/gpl.html,
+ * or by writing to:
+ *     Free Software Foundation, Inc.
+ *     59 Temple Place - Suite 330
+ *     Boston, MA 02111-1307, USA
+ * 
+ * The copyright to this program is held by it's authors
+ * Copyright: 2004
+ */
 package org.crosswire.flashcards;
 
 import java.awt.AWTEvent;
@@ -16,15 +36,6 @@
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 
-/**
- * <p>Title: </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2003</p>
- * <p>Company: </p>
- * @author not attributable
- * @version 1.0
- */
-
 public class EditorFrame_AboutBox extends JDialog implements ActionListener {
 
   JPanel panel1 = new JPanel();

Added: trunk/app/src/org/crosswire/flashcards/FlashCard.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/FlashCard.java	2004-09-11 02:49:16 UTC (rev 22)
+++ trunk/app/src/org/crosswire/flashcards/FlashCard.java	2004-09-14 20:36:02 UTC (rev 23)
@@ -0,0 +1,159 @@
+/*
+ * Distribution Licence:
+ * FlashCard 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.
+ * 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.
+ * The License is available on the internet at:
+ *     http://www.gnu.org/copyleft/gpl.html,
+ * or by writing to:
+ *     Free Software Foundation, Inc.
+ *     59 Temple Place - Suite 330
+ *     Boston, MA 02111-1307, USA
+ * 
+ * The copyright to this program is held by it's authors
+ * Copyright: 2004
+ */
+package org.crosswire.flashcards;
+
+
+/**
+ * A FlashCard has a front and a back. The front has the test
+ * and the back has the answer.
+ * 
+ * @author Troy A. Griffitts [scribe at crosswire dot org]
+ * @author DM Smith [ dmsmith555 at yahoo dot com]
+ */
+public class FlashCard implements Cloneable, Comparable
+{
+    /**
+     * Create a partial FlashCard.
+     * @param front
+     */
+    public FlashCard(String front)
+    {
+        this(front, "");
+    }
+
+    /**
+     * Create a complete FlashCard
+     * @param front
+     * @param back
+     */
+    public FlashCard(String front, String back)
+    {
+        this.front = front;
+        this.back = back;
+        modified = true;
+    }
+
+    /**
+     * @return Returns the back.
+     */
+    public String getBack()
+    {
+        return back;
+    }
+
+    /**
+     * @param newBack The back to set.
+     */
+    public void setBack(String newBack)
+    {
+        if (newBack != null && !newBack.equals(back))
+        {
+            modified = true;
+            back = newBack;
+        }
+    }
+
+    /**
+     * @return Returns the front.
+     */
+    public String getFront()
+    {
+        return front;
+    }
+
+    /**
+     * @param newFront The front to set.
+     */
+    public void setFront(String newFront)
+    {
+        if (newFront != null && !newFront.equals(this.front))
+        {
+            modified = true;
+        	front = newFront;
+        }
+    }
+
+    /**
+     * @return Returns whether this FlashCard has been modified.
+     */
+    protected boolean isModified()
+    {
+        return modified;
+    }
+
+    /* (non-Javadoc)
+     * @see java.lang.Object#clone()
+     */
+    public Object clone() throws CloneNotSupportedException
+    {
+        return super.clone();
+    }
+
+    /* (non-Javadoc)
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    public boolean equals(Object obj)
+    {
+        if (obj == this)
+            return true;
+        if (!(obj instanceof FlashCard))
+            return false;
+        FlashCard otherCard = (FlashCard) obj;
+        return front.equals(otherCard.front)
+        	&& back.equals(otherCard.back);
+    }
+
+    /* (non-Javadoc)
+     * @see java.lang.Object#hashCode()
+     */
+    public int hashCode()
+    {
+        int hashCode = 31 + front.hashCode();
+        hashCode = 31 * hashCode + back.hashCode();
+        return hashCode;
+    }
+
+    /* (non-Javadoc)
+     * @see java.lang.Object#toString()
+     */
+    public String toString()
+    {
+        return front + " " + back;
+    }
+
+    /* (non-Javadoc)
+     * @see java.lang.Comparable#compareTo(java.lang.Object)
+     */
+    public int compareTo(Object obj)
+    {
+        FlashCard otherCard = (FlashCard) obj;
+        int result = front.compareTo(otherCard.front);
+        if (result == 0)
+        {
+            result = back.compareTo(otherCard.back);
+        }
+        return result;
+    }
+
+    private String front;
+    private String back;
+    private transient boolean modified;
+}
\ No newline at end of file

Added: trunk/app/src/org/crosswire/flashcards/Lesson.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/Lesson.java	2004-09-11 02:49:16 UTC (rev 22)
+++ trunk/app/src/org/crosswire/flashcards/Lesson.java	2004-09-14 20:36:02 UTC (rev 23)
@@ -0,0 +1,213 @@
+/*
+ * Distribution Licence:
+ * FlashCard 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.
+ * 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.
+ * The License is available on the internet at:
+ *     http://www.gnu.org/copyleft/gpl.html,
+ * or by writing to:
+ *     Free Software Foundation, Inc.
+ *     59 Temple Place - Suite 330
+ *     Boston, MA 02111-1307, USA
+ * 
+ * The copyright to this program is held by it's authors
+ * Copyright: 2004
+ */
+package org.crosswire.flashcards;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Iterator;
+import java.util.Properties;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.crosswire.common.CWClassLoader;
+import org.crosswire.common.ResourceUtil;
+
+/**
+ * A Lesson is an ordered list of FlashCards.
+ * The lesson also has a description which is useful for showing to a user.
+ * 
+ * @author Troy A. Griffitts [scribe at crosswire dot org]
+ * @author DM Smith [dmsmith555 at yahoo dot com]
+ */
+public class Lesson implements Comparable
+{
+    /**
+     * Construct a fully described, empty lesson.
+     * @param filename
+     * @param description
+     */
+    public Lesson(String filename, String description)
+    {
+        this.filename = filename;
+        this.description = description;
+        flashCards = new TreeSet();
+        load();
+    }
+
+    /**
+     * Appends the specified <code>FlashCard</code> to the end of this list.
+     *
+     * @param flashCard to be appended to this list.
+     */
+    public void add(FlashCard flashCard)
+    {
+        flashCards.add(flashCard);
+    }
+
+    /**
+     * @return Returns the filename.
+     */
+    public String getFilename()
+    {
+        return filename;
+    }
+
+    /**
+     * @return Returns the description.
+     */
+    public String getDescription()
+    {
+        return description;
+    }
+
+    /**
+     * @param newDescription The description to set.
+     */
+    public void setDescription(String newDescription)
+    {
+        if (newDescription != null && !newDescription.equals(description))
+        {
+            modified = true;
+            description = newDescription;
+        }
+    }
+
+    /**
+     * @return Returns the flashCards.
+     */
+    public Iterator iterator()
+    {
+        return flashCards.iterator();
+    }
+
+    /**
+     * @return whether this lesson has been modified
+     */
+    public boolean isModified()
+    {
+        if (modified)
+        {
+            return true;
+        }
+        Iterator iter = flashCards.iterator();
+        while (iter.hasNext())
+        {
+            FlashCard flashCard = (FlashCard) iter.next();
+            if (flashCard.isModified())
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /* (non-Javadoc)
+     * @see java.lang.Comparable#compareTo(java.lang.Object)
+     */
+    public int compareTo(Object obj)
+    {
+        Lesson lesson = (Lesson) obj;
+        return description.compareTo(lesson.description);
+    }
+
+    /**
+     * Load this lesson from persistent store named by the lesson's <code>filename</code>.
+     */
+    public void load()
+    {
+        Properties lesson = new Properties();
+        try
+        {
+            URL lessonURL = ResourceUtil.getResource(filename);
+            lesson.load(lessonURL.openConnection().getInputStream());
+        }
+        catch (Exception e1)
+        {
+            e1.printStackTrace();
+        }
+
+        int wordCount = Integer.parseInt(lesson.getProperty("wordCount"));
+        for (int i = 0; i < wordCount; i++)
+        {
+            add(new FlashCard(lesson.getProperty("word" + i), lesson.getProperty("answers" + i)));
+        }
+    }
+
+    /**
+     * Save this lesson to persistent store named by the lesson's <code>filename</code>.
+     */
+    public void store()
+    {
+        Properties lesson = new Properties();
+        try
+        {
+            lesson.setProperty("lessonTitle", description);
+            Iterator iter = flashCards.iterator();
+            int i = 0;
+            while (iter.hasNext())
+            {
+                FlashCard flashCard = (FlashCard) iter.next();
+                lesson.setProperty("word" + i, flashCard.getFront());
+                lesson.setProperty("answers" + i, flashCard.getBack());
+                i++;
+            }
+            lesson.setProperty("wordCount", Integer.toString(i));
+
+            // Save it as a "home" resource.
+            URL filePath = CWClassLoader.getHomeResource(filename);
+            File file = new File(filePath.getFile());
+            File dir = file.getParentFile();
+            // Is it already a directory ?
+            if (!dir.isDirectory())
+            {
+                dir.mkdirs();
+            }
+            lesson.store(new FileOutputStream(file), "Flash Lesson");
+        }
+        catch (IOException ex)
+        {
+            ex.printStackTrace();
+        }
+    }
+
+    /**
+     * The <code>filename</code> gives the relative location of the lesson.
+     * Typically this is something like lesson/setname/lessonname.flash.
+     */
+    private String filename;
+
+    /**
+     * A <code>description</code> of the lesson to be displayed to the user.
+     */
+    private String description;
+
+    /**
+     * An ordered list of <code>flashCards</code>
+     */
+    private Set flashCards;
+
+    /**
+     * Flag indicating whether this lesson has been <code>modified</code>
+     */
+    private boolean modified;
+}
\ No newline at end of file

Added: trunk/app/src/org/crosswire/flashcards/LessonManager.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/LessonManager.java	2004-09-11 02:49:16 UTC (rev 22)
+++ trunk/app/src/org/crosswire/flashcards/LessonManager.java	2004-09-14 20:36:02 UTC (rev 23)
@@ -0,0 +1,246 @@
+/*
+ * Distribution Licence:
+ * FlashCard 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.
+ * 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.
+ * The License is available on the internet at:
+ *     http://www.gnu.org/copyleft/gpl.html,
+ * or by writing to:
+ *     Free Software Foundation, Inc.
+ *     59 Temple Place - Suite 330
+ *     Boston, MA 02111-1307, USA
+ * 
+ * The copyright to this program is held by it's authors
+ * Copyright: 2004
+ */
+package org.crosswire.flashcards;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.JarURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+
+import javax.swing.JCheckBox;
+
+import org.crosswire.common.CWClassLoader;
+import org.crosswire.common.ResourceUtil;
+
+/**
+ * The <code>LessonManager</code> provides the management of <code>LessonSet</code>s.
+ * 
+ * @author Troy A. Griffitts [scribe at crosswire dot org]
+ * @author DM Smith [dmsmith555 at yahoo dot com]
+ */
+public class LessonManager implements Comparable
+{
+    public LessonManager(String dirname)
+    {
+        this.dirname = dirname;
+        lessonSets = new ArrayList();
+    }
+
+    /**
+     * Appends the specified <code>Lesson</code> to the end of this list.
+     *
+     * @param flashCard to be appended to this list.
+     */
+    public void add(LessonSet aLessonSet)
+    {
+        lessonSets.add(aLessonSet);
+    }
+
+    /**
+     * Removes the <code>LessonSet</code> at the specified position in this list.
+     * Shifts any subsequent FlashCards to the left (subtracts one from their
+     * indices).
+     *
+     * @param index the index of the Lesson to removed.
+     * @return the Lesson that was removed from the list.
+     * @throws    IndexOutOfBoundsException if index out of range <tt>(index
+     * 		  &lt; 0 || index &gt;= size())</tt>.
+     */
+    public LessonSet remove(int index)
+    {
+        return (LessonSet) lessonSets.remove(index);
+    }
+
+    /**
+     * @return Returns the description.
+     */
+    public String getDescription()
+    {
+        return description;
+    }
+
+    /**
+     * @param description The description to set.
+     */
+    public void setDescription(String newDescription)
+    {
+        description = newDescription;
+    }
+
+    /**
+     * @return Returns the dirname.
+     */
+    public String getDirname()
+    {
+        return dirname;
+    }
+
+    /**
+     * @param dirname The dirname to set.
+     */
+    public void setDirname(String newFilename)
+    {
+        dirname = newFilename;
+    }
+
+    /* (non-Javadoc)
+     * @see java.lang.Comparable#compareTo(java.lang.Object)
+     */
+    public int compareTo(Object obj)
+    {
+        LessonManager lesson = (LessonManager) obj;
+        return description.compareTo(lesson.description);
+    }
+
+    /**
+     * Load this lesson from persistent store named by the lesson's <code>dirname</code>.
+     */
+    public void load()
+    {
+        loadJarLessonSets();
+        loadHomeLessonSets();
+    }
+
+    /**
+     * Load lesson sets from the jar file
+     */
+    private void loadJarLessonSets()
+    {
+
+        // Dig into the jar for lessonSets
+        URL lessonsURL = this.getClass().getResource('/' + dirname);
+        if (lessonsURL == null)
+        {
+            return;
+        }
+        URLConnection connection = null;
+        try
+        {
+            connection = lessonsURL.openConnection();
+        }
+        catch (Exception e1)
+        {
+            assert false;
+        }
+        if (connection instanceof JarURLConnection)
+        {
+            JarURLConnection jarConnection = (JarURLConnection) connection;
+            JarFile jarFile = null;
+            try
+            {
+                jarFile = jarConnection.getJarFile();
+            }
+            catch (IOException e2)
+            {
+                assert false;
+            }
+            Enumeration entries = jarFile.entries();
+            while (entries.hasMoreElements())
+            {
+                JarEntry jarEntry = (JarEntry) entries.nextElement();
+                if (jarEntry.isDirectory())
+                {
+                    String entryName = jarEntry.getName();
+                    // remove trailing '/'
+                    entryName = entryName.substring(0, entryName.length() - 1);
+                    if (entryName.startsWith(dirname) && ! entryName.equals(dirname))
+                    {
+                        // let the description be just the directory name and not the path
+                        add(new LessonSet(entryName, entryName.substring(entryName.indexOf('/') + 1)));
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * Load lesson sets from the "home" directory
+     */
+    private void loadHomeLessonSets()
+    {
+        try
+        {
+            URL dirURL = CWClassLoader.getHomeResource(dirname);
+            File directory = new File(dirURL.getFile());
+            File[] files = directory.listFiles();
+            if (files == null)
+            {
+                return;
+            }
+            Arrays.sort(files);
+            for (int i = 0; i < files.length; i++)
+            {
+                File file = files[i];
+                if (file.isDirectory())
+                {
+                    // let the description be just the directory name and not the path
+                    add(new LessonSet(file.getPath(), file.getName()));
+                }
+            }
+        }
+        catch (Exception e)
+        {
+            // that's fine.  We just failed to load local files.
+        }
+    }
+    
+    /**
+     * Save all the modified lesson sets to persistent store named by the lesson's <code>dirname</code>.
+     */
+    public void store()
+    {
+        Iterator iter = lessonSets.iterator();
+        while (iter.hasNext())
+        {
+            LessonSet lessonSet = (LessonSet) iter.next();
+            if (lessonSet.isModified())
+            {
+                lessonSet.store();
+            }
+        }
+    }
+
+    /**
+     * The <code>dirname</code> of the lesson
+     */
+    private String dirname;
+
+    /**
+     * A <code>description</code> of the lesson to be displayed to the user.
+     */
+    private String description;
+
+    /**
+     * An ordered list of <code>lessonSets</code>
+     */
+    private List lessonSets;
+}
\ No newline at end of file

Added: trunk/app/src/org/crosswire/flashcards/LessonSet.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/LessonSet.java	2004-09-11 02:49:16 UTC (rev 22)
+++ trunk/app/src/org/crosswire/flashcards/LessonSet.java	2004-09-14 20:36:02 UTC (rev 23)
@@ -0,0 +1,293 @@
+/*
+ * Distribution Licence:
+ * FlashCard 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.
+ * 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.
+ * The License is available on the internet at:
+ *     http://www.gnu.org/copyleft/gpl.html,
+ * or by writing to:
+ *     Free Software Foundation, Inc.
+ *     59 Temple Place - Suite 330
+ *     Boston, MA 02111-1307, USA
+ * 
+ * The copyright to this program is held by it's authors
+ * Copyright: 2004
+ */
+package org.crosswire.flashcards;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.JarURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+
+import org.crosswire.common.CWClassLoader;
+import org.crosswire.common.ResourceUtil;
+
+/**
+ * A <code>LessonSet</code> is an ordered list of <code>Lesson</code>s.
+ * The lessons are sorted by filename.
+ * The lesson set also has a description which is useful for showing to a user
+ * and a directory name where its Lessons are stored. This directory name is expected to be a relative
+ * path and will be stored either in a jar or in the user's FlashCard directory.
+ * 
+ * @author Troy A. Griffitts [scribe at crosswire dot org]
+ * @author DM Smith [dmsmith555 at yahoo dot com]
+ */
+public class LessonSet implements Comparable
+{
+    public LessonSet(String dirname, String description)
+    {
+        this.dirname = dirname;
+        this.description = description;
+        lessons = new TreeSet();
+        load();
+    }
+
+    /**
+     * Adds the specified <code>Lesson</code> to this lesson set.
+     *
+     * @param flashCard to be added.
+     */
+    public void add(Lesson lesson)
+    {
+        modified = true;
+        lessons.add(lesson);
+    }
+
+    /**
+     * @return Returns the description.
+     */
+    public String getDescription()
+    {
+        return description;
+    }
+
+    /**
+     * @param description The description to set.
+     */
+    public void setDescription(String newDescription)
+    {
+        if (newDescription != null && !newDescription.equals(description))
+        {
+            modified = true;
+            description = newDescription;
+        }
+    }
+
+    /**
+     * @return Returns the dirname.
+     */
+    public String getDirname()
+    {
+        return dirname;
+    }
+
+    /* (non-Javadoc)
+     * @see java.lang.Comparable#compareTo(java.lang.Object)
+     */
+    public int compareTo(Object obj)
+    {
+        LessonSet lesson = (LessonSet) obj;
+        return description.compareTo(lesson.description);
+    }
+
+    /**
+     * Load this lesson set from persistent store named by the lesson set's <code>dirname</code>.
+     * This is the union of lessons in the Jar and in the user's flashcard home directory.
+     */
+    public void load()
+    {
+        loadJarLessons();
+        loadHomeLessons();
+        Iterator iter = lessons.iterator();
+        while (iter.hasNext())
+        {
+            String lessonPath = (String) iter.next();
+            String lessonDescription = getLessonDescription(lessonPath);
+            add(new Lesson(lessonPath, lessonDescription));
+        }
+    }
+
+    /**
+     * Get the relative path names of the lessons in this lesson set from
+     * the jar file.
+     * @param lessonSet
+     */
+    private void loadJarLessons()
+    {
+
+        // Dig into the jar for lessons
+        URL lessonsURL = this.getClass().getResource('/' + dirname);
+        if (lessonsURL == null)
+        {
+            return;
+        }
+        URLConnection connection = null;
+        try
+        {
+            connection = lessonsURL.openConnection();
+        }
+        catch (Exception e1)
+        {
+            assert false;
+        }
+        if (connection instanceof JarURLConnection)
+        {
+            JarURLConnection jarConnection = (JarURLConnection) connection;
+            JarFile jarFile = null;
+            try
+            {
+                jarFile = jarConnection.getJarFile();
+            }
+            catch (IOException e2)
+            {
+                assert false;
+            }
+            if (jarFile == null)
+            {
+                return;
+            }
+            Enumeration entries = jarFile.entries();
+            while (entries.hasMoreElements())
+            {
+                JarEntry jarEntry = (JarEntry) entries.nextElement();
+                String entryName = jarEntry.getName();
+                if (entryName.startsWith(dirname) && ! jarEntry.isDirectory())
+                {
+                    lessons.add(entryName);
+                }
+            }
+        }
+    }
+
+    /**
+     * Get the relative path names of the lessons in this lesson set from
+     * the user's program home.
+     * @param lessonSet
+     */
+    private void loadHomeLessons()
+    {
+        try
+        {
+            URL dirURL = CWClassLoader.getHomeResource(dirname);
+            File directory = new File(dirURL.getFile());
+            File[] files = directory.listFiles();
+            if (files == null)
+            {
+                return;
+            }
+            Arrays.sort(files);
+            for (int i = 0; i < files.length; i++)
+            {
+                // convert the path to one that is relative to the home and has forward slashes
+                File file = files[i];
+                String lessonPath = file.getPath();
+                // If it uses \ as a path separator then replace it w/ /
+                lessonPath = lessonPath.replace('\\', '/');
+                int offset = lessonPath.indexOf(dirname);
+                lessonPath = lessonPath.substring(offset, lessonPath.length());
+                lessons.add(lessonPath);
+            }
+        }
+        catch (Exception e)
+        {
+            // that's fine.  We just failed to load local files.
+        }
+    }
+    
+    /**
+     * Get the description of the lesson
+     * @param lessonpath the relative path to the lesson
+     * @return the description of the lesson
+     */
+    private String getLessonDescription(String lessonpath)
+    {
+        URL lessonURL = ResourceUtil.getResource(lessonpath);
+        Properties p = new Properties();
+        try
+        {
+            p.load(lessonURL.openConnection().getInputStream());
+        }
+        catch (IOException ex)
+        {
+            assert false;
+        }
+        return p.getProperty("lessonTitle");
+    }
+
+    /**
+     * Save this lesson to persistent store named by the lesson's <code>dirname</code>.
+     */
+    public void store()
+    {
+        Iterator iter = lessons.iterator();
+        while (iter.hasNext())
+        {
+            Lesson lesson = (Lesson) iter.next();
+            if (lesson.isModified())
+            {
+                lesson.store();
+            }
+        }
+    }
+
+    /**
+     * @return whether the lesson set has been modified
+     */
+    public boolean isModified()
+    {
+        if (modified)
+        {
+            return true;
+        }
+
+        Iterator iter = lessons.iterator();
+        while (iter.hasNext())
+        {
+            Lesson lesson = (Lesson) iter.next();
+            if (lesson.isModified())
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * The <code>dirname</code> of the lesson
+     */
+    private String dirname;
+
+    /**
+     * A <code>description</code> of the lesson to be displayed to the user.
+     */
+    private String description;
+
+    /**
+     * An ordered list of <code>lessons</code>
+     */
+    private Set lessons;
+
+    /**
+     * Flag indicating whether this lesson set has been <code>modified</code>
+     */
+    private boolean modified;
+}
\ No newline at end of file

Modified: trunk/app/src/org/crosswire/flashcards/MainFrame.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/MainFrame.java	2004-09-11 02:49:16 UTC (rev 22)
+++ trunk/app/src/org/crosswire/flashcards/MainFrame.java	2004-09-14 20:36:02 UTC (rev 23)
@@ -1,3 +1,23 @@
+/*
+ * Distribution Licence:
+ * FlashCard 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.
+ * 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.
+ * The License is available on the internet at:
+ *     http://www.gnu.org/copyleft/gpl.html,
+ * or by writing to:
+ *     Free Software Foundation, Inc.
+ *     59 Temple Place - Suite 330
+ *     Boston, MA 02111-1307, USA
+ * 
+ * The copyright to this program is held by it's authors
+ * Copyright: 2004
+ */
 package org.crosswire.flashcards;
 
 import java.awt.AWTEvent;
@@ -48,15 +68,6 @@
 import org.crosswire.common.CWClassLoader;
 import org.crosswire.common.ResourceUtil;
 
-/**
- * <p>Title: </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2003</p>
- * <p>Company: </p>
- * @author not attributable
- * @version 1.0
- */
-
 public class MainFrame extends JFrame {
   JPanel contentPane;
   JLabel statusBar = new JLabel();

Modified: trunk/app/src/org/crosswire/flashcards/MainFrame_AboutBox.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/MainFrame_AboutBox.java	2004-09-11 02:49:16 UTC (rev 22)
+++ trunk/app/src/org/crosswire/flashcards/MainFrame_AboutBox.java	2004-09-14 20:36:02 UTC (rev 23)
@@ -1,3 +1,23 @@
+/*
+ * Distribution Licence:
+ * FlashCard 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.
+ * 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.
+ * The License is available on the internet at:
+ *     http://www.gnu.org/copyleft/gpl.html,
+ * or by writing to:
+ *     Free Software Foundation, Inc.
+ *     59 Temple Place - Suite 330
+ *     Boston, MA 02111-1307, USA
+ * 
+ * The copyright to this program is held by it's authors
+ * Copyright: 2004
+ */
 package org.crosswire.flashcards;
 
 import java.awt.AWTEvent;
@@ -16,15 +36,6 @@
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 
-/**
- * <p>Title: </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2003</p>
- * <p>Company: </p>
- * @author not attributable
- * @version 1.0
- */
-
 public class MainFrame_AboutBox extends JDialog implements ActionListener {
 
   JPanel panel1 = new JPanel();

Modified: trunk/app/src/org/crosswire/flashcards/MainMenu.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/MainMenu.java	2004-09-11 02:49:16 UTC (rev 22)
+++ trunk/app/src/org/crosswire/flashcards/MainMenu.java	2004-09-14 20:36:02 UTC (rev 23)
@@ -1,3 +1,23 @@
+/*
+ * Distribution Licence:
+ * FlashCard 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.
+ * 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.
+ * The License is available on the internet at:
+ *     http://www.gnu.org/copyleft/gpl.html,
+ * or by writing to:
+ *     Free Software Foundation, Inc.
+ *     59 Temple Place - Suite 330
+ *     Boston, MA 02111-1307, USA
+ * 
+ * The copyright to this program is held by it's authors
+ * Copyright: 2004
+ */
 ///////////////////////////////////////////////////////////////////////////
 //
 // MainMenu.java

Modified: trunk/app/src/org/crosswire/flashcards/OpenFile.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/OpenFile.java	2004-09-11 02:49:16 UTC (rev 22)
+++ trunk/app/src/org/crosswire/flashcards/OpenFile.java	2004-09-14 20:36:02 UTC (rev 23)
@@ -1,3 +1,23 @@
+/*
+ * Distribution Licence:
+ * FlashCard 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.
+ * 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.
+ * The License is available on the internet at:
+ *     http://www.gnu.org/copyleft/gpl.html,
+ * or by writing to:
+ *     Free Software Foundation, Inc.
+ *     59 Temple Place - Suite 330
+ *     Boston, MA 02111-1307, USA
+ * 
+ * The copyright to this program is held by it's authors
+ * Copyright: 2004
+ */
 package org.crosswire.flashcards;
 
 import java.awt.BorderLayout;
@@ -7,15 +27,6 @@
 import javax.swing.JFileChooser;
 import javax.swing.JPanel;
 
-/**
- * <p>Title: </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2003</p>
- * <p>Company: </p>
- * @author not attributable
- * @version 1.0
- */
-
 public class OpenFile extends JDialog {
   JPanel panel1 = new JPanel();
   BorderLayout borderLayout1 = new BorderLayout();

Modified: trunk/app/src/org/crosswire/flashcards/Quiz.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/Quiz.java	2004-09-11 02:49:16 UTC (rev 22)
+++ trunk/app/src/org/crosswire/flashcards/Quiz.java	2004-09-14 20:36:02 UTC (rev 23)
@@ -1,3 +1,23 @@
+/*
+ * Distribution Licence:
+ * FlashCard 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.
+ * 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.
+ * The License is available on the internet at:
+ *     http://www.gnu.org/copyleft/gpl.html,
+ * or by writing to:
+ *     Free Software Foundation, Inc.
+ *     59 Temple Place - Suite 330
+ *     Boston, MA 02111-1307, USA
+ * 
+ * The copyright to this program is held by it's authors.
+ * Copyright: 2004
+ */
 ///////////////////////////////////////////////////////////////////////////
 //
 // Quiz.java



More information about the sword-cvs mailing list