package org.helioviewer.jhv.gui.actions;

import java.awt.FileDialog;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.net.URI;

import javax.swing.AbstractAction;
import javax.swing.KeyStroke;

import org.helioviewer.jhv.JHVGlobals;
import org.helioviewer.jhv.Settings;
import org.helioviewer.jhv.gui.ImageViewerGui;
import org.helioviewer.jhv.gui.actions.filefilters.AllSupportedImageTypesFilenameFilter;
import org.helioviewer.jhv.input.KeyShortcuts;
import org.helioviewer.jhv.io.LoadURITask;
import org.helioviewer.jhv.layers.ImageLayer;

/**
 * Action to open a local file
 * Opens a file chooser dialog, opens the selected file. Currently supports the
 * following file extensions: "jpg", "jpeg", "png", "fts", "fits", "jp2" and
 * "jpx"
 */
@SuppressWarnings("serial")
public class OpenLocalFileAction extends AbstractAction {

    public OpenLocalFileAction() {
        super("Open...");

        KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_O, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
        putValue(ACCELERATOR_KEY, key);
        KeyShortcuts.registerKey(key, this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        FileDialog fileDialog = new FileDialog(ImageViewerGui.getMainFrame(), "Choose a file", FileDialog.LOAD);
        // does not work on Windows
        fileDialog.setFilenameFilter(new AllSupportedImageTypesFilenameFilter());
        fileDialog.setDirectory(Settings.getSingletonInstance().getProperty("default.local.path"));
        fileDialog.setVisible(true);

        String directory = fileDialog.getDirectory();
        String fileName = fileDialog.getFile();

        if (fileName != null && directory != null) {
            File selectedFile = new File(directory + File.separator + fileName).getAbsoluteFile();
            if (selectedFile.exists() && selectedFile.isFile()) {
                // remember the current directory for future
                Settings.getSingletonInstance().setProperty("default.local.path", directory);
                Settings.getSingletonInstance().save("default.local.path");

                URI uri = selectedFile.toURI();
                LoadURITask uriTask = new LoadURITask(ImageLayer.createImageLayer(), uri);
                JHVGlobals.getExecutorService().execute(uriTask);
            }
        }
    }

}

@@ -1466,24 +1466,66 @@
 eFilter());%0A
+        fileDialog.setMultipleMode(true);%0A
         file
@@ -1712,22 +1712,22 @@
         
-String
+File%5B%5D
  fileNam
@@ -1727,16 +1727,17 @@
 fileName
+s
  = fileD
@@ -1753,13 +1753,13 @@
 File
+s
 ();%0A
-%0A
     
@@ -1774,24 +1774,28 @@
 fileName
- != null
+s.length %3E 0
  && dire
@@ -1815,177 +1815,8 @@
 ) %7B%0A
-            File selectedFile = new File(directory + File.separator + fileName).getAbsoluteFile();%0A            if (selectedFile.exists() && selectedFile.isFile()) %7B%0A    
     
@@ -1872,36 +1872,32 @@
 ure%0A            
-   
- 
 Settings.getSing
@@ -1962,36 +1962,32 @@
 y);%0A            
-    
 Settings.getSing
@@ -2034,46 +2034,132 @@
 %22);%0A
-%0A                URI uri = selectedFil
+            for (File fileName : fileNames) %7B%0A                if (fileName.isFile()) %7B%0A                    URI uri = fileNam
 e.to
@@ -2181,16 +2181,20 @@
         
+   
+ 
 LoadURIT
@@ -2276,16 +2276,20 @@
         
+    
 JHVGloba
@@ -2330,16 +2330,34 @@
 iTask);%0A
+                %7D%0A
         

Allow to open multiple files