From d195242662a45f56e549743f45f8e30bde3bd46c Mon Sep 17 00:00:00 2001 From: Roan Date: Sun, 16 Apr 2017 12:17:41 +0200 Subject: [PATCH] Add support for saving the on screen position of the program fix issue #37 --- .../src/me/roan/kps/Configuration.java | 38 ++++++++++++++++++- KeysPerSecond/src/me/roan/kps/Main.java | 10 ++--- KeysPerSecond/src/me/roan/kps/Menu.java | 2 +- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/KeysPerSecond/src/me/roan/kps/Configuration.java b/KeysPerSecond/src/me/roan/kps/Configuration.java index ed54294e..bef864d1 100644 --- a/KeysPerSecond/src/me/roan/kps/Configuration.java +++ b/KeysPerSecond/src/me/roan/kps/Configuration.java @@ -1,6 +1,7 @@ package me.roan.kps; import java.awt.Color; +import java.awt.Point; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; @@ -357,6 +358,9 @@ private final boolean loadNewFormat(File saveloc){ modified = true; } break; + case "position": + Main.frame.setLocation(parsePosition(args[1])); + break; } } in.close(); @@ -425,6 +429,29 @@ private final Color parseColor(String arg){ } return new Color(r, g, b); } + + /** + * Parses the text representation of the position + * to it's actual data + * @param arg The text data + * @return The position data + */ + private final Point parsePosition(String data){ + data = data.replace(" ", "").substring(1, data.length() - 1); + String[] args = data.split(","); + Point loc = new Point(); + try{ + for(String arg : args){ + if(arg.startsWith("x=")){ + loc.x = Integer.parseInt(arg.replace("x=", "")); + }else if(arg.startsWith("y=")){ + loc.y = Integer.parseInt(arg.replace("y=", "")); + } + } + }catch(Exception e){ + } + return loc; + } /** * Loads a legacy configuration file @@ -487,8 +514,12 @@ private final boolean loadLegacyFormat(File saveloc){ /** * Saves this configuration file + * @param pos Whether or not the ask + * to save the on screen position + * of the program */ - protected final void saveConfig(){ + protected final void saveConfig(boolean pos){ + boolean savepos = (!pos) ? false : (JOptionPane.showConfirmDialog(null, "Do you want to save the onscreen position of the program?", "Keys per Second", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION); JFileChooser chooser = new JFileChooser(); chooser.setFileFilter(new FileNameExtensionFilter("Keys per second configuration file", "kpsconf", "kpsconf2")); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); @@ -524,6 +555,11 @@ protected final void saveConfig(){ out.println("foregroundOpacity: " + opacityfg); out.println("backgroundOpacity: " + opacitybg); out.println(); + if(savepos){ + out.println("# Position"); + out.println("position: [x=" + Main.frame.getLocationOnScreen().x + ",y=" + Main.frame.getLocationOnScreen().y + "]"); + out.println(); + } out.println("# Keys"); out.println("keys: "); for(KeyInformation i : keyinfo){ diff --git a/KeysPerSecond/src/me/roan/kps/Main.java b/KeysPerSecond/src/me/roan/kps/Main.java index 6817da42..42781c99 100644 --- a/KeysPerSecond/src/me/roan/kps/Main.java +++ b/KeysPerSecond/src/me/roan/kps/Main.java @@ -546,7 +546,7 @@ private static final void configure(){ save.setEnabled(true); }); save.addActionListener((e)->{ - config.saveConfig(); + config.saveConfig(false); }); load.addActionListener((e)->{ if(!Configuration.loadConfiguration()){ @@ -847,13 +847,13 @@ public void setValueAt(Object value, int row, int col){ keyform.add(pane, BorderLayout.CENTER); JButton newkey = new JButton("Add Key"); newkey.addActionListener((evt)->{ - if(JOptionPane.showOptionDialog(null, "Press a key and press 'OK' to add it.", "Keys per second", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[]{"OK", "Cancel"}, 0) == 0){ + if(JOptionPane.showOptionDialog(frame.isVisible() ? frame : null, "Press a key and press 'OK' to add it.", "Keys per second", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[]{"OK", "Cancel"}, 0) == 0){ if(lastevent == null){ - JOptionPane.showMessageDialog(null, "No key pressed!", "Keys per second", JOptionPane.ERROR_MESSAGE); + JOptionPane.showMessageDialog(frame.isVisible() ? frame : null, "No key pressed!", "Keys per second", JOptionPane.ERROR_MESSAGE); return; } KeyInformation info = new KeyInformation(NativeKeyEvent.getKeyText(lastevent.getKeyCode()), lastevent.getKeyCode()); - if(JOptionPane.showConfirmDialog(null, "Add the " + info.name + " key?", "Keys per second", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){ + if(JOptionPane.showConfirmDialog(frame.isVisible() ? frame : null, "Add the " + info.name + " key?", "Keys per second", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){ config.keyinfo.add(info); } model.fireTableDataChanged(); @@ -899,7 +899,7 @@ public void setValueAt(Object value, int row, int col){ addform.add(buttons, BorderLayout.CENTER); - if(JOptionPane.showOptionDialog(null, addform, "Keys per second", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[]{"OK", "Cancel"}, 0) == 0){ + if(JOptionPane.showOptionDialog(frame.isVisible() ? frame : null, addform, "Keys per second", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[]{"OK", "Cancel"}, 0) == 0){ if(cm1.isSelected()){ config.keyinfo.add(new KeyInformation("M1", -NativeMouseEvent.BUTTON1)); } diff --git a/KeysPerSecond/src/me/roan/kps/Menu.java b/KeysPerSecond/src/me/roan/kps/Menu.java index 85329504..59e9952e 100644 --- a/KeysPerSecond/src/me/roan/kps/Menu.java +++ b/KeysPerSecond/src/me/roan/kps/Menu.java @@ -441,7 +441,7 @@ protected static final void createMenu(){ e.setOpaque(true); } save.addActionListener((e)->{ - Main.config.saveConfig(); + Main.config.saveConfig(true); }); load.addActionListener((e)->{ double oldScale = Main.config.size;