| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
package org.homeunix.thecave.buddi.view.menu.items; |
| 5 | |
|
| 6 | |
import java.awt.BorderLayout; |
| 7 | |
import java.awt.Dimension; |
| 8 | |
import java.awt.event.ActionEvent; |
| 9 | |
import java.io.File; |
| 10 | |
import java.util.LinkedList; |
| 11 | |
import java.util.List; |
| 12 | |
import java.util.logging.Level; |
| 13 | |
import java.util.logging.Logger; |
| 14 | |
|
| 15 | |
import javax.swing.JDialog; |
| 16 | |
import javax.swing.JOptionPane; |
| 17 | |
import javax.swing.JScrollPane; |
| 18 | |
import javax.swing.JTextArea; |
| 19 | |
import javax.swing.filechooser.FileFilter; |
| 20 | |
|
| 21 | |
import net.java.dev.SwingWorker; |
| 22 | |
|
| 23 | |
import org.homeunix.thecave.buddi.i18n.BuddiKeys; |
| 24 | |
import org.homeunix.thecave.buddi.i18n.keys.ButtonKeys; |
| 25 | |
import org.homeunix.thecave.buddi.model.BudgetCategory; |
| 26 | |
import org.homeunix.thecave.buddi.model.Document; |
| 27 | |
import org.homeunix.thecave.buddi.model.impl.ModelFactory; |
| 28 | |
import org.homeunix.thecave.buddi.model.prefs.PrefsModel; |
| 29 | |
import org.homeunix.thecave.buddi.plugin.api.BuddiExportPlugin; |
| 30 | |
import org.homeunix.thecave.buddi.plugin.api.BuddiImportPlugin; |
| 31 | |
import org.homeunix.thecave.buddi.plugin.api.BuddiSynchronizePlugin; |
| 32 | |
import org.homeunix.thecave.buddi.plugin.api.MenuPlugin; |
| 33 | |
import org.homeunix.thecave.buddi.plugin.api.exception.ModelException; |
| 34 | |
import org.homeunix.thecave.buddi.plugin.api.exception.PluginException; |
| 35 | |
import org.homeunix.thecave.buddi.plugin.api.exception.PluginMessage; |
| 36 | |
import org.homeunix.thecave.buddi.plugin.api.model.impl.MutableDocumentImpl; |
| 37 | |
import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter; |
| 38 | |
import org.homeunix.thecave.buddi.view.MainFrame; |
| 39 | |
import org.homeunix.thecave.buddi.view.TransactionFrame; |
| 40 | |
|
| 41 | |
import ca.digitalcave.moss.swing.MossDocumentFrame; |
| 42 | |
import ca.digitalcave.moss.swing.MossMenuItem; |
| 43 | |
import ca.digitalcave.moss.swing.MossSmartFileChooser; |
| 44 | |
import ca.digitalcave.moss.swing.MossStatusDialog; |
| 45 | |
import ca.digitalcave.moss.swing.exception.WindowOpenException; |
| 46 | |
|
| 47 | 0 | public class PluginMenuEntry extends MossMenuItem { |
| 48 | |
public static final long serialVersionUID = 0; |
| 49 | |
|
| 50 | |
private final MenuPlugin plugin; |
| 51 | |
|
| 52 | |
public PluginMenuEntry(MossDocumentFrame parentFrame, MenuPlugin plugin) { |
| 53 | 0 | super(parentFrame, TextFormatter.getTranslation(plugin.getName())); |
| 54 | |
|
| 55 | 0 | this.plugin = plugin; |
| 56 | 0 | } |
| 57 | |
|
| 58 | |
@Override |
| 59 | |
public void actionPerformed(ActionEvent e) { |
| 60 | |
final Document d; |
| 61 | 0 | if (plugin instanceof BuddiImportPlugin && ((BuddiImportPlugin) plugin).isCreateNewFile()){ |
| 62 | |
try { |
| 63 | 0 | d = ModelFactory.createDocument(); |
| 64 | 0 | List<BudgetCategory> bcs = new LinkedList<BudgetCategory>(d.getBudgetCategories()); |
| 65 | 0 | for (BudgetCategory bc : bcs) { |
| 66 | 0 | d.removeBudgetCategory(bc); |
| 67 | |
} |
| 68 | |
} |
| 69 | 0 | catch (ModelException me){ |
| 70 | 0 | Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Could not create a new document!", me); |
| 71 | 0 | return; |
| 72 | 0 | } |
| 73 | |
} |
| 74 | |
else |
| 75 | 0 | d = (Document) ((MossDocumentFrame) getFrame()).getDocument(); |
| 76 | |
|
| 77 | 0 | File f = null; |
| 78 | |
|
| 79 | 0 | if (plugin.isPromptForFile()){ |
| 80 | 0 | FileFilter filter = new FileFilter(){ |
| 81 | |
@Override |
| 82 | |
public boolean accept(File f) { |
| 83 | 0 | if (f.isDirectory()) |
| 84 | 0 | return true; |
| 85 | 0 | if (plugin.getFileExtensions() != null){ |
| 86 | 0 | for (String s : plugin.getFileExtensions()) { |
| 87 | 0 | if (f.getName().endsWith(s)) |
| 88 | 0 | return true; |
| 89 | |
} |
| 90 | |
} |
| 91 | |
else |
| 92 | 0 | return true; |
| 93 | 0 | return false; |
| 94 | |
} |
| 95 | |
|
| 96 | |
@Override |
| 97 | |
public String getDescription() { |
| 98 | 0 | return TextFormatter.getTranslation(plugin.getDescription()); |
| 99 | |
} |
| 100 | |
}; |
| 101 | |
|
| 102 | 0 | String title = "Unknown Plugin Type"; |
| 103 | 0 | if (plugin instanceof BuddiExportPlugin) |
| 104 | 0 | title = PrefsModel.getInstance().getTranslator().get(BuddiKeys.FILECHOOSER_EXPORT_FILE_TITLE); |
| 105 | 0 | else if (plugin instanceof BuddiImportPlugin) |
| 106 | 0 | title = PrefsModel.getInstance().getTranslator().get(BuddiKeys.FILECHOOSER_IMPORT_FILE_TITLE); |
| 107 | 0 | else if (plugin instanceof BuddiSynchronizePlugin) |
| 108 | 0 | title = PrefsModel.getInstance().getTranslator().get(BuddiKeys.FILECHOOSER_SYNCHRONIZE_FILE_TITLE); |
| 109 | |
|
| 110 | 0 | if (plugin.isFileChooserSave()) |
| 111 | 0 | f = MossSmartFileChooser.showSaveFileDialog( |
| 112 | |
getFrame(), |
| 113 | |
filter, |
| 114 | |
plugin.getFileExtensions() == null || plugin.getFileExtensions().length == 0 ? |
| 115 | |
null : |
| 116 | |
plugin.getFileExtensions()[0], |
| 117 | |
title, |
| 118 | |
PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_OK), |
| 119 | |
PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_CANCEL), |
| 120 | |
PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_REPLACE), |
| 121 | |
PrefsModel.getInstance().getTranslator().get(BuddiKeys.MESSAGE_ERROR_CANNOT_WRITE_DATA_FILE), |
| 122 | |
PrefsModel.getInstance().getTranslator().get(BuddiKeys.ERROR), |
| 123 | |
PrefsModel.getInstance().getTranslator().get(BuddiKeys.MESSAGE_PROMPT_OVERWRITE_FILE), |
| 124 | |
PrefsModel.getInstance().getTranslator().get(BuddiKeys.MESSAGE_PROMPT_OVERWRITE_FILE_TITLE)); |
| 125 | |
|
| 126 | |
else |
| 127 | 0 | f = MossSmartFileChooser.showOpenDialog( |
| 128 | |
getFrame(), |
| 129 | |
filter, |
| 130 | |
title, |
| 131 | |
PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_OK), |
| 132 | |
PrefsModel.getInstance().getTranslator().get(BuddiKeys.MESSAGE_ERROR_CANNOT_WRITE_DATA_FILE), |
| 133 | |
PrefsModel.getInstance().getTranslator().get(BuddiKeys.MESSAGE_ERROR_CANNOT_READ_DATA_FILE), |
| 134 | |
PrefsModel.getInstance().getTranslator().get(BuddiKeys.ERROR)); |
| 135 | |
|
| 136 | 0 | if (f == null) |
| 137 | 0 | return; |
| 138 | |
} |
| 139 | |
|
| 140 | 0 | d.startBatchChange(); |
| 141 | |
|
| 142 | |
try { |
| 143 | 0 | final File fFinal = f; |
| 144 | 0 | final MossStatusDialog status = new MossStatusDialog( |
| 145 | |
getFrame(), |
| 146 | |
TextFormatter.getTranslation(plugin.getProcessingMessage())); |
| 147 | |
|
| 148 | 0 | if (plugin.getProcessingMessage() != null) |
| 149 | 0 | status.openWindow(); |
| 150 | |
|
| 151 | 0 | SwingWorker worker = new SwingWorker(){ |
| 152 | |
@Override |
| 153 | |
public Object construct() { |
| 154 | |
try { |
| 155 | 0 | plugin.processData(new MutableDocumentImpl(d), ((MossDocumentFrame) getFrame()), fFinal); |
| 156 | |
} |
| 157 | 0 | catch (PluginMessage pm){ |
| 158 | 0 | return pm; |
| 159 | |
} |
| 160 | 0 | catch (PluginException pe){ |
| 161 | 0 | Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Error processing data in plugin: ", pe); |
| 162 | 0 | return null; |
| 163 | |
} |
| 164 | 0 | catch (RuntimeException re){ |
| 165 | 0 | Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Runtime exception processing data in plugin: ", re); |
| 166 | 0 | return null; |
| 167 | |
} |
| 168 | 0 | catch (Error e){ |
| 169 | 0 | Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Error processing data in plugin: ", e); |
| 170 | 0 | return null; |
| 171 | 0 | } |
| 172 | |
|
| 173 | 0 | return new Object(); |
| 174 | |
} |
| 175 | |
|
| 176 | |
@Override |
| 177 | |
public void finished() { |
| 178 | 0 | status.closeWindow(); |
| 179 | |
|
| 180 | 0 | d.updateAllBalances(); |
| 181 | 0 | d.finishBatchChange(); |
| 182 | |
|
| 183 | 0 | if (plugin instanceof BuddiImportPlugin && ((BuddiImportPlugin) plugin).isCreateNewFile()){ |
| 184 | |
try { |
| 185 | 0 | MainFrame mainWndow = new MainFrame(d); |
| 186 | 0 | mainWndow.openWindow( |
| 187 | |
PrefsModel.getInstance().getWindowSize(d.getFile() + ""), |
| 188 | |
PrefsModel.getInstance().getWindowLocation(d.getFile() + "")); |
| 189 | |
} |
| 190 | 0 | catch (WindowOpenException woe){} |
| 191 | |
} |
| 192 | |
else { |
| 193 | 0 | if (getFrame() instanceof MainFrame) |
| 194 | 0 | ((MainFrame) getFrame()).fireStructureChanged(); |
| 195 | 0 | if (getFrame() instanceof TransactionFrame) |
| 196 | 0 | ((TransactionFrame) getFrame()).fireStructureChanged(); |
| 197 | |
} |
| 198 | |
|
| 199 | 0 | MainFrame.updateAllContent(); |
| 200 | |
|
| 201 | 0 | if (get() == null){ |
| 202 | 0 | String[] options = new String[1]; |
| 203 | 0 | options[0] = PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_OK); |
| 204 | |
|
| 205 | 0 | JOptionPane.showOptionDialog( |
| 206 | |
getFrame(), |
| 207 | |
TextFormatter.getTranslation(BuddiKeys.MESSAGE_ERROR_CHECK_LOGS_FOR_DETAILS), |
| 208 | |
TextFormatter.getTranslation(BuddiKeys.ERROR), |
| 209 | |
JOptionPane.DEFAULT_OPTION, |
| 210 | |
JOptionPane.ERROR_MESSAGE, |
| 211 | |
null, |
| 212 | |
options, |
| 213 | |
options[0] |
| 214 | |
); |
| 215 | 0 | } |
| 216 | 0 | else if (get() instanceof PluginMessage){ |
| 217 | 0 | String[] options = new String[1]; |
| 218 | 0 | options[0] = PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_OK); |
| 219 | |
|
| 220 | 0 | PluginMessage message = (PluginMessage) get(); |
| 221 | 0 | JOptionPane optionPane = new JOptionPane(message.getMessage(), message.getType(), JOptionPane.OK_OPTION, null, options, options[0]); |
| 222 | 0 | JDialog dial = optionPane.createDialog(null, message.getTitle()); |
| 223 | 0 | dial.setModal(true); |
| 224 | 0 | JTextArea text = new JTextArea(); |
| 225 | 0 | JScrollPane scroller = new JScrollPane(text); |
| 226 | 0 | if (message.getLongMessage() != null){ |
| 227 | 0 | text.setEditable(false); |
| 228 | 0 | text.setWrapStyleWord(true); |
| 229 | 0 | text.setLineWrap(true); |
| 230 | 0 | text.setText(message.getLongMessage()); |
| 231 | 0 | scroller.setPreferredSize(new Dimension(350, 200)); |
| 232 | 0 | scroller.setBorder(null); |
| 233 | 0 | dial.getContentPane().add(scroller, BorderLayout.SOUTH); |
| 234 | |
} |
| 235 | |
|
| 236 | 0 | dial.pack(); |
| 237 | |
|
| 238 | 0 | scroller.getVerticalScrollBar().setValue(0); |
| 239 | 0 | scroller.getHorizontalScrollBar().setValue(0); |
| 240 | |
|
| 241 | 0 | dial.setVisible(true); |
| 242 | 0 | optionPane.getValue(); |
| 243 | |
} |
| 244 | |
|
| 245 | 0 | super.finished(); |
| 246 | 0 | } |
| 247 | |
}; |
| 248 | |
|
| 249 | 0 | worker.start(); |
| 250 | |
} |
| 251 | 0 | catch (Exception ex){ |
| 252 | 0 | Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Error encountered in plugin: " + ex); |
| 253 | 0 | } |
| 254 | 0 | } |
| 255 | |
} |