package org.plantuml.idea.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileChooser.FileChooserFactory;
import com.intellij.openapi.fileChooser.FileSaverDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileWrapper;
import org.plantuml.idea.plantuml.PlantUml;
import org.plantuml.idea.util.UIUtils;

import java.io.File;
import java.io.IOException;

/**
 * @author Eugene Steinberg
 */
public abstract class AbstractSaveDiagramAction extends AnAction {

    public static final String[] extensions;
    public static VirtualFile homeDir = null;
    private static VirtualFile lastFile = null;
    public static final String FILENAME = "diagram";
    Logger logger = Logger.getInstance(SaveDiagramToFileAction.class);

    static {
        PlantUml.ImageFormat[] values = PlantUml.ImageFormat.values();
        extensions = new String[values.length];
        for (int i = 0; i < values.length; i++) {
            extensions[i] = values[i].toString().toLowerCase();
        }

        homeDir = LocalFileSystem.getInstance().findFileByPath(System.getProperty("user.home"));
    }

    @Override
    public void actionPerformed(AnActionEvent e) {
        FileSaverDescriptor fsd = new FileSaverDescriptor("Save diagram", "Please choose where to save diagram", extensions);

        final VirtualFile baseDir = lastFile == null ? homeDir : lastFile;
        String defaultFileName = getDefaultFileName(e.getProject());

        final VirtualFileWrapper wrapper = FileChooserFactory.getInstance().createSaveFileDialog(
                fsd, e.getProject()).save(baseDir, defaultFileName);

        if (wrapper != null) {
            try {
                VirtualFile virtualFile = wrapper.getVirtualFile();
                if (virtualFile != null)
                    lastFile = virtualFile.getParent();
                File file = wrapper.getFile();
                String[] tokens = file.getAbsolutePath().split("\\.(?=[^\\.]+$)");
                String base = tokens[0];
                String extension = tokens.length < 2 ? "" : tokens[1];
                PlantUml.ImageFormat imageFormat;
                try {
                    imageFormat = PlantUml.ImageFormat.valueOf(extension.toUpperCase());
                } catch (Exception ex) {
                    throw new IOException("Extension '" + extension + "' is not supported");
                }
                String selectedSource = getSource(e.getProject());

                String fileNameTemplate = base + "-%03d." + extension;

                PlantUml.renderAndSave(selectedSource, UIUtils.getSelectedDir(e.getProject()),
                        imageFormat, file.getAbsolutePath(), fileNameTemplate);

            } catch (IOException e1) {
                String title = "Error writing diagram";
                String message = title + " to file:" + wrapper.getFile() + " : " + e1.toString();
                logger.warn(message);
                Messages.showErrorDialog(message, title);
            }
        }
    }

    protected abstract String getSource(Project project);

    private String getDefaultFileName(Project myProject) {
        VirtualFile selectedFile = UIUtils.getSelectedFile(myProject);
        return selectedFile == null ? FILENAME : selectedFile.getNameWithoutExtension();
    }
}

@@ -32,59 +32,8 @@
 n;%0A%0A
-import com.intellij.openapi.actionSystem.AnAction;%0A
 impo
@@ -84,16 +84,16 @@
 nEvent;%0A
+
 import c
@@ -252,16 +252,69 @@
 riptor;%0A
+import com.intellij.openapi.project.DumbAwareAction;%0A
 import c
@@ -533,24 +533,66 @@
 ileWrapper;%0A
+import org.jetbrains.annotations.NotNull;%0A
 import org.p
@@ -805,18 +805,25 @@
 extends 
-An
+DumbAware
 Action %7B
@@ -950,20 +950,19 @@
 ile last
-File
+Dir
  = null;
@@ -1494,16 +1494,25 @@
 rformed(
+@NotNull 
 AnAction
@@ -1649,38 +1649,32 @@
 ions);%0A%0A        
-final 
 VirtualFile base
@@ -1687,42 +1687,222 @@
 last
-File == null ? homeDir : lastFile;
+Dir;%0A%0A        if (baseDir == null) %7B%0A            if (e.getProject() == null) %7B%0A                baseDir = homeDir;%0A            %7D else %7B%0A                baseDir = e.getProject().getBaseDir();%0A            %7D%0A        %7D%0A
 %0A   
@@ -2246,24 +2246,28 @@
 VirtualFile(
+true
 );%0A         
@@ -2297,16 +2297,18 @@
 != null)
+ %7B
 %0A       
@@ -2324,20 +2324,19 @@
     last
-File
+Dir
  = virtu
@@ -2351,24 +2351,104 @@
 etParent();%0A
+                    logger.info(%22lastDir set to %22 + lastDir);%0A                %7D%0A
             

Save image remembering last directory fix. Also default directory is set to project home directory. Closes #79