import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SpringLayout;
+import javax.swing.SwingUtilities;
+import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.plaf.basic.BasicFileChooserUI;
public int showSaveDialog(Component parent) throws HeadlessException
{
this.setAccessory(null);
+ this.setSelectedFile(null);
+ return super.showSaveDialog(parent);
+ }
- setDialogType(SAVE_DIALOG);
+ /**
+ * If doing a Save, and an existing file is chosen or entered, prompt for
+ * confirmation of overwrite. Proceed if Yes, else leave the file chooser
+ * open.
+ *
+ * @see https://stackoverflow.com/questions/8581215/jfilechooser-and-checking-for-overwrite
+ */
+ @Override
+ public void approveSelection()
+ {
+ if (getDialogType() != SAVE_DIALOG)
+ {
+ super.approveSelection();
+ return;
+ }
- this.setSelectedFile(null);
- int ret = showDialog(parent, MessageManager.getString("action.save"));
ourselectedFile = getSelectedFile();
- if (getSelectedFile() == null)
+ if (ourselectedFile == null)
{
// Workaround for Java 9,10 on OSX - no selected file, but there is a
// filename typed in
"Unexpected exception when trying to get filename.");
x.printStackTrace();
}
+ // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND
+ // THE
+ // USER PROMPTED FOR A NEW FILENAME
}
if (ourselectedFile == null)
{
- return JalviewFileChooser.CANCEL_OPTION;
+ return;
}
+
if (getFileFilter() instanceof JalviewFileFilter)
{
JalviewFileFilter jvf = (JalviewFileFilter) getFileFilter();
}
}
- // TODO: ENSURE THAT FILES SAVED WITH A ':' IN THE NAME ARE REFUSED AND THE
- // USER PROMPTED FOR A NEW FILENAME.
- // DO NOT need to confirm file overwrite if using backup files (the files
- // aren't being overwritten!)
- if ((ret == JalviewFileChooser.APPROVE_OPTION)
- && ourselectedFile.exists() && (!BackupFiles.getEnabled()))
+ if (ourselectedFile.exists())
{
- int confirm = JvOptionPane.showConfirmDialog(parent,
+ 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)
{
- ret = JalviewFileChooser.CANCEL_OPTION;
+ return;
}
-
}
- return ret;
+ super.approveSelection();
}
void recentListSelectionChanged(Object selection)
}
}
+ /**
+ * A panel to set as the 'accessory' component to the file chooser dialog,
+ * holding a list of recently opened files (if any). These are held as a
+ * tab-separated list of file paths under key <code>RECENT_FILE</code> in
+ * <code>.jalview_properties</code>. A click in the list calls a method in
+ * JalviewFileChooser to set the chosen file as the selection.
+ */
class RecentlyOpened extends JPanel
{
- JList list;
+ private static final long serialVersionUID = 1L;
- public RecentlyOpened()
- {
+ private JList<String> list;
- String historyItems = jalview.bin.Cache.getProperty("RECENT_FILE");
+ RecentlyOpened()
+ {
+ String historyItems = Cache.getProperty("RECENT_FILE");
StringTokenizer st;
- Vector recent = new Vector();
+ Vector<String> recent = new Vector<>();
if (historyItems != null)
{
st = new StringTokenizer(historyItems, "\t");
-
while (st.hasMoreTokens())
{
- recent.addElement(st.nextElement());
+ recent.addElement(st.nextToken());
}
}
- list = new JList(recent);
+ list = new JList<>(recent);
DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
}
});
- this.setBorder(new javax.swing.border.TitledBorder(
+ this.setBorder(new TitledBorder(
MessageManager.getString("label.recently_opened")));
final JScrollPane scroller = new JScrollPane(list);
layout.putConstraint(SpringLayout.NORTH, scroller, 5,
SpringLayout.NORTH, this);
- if (new Platform().isAMac())
+ if (Platform.isAMac())
{
scroller.setPreferredSize(new Dimension(500, 100));
}
this.add(scroller);
- javax.swing.SwingUtilities.invokeLater(new Runnable()
+ SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
.setValue(scroller.getHorizontalScrollBar().getMaximum());
}
});
-
}
-
}
}