package jalview.io;
import jalview.bin.Cache;
+import jalview.bin.Jalview;
import jalview.gui.JvOptionPane;
import jalview.util.MessageManager;
import jalview.util.Platform;
import jalview.util.dialogrunner.DialogRunner;
import jalview.util.dialogrunner.DialogRunnerI;
-import jalview.util.dialogrunner.Response;
import jalview.util.dialogrunner.RunResponse;
import java.awt.Component;
* @author AMW
*
*/
-public class JalviewFileChooser extends JFileChooser
- implements PropertyChangeListener, DialogRunnerI<JalviewFileChooser>
+public class JalviewFileChooser extends JFileChooser implements DialogRunnerI,
+ PropertyChangeListener
{
- DialogRunner<JalviewFileChooser> runner = new DialogRunner<>(this);
+ private static final long serialVersionUID = 1L;
+
+ private DialogRunnerI runner = new DialogRunner();
+ File selectedFile = null;
+
/**
* Factory method to return a file chooser that offers readable alignment file
* formats
}
JalviewFileChooser(String dir, String[] extensions, String[] descs,
- String selected, boolean allFiles)
+ String selected, boolean acceptAny)
{
super(safePath(dir));
if (extensions.length == descs.length)
{
formats.add(new String[] { extensions[i], descs[i] });
}
- init(formats, selected, allFiles);
+ init(formats, selected, acceptAny);
}
else
{
}
}
- @Override
- public void propertyChange(PropertyChangeEvent evt)
- {
- // TODO other properties need runners...
- switch (evt.getPropertyName())
- {
- case "SelectedFile":
- runner.run(APPROVE_OPTION);
- break;
- }
- }
-
private static File safePath(String dir)
{
if (dir == null)
@Override
public int showOpenDialog(Component parent)
{
- runner.resetResponses();
+ // runner.resetResponses();
int value = super.showOpenDialog(this);
- /**
- * @j2sNative
- */
+ if (!Jalview.isJS())
{
- runner.firstRun(value);
+ runner.handleResponse(value);
}
return value;
}
* @param formats
* a list of {extensions, description} for each file format
* @param selected
- * @param allFiles
+ * @param acceptAny
* if true, 'any format' option is included
*/
- void init(List<String[]> formats, String selected, boolean allFiles)
+ void init(List<String[]> formats, String selected, boolean acceptAny)
{
JalviewFileFilter chosen = null;
// SelectAllFilter needs to be set first before adding further
// file filters to fix bug on Mac OSX
- setAcceptAllFileFilterUsed(allFiles);
+ setAcceptAllFileFilterUsed(acceptAny);
for (String[] format : formats)
{
}
return null;
}
-
- File ourselectedFile = null;
@Override
public File getSelectedFile()
{
- File selfile = super.getSelectedFile();
- if (selfile == null && ourselectedFile != null)
- {
- return ourselectedFile;
- }
- return selfile;
+ File f = super.getSelectedFile();
+ return f == null ? selectedFile : f;
}
@Override
return;
}
- ourselectedFile = getSelectedFile();
+ selectedFile = getSelectedFile();
- if (ourselectedFile == null)
+ if (selectedFile == null)
{
// Workaround for Java 9,10 on OSX - no selected file, but there is a
// filename typed in
String filename = ((BasicFileChooserUI) getUI()).getFileName();
if (filename != null && filename.length() > 0)
{
- ourselectedFile = new File(getCurrentDirectory(), filename);
+ selectedFile = new File(getCurrentDirectory(), filename);
}
} catch (Throwable x)
{
// THE
// USER PROMPTED FOR A NEW FILENAME
}
- if (ourselectedFile == null)
+
+ if (selectedFile == null)
{
return;
}
{
JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
- if (!jvf.accept(ourselectedFile))
+ if (!jvf.accept(selectedFile))
{
String withExtension = getSelectedFile().getName() + "."
+ jvf.getAcceptableExtension();
- ourselectedFile = (new File(getCurrentDirectory(), withExtension));
- setSelectedFile(ourselectedFile);
+ selectedFile = (new File(getCurrentDirectory(), withExtension));
+ setSelectedFile(selectedFile);
}
}
- if (ourselectedFile.exists())
+ if (selectedFile.exists())
{
int confirm = JvOptionPane.showConfirmDialog(this,
MessageManager.getString("label.overwrite_existing_file"),
MessageManager.getString("label.file_already_exists"),
JvOptionPane.YES_NO_OPTION);
-
if (confirm != JvOptionPane.YES_OPTION)
{
return;
class RecentlyOpened extends JPanel
{
+ private static final long serialVersionUID = 1L;
JList<String> list;
- public RecentlyOpened()
+ RecentlyOpened()
{
-
setPreferredSize(new Dimension(300,100));
- String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
+ String historyItems = Cache.getProperty("RECENT_FILE");
StringTokenizer st;
Vector<String> recent = new Vector<>();
}
@Override
- public JalviewFileChooser addResponse(RunResponse action)
+ public DialogRunnerI addResponse(Object response, RunResponse action)
{
- return runner.addResponse(action);
+ return runner.addResponse(response, action);
}
+ /**
+ * JalviewJS signals file selection by a property change event
+ * for property "SelectedFile". This methods responds to that by
+ * running the response action for 'OK' in the dialog.
+ *
+ * @param evt
+ */
+ @Override
+ public void propertyChange(PropertyChangeEvent evt)
+ {
+ // TODO other properties need runners...
+ switch (evt.getPropertyName())
+ {
+ case "SelectedFile":
+ runner.handleResponse(APPROVE_OPTION);
+ break;
+ }
+ }
}