Index: applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/LoadPictureCalibrationAction.java
===================================================================
--- applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/LoadPictureCalibrationAction.java	(revision 24296)
+++ applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/LoadPictureCalibrationAction.java	(revision 24300)
@@ -38,7 +38,6 @@
 
 /**
- * Action for resetting properties of an image.
+ * Action to load the calibration file.
  * 
- * TODO Four almost identical classes. Refactoring needed.
  */
 public class LoadPictureCalibrationAction extends JosmAction {
@@ -47,13 +46,16 @@
     PicLayerAbstract m_owner = null;
     
+    // Persistent FileChooser instance to remember last directory
+    JFileChooser m_filechooser = null;
+
     /**
      * Constructor
      */
     public LoadPictureCalibrationAction( PicLayerAbstract owner ) {
-        super(tr("Load Picture Calibration..."), null, tr("Loads calibration data to a file"), null, false);
+        super(tr("Load Picture Calibration..."), null, tr("Loads calibration data from a file"), null, false);
         // Remember the owner...
         m_owner = owner;
     }
-    
+
     /**
      * Action handler
@@ -61,5 +63,5 @@
     public void actionPerformed(ActionEvent arg0) {
         // Save dialog
-        final JFileChooser fc = new JFileChooser();
+        JFileChooser fc = new JFileChooser();
         fc.setAcceptAllFileFilterUsed( false );
         fc.setFileFilter( new CalibrationFileFilter() );
@@ -71,7 +73,5 @@
             // Load 
             try {
-                Properties props = new Properties();
-                props.load(new FileInputStream(fc.getSelectedFile()));
-                m_owner.loadCalibration(props);
+                m_owner.loadCalibration(fc.getSelectedFile());
             } catch (Exception e) {
                 // Error
@@ -81,3 +81,4 @@
         }
     }
+    
 }
Index: applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/NewLayerFromFileAction.java
===================================================================
--- applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/NewLayerFromFileAction.java	(revision 24296)
+++ applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/NewLayerFromFileAction.java	(revision 24300)
@@ -33,10 +33,13 @@
 import org.openstreetmap.josm.Main;
 import org.openstreetmap.josm.actions.JosmAction;
+import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
+import org.openstreetmap.josm.gui.layer.Layer;
 
 /**
- * Action responsible for creation of a new layer based on
- * an image file.
+ * Action responsible for creation of new layers based on image files.
  */
 public class NewLayerFromFileAction extends JosmAction {
+
+    String m_lastdirprefname = "piclayer.lastdir";
 
     /**
@@ -80,27 +83,50 @@
 
         // Choose a file
-        JFileChooser fc = new JFileChooser();
+        JFileChooser fc = new JFileChooser(Main.pref.get(m_lastdirprefname));
         fc.setAcceptAllFileFilterUsed( false );
         fc.setFileFilter( new ImageFileFilter() );
+        fc.setMultiSelectionEnabled(true); 
         int result = fc.showOpenDialog( Main.parent );
 
         // Create a layer?
         if ( result == JFileChooser.APPROVE_OPTION ) {
-            // Create layer from file
-            PicLayerFromFile layer = new PicLayerFromFile( fc.getSelectedFile() );
-            // Add layer only if successfully initialized
-            try {
-                layer.initialize();
+            // The first loaded layer will be placed at the top of any other layer of the same class,
+            // or at the bottom of the stack if there is no such layer yet
+            // The next layers we load will be placed one after the other after this first layer
+            int newLayerPos = Main.map.mapView.getAllLayers().size();
+            for(Layer l : Main.map.mapView.getLayersOfType(PicLayerFromFile.class)) {
+                int pos = Main.map.mapView.getLayerPos(l);
+                if (pos < newLayerPos) newLayerPos = pos;
             }
-            catch (IOException e) {
-                // Failed
-                System.out.println( "NewLayerFromFileAction::actionPerformed - " + e.getMessage() );
-                JOptionPane.showMessageDialog(null, e.getMessage() );
-                return;
+
+            for(File file : fc.getSelectedFiles() ) {
+                // TODO: we need a progress bar here, it can take quite some time
+                
+                // Create layer from file
+                PicLayerFromFile layer = new PicLayerFromFile( file );
+                // Add layer only if successfully initialized
+                try {
+                    layer.initialize();
+                }
+                catch (IOException e) {
+                    // Failed
+                    System.out.println( "NewLayerFromFileAction::actionPerformed - " + e.getMessage() );
+                    JOptionPane.showMessageDialog(null, e.getMessage() );
+                    return;
+                }
+                Main.pref.put(m_lastdirprefname, file.getParent());
+        
+                Main.main.addLayer( layer );
+                Main.map.mapView.moveLayer(layer, newLayerPos++);
+                
+                if ( fc.getSelectedFiles().length == 1 && Main.pref.getInteger("piclayer.zoom-on-load", 1) != 0 ) {
+                    // if we are loading a single picture file, zoom on it, so that the user can see something
+                    BoundingXYVisitor v = new BoundingXYVisitor();
+                    layer.visitBoundingBox(v);
+                    Main.map.mapView.recalculateCenterScale(v);
+                }
+
             }
-            // Add layer
-            Main.main.addLayer( layer );
         }
-
     }
 }
Index: applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerAbstract.java
===================================================================
--- applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerAbstract.java	(revision 24296)
+++ applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerAbstract.java	(revision 24300)
@@ -31,4 +31,8 @@
 import java.awt.event.ActionEvent;
 import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.List;
@@ -71,5 +75,5 @@
     private double m_scaley = 1.0;
     // The scale that was set on the map during image creation
-    private double m_initial_scale = 0;
+    private double m_initial_scale = 1.0;
     // Layer icon
     private Icon m_layericon = null;
@@ -103,4 +107,22 @@
      */
     public void initialize() throws IOException {
+        // First, we initialize the calibration, so that createImage() can rely on it
+        
+        // If the map does not exist - we're screwed. We should not get into this situation in the first place!
+        if ( Main.map != null && Main.map.mapView != null ) {
+            // Geographical position of the image
+            // getCenter() documentation claims it returns a clone, but this is not in line with the code,
+            // which actually returns the same object upon subsequent calls. This messes things up
+            // when we loading several pictures and associated cal's in one go.
+            // So as a workaround, copy the object manually :
+            // TODO: not sure about this code below, probably there is a better way to clone the objects
+            EastNorth center = Main.map.mapView.getCenter();
+            m_initial_position = new EastNorth(center.east(), center.north());
+            m_position = new EastNorth(center.east(), center.north());
+            // Initial scale at which the image was loaded
+            m_initial_scale = Main.map.mapView.getDist100Pixel();
+        } else {
+            throw new IOException(tr("Could not find the map object."));
+        }
 
         // Create image
@@ -113,14 +135,4 @@
         Graphics g = m_image.getGraphics();
         g.drawImage( image, 0, 0, null );
-
-        // If the map does not exist - we're screwed. We should not get into this situation in the first place!
-        if ( Main.map != null && Main.map.mapView != null ) {
-            // Geographical position of the image
-            m_initial_position = m_position = Main.map.mapView.getCenter();
-            // Initial scale at which the image was loaded
-            m_initial_scale = Main.map.mapView.getDist100Pixel();
-        } else {
-            throw new IOException(tr("Could not find the map object."));
-        }
     }
 
@@ -231,9 +243,9 @@
 
     /**
-     * Scales the picture. Scaled in... don't know but works ok :)
+     * Scales the picture. scalex and scaley will multiply the current factor
      */
     public void scalePictureBy( double scalex, double scaley ) {
-        m_scalex += scalex;
-        m_scaley += scaley;
+        m_scalex *= scalex;
+        m_scaley *= scaley;
     }
 
@@ -268,7 +280,38 @@
 
     @Override
+    /**
+     * Computes the (rough) bounding box.
+     * We ignore the rotation, the resulting bounding box contains any possible
+     * rotation.
+     */
     public void visitBoundingBox(BoundingXYVisitor arg0) {
-        // TODO Auto-generated method stub
-
+        if ( m_image == null )
+            return;
+        String projcode = Main.proj.toCode();
+
+        // TODO: bounding box only supported when coordinates are in meters
+        // The reason for that is that this .cal think makes us a hard time. 
+        // The position is stored as a raw data (can be either in degrees or 
+        // in meters, depending on the projection used at creation), but the 
+        // initial scale is in m/100pix
+        // So for now, we support the bounding box only when everything is in meters
+        if ( projcode.equals("EPSG:3857") || projcode.equals("EPSG:4326") )
+            return;
+            
+        EastNorth center = m_position;
+        double w = m_image.getWidth(null);
+        double h = m_image.getHeight(null);
+        double diag_pix = Math.sqrt(w*w+h*h);
+        
+        // m_initial_scale is a the scale (unit: m/100pix) at creation time
+        double diag_m = (diag_pix/100) * m_initial_scale;
+        
+        double factor = Math.max(m_scalex, m_scaley);
+        double offset = factor * diag_m / 2.0;
+
+        EastNorth topleft = center.add(-offset, -offset);
+        EastNorth bottomright = center.add(offset, offset);
+        arg0.visit(topleft);
+        arg0.visit(bottomright);
     }
 
@@ -287,4 +330,15 @@
         props.put(SCALEY, "" + m_scaley);
         props.put(ANGLE, "" + m_angle);
+    }
+
+    /**
+     * Loads calibration data from file
+     * @param file The file to read from
+     * @return
+     */
+    public void loadCalibration(File file) throws IOException {
+        Properties props = new Properties();
+        props.load(new FileInputStream(file));
+        loadCalibration(props);
     }
 
Index: applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerFromFile.java
===================================================================
--- applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerFromFile.java	(revision 24296)
+++ applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/PicLayerFromFile.java	(revision 24300)
@@ -21,9 +21,14 @@
 package org.openstreetmap.josm.plugins.piclayer;
 
+import static org.openstreetmap.josm.tools.I18n.tr;
+
+import org.openstreetmap.josm.Main;
+
 import java.awt.Image;
 import java.io.File;
 import java.io.IOException;
 import javax.imageio.ImageIO;
-
+import javax.swing.JDialog;
+import javax.swing.JOptionPane;
 /**
  * Layer displaying a picture loaded from a file.
@@ -39,4 +44,5 @@
         // Remember the file
         m_file = file;
+
         // Generate tooltip text
         m_tooltiptext = m_file.getAbsolutePath();
@@ -44,5 +50,4 @@
         // Set the name of the layer as the base name of the file
         setName(m_file.getName());
-        System.out.println( "name="+m_file.getName() );
     }
 
@@ -55,5 +60,5 @@
         return calFile;
     }
-
+    
     @Override
     protected Image createImage() throws IOException {
@@ -61,4 +66,44 @@
         Image image = null;
         image = ImageIO.read( m_file );
+        
+        // Manage a potential existing calibration file
+        File calFile = getDefaultCalPath();
+        if ( calFile.exists() ) {
+            String prefkey = "piclayer.autoloadcal";
+            String policy = Main.pref.get(prefkey, "");
+            policy = policy.trim().toLowerCase();
+            boolean loadcal = false;
+
+            String msg = tr("A calibration file associated to the picture file was found:")+"\n"+calFile.getName();
+            if ( policy.equals("yes") ) {
+                loadcal = true;
+            }
+            else if ( policy.equals("no") ) {
+                loadcal = false;
+            }
+            else if ( policy.equals("ask") ) {
+                msg += "\n" + tr("(set  \"{0}\"  to yes/no/ask in the preferences\n"+
+                                "to control the autoloading of calibration files)", prefkey);
+                msg += "\n" + tr("Do you want to apply it ?");
+                int answer = JOptionPane.showConfirmDialog(Main.parent, msg, tr("Load calibration file ?"), JOptionPane.YES_NO_OPTION);
+                if (answer == JOptionPane.YES_OPTION) {
+                    loadcal = true;
+                }
+            }
+            else {
+                msg += "\n" + tr("It will be applied automatically.");
+                msg += "\n" + tr("Also, frow now on, cal files will always be loaded automatically.");
+                msg += "\n" + tr("Set  \"{0}\"  to yes/no/ask in the preferences\n"+
+                                "to control the autoloading of calibration files.", prefkey);
+                // TODO: there should be here a yes/no dialog with a checkbox "do not ask again"
+                JOptionPane.showMessageDialog(Main.parent, msg,
+                    "Automatic loading of the calibration", JOptionPane.INFORMATION_MESSAGE);
+                Main.pref.put(prefkey, "yes");
+                loadcal = true;
+            }
+            if ( loadcal )
+                loadCalibration(calFile);
+        }
+                
         return image;
     }
Index: applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/ScalePictureActionAbstract.java
===================================================================
--- applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/ScalePictureActionAbstract.java	(revision 24296)
+++ applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/ScalePictureActionAbstract.java	(revision 24300)
@@ -87,10 +87,10 @@
             double factor;
             if ( ( e.getModifiersEx() & e.SHIFT_DOWN_MASK ) != 0 ) {
-                factor = Main.pref.getDouble("piclayer.scalefactors.high_precision", 4000);
+                factor = Main.pref.getDouble("piclayer.scalefactors.high_precision", 1.0001);
             }
             else {
-                factor = Main.pref.getDouble("piclayer.scalefactors.low_precision", 400);
+                factor = Main.pref.getDouble("piclayer.scalefactors.low_precision", 1.015);
             }            
-            doTheScale( ( e.getY() - m_prevY ) / factor );
+            doTheScale( Math.pow(factor, m_prevY - e.getY() ) );
             m_prevY = e.getY();
             Main.map.mapView.repaint();
Index: applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/ScaleXPictureAction.java
===================================================================
--- applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/ScaleXPictureAction.java	(revision 24296)
+++ applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/ScaleXPictureAction.java	(revision 24300)
@@ -44,5 +44,5 @@
 
     public void doTheScale( double scale ) {
-            m_currentLayer.scalePictureBy( scale, 0.0 );
+            m_currentLayer.scalePictureBy( scale, 1.0 );
         }
 }
Index: applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/ScaleYPictureAction.java
===================================================================
--- applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/ScaleYPictureAction.java	(revision 24296)
+++ applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/ScaleYPictureAction.java	(revision 24300)
@@ -44,5 +44,5 @@
 
     public void doTheScale( double scale ) {
-            m_currentLayer.scalePictureBy( 0.0, scale );
+            m_currentLayer.scalePictureBy( 1.0, scale );
         }
 }
