diff --git a/src/hooker/Log.java b/src/hooker/Log.java index 88fcdae..b5993bf 100644 --- a/src/hooker/Log.java +++ b/src/hooker/Log.java @@ -32,7 +32,7 @@ public class Log public Log(int maxLen) { - this.maxLen = maxLen; + this.maxLen = maxLen + 1; log = new LinkedList<>(); } diff --git a/src/main/Persist.java b/src/main/Persist.java index ad47c64..30cc2b7 100644 --- a/src/main/Persist.java +++ b/src/main/Persist.java @@ -3,9 +3,17 @@ import ui.Line; import ui.Page; +import ui.UI; +import ui.menubar.MenubarBuilder; +import javax.imageio.ImageIO; +import javax.swing.*; import java.awt.*; +import java.awt.image.BufferedImage; import java.io.*; +import java.nio.charset.Charset; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.Date; /** @@ -13,6 +21,11 @@ */ public class Persist implements Serializable { + + public static int exportedThisSession = 0; + public static int exportedBeforeSession = -1; + + //useless statistics public Date firstStartup; public long startupCount; @@ -29,7 +42,7 @@ public class Persist implements Serializable public String lastWindowHookName; //non persist data (but persist specific settings) - private static long serialVersionUID = 1L; + private static final long serialVersionUID = 4155401967378296134L; private transient long lastSyncTime; private static long syncPeriod = 600000L;//10 minutes @@ -63,6 +76,114 @@ public static Persist load(File file) return new Persist(); } + public static int getLineExportCount() + { + if(exportedBeforeSession != -1)return exportedBeforeSession + exportedThisSession; + //calculate on first call + if(Main.options.getOption("exportDisplay").equals("external")) + { + exportedBeforeSession = Utils.countLines(Main.options.getFile("lineExportPath")); + } + else exportedBeforeSession = 0; + + return exportedBeforeSession + exportedThisSession; + } + + public static void exportLine() + { + JFrame frame = Main.ui.disp.getFrame(); + try + { + Date date = new Date(); + DateFormat df = new SimpleDateFormat(Main.options.getOption("timeStampFormat")); + File textFile = new File(Main.options.getOption("lineExportPath")); + String note = ""; + + if(Main.options.getOptionBool("commentOnExportLine")) + { + note = (String)JOptionPane.showInputDialog(frame, + "Enter comment\n(You may also leave this blank)", + "Exporting line", + JOptionPane.PLAIN_MESSAGE, + null, + null, + UI.userComment); + if(note == null)return;//cancelled + + UI.userComment = note;//update for next time + Thread.sleep(500);//give the popup time to disappear + } + + Writer fr = new OutputStreamWriter(new FileOutputStream(textFile, true), Charset.forName("UTF-8")); + fr.append(df.format(date)) + .append("\t") + .append(Main.currPage.getText().replace("\n", "
")) + .append("\t") + .append(note) + .append("\n"); + fr.close(); + exportedThisSession++; + Main.persist.exportCount++; + + //take a screenshot with the exported line + if(Main.options.getOptionBool("exportImage")) + { + File imageFolder = Main.options.getFile("screenshotExportPath"); + if (!imageFolder.exists()) + { + boolean success = imageFolder.mkdirs(); + if (!success) throw new IOException("Failed to create folder(s) for screenshots: directory" + + Main.options.getOption("screenshotExportPath")); + } + Robot robot = new Robot(); + Point pos = frame.getLocationOnScreen(); + Rectangle area; + if(Main.options.getOptionBool("fullscreenScreenshot")) + { + //whole screen + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + area = new Rectangle(0, 0, screenSize.width, screenSize.height); + } + else + { + //game area + if(Main.options.getOptionBool("defsShowUpwards")) + { + area = new Rectangle(pos.x, pos.y + UI.furiganaStartY - Main.options.getOptionInt("maxHeight"), + Main.options.getOptionInt("windowWidth"), + Main.options.getOptionInt("maxHeight")); + } + else + { + area = new Rectangle(pos.x, pos.y + UI.defStartY, + Main.options.getOptionInt("windowWidth"), + Main.options.getOptionInt("maxHeight")); + } + } + + //hide Spark Reader and take the screenshot + UI.hidden = true; + Main.ui.render(); + BufferedImage screenshot = robot.createScreenCapture(area); + UI.hidden = false; + Main.ui.render(); + + String fileName = imageFolder.getAbsolutePath(); + if(!fileName.endsWith("/") && !fileName.endsWith("\\"))fileName += "/"; + fileName += df.format(date) + ".png"; + + System.out.println("saving screenshot as " + fileName); + ImageIO.write(screenshot, "png", new File(fileName)); + } + + }catch(IOException | AWTException | InterruptedException err) + { + JOptionPane.showInputDialog(frame, "Error while exporting line:\n" + err); + } + + MenubarBuilder.updateExportCount(); + } + public void checkForSave() { if(System.currentTimeMillis() - lastSyncTime > syncPeriod)save(Main.options.getFile("persistPath")); diff --git a/src/options/PrefDef.java b/src/options/PrefDef.java index 0f700cd..5e4d347 100644 --- a/src/options/PrefDef.java +++ b/src/options/PrefDef.java @@ -56,9 +56,9 @@ public PrefDef(File file)throws IOException String bits[] = line.split("="); if(bits.length == 2) { - if(bits[1].startsWith("-"))//negative numbers: files from 0.6 and below + if(bits[1].startsWith("-"))//negative numbers: files from 0.7 and below { - int option = JOptionPane.showConfirmDialog(Main.getParentFrame(), "Warning: Spark Reader 0.7 and up's preferred definition files are not backwards compatible with older versions.\n" + + int option = JOptionPane.showConfirmDialog(Main.getParentFrame(), "Warning: Spark Reader 0.8 and up's preferred definition files are not backwards compatible with older versions.\n" + "Select yes to reset your preferred definitions. Pressing no will close the program."); if(option == JOptionPane.YES_OPTION) { diff --git a/src/ui/menubar/MenubarBuilder.java b/src/ui/menubar/MenubarBuilder.java index fbc38a2..6869302 100644 --- a/src/ui/menubar/MenubarBuilder.java +++ b/src/ui/menubar/MenubarBuilder.java @@ -2,6 +2,7 @@ import hooker.ClipboardHook; import main.Main; +import main.Persist; import network.Client; import network.Host; import options.OptionsUI; @@ -85,6 +86,15 @@ public void actionPerformed(ActionEvent e) ClipboardHook.setClipboard(currPage.getText()); } }, "CopyLine"); + item.addMenuItem(new AbstractAction("Add line as flashcard (" + Persist.getLineExportCount() + ")") + { + @Override + public void actionPerformed(ActionEvent e) + { + item.hide();//ensure this menu's already gone for the screenshot + Persist.exportLine(); + } + }, "ExportLine"); item.addMenuItem(new AbstractAction("Set line text") { @Override @@ -341,4 +351,13 @@ public static void importPrompt() Main.ui.render();//redraw known words on current text } + + public static void updateExportCount() + { + if(Main.ui == null || Main.ui.menubar == null) + return; + + ((JMenuItem)Main.ui.menubar.getMenuItem("Edit", "ExportLine").getComponent()) + .setText("Add line as flashcard (" + Persist.getLineExportCount() + ")"); + } } diff --git a/src/ui/menubar/MenubarItem.java b/src/ui/menubar/MenubarItem.java index e035dab..b53af26 100644 --- a/src/ui/menubar/MenubarItem.java +++ b/src/ui/menubar/MenubarItem.java @@ -69,4 +69,9 @@ public void addSpacer() { menu.add(new JSeparator()); } + + public void hide() + { + menu.setVisible(false); + } } diff --git a/src/ui/popup/DefPopup.java b/src/ui/popup/DefPopup.java index a774409..85de279 100644 --- a/src/ui/popup/DefPopup.java +++ b/src/ui/popup/DefPopup.java @@ -34,7 +34,6 @@ import java.util.Set; import static language.dictionary.Japanese.isJapanese; -import static main.Main.options; /** * When right clicking on the definition window @@ -114,7 +113,7 @@ public void actionPerformed(ActionEvent e) } }); - anki.setText("Add as flashcard (" + getExportedCount() + ")"); + anki.setText("Add as flashcard (" + getDefExportCount() + ")"); add(anki); add(setDef); @@ -141,7 +140,7 @@ public void show(int x, int y) private static int exportedThisSession = 0; private static int exportedBeforeSession = -1; - private int getExportedCount() + public static int getDefExportCount() { if(exportedBeforeSession != -1)return exportedBeforeSession + exportedThisSession; //calculate on first call diff --git a/src/ui/popup/WordPopup.java b/src/ui/popup/WordPopup.java index c54699c..9726f52 100644 --- a/src/ui/popup/WordPopup.java +++ b/src/ui/popup/WordPopup.java @@ -19,24 +19,14 @@ import hooker.ClipboardHook; import language.splitter.FoundWord; import main.Main; -import main.Utils; +import main.Persist; import ui.Line; import ui.UI; -import language.dictionary.DefSource; -import language.dictionary.UserDefinition; import ui.WordEditUI; -import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; -import java.awt.event.MouseEvent; -import java.awt.image.BufferedImage; -import java.io.*; -import java.nio.charset.Charset; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; /** * @@ -48,6 +38,7 @@ public class WordPopup extends JPopupMenu JMenuItem addBreak; JMenuItem exportLine; + JMenuItem exportWord; JMenuItem makeDefinition; JMenuItem copy; JMenuItem append; @@ -65,7 +56,15 @@ public WordPopup(Line line, FoundWord word, UI ui) public void actionPerformed(ActionEvent e) { setVisible(false);//ensure this menu's already gone for the screenshot - exportLine(); + Persist.exportLine(); + } + }); + exportWord = new JMenuItem(new AbstractAction("Add word as flashcard") + { + @Override + public void actionPerformed(ActionEvent e) + { + DefPopup.ankiExport(word); } }); makeDefinition = new JMenuItem(new AbstractAction("Create new userdict entry") @@ -121,10 +120,12 @@ public void actionPerformed(ActionEvent e) }); markKnown.setSelected(Main.known.isKnown(word)); - exportLine.setText("Export whole line (" + getExportedCount() + ")"); - + exportLine.setText("Export whole line (" + Persist.getLineExportCount() + ")"); + exportWord.setText("Add word as flashcard (" + DefPopup.getDefExportCount() + ")"); + add(addBreak); - add(exportLine); + //add(exportLine); + add(exportWord); if(Main.options.getOptionBool("enableKnown")) add(markKnown); //FIXME disabled for now as it causes problems @@ -142,113 +143,6 @@ public void show(int x, int y) show(ui.disp.getFrame(), x, y); } - private static int exportedThisSession = 0; - private static int exportedBeforeSession = -1; - private int getExportedCount() - { - if(exportedBeforeSession != -1)return exportedBeforeSession + exportedThisSession; - //calculate on first call - if(Main.options.getOption("exportDisplay").equals("external")) - { - exportedBeforeSession = Utils.countLines(Main.options.getFile("lineExportPath")); - } - else exportedBeforeSession = 0; - - return exportedBeforeSession + exportedThisSession; - } - - public static void exportLine() - { - JFrame frame = Main.ui.disp.getFrame(); - try - { - Date date = new Date(); - DateFormat df = new SimpleDateFormat(Main.options.getOption("timeStampFormat")); - File textFile = new File(Main.options.getOption("lineExportPath")); - String note = ""; - - if(Main.options.getOptionBool("commentOnExportLine")) - { - note = (String)JOptionPane.showInputDialog(frame, - "Enter comment\n(You may also leave this blank)", - "Exporting line", - JOptionPane.PLAIN_MESSAGE, - null, - null, - UI.userComment); - if(note == null)return;//cancelled - - UI.userComment = note;//update for next time - Thread.sleep(500);//give the popup time to disappear - } - - Writer fr = new OutputStreamWriter(new FileOutputStream(textFile, true), Charset.forName("UTF-8")); - fr.append(df.format(date)) - .append("\t") - .append(Main.currPage.getText().replace("\n", "
")) - .append("\t") - .append(note) - .append("\n"); - fr.close(); - exportedThisSession++; - Main.persist.exportCount++; - - //take a screenshot with the exported line - if(Main.options.getOptionBool("exportImage")) - { - File imageFolder = Main.options.getFile("screenshotExportPath"); - if (!imageFolder.exists()) - { - boolean success = imageFolder.mkdirs(); - if (!success) throw new IOException("Failed to create folder(s) for screenshots: directory" - + Main.options.getOption("screenshotExportPath")); - } - Robot robot = new Robot(); - Point pos = frame.getLocationOnScreen(); - Rectangle area; - if(Main.options.getOptionBool("fullscreenScreenshot")) - { - //whole screen - Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); - area = new Rectangle(0, 0, screenSize.width, screenSize.height); - } - else - { - //game area - if(Main.options.getOptionBool("defsShowUpwards")) - { - area = new Rectangle(pos.x, pos.y + UI.furiganaStartY - Main.options.getOptionInt("maxHeight"), - Main.options.getOptionInt("windowWidth"), - Main.options.getOptionInt("maxHeight")); - } - else - { - area = new Rectangle(pos.x, pos.y + UI.defStartY, - Main.options.getOptionInt("windowWidth"), - Main.options.getOptionInt("maxHeight")); - } - } - - //hide Spark Reader and take the screenshot - UI.hidden = true; - Main.ui.render(); - BufferedImage screenshot = robot.createScreenCapture(area); - UI.hidden = false; - Main.ui.render(); - - String fileName = imageFolder.getAbsolutePath(); - if(!fileName.endsWith("/") && !fileName.endsWith("\\"))fileName += "/"; - fileName += df.format(date) + ".png"; - - System.out.println("saving screenshot as " + fileName); - ImageIO.write(screenshot, "png", new File(fileName)); - } - - }catch(IOException | AWTException | InterruptedException err) - { - JOptionPane.showInputDialog(frame, "Error while exporting line:\n" + err); - } - } public static void makeDefPopup(Line line) { JFrame frame = Main.ui.disp.getFrame(); diff --git a/test/tests/LogTest.java b/test/tests/LogTest.java index 6db78ae..74716c4 100644 --- a/test/tests/LogTest.java +++ b/test/tests/LogTest.java @@ -23,7 +23,7 @@ public LogTest() @Test public void testLinePointer() { - Log l = new Log(4); + Log l = new Log(3); l.addLine("A"); l.addLine("B"); l.addLine("C"); @@ -51,7 +51,7 @@ public void testLinePointer() @Test public void testLogPosition() { - Log l = new Log(4); + Log l = new Log(3); l.addLine("A"); l.addLine("B"); l.addLine("C");