[sword-svn] r104 - in trunk: app/src/org/crosswire/flashcards micro/src/org/crosswire/flashcards micro/src/org/crosswire/flashcards/mobile src/org/crosswire/flashcards

Apache apache at www.crosswire.org
Sat Dec 9 16:14:41 MST 2006


Author: 
Date: 2006-12-09 16:14:40 -0700 (Sat, 09 Dec 2006)
New Revision: 104

Modified:
   trunk/app/src/org/crosswire/flashcards/ComplexLesson.java
   trunk/app/src/org/crosswire/flashcards/ComplexLessonSet.java
   trunk/app/src/org/crosswire/flashcards/EditPane.java
   trunk/app/src/org/crosswire/flashcards/LessonManager.java
   trunk/micro/src/org/crosswire/flashcards/Properties.java
   trunk/micro/src/org/crosswire/flashcards/mobile/Lessons.java
   trunk/micro/src/org/crosswire/flashcards/mobile/Quiz.java
   trunk/src/org/crosswire/flashcards/Quizer.java
Log:
Added support to desktop flashcards editor to prerender word images
Added support in micro reader to use image if available


Modified: trunk/app/src/org/crosswire/flashcards/ComplexLesson.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/ComplexLesson.java	2006-12-09 13:51:24 UTC (rev 103)
+++ trunk/app/src/org/crosswire/flashcards/ComplexLesson.java	2006-12-09 23:14:40 UTC (rev 104)
@@ -33,6 +33,10 @@
 import java.util.Properties;
 import java.util.Set;
 import java.util.Vector;
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.awt.Graphics2D;
+import java.awt.Color;
 
 
 /**
@@ -42,16 +46,19 @@
  * @author Troy A. Griffitts [scribe at crosswire dot org]
  * @author DM Smith [dmsmith555 at yahoo dot com]
  */
-public class ComplexLesson extends Lesson {
+public class ComplexLesson
+          extends Lesson {
 
      public ComplexLesson(String url) throws Exception {
           super(url);
      }
 
+
      public ComplexLesson(String url, String description) throws Exception {
           super(url, description);
      }
 
+
      /**
       * Load this lesson from persistent store named by the lesson's <code>filename</code>.
       */
@@ -67,7 +74,7 @@
                if (baseOffset < 0) {
                     baseOffset = getURL().lastIndexOf( ("\\"));
                }
-               String lname = getURL().substring(baseOffset+1);
+               String lname = getURL().substring(baseOffset + 1);
                lname = lname.substring(0, lname.indexOf(".flash"));
                String audioPath = getURL().substring(0, baseOffset) + "/audio";
 
@@ -99,7 +106,7 @@
           try {
                lesson.setProperty("lessonTitle", getDescription());
                int i = 0;
-               for (;i < getFlashcards().size(); i++) {
+               for (; i < getFlashcards().size(); i++) {
                     FlashCard flashCard = (FlashCard) getFlashcards().get(i);
                     lesson.setProperty("word" + i, flashCard.getFront());
                     lesson.setProperty("answers" + i, flashCard.getBack());
@@ -112,7 +119,8 @@
                File file = null;
                URLConnection connection = filePath.openConnection();
                if (connection instanceof JarURLConnection) {
-                    file = new File(LessonManager.instance().getHomeProjectPath() + File.separator + ((JarURLConnection)connection).getEntryName());
+                    file = new File(LessonManager.instance().getHomeProjectPath() + File.separator +
+                                    ( (JarURLConnection) connection).getEntryName());
                }
                else {
                     file = new File(filePath.getFile());
@@ -127,18 +135,98 @@
                setModified(false);
           }
           catch (IOException ex) {
-              Debug.error(this.getClass().getName(), ex.getMessage());
-          } finally {
-              if (outStream != null) {
-                  try
-                  {
-                      outStream.close();
-                  }
-                  catch (IOException e)
-                  {
-                      Debug.error(this.getClass().getName(), e.getMessage());
-                  }
-              }
+               Debug.error(this.getClass().getName(), ex.getMessage());
           }
+          finally {
+               if (outStream != null) {
+                    try {
+                         outStream.close();
+                    }
+                    catch (IOException e) {
+                         Debug.error(this.getClass().getName(), e.getMessage());
+                    }
+               }
+          }
      }
+
+
+     /**
+      * Save this lesson to persistent store named by the lesson's <code>filename</code>.
+      */
+     public void generateImages() {
+          OutputStream outStream = null;
+          try {
+               // Create an image to save
+               int baseOffset = getURL().lastIndexOf("/");
+               if (baseOffset < 0) {
+                    baseOffset = getURL().lastIndexOf( ("\\"));
+               }
+               String lname = getURL().substring(baseOffset + 1);
+               lname = lname.substring(0, lname.indexOf(".flash"));
+               String imagesPath = getURL().substring(0, baseOffset) + "/images";
+
+               int i = 0;
+               for (; i < getFlashcards().size(); i++) {
+                    FlashCard f = (FlashCard)getFlashcards().elementAt(i);
+                    String imageURLString = imagesPath + "/" + lname + "_" + Integer.toString(i) + ".png";
+                    // Save it as a "home" resource.
+                    URL filePath = new URL(imageURLString);
+                    File file = null;
+                    URLConnection connection = filePath.openConnection();
+                    if (connection instanceof JarURLConnection) {
+                         file = new File(LessonManager.instance().getHomeProjectPath() + File.separator +
+                                         ( (JarURLConnection) connection).getEntryName());
+                    }
+                    else {
+                         file = new File(filePath.getFile());
+                    }
+                    File dir = file.getParentFile();
+                    // Is it already a directory ?
+                    if (!dir.isDirectory()) {
+                         dir.mkdirs();
+                    }
+                    outStream = new FileOutputStream(file);
+                    int width = 100;
+                    int height = 20;
+
+                    // Create a buffered image in which to draw
+                    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+
+                    // Create a graphics contents on the buffered image
+                    Graphics2D g2d = bufferedImage.createGraphics();
+
+                    // Draw graphics
+                    g2d.setColor(Color.white);
+                    g2d.fillRect(0, 0, width, height);
+                    g2d.setColor(Color.black);
+                    g2d.drawString(f.getFront(), 2, 15);
+
+                    // Graphics context no longer needed so dispose it
+                    g2d.dispose();
+
+                    // Write generated image to a file
+                    try {
+                         // Save as PNG
+                         ImageIO.write(bufferedImage, "png", outStream);
+
+                    }
+                    catch (IOException e) {
+                    }
+
+               }
+          }
+          catch (IOException ex) {
+               Debug.error(this.getClass().getName(), ex.getMessage());
+          }
+          finally {
+               if (outStream != null) {
+                    try {
+                         outStream.close();
+                    }
+                    catch (IOException e) {
+                         Debug.error(this.getClass().getName(), e.getMessage());
+                    }
+               }
+          }
+     }
 }

Modified: trunk/app/src/org/crosswire/flashcards/ComplexLessonSet.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/ComplexLessonSet.java	2006-12-09 13:51:24 UTC (rev 103)
+++ trunk/app/src/org/crosswire/flashcards/ComplexLessonSet.java	2006-12-09 23:14:40 UTC (rev 104)
@@ -142,4 +142,16 @@
               return name.toUpperCase(Locale.ENGLISH).endsWith(".FLASH");
          }
     }
+
+
+     /**
+      * Save this lesson to persistent store named by the lesson's <code>dirname</code>.
+      */
+     public void generateImages() {
+          for (int i = 0; i < getLessons().size(); i++) {
+               ComplexLesson lesson = (ComplexLesson) getLessons().elementAt(i);
+		     lesson.generateImages();
+          }
+     }
+
 }

Modified: trunk/app/src/org/crosswire/flashcards/EditPane.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/EditPane.java	2006-12-09 13:51:24 UTC (rev 103)
+++ trunk/app/src/org/crosswire/flashcards/EditPane.java	2006-12-09 23:14:40 UTC (rev 104)
@@ -47,8 +47,9 @@
      * Serialization ID
      */
     private static final long serialVersionUID = 8637424690635575114L;
+     JButton imageGenButton = new JButton();
 
-    //Construct the frame
+     //Construct the frame
     public EditPane()
     {
         jbInit();
@@ -182,10 +183,32 @@
         verticalSplitPane.setResizeWeight(0.35D);
         verticalSplitPane.setTopComponent(lessonSplitPane);
         verticalSplitPane.setBottomComponent(flashCardSplitPane);
-        add(verticalSplitPane, BorderLayout.CENTER);
+          imageGenButton.setText("Prerender Word Images");
+          imageGenButton.addActionListener(new EditPane_imageGenButton_actionAdapter(this));
+          add(verticalSplitPane, BorderLayout.CENTER);
 
         JPanel buttonPane = new JPanel();
         buttonPane.add(saveButton);
-        add(buttonPane, BorderLayout.SOUTH);
-    }
+          buttonPane.add(imageGenButton);
+          add(buttonPane, BorderLayout.SOUTH);
+     }
+
+
+     public void imageGenButton_actionPerformed(ActionEvent e) {
+	      LessonManager.instance().genImages();
+     }
 }
+
+
+class EditPane_imageGenButton_actionAdapter
+          implements ActionListener {
+     private EditPane adaptee;
+     EditPane_imageGenButton_actionAdapter(EditPane adaptee) {
+          this.adaptee = adaptee;
+     }
+
+
+     public void actionPerformed(ActionEvent e) {
+          adaptee.imageGenButton_actionPerformed(e);
+     }
+}

Modified: trunk/app/src/org/crosswire/flashcards/LessonManager.java
===================================================================
--- trunk/app/src/org/crosswire/flashcards/LessonManager.java	2006-12-09 13:51:24 UTC (rev 103)
+++ trunk/app/src/org/crosswire/flashcards/LessonManager.java	2006-12-09 23:14:40 UTC (rev 104)
@@ -254,7 +254,18 @@
           }
      }
 
+     /**
+      * Save all the modified lesson sets to persistent store named by the lesson's <code>LESSON_ROOT</code>.
+      */
+     public void genImages() {
+          Iterator iter = lessonSets.iterator();
+          while (iter.hasNext()) {
+               ComplexLessonSet lessonSet = (ComplexLessonSet) iter.next();
+               lessonSet.generateImages();
+          }
+     }
 
+
      public Iterator iterator() {
           return lessonSets.iterator();
      }

Modified: trunk/micro/src/org/crosswire/flashcards/Properties.java
===================================================================
--- trunk/micro/src/org/crosswire/flashcards/Properties.java	2006-12-09 13:51:24 UTC (rev 103)
+++ trunk/micro/src/org/crosswire/flashcards/Properties.java	2006-12-09 23:14:40 UTC (rev 104)
@@ -59,6 +59,28 @@
 
   }
 
+  private String getInputStreamContents(InputStream is) {
+    InputStreamReader isr = null;
+    StringBuffer buffer = null;
+    try {
+      isr = new InputStreamReader(is, "UTF8");
+
+      buffer = new StringBuffer();
+      int ch;
+      while ( (ch = isr.read()) > -1) {
+        buffer.append( (char) ch);
+      }
+      if (isr != null) {
+        isr.close();
+      }
+    }
+    catch (Exception ex) {
+      System.out.println(ex);
+    }
+    return buffer.toString();
+
+  }
+
   public void setProperty(String key, String value) {
     values.put(key, value);
   }
@@ -98,25 +120,4 @@
     return in;
   }
 
-  private String getInputStreamContents(InputStream is) {
-    InputStreamReader isr = null;
-    StringBuffer buffer = null;
-    try {
-      isr = new InputStreamReader(is, "UTF8");
-
-      buffer = new StringBuffer();
-      int ch;
-      while ( (ch = isr.read()) > -1) {
-        buffer.append( (char) ch);
-      }
-      if (isr != null) {
-        isr.close();
-      }
-    }
-    catch (Exception ex) {
-      System.out.println(ex);
-    }
-    return buffer.toString();
-
-  }
 }

Modified: trunk/micro/src/org/crosswire/flashcards/mobile/Lessons.java
===================================================================
--- trunk/micro/src/org/crosswire/flashcards/mobile/Lessons.java	2006-12-09 13:51:24 UTC (rev 103)
+++ trunk/micro/src/org/crosswire/flashcards/mobile/Lessons.java	2006-12-09 23:14:40 UTC (rev 104)
@@ -18,7 +18,7 @@
  */
 public class Lessons extends Form implements CommandListener {
 
-  ChoiceGroup lessonChoice = new ChoiceGroup("", ChoiceGroup.EXCLUSIVE);
+  ChoiceGroup lessonChoice = new ChoiceGroup("", ChoiceGroup.MULTIPLE);
 
   public Lessons() {
     super("Choose Lessons");

Modified: trunk/micro/src/org/crosswire/flashcards/mobile/Quiz.java
===================================================================
--- trunk/micro/src/org/crosswire/flashcards/mobile/Quiz.java	2006-12-09 13:51:24 UTC (rev 103)
+++ trunk/micro/src/org/crosswire/flashcards/mobile/Quiz.java	2006-12-09 23:14:40 UTC (rev 104)
@@ -5,6 +5,7 @@
 import org.crosswire.flashcards.Quizer;
 import org.crosswire.flashcards.FlashCard;
 import java.util.Vector;
+import java.io.InputStream;
 
 /**
  * <p>Title: </p>
@@ -20,11 +21,10 @@
  */
 public class Quiz extends Form implements CommandListener {
 
-  StringItem wordDisplay = new StringItem("", "");
+  ImageItem wordImage = new ImageItem("", null, ImageItem.LAYOUT_DEFAULT, "");
   ChoiceGroup answersDisplay = new ChoiceGroup("", ChoiceGroup.EXCLUSIVE);
   StringItem statusBar = new StringItem("", "");
-  Spacer spacer1 = new Spacer(0, 0);
-  Spacer spacer2 = new Spacer(0, 0);
+
   Quizer quizer = new Quizer();
   FlashCard currentWord = null;
   int wrongThisTime = 0;
@@ -44,31 +44,27 @@
     setCommandListener(this);
     addCommand(new Command("End", Command.BACK, 1));
     addCommand(new Command("Answer", Command.SCREEN, 2));
-    wordDisplay.setLayout(Item.LAYOUT_CENTER | Item.LAYOUT_TOP |
-                          Item.LAYOUT_NEWLINE_BEFORE |
-                          Item.LAYOUT_NEWLINE_AFTER);
-    wordDisplay.setText("Word");
-    this.append(spacer1);
-    this.append(wordDisplay);
-    this.append(spacer2);
+    this.append(wordImage);
     this.append(answersDisplay);
     this.append(statusBar);
-    answersDisplay.setLabel("Answers");
-    answersDisplay.setLayout(Item.LAYOUT_CENTER | Item.LAYOUT_VCENTER |
+    answersDisplay.setLabel(null);
+    answersDisplay.setLayout(Item.LAYOUT_LEFT | Item.LAYOUT_TOP |
                              Item.LAYOUT_VEXPAND);
-    statusBar.setLayout(Item.LAYOUT_BOTTOM | Item.LAYOUT_VEXPAND |
-                        Item.LAYOUT_NEWLINE_BEFORE);
     statusBar.setText("StatusBar");
-//    spacer1.setPreferredSize();
-    spacer1.setMinimumSize(2, 2);
+    wordImage.setLayout(Item.LAYOUT_LEFT | Item.LAYOUT_TOP |
+                        Item.LAYOUT_VSHRINK);
   }
 
   void show() {
-    int lessonNum = FlashCards.instance.lessons.lessonChoice.getSelectedIndex();
-    String lessonName = FlashCards.instance.lessons.lessonChoice.getString(lessonNum);
-    Lesson l = FlashCards.instance.lessonSet.getLesson(lessonName);
+    int lessonCount = FlashCards.instance.lessons.lessonChoice.size();
     quizer.clear();
-    quizer.loadLesson(l);
+    for (int i = 0; i < lessonCount; i++) {
+      if (FlashCards.instance.lessons.lessonChoice.isSelected(i)) {
+        String lessonName = FlashCards.instance.lessons.lessonChoice.getString(i);
+        Lesson l = FlashCards.instance.lessonSet.getLesson(lessonName);
+        quizer.loadLesson(l);
+      }
+    }
     wordDisplay(-1, "Begin");
     Display.getDisplay(FlashCards.instance).setCurrent(this);
   }
@@ -91,7 +87,7 @@
           wordDisplay(wrongThisTime, "Correct");
         }
         else {
-          setStatus("Wrong. Try again.");
+          setStatus("Try again.");
           wrongThisTime++;
         }
       }
@@ -108,8 +104,22 @@
       setStatus("Great Job!");
       return;
     }
-    wordDisplay.setText(currentWord.getFront());
-    Vector answers = quizer.getRandomAnswers(4);
+    InputStream is = null;
+    try {
+      Class c = currentWord.getClass();
+      is = c.getResourceAsStream(currentWord.getImageURL());
+      if (is != null) {
+	Image image = Image.createImage(is);
+	wordImage.setImage(image);
+	wordImage.setLabel(null);
+      }
+    }
+    catch (Exception e) { e.printStackTrace();}
+    if (is == null) {
+      wordImage.setImage(null);
+      wordImage.setLabel(currentWord.getFront());
+    }
+    Vector answers = quizer.getRandomAnswers(5);
     answersDisplay.deleteAll();
     for (int i = 0; i < answers.size(); i++) {
       String a = (String) answers.elementAt(i);

Modified: trunk/src/org/crosswire/flashcards/Quizer.java
===================================================================
--- trunk/src/org/crosswire/flashcards/Quizer.java	2006-12-09 13:51:24 UTC (rev 103)
+++ trunk/src/org/crosswire/flashcards/Quizer.java	2006-12-09 23:14:40 UTC (rev 104)
@@ -113,19 +113,27 @@
       }
       else {
         lastWord.incrementFailures(-1);
-        // if we're the last word, don't ask again
-        if ((notLearned.size() == 1) || (lastWord.getFailures() < 0)) {
-          notLearned.removeElement(lastWord);
-	  notLearned.trimToSize();
-        }
       }
+      // if we're the last word, don't ask again
+      if ((notLearned.size() == 1) || (lastWord.getFailures() < 0)) {
+	notLearned.removeElement(lastWord);
+	notLearned.trimToSize();
+      }
     }
 
     int numToLearn = notLearned.size();
     if (numToLearn == 0) {
       return null;
     }
-    WordEntry currentWord = lastWord;
+
+    WordEntry currentWord = null;
+
+    // if there are more than 1 words available be sure we don't get the same word
+    if (numToLearn != 1) currentWord = lastWord;
+
+    // if we just want a new word and not report anything, find the NEXT word
+    // because we're likely cycling throw the words looking at answers and don't
+    // want random answers which might include repeats
     if ( (wrongCount < 0) && (currentWord != null)) {
       int next = notLearned.indexOf(lastWord) + 1;
       if (next >= notLearned.size()) {
@@ -133,10 +141,13 @@
       }
       currentWord = (WordEntry) notLearned.elementAt(next);
     }
+
+    // if we need to randomly find a new word, let's do it
     while (currentWord == lastWord) {
       int wordNum = rand.nextInt(notLearned.size());
       currentWord = (WordEntry) notLearned.elementAt(wordNum);
     }
+
     lastWord = currentWord;
     return currentWord.getFlashCard();
   }




More information about the sword-cvs mailing list