package ij.io;
import ij.*;
import ij.gui.*;
import ij.plugin.frame.Recorder;
import ij.util.Java2;
import ij.macro.Interpreter;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.*;
public class OpenDialog {
private String dir;
private String name;
private boolean recordPath;
private static String defaultDirectory;
private static Frame sharedFrame;
private String title;
private static String lastDir, lastName;
public OpenDialog(String title) {
this(title, null);
}
public OpenDialog(String title, String path) {
String macroOptions = Macro.getOptions();
if (macroOptions!=null && (path==null||path.equals(""))) {
path = Macro.getValue(macroOptions, title, path);
if (path==null || path.equals(""))
path = Macro.getValue(macroOptions, "path", path);
if ((path==null || path.equals("")) && title!=null && title.equals("Open As String"))
path = Macro.getValue(macroOptions, "OpenAsString", path);
path = lookupPathVariable(path);
}
if (path==null || path.equals("")) {
if (Prefs.useJFileChooser)
jOpen(title, getDefaultDirectory(), null);
else
open(title, getDefaultDirectory(), null);
if (name!=null) defaultDirectory = dir;
this.title = title;
recordPath = true;
} else {
decodePath(path);
recordPath = IJ.macroRunning();
}
IJ.register(OpenDialog.class);
}
public OpenDialog(String title, String defaultDir, String defaultName) {
String path = null;
String macroOptions = Macro.getOptions();
if (macroOptions!=null)
path = Macro.getValue(macroOptions, title, path);
if (path!=null)
decodePath(path);
else {
if (Prefs.useJFileChooser)
jOpen(title, defaultDir, defaultName);
else
open(title, defaultDir, defaultName);
this.title = title;
recordPath = true;
}
}
public static String lookupPathVariable(String path) {
if (path!=null && path.indexOf(".")==-1 && !((new File(path)).exists())) {
if (path.startsWith("&")) path=path.substring(1);
Interpreter interp = Interpreter.getInstance();
String path2 = interp!=null?interp.getStringVariable(path):null;
if (path2!=null) path = path2;
}
return path;
}
void jOpen(String title, String path, String fileName) {
Java2.setSystemLookAndFeel();
if (EventQueue.isDispatchThread())
jOpenDispatchThread(title, path, fileName);
else
jOpenInvokeAndWait(title, path, fileName);
}
void jOpenDispatchThread(String title, String path, final String fileName) {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle(title);
File fdir = null;
if (path!=null)
fdir = new File(path);
if (fdir!=null)
fc.setCurrentDirectory(fdir);
if (fileName!=null)
fc.setSelectedFile(new File(fileName));
int returnVal = fc.showOpenDialog(IJ.getInstance());
if (returnVal!=JFileChooser.APPROVE_OPTION)
{Macro.abort(); return;}
File file = fc.getSelectedFile();
if (file==null)
{Macro.abort(); return;}
name = file.getName();
dir = fc.getCurrentDirectory().getPath()+File.separator;
}
void jOpenInvokeAndWait(final String title, final String path, final String fileName) {
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle(title);
File fdir = null;
if (path!=null)
fdir = new File(path);
if (fdir!=null)
fc.setCurrentDirectory(fdir);
if (fileName!=null)
fc.setSelectedFile(new File(fileName));
int returnVal = fc.showOpenDialog(IJ.getInstance());
if (returnVal!=JFileChooser.APPROVE_OPTION)
{Macro.abort(); return;}
File file = fc.getSelectedFile();
if (file==null)
{Macro.abort(); return;}
name = file.getName();
dir = fc.getCurrentDirectory().getPath()+File.separator;
}
});
} catch (Exception e) {}
}
void open(String title, String path, String fileName) {
Frame parent = IJ.getInstance();
if (parent==null) {
if (sharedFrame==null) sharedFrame = new Frame();
parent = sharedFrame;
}
if (IJ.isMacOSX() && IJ.isJava18()) {
ImageJ ij = IJ.getInstance();
if (ij!=null && ij.isActive())
parent = ij;
else
parent = null;
}
FileDialog fd = new FileDialog(parent, title);
if (path!=null)
fd.setDirectory(path);
if (fileName!=null)
fd.setFile(fileName);
fd.show();
name = fd.getFile();
if (name==null) {
if (IJ.isMacOSX())
System.setProperty("apple.awt.fileDialogForDirectories", "false");
Macro.abort();
} else
dir = fd.getDirectory();
}
void decodePath(String path) {
int i = path.lastIndexOf('/');
if (i==-1)
i = path.lastIndexOf('\\');
if (i>0) {
dir = path.substring(0, i+1);
name = path.substring(i+1);
} else {
dir = "";
name = path;
}
}
public String getDirectory() {
lastDir = dir;
return dir;
}
public String getFileName() {
if (name!=null) {
if (Recorder.record && recordPath && dir!=null)
Recorder.recordPath(title, dir+name);
lastName = name;
}
return name;
}
public String getPath() {
if (getFileName()==null)
return null;
else return
getDirectory() + getFileName();
}
public static String getDefaultDirectory() {
if (defaultDirectory==null)
defaultDirectory = Prefs.getDefaultDirectory();
return defaultDirectory;
}
public static void setDefaultDirectory(String defaultDir) {
defaultDirectory = defaultDir;
if (defaultDirectory!=null && !defaultDirectory.endsWith(File.separator) && !defaultDirectory.endsWith("/"))
defaultDirectory = defaultDirectory + File.separator;
}
public static String getLastDirectory() {
return lastDir;
}
public static void setLastDirectory(String dir) {
lastDir = dir;
}
public static String getLastName() {
return lastName;
}
public static void setLastName(String name) {
lastName = name;
}
}