| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
package org.homeunix.thecave.buddi.view.menu.items; |
| 5 | |
|
| 6 | |
import java.awt.Toolkit; |
| 7 | |
import java.awt.event.ActionEvent; |
| 8 | |
import java.awt.event.KeyEvent; |
| 9 | |
import java.io.File; |
| 10 | |
|
| 11 | |
import javax.swing.JOptionPane; |
| 12 | |
import javax.swing.KeyStroke; |
| 13 | |
|
| 14 | |
import org.homeunix.thecave.buddi.Const; |
| 15 | |
import org.homeunix.thecave.buddi.i18n.BuddiKeys; |
| 16 | |
import org.homeunix.thecave.buddi.i18n.keys.ButtonKeys; |
| 17 | |
import org.homeunix.thecave.buddi.i18n.keys.MenuKeys; |
| 18 | |
import org.homeunix.thecave.buddi.model.Document; |
| 19 | |
import org.homeunix.thecave.buddi.model.impl.ConcurrentSaveException; |
| 20 | |
import org.homeunix.thecave.buddi.model.impl.ModelFactory; |
| 21 | |
import org.homeunix.thecave.buddi.model.prefs.PrefsModel; |
| 22 | |
import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter; |
| 23 | |
|
| 24 | |
import ca.digitalcave.moss.application.document.exception.DocumentSaveException; |
| 25 | |
import ca.digitalcave.moss.swing.MossDocumentFrame; |
| 26 | |
import ca.digitalcave.moss.swing.MossFrame; |
| 27 | |
import ca.digitalcave.moss.swing.MossMenuItem; |
| 28 | |
import ca.digitalcave.moss.swing.MossSmartFileChooser; |
| 29 | |
|
| 30 | |
public class FileSaveAs extends MossMenuItem { |
| 31 | |
public static final long serialVersionUID = 0; |
| 32 | |
|
| 33 | |
public FileSaveAs(MossFrame frame) { |
| 34 | 6135 | super(frame, PrefsModel.getInstance().getTranslator().get(MenuKeys.MENU_FILE_SAVE_AS), |
| 35 | |
KeyStroke.getKeyStroke(KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() + KeyEvent.SHIFT_MASK)); |
| 36 | 6135 | } |
| 37 | |
|
| 38 | |
@Override |
| 39 | |
public void actionPerformed(ActionEvent e) { |
| 40 | 182 | boolean removeAutosave = false; |
| 41 | 182 | if (((MossDocumentFrame) getFrame()).getDocument().getFile() == null) |
| 42 | 182 | removeAutosave = true; |
| 43 | |
|
| 44 | 182 | File f = MossSmartFileChooser.showSaveFileDialog( |
| 45 | |
getFrame(), |
| 46 | |
Const.FILE_FILTER_DATA, |
| 47 | |
Const.DATA_FILE_EXTENSION, |
| 48 | |
PrefsModel.getInstance().getTranslator().get(BuddiKeys.FILECHOOSER_SAVE_DATA_FILE_TITLE), |
| 49 | |
PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_OK), |
| 50 | |
PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_CANCEL), |
| 51 | |
PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_REPLACE), |
| 52 | |
PrefsModel.getInstance().getTranslator().get(BuddiKeys.MESSAGE_ERROR_CANNOT_WRITE_DATA_FILE), |
| 53 | |
PrefsModel.getInstance().getTranslator().get(BuddiKeys.ERROR), |
| 54 | |
PrefsModel.getInstance().getTranslator().get(BuddiKeys.MESSAGE_PROMPT_OVERWRITE_FILE), |
| 55 | |
PrefsModel.getInstance().getTranslator().get(BuddiKeys.MESSAGE_PROMPT_OVERWRITE_FILE_TITLE) |
| 56 | |
); |
| 57 | |
|
| 58 | |
|
| 59 | 22 | if (f == null) |
| 60 | 21 | return; |
| 61 | |
|
| 62 | |
try { |
| 63 | |
|
| 64 | 1 | String[] options = new String[2]; |
| 65 | 1 | options[0] = TextFormatter.getTranslation(ButtonKeys.BUTTON_YES); |
| 66 | 1 | options[1] = TextFormatter.getTranslation(ButtonKeys.BUTTON_NO); |
| 67 | |
|
| 68 | |
|
| 69 | 1 | ((Document) ((MossDocumentFrame) getFrame()).getDocument()).setFlag(Document.RESET_PASSWORD, true); |
| 70 | 1 | ((Document) ((MossDocumentFrame) getFrame()).getDocument()).setFlag(Document.CHANGE_PASSWORD, |
| 71 | |
(JOptionPane.showOptionDialog( |
| 72 | |
getFrame(), |
| 73 | |
TextFormatter.getTranslation(BuddiKeys.MESSAGE_ASK_FOR_DATA_FILE_ENCRYPTION), |
| 74 | |
TextFormatter.getTranslation(BuddiKeys.MESSAGE_ASK_FOR_DATA_FILE_ENCRYPTION_TITLE), |
| 75 | |
JOptionPane.YES_NO_OPTION, |
| 76 | |
JOptionPane.INFORMATION_MESSAGE, |
| 77 | |
null, |
| 78 | |
options, |
| 79 | |
options[0] |
| 80 | |
) == JOptionPane.YES_OPTION)); |
| 81 | |
|
| 82 | 0 | ((MossDocumentFrame) getFrame()).getDocument().saveAs(f); |
| 83 | 0 | getFrame().updateContent(); |
| 84 | |
} |
| 85 | 0 | catch (ConcurrentSaveException cse){ |
| 86 | 0 | JOptionPane.showMessageDialog( |
| 87 | |
null, |
| 88 | |
TextFormatter.getTranslation(BuddiKeys.MESSAGE_CONCURRENT_SAVE_EXCEPTION_TEXT), |
| 89 | |
TextFormatter.getTranslation(BuddiKeys.WARNING), |
| 90 | |
JOptionPane.WARNING_MESSAGE); |
| 91 | |
} |
| 92 | 0 | catch (DocumentSaveException dse){ |
| 93 | 0 | throw new RuntimeException("Error saving file: " + dse); |
| 94 | 0 | } |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | 0 | if (removeAutosave) |
| 103 | 0 | ModelFactory.getAutoSaveLocation(null).delete(); |
| 104 | 0 | } |
| 105 | |
|
| 106 | |
@Override |
| 107 | |
public void updateMenus() { |
| 108 | 42468 | super.updateMenus(); |
| 109 | |
|
| 110 | 42468 | this.setEnabled(getFrame() instanceof MossDocumentFrame); |
| 111 | 42468 | } |
| 112 | |
} |