| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
package org.homeunix.thecave.buddi.view; |
| 5 | |
|
| 6 | |
import java.awt.BorderLayout; |
| 7 | |
import java.awt.event.ComponentAdapter; |
| 8 | |
import java.awt.event.ComponentEvent; |
| 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.JOptionPane; |
| 16 | |
import javax.swing.JTabbedPane; |
| 17 | |
import javax.swing.SwingUtilities; |
| 18 | |
|
| 19 | |
import org.homeunix.thecave.buddi.i18n.BuddiKeys; |
| 20 | |
import org.homeunix.thecave.buddi.i18n.keys.ButtonKeys; |
| 21 | |
import org.homeunix.thecave.buddi.model.Account; |
| 22 | |
import org.homeunix.thecave.buddi.model.BudgetCategory; |
| 23 | |
import org.homeunix.thecave.buddi.model.BudgetCategoryType; |
| 24 | |
import org.homeunix.thecave.buddi.model.Document; |
| 25 | |
import org.homeunix.thecave.buddi.model.impl.DocumentImpl; |
| 26 | |
import org.homeunix.thecave.buddi.model.impl.ModelFactory; |
| 27 | |
import org.homeunix.thecave.buddi.model.prefs.PrefsModel; |
| 28 | |
import org.homeunix.thecave.buddi.plugin.BuddiPluginFactory; |
| 29 | |
import org.homeunix.thecave.buddi.plugin.api.BuddiPanelPlugin; |
| 30 | |
import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter; |
| 31 | |
import org.homeunix.thecave.buddi.view.menu.bars.BuddiMenuBar; |
| 32 | |
import org.homeunix.thecave.buddi.view.menu.items.FileSave; |
| 33 | |
import org.homeunix.thecave.buddi.view.panels.MyAccountsPanel; |
| 34 | |
import org.homeunix.thecave.buddi.view.panels.MyBudgetPanel; |
| 35 | |
import org.homeunix.thecave.buddi.view.panels.MyReportsPanel; |
| 36 | |
|
| 37 | |
import ca.digitalcave.moss.common.ClassLoaderFunctions; |
| 38 | |
import ca.digitalcave.moss.swing.ApplicationModel; |
| 39 | |
import ca.digitalcave.moss.swing.MossDocumentFrame; |
| 40 | |
import ca.digitalcave.moss.swing.MossFrame; |
| 41 | |
|
| 42 | 264 | public class MainFrame extends MossDocumentFrame { |
| 43 | |
public static final long serialVersionUID = 0; |
| 44 | |
|
| 45 | |
private final MyAccountsPanel myAccounts; |
| 46 | |
private final MyBudgetPanel myBudget; |
| 47 | |
private final MyReportsPanel myReports; |
| 48 | |
|
| 49 | |
private final JTabbedPane tabs; |
| 50 | |
|
| 51 | |
private List<BuddiPanelPlugin> panelPlugins; |
| 52 | |
|
| 53 | |
@SuppressWarnings("unchecked") |
| 54 | |
public MainFrame(Document model) { |
| 55 | 3034 | super(model, MainFrame.class.getName() + model.getUid() + "_" + model.getFile()); |
| 56 | 3034 | this.setIconImage(ClassLoaderFunctions.getImageFromClasspath("img/BuddiFrameIcon.gif")); |
| 57 | 3034 | myAccounts = new MyAccountsPanel(this); |
| 58 | 3034 | myBudget = new MyBudgetPanel(this); |
| 59 | 3025 | myReports = new MyReportsPanel(this); |
| 60 | 3025 | panelPlugins = (List<BuddiPanelPlugin>) BuddiPluginFactory.getPlugins(BuddiPanelPlugin.class); |
| 61 | 3025 | tabs = new JTabbedPane(); |
| 62 | 3025 | } |
| 63 | |
|
| 64 | |
@Override |
| 65 | |
public void init() { |
| 66 | 3025 | super.init(); |
| 67 | |
|
| 68 | 3025 | tabs.addTab(TextFormatter.getTranslation(BuddiKeys.MY_ACCOUNTS), myAccounts); |
| 69 | 3025 | tabs.addTab(TextFormatter.getTranslation(BuddiKeys.MY_BUDGET), myBudget); |
| 70 | 3025 | tabs.addTab(TextFormatter.getTranslation(BuddiKeys.MY_REPORTS), myReports); |
| 71 | |
|
| 72 | 3025 | if (panelPlugins != null){ |
| 73 | 3025 | for (BuddiPanelPlugin panelPlugin : panelPlugins){ |
| 74 | 0 | if (panelPlugin.isStartedAutomatically() || panelPlugin.wasInUse()){ |
| 75 | 0 | panelPlugin.setParentFrame(this); |
| 76 | 0 | addPanel(TextFormatter.getTranslation(panelPlugin.getTabLabelKey()), panelPlugin); |
| 77 | |
} |
| 78 | |
} |
| 79 | |
} |
| 80 | |
|
| 81 | 3025 | this.setJMenuBar(new BuddiMenuBar(this)); |
| 82 | |
|
| 83 | 3025 | ComponentAdapter tabSelectListener = new ComponentAdapter(){ |
| 84 | |
@Override |
| 85 | |
public void componentShown(ComponentEvent e) { |
| 86 | 310 | super.componentShown(e); |
| 87 | |
|
| 88 | 310 | updateContent(); |
| 89 | 310 | } |
| 90 | |
}; |
| 91 | |
|
| 92 | 3025 | myAccounts.addComponentListener(tabSelectListener); |
| 93 | 3025 | myBudget.addComponentListener(tabSelectListener); |
| 94 | 3025 | myReports.addComponentListener(tabSelectListener); |
| 95 | |
|
| 96 | 3025 | if (panelPlugins != null){ |
| 97 | 3025 | for (BuddiPanelPlugin panelPlugin : panelPlugins){ |
| 98 | 0 | panelPlugin.addComponentListener(tabSelectListener); |
| 99 | |
} |
| 100 | |
} |
| 101 | |
|
| 102 | 3025 | this.add(tabs, BorderLayout.CENTER); |
| 103 | 3025 | } |
| 104 | |
|
| 105 | |
@Override |
| 106 | |
public void updateContent() { |
| 107 | 9285 | super.updateContent(); |
| 108 | |
|
| 109 | 9285 | String dataFile = getDocument().getFile() == null ? "" : " - " + getDocument().getFile().getAbsolutePath(); |
| 110 | 9285 | this.setTitle(TextFormatter.getTranslation(BuddiKeys.BUDDI) + dataFile); |
| 111 | |
|
| 112 | 9285 | myAccounts.updateContent(); |
| 113 | 9285 | myBudget.updateContent(); |
| 114 | 9285 | myReports.updateContent(); |
| 115 | |
|
| 116 | 9285 | if (panelPlugins != null){ |
| 117 | 9285 | for (BuddiPanelPlugin panelPlugin : panelPlugins){ |
| 118 | 0 | panelPlugin.updateContent(); |
| 119 | |
} |
| 120 | |
} |
| 121 | |
|
| 122 | 9285 | this.repaint(); |
| 123 | 9285 | } |
| 124 | |
|
| 125 | |
@Override |
| 126 | |
public boolean canClose() { |
| 127 | 23 | if (getDocument().isChanged()){ |
| 128 | 23 | String[] buttons = new String[3]; |
| 129 | 23 | buttons[0] = PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_YES); |
| 130 | 23 | buttons[1] = PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_NO); |
| 131 | 23 | buttons[2] = PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_CANCEL); |
| 132 | |
|
| 133 | 23 | int reply = JOptionPane.showOptionDialog( |
| 134 | |
this, |
| 135 | |
PrefsModel.getInstance().getTranslator().get(BuddiKeys.MESSAGE_PROMPT_FOR_SAVE), |
| 136 | |
PrefsModel.getInstance().getTranslator().get(BuddiKeys.MESSAGE_PROMPT_FOR_SAVE_TITLE), |
| 137 | |
JOptionPane.YES_NO_CANCEL_OPTION, |
| 138 | |
JOptionPane.QUESTION_MESSAGE, |
| 139 | |
null, |
| 140 | |
buttons, |
| 141 | |
buttons[0]); |
| 142 | |
|
| 143 | |
|
| 144 | 0 | if (reply == JOptionPane.YES_OPTION){ |
| 145 | |
|
| 146 | 0 | new FileSave(this).doClick(); |
| 147 | 0 | return true; |
| 148 | |
} |
| 149 | 0 | else if (reply == JOptionPane.NO_OPTION){ |
| 150 | |
|
| 151 | 0 | return true; |
| 152 | |
} |
| 153 | |
else { |
| 154 | |
|
| 155 | 0 | return false; |
| 156 | |
} |
| 157 | |
} |
| 158 | 0 | return true; |
| 159 | |
} |
| 160 | |
|
| 161 | |
@Override |
| 162 | |
public Object closeWindow() { |
| 163 | 1 | super.closeWindow(); |
| 164 | 0 | List<MossFrame> frames = ApplicationModel.getInstance().getOpenFrames(); |
| 165 | 0 | List<File> openFiles = new LinkedList<File>(); |
| 166 | 0 | for (MossFrame frame : frames) { |
| 167 | 0 | if (frame instanceof MainFrame){ |
| 168 | 0 | openFiles.add(((MainFrame) frame).getDocument().getFile()); |
| 169 | |
} |
| 170 | |
} |
| 171 | 0 | if (openFiles.size() > 0) |
| 172 | 0 | PrefsModel.getInstance().setLastDataFiles(openFiles); |
| 173 | |
|
| 174 | 0 | PrefsModel.getInstance().save(); |
| 175 | |
|
| 176 | 0 | return null; |
| 177 | |
} |
| 178 | |
|
| 179 | |
@Override |
| 180 | |
public void closeWindowWithoutPrompting() { |
| 181 | |
|
| 182 | 0 | if (((DocumentImpl) getDocument()).isCurrentlySaving()){ |
| 183 | |
try { |
| 184 | 0 | ((DocumentImpl) getDocument()).waitUntilFinishedSaving(); |
| 185 | |
} |
| 186 | 0 | catch (InterruptedException ie) { |
| 187 | 0 | Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Thread interrupted while waiting for save operation to complete. Cancelling exit request.", ie); |
| 188 | 0 | return; |
| 189 | 0 | } |
| 190 | |
} |
| 191 | |
|
| 192 | 0 | PrefsModel.getInstance().putWindowSize(this.getDocument().getFile() + "", this.getSize()); |
| 193 | 0 | PrefsModel.getInstance().putWindowLocation(this.getDocument().getFile() + "", this.getLocation()); |
| 194 | 0 | PrefsModel.getInstance().save(); |
| 195 | |
|
| 196 | 0 | ModelFactory.getAutoSaveLocation(getDocument().getFile()).delete(); |
| 197 | |
|
| 198 | 0 | super.closeWindowWithoutPrompting(); |
| 199 | 0 | } |
| 200 | |
|
| 201 | |
public List<BudgetCategory> getSelectedBudgetCategories(){ |
| 202 | 3952 | if (isMyBudgetTabSelected()) |
| 203 | 3880 | return myBudget.getSelectedBudgetCategories(); |
| 204 | |
|
| 205 | 72 | return new LinkedList<BudgetCategory>(); |
| 206 | |
} |
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
public List<BudgetCategory> getBudgetCategoriesInSelectedPeriod(){ |
| 216 | 840 | List<BudgetCategory> budgetCategories = new LinkedList<BudgetCategory>(); |
| 217 | 840 | BudgetCategoryType periodType = myBudget.getTreeTableModel().getSelectedBudgetPeriodType(); |
| 218 | |
|
| 219 | 840 | for (BudgetCategory bc : ((Document) getDocument()).getBudgetCategories()) { |
| 220 | 8400 | if (bc.getBudgetPeriodType().equals(periodType)) |
| 221 | 8400 | budgetCategories.add(bc); |
| 222 | |
} |
| 223 | |
|
| 224 | 840 | return budgetCategories; |
| 225 | |
} |
| 226 | |
|
| 227 | |
public MyAccountsPanel getMyAccountsPanel(){ |
| 228 | 0 | return myAccounts; |
| 229 | |
} |
| 230 | |
|
| 231 | |
public MyBudgetPanel getMyBudgetPanel(){ |
| 232 | 840 | return myBudget; |
| 233 | |
} |
| 234 | |
|
| 235 | |
public List<Account> getSelectedAccounts(){ |
| 236 | 673342 | if (isMyAccountsTabSelected()) |
| 237 | 659692 | return myAccounts.getSelectedAccounts(); |
| 238 | |
|
| 239 | 13650 | return new LinkedList<Account>(); |
| 240 | |
} |
| 241 | |
|
| 242 | |
public boolean isMyAccountsTabSelected(){ |
| 243 | 904192 | return tabs.getSelectedIndex() == 0; |
| 244 | |
} |
| 245 | |
public boolean isMyBudgetTabSelected(){ |
| 246 | 9922 | return tabs.getSelectedIndex() == 1; |
| 247 | |
} |
| 248 | |
public boolean isMyReportsTabSelected(){ |
| 249 | 250 | return tabs.getSelectedIndex() == 2; |
| 250 | |
} |
| 251 | |
|
| 252 | |
public boolean isPanelOpen(BuddiPanelPlugin plugin){ |
| 253 | 0 | return (tabs.indexOfComponent(plugin) != -1); |
| 254 | |
} |
| 255 | |
|
| 256 | |
public void addPanel(String title, BuddiPanelPlugin plugin){ |
| 257 | 0 | tabs.addTab(title, plugin); |
| 258 | 0 | } |
| 259 | |
|
| 260 | |
public void removePanel(BuddiPanelPlugin plugin){ |
| 261 | 0 | tabs.removeTabAt(tabs.indexOfComponent(plugin)); |
| 262 | 0 | } |
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
public static void updateAllContent(){ |
| 268 | 90 | for (MossFrame frame : ApplicationModel.getInstance().getOpenFrames()) { |
| 269 | 182 | frame.updateContent(); |
| 270 | 182 | if (frame instanceof MainFrame) |
| 271 | 90 | ((MainFrame) frame).fireStructureChanged(); |
| 272 | |
} |
| 273 | 90 | } |
| 274 | |
|
| 275 | |
public void fireStructureChanged(){ |
| 276 | 132 | SwingUtilities.invokeLater(new Runnable(){ |
| 277 | |
public void run() { |
| 278 | 132 | myAccounts.fireStructureChanged(); |
| 279 | 132 | myBudget.fireStructureChanged(); |
| 280 | 132 | } |
| 281 | |
}); |
| 282 | 132 | } |
| 283 | |
} |