]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/GpsPrune.java
Version 19, May 2018
[GpsPrune.git] / tim / prune / GpsPrune.java
index 41dac2b0d0300538ecb9813bd8f71462e5b8c828..67b197cde70a2efab900ec6631c2ae73c900a568 100644 (file)
@@ -3,6 +3,7 @@ package tim.prune;
 import java.awt.event.WindowAdapter;
 import java.awt.BorderLayout;
 import java.awt.Component;
+import java.awt.Image;
 import java.awt.event.WindowEvent;
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -27,17 +28,17 @@ import tim.prune.gui.profile.ProfileChart;
 
 /**
  * GpsPrune is a tool to visualize, edit, convert and prune GPS data
- * Please see the included readme.txt or http://activityworkshop.net
- * This software is copyright activityworkshop.net 2006-2011 and made available through the Gnu GPL version 2.
+ * Please see the included readme.txt or https://activityworkshop.net
+ * This software is copyright activityworkshop.net 2006-2018 and made available through the Gnu GPL version 2.
  * For license details please see the included license.txt.
  * GpsPrune is the main entry point to the application, including initialisation and launch
  */
 public class GpsPrune
 {
        /** Version number of application, used in about screen and for version check */
-       public static final String VERSION_NUMBER = "13";
+       public static final String VERSION_NUMBER = "19";
        /** Build number, just used for about screen */
-       public static final String BUILD_NUMBER = "240";
+       public static final String BUILD_NUMBER = "362";
        /** Static reference to App object */
        private static App APP = null;
 
@@ -87,17 +88,20 @@ public class GpsPrune
                        {
                                // Check if a data file has been given
                                File f = new File(arg);
-                               if (f.exists() && f.canRead()) {
+                               if (f.exists() && f.isFile() && f.canRead()) {
                                        dataFiles.add(f);
                                }
-                               else {
-                                       System.out.println("Unknown parameter '" + arg + "'.");
+                               else
+                               {
+                                       // Make a useful String from the unknown parameter - could it be a mistyped filename?
+                                       System.out.println(makeUnknownParameterString(arg));
                                        showUsage = true;
                                }
                        }
                }
-               if (showUsage) {
-                       System.out.println("Possible parameters:"
+               if (showUsage)
+               {
+                       System.out.println("GpsPrune - a tool for editing GPS data.\nPossible parameters:"
                                + "\n   --configfile=<file> used to specify a configuration file"
                                + "\n   --lang=<code>       used to specify language code such as DE"
                                + "\n   --langfile=<file>   used to specify an alternative language file\n");
@@ -134,13 +138,15 @@ public class GpsPrune
                        // If langfilename is blank on command line parameters then don't use setting from config
                        langFilename = Config.getConfigString(Config.KEY_LANGUAGE_FILE);
                }
-               if (langFilename != null && !langFilename.equals("")) {
+               if (langFilename != null)
+               {
                        try {
                                I18nManager.addLanguageFile(langFilename);
                                Config.setConfigString(Config.KEY_LANGUAGE_FILE, langFilename);
                        }
                        catch (FileNotFoundException fnfe) {
                                System.err.println("Failed to load language file: " + langFilename);
+                               Config.setConfigString(Config.KEY_LANGUAGE_FILE, "");
                        }
                }
                // Set up the window and go
@@ -223,15 +229,36 @@ public class GpsPrune
                // Avoid automatically shutting down if window closed
                frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
 
-               // set icon
-               try {
-                       frame.setIconImage(IconManager.getImageIcon(IconManager.WINDOW_ICON).getImage());
+               // set window icons of different resolutions (1.6+)
+               try
+               {
+                       ArrayList<Image> icons = new ArrayList<Image>();
+                       String[] resolutions = {"_16", "_20", "_22", "_24", "_32", "_36", "_48", "_64", "_72", "_96", "_128"};
+                       for (String r : resolutions) {
+                               icons.add(IconManager.getImageIcon(IconManager.WINDOW_ICON + r + ".png").getImage());
+                       }
+                       Class<?> d = java.awt.Window.class;
+                       // This is the same as frame.setIconImages(icons) but is compilable also for java1.5 where this isn't available
+                       d.getDeclaredMethod("setIconImages", new Class[]{java.util.List.class}).invoke(frame, icons);
+               }
+               catch (Exception e)
+               {
+                       // setting a list of icon images didn't work, so try with just one image instead
+                       try {
+                               frame.setIconImage(IconManager.getImageIcon(IconManager.WINDOW_ICON + "_16.png").getImage());
+                       }
+                       catch (Exception e2) {}
                }
-               catch (Exception e) {} // ignore
+
+               // Set up drag-and-drop handler to accept dropped files
+               frame.setTransferHandler(new FileDropHandler(APP));
 
                // finish off and display frame
                frame.pack();
-               frame.setSize(650, 450);
+               if (!setFrameBoundsFromConfig(frame))
+               {
+                       frame.setSize(650, 450);
+               }
                frame.setVisible(true);
                // Set position of map/profile splitter
                midSplit.setDividerLocation(0.75);
@@ -245,4 +272,61 @@ public class GpsPrune
                // Finally, give the files to load to the App
                APP.loadDataFiles(inDataFiles);
        }
+
+
+       /**
+        * Set the frame bounds using the saved config setting
+        * @param inFrame frame to set the bounds of
+        * @return true on success
+        */
+       private static boolean setFrameBoundsFromConfig(JFrame inFrame)
+       {
+               // Try to get bounds from config
+               String bounds = Config.getConfigString(Config.KEY_WINDOW_BOUNDS);
+               try
+               {
+                       String[] boundValues = bounds.split("x");
+                       if (boundValues.length == 4)
+                       {
+                               int[] elems = new int[4];
+                               for (int i=0; i<4; i++) {
+                                       elems[i] = Integer.parseInt(boundValues[i]);
+                               }
+                               // Make sure width and height aren't stupid
+                               elems[2] = Math.max(elems[2], 400);
+                               elems[3] = Math.max(elems[3], 300);
+                               inFrame.setBounds(elems[0], elems[1], elems[2], elems[3]);
+                               return true;
+                       }
+               }
+               catch (NullPointerException npe) {}  // if no entry found in config
+               catch (NumberFormatException nfe) {} // if string couldn't be parsed
+               return false;
+       }
+
+
+       /**
+        * Try to guess whether it's a mistyped parameter or a mistyped filename
+        * @param inParam command line argument
+        * @return error message
+        */
+       private static String makeUnknownParameterString(String inParam)
+       {
+               File file = new File(inParam);
+               if (file.exists())
+               {
+                       if (file.isDirectory()) return "'" + inParam + "' is a directory";
+                       if (!file.canRead())    return "Cannot read file '" + inParam + "'";
+                       return "Something wrong with file '" + inParam + "'";
+               }
+               do
+               {
+                       String name = file.getName();
+                       file = file.getParentFile();
+                       if (file != null && file.exists() && file.canRead()) return "Tried to load file '" + inParam + "' but cannot find '" + name + "'";
+               }
+               while (file != null);
+
+               return "Unknown parameter '" + inParam + "'";
+       }
 }