Skip to content

Commit

Permalink
More minor enhancements
Browse files Browse the repository at this point in the history
- The backlog now finally has 50 instead of 49 as a maximum
- PrefDef format change now gives the correct version number in the warning message
- Right clicking words now allows export definition from there too - seems a bit more logical that way
- Export whole line moved to 'Edit' window along with other lines settings - again, seems more logical
- Persistence serialVersionUID is now finally, really working (forgot 'final'). Set ID to match the previous file's autogenerated code.
  • Loading branch information
LaurensWeyn committed Dec 15, 2017
1 parent be37537 commit 34b4522
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 131 deletions.
2 changes: 1 addition & 1 deletion src/hooker/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class Log

public Log(int maxLen)
{
this.maxLen = maxLen;
this.maxLen = maxLen + 1;
log = new LinkedList<>();
}

Expand Down
123 changes: 122 additions & 1 deletion src/main/Persist.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@

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;

/**
* Persistence object. Stored in binary(Java serial) form.
*/
public class Persist implements Serializable
{

public static int exportedThisSession = 0;
public static int exportedBeforeSession = -1;


//useless statistics
public Date firstStartup;
public long startupCount;
Expand All @@ -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

Expand Down Expand Up @@ -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", "<br>"))
.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"));
Expand Down
4 changes: 2 additions & 2 deletions src/options/PrefDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
19 changes: 19 additions & 0 deletions src/ui/menubar/MenubarBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import hooker.ClipboardHook;
import main.Main;
import main.Persist;
import network.Client;
import network.Host;
import options.OptionsUI;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() + ")");
}
}
5 changes: 5 additions & 0 deletions src/ui/menubar/MenubarItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@ public void addSpacer()
{
menu.add(new JSeparator());
}

public void hide()
{
menu.setVisible(false);
}
}
5 changes: 2 additions & 3 deletions src/ui/popup/DefPopup.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
Loading

0 comments on commit 34b4522

Please sign in to comment.