package hirondelle.stocks.file;
import java.awt.event.*;
import javax.swing.*;
import hirondelle.stocks.portfolio.PortfolioDAO;
import hirondelle.stocks.util.Consts;
import hirondelle.stocks.util.Args;
import hirondelle.stocks.util.ui.UiUtil;
import hirondelle.stocks.portfolio.CurrentPortfolio;
import java.util.logging.Logger;
import hirondelle.stocks.util.Util;
public final class FileSaveAsAction extends AbstractAction {
public FileSaveAsAction(CurrentPortfolio aCurrentPortfolio, JFrame aFrame) {
super("Save As...", UiUtil.getImageIcon("/toolbarButtonGraphics/general/SaveAs"));
Args.checkForNull(aCurrentPortfolio);
Args.checkForNull(aFrame);
fCurrentPortfolio = aCurrentPortfolio;
fFrame = aFrame;
putValue(SHORT_DESCRIPTION, "Save the current portfolio under a new name");
putValue(LONG_DESCRIPTION, "Save the current portfolio under a new given name");
putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_A) );
}
@Override public void actionPerformed(ActionEvent e) {
fLogger.info("Saving the current portfolio under a new name.");
fPortfolioDAO = new PortfolioDAO();
showDialog();
}
private CurrentPortfolio fCurrentPortfolio;
private PortfolioDAO fPortfolioDAO;
private JFrame fFrame;
private static final Logger fLogger = Util.getLogger(FileSaveAsAction.class);
private void showDialog(){
String newName = Consts.EMPTY_STRING;
while ( isInvalid(newName) ) {
newName = askForNewName();
if (newName == null) return;
if (isInvalid(newName)) {
String title = UiUtil.getDialogTitle("Invalid Input");
String message =
"New name must have content, and must not " +
"duplicate a known Portfolio name."
;
JOptionPane.showMessageDialog(
fFrame, message, title, JOptionPane.INFORMATION_MESSAGE
);
}
}
savePortfolio(newName);
}
private String askForNewName(){
String title = UiUtil.getDialogTitle("Save As");
return JOptionPane.showInputDialog(
fFrame, "File Name:", title, JOptionPane.QUESTION_MESSAGE
);
}
private boolean isInvalid( String aNewName ){
return ! fPortfolioDAO.isValidCandidateName(aNewName);
}
private void savePortfolio(String aNewName ){
fCurrentPortfolio.setName( aNewName );
fCurrentPortfolio.setNeedsSave(false);
fPortfolioDAO.saveAs(fCurrentPortfolio.getPortfolio());
fCurrentPortfolio.notifyObservers();
}
}