| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
package org.homeunix.thecave.buddi.view.dialogs; |
| 5 | |
|
| 6 | |
import java.awt.BorderLayout; |
| 7 | |
import java.awt.Color; |
| 8 | |
import java.awt.Component; |
| 9 | |
import java.awt.Dimension; |
| 10 | |
import java.awt.FlowLayout; |
| 11 | |
import java.awt.GridLayout; |
| 12 | |
import java.awt.event.ActionEvent; |
| 13 | |
import java.awt.event.ActionListener; |
| 14 | |
import java.awt.event.FocusEvent; |
| 15 | |
import java.awt.event.FocusListener; |
| 16 | |
import java.awt.event.KeyAdapter; |
| 17 | |
import java.awt.event.KeyEvent; |
| 18 | |
import java.util.logging.Logger; |
| 19 | |
|
| 20 | |
import javax.swing.DefaultListCellRenderer; |
| 21 | |
import javax.swing.JButton; |
| 22 | |
import javax.swing.JComboBox; |
| 23 | |
import javax.swing.JLabel; |
| 24 | |
import javax.swing.JList; |
| 25 | |
import javax.swing.JOptionPane; |
| 26 | |
import javax.swing.JPanel; |
| 27 | |
import javax.swing.JScrollPane; |
| 28 | |
|
| 29 | |
import org.homeunix.thecave.buddi.Const; |
| 30 | |
import org.homeunix.thecave.buddi.i18n.BuddiKeys; |
| 31 | |
import org.homeunix.thecave.buddi.i18n.keys.ButtonKeys; |
| 32 | |
import org.homeunix.thecave.buddi.model.Account; |
| 33 | |
import org.homeunix.thecave.buddi.model.AccountType; |
| 34 | |
import org.homeunix.thecave.buddi.model.Document; |
| 35 | |
import org.homeunix.thecave.buddi.model.impl.ModelFactory; |
| 36 | |
import org.homeunix.thecave.buddi.model.prefs.PrefsModel; |
| 37 | |
import org.homeunix.thecave.buddi.plugin.api.exception.ModelException; |
| 38 | |
import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter; |
| 39 | |
import org.homeunix.thecave.buddi.util.InternalFormatter; |
| 40 | |
import org.homeunix.thecave.buddi.view.MainFrame; |
| 41 | |
|
| 42 | |
import ca.digitalcave.moss.common.OperatingSystemUtil; |
| 43 | |
import ca.digitalcave.moss.swing.MossDecimalField; |
| 44 | |
import ca.digitalcave.moss.swing.MossDialog; |
| 45 | |
import ca.digitalcave.moss.swing.MossHintTextArea; |
| 46 | |
import ca.digitalcave.moss.swing.MossHintTextField; |
| 47 | |
import ca.digitalcave.moss.swing.model.BackedComboBoxModel; |
| 48 | |
|
| 49 | |
public class AccountEditorDialog extends MossDialog implements ActionListener { |
| 50 | |
|
| 51 | |
public static final long serialVersionUID = 0; |
| 52 | |
|
| 53 | |
private final MossHintTextField name; |
| 54 | |
private final JComboBox type; |
| 55 | |
private final MossDecimalField startingBalance; |
| 56 | |
private final MossHintTextArea notes; |
| 57 | |
private final MossDecimalField overdraftCreditLimit; |
| 58 | |
private final MossDecimalField interestRate; |
| 59 | |
private final JLabel overdraftCreditLimitLabel; |
| 60 | |
private final JLabel interestRateLabel; |
| 61 | |
|
| 62 | |
private final JButton ok; |
| 63 | |
private final JButton cancel; |
| 64 | |
|
| 65 | |
private final Account selected; |
| 66 | |
|
| 67 | |
private final Document model; |
| 68 | |
|
| 69 | |
public AccountEditorDialog(MainFrame frame, Document model, Account selected) { |
| 70 | 0 | super(frame); |
| 71 | |
|
| 72 | 0 | this.selected = selected; |
| 73 | 0 | this.model = model; |
| 74 | |
|
| 75 | 0 | name = new MossHintTextField(PrefsModel.getInstance().getTranslator().get(BuddiKeys.HINT_NAME)); |
| 76 | 0 | type = new JComboBox(new BackedComboBoxModel<AccountType>(model.getAccountTypes())); |
| 77 | 0 | if (type.getModel().getSize() > 0) |
| 78 | 0 | type.setSelectedIndex(0); |
| 79 | 0 | startingBalance = new MossDecimalField(true); |
| 80 | 0 | overdraftCreditLimit = new MossDecimalField(false); |
| 81 | 0 | interestRate = new MossDecimalField(0, false, 3); |
| 82 | 0 | overdraftCreditLimitLabel = new JLabel(); |
| 83 | 0 | interestRateLabel = new JLabel(PrefsModel.getInstance().getTranslator().get(BuddiKeys.INTEREST_RATE)); |
| 84 | 0 | notes = new MossHintTextArea(PrefsModel.getInstance().getTranslator().get(BuddiKeys.HINT_NOTES)); |
| 85 | |
|
| 86 | 0 | ok = new JButton(PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_OK)); |
| 87 | 0 | cancel = new JButton(PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_CANCEL)); |
| 88 | 0 | } |
| 89 | |
|
| 90 | |
public void init() { |
| 91 | 0 | super.init(); |
| 92 | |
|
| 93 | 0 | JPanel textPanel = new JPanel(new BorderLayout()); |
| 94 | 0 | JPanel textPanelLeft = new JPanel(new GridLayout(0, 1)); |
| 95 | 0 | JPanel textPanelRight = new JPanel(new GridLayout(0, 1)); |
| 96 | 0 | textPanel.add(textPanelLeft, BorderLayout.WEST); |
| 97 | 0 | textPanel.add(textPanelRight, BorderLayout.EAST); |
| 98 | |
|
| 99 | 0 | textPanelLeft.add(new JLabel(PrefsModel.getInstance().getTranslator().get(BuddiKeys.ACCOUNT_EDITOR_NAME))); |
| 100 | 0 | textPanelLeft.add(new JLabel(PrefsModel.getInstance().getTranslator().get(BuddiKeys.ACCOUNT_EDITOR_TYPE))); |
| 101 | 0 | textPanelLeft.add(new JLabel(PrefsModel.getInstance().getTranslator().get(BuddiKeys.ACCOUNT_EDITOR_STARTING_BALANCE))); |
| 102 | 0 | textPanelRight.add(name); |
| 103 | 0 | textPanelRight.add(type); |
| 104 | 0 | textPanelRight.add(startingBalance); |
| 105 | |
|
| 106 | 0 | if (PrefsModel.getInstance().isShowOverdraft() || PrefsModel.getInstance().isShowCreditRemaining()){ |
| 107 | 0 | textPanelLeft.add(overdraftCreditLimitLabel); |
| 108 | 0 | textPanelRight.add(overdraftCreditLimit); |
| 109 | |
} |
| 110 | 0 | if (PrefsModel.getInstance().isShowInterestRates()){ |
| 111 | 0 | textPanelLeft.add(interestRateLabel); |
| 112 | 0 | textPanelRight.add(interestRate); |
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | 0 | JScrollPane notesScroller = new JScrollPane(notes); |
| 117 | 0 | notesScroller.setPreferredSize(new Dimension(150, 75)); |
| 118 | 0 | textPanel.add(notesScroller, BorderLayout.SOUTH); |
| 119 | |
|
| 120 | 0 | ok.setPreferredSize(InternalFormatter.getButtonSize(ok)); |
| 121 | 0 | cancel.setPreferredSize(InternalFormatter.getButtonSize(cancel)); |
| 122 | |
|
| 123 | 0 | ok.addActionListener(this); |
| 124 | 0 | cancel.addActionListener(this); |
| 125 | |
|
| 126 | 0 | type.addActionListener(this); |
| 127 | |
|
| 128 | 0 | type.setRenderer(new DefaultListCellRenderer(){ |
| 129 | |
private static final long serialVersionUID = 0; |
| 130 | |
|
| 131 | |
@Override |
| 132 | |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
| 133 | 0 | super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
| 134 | |
|
| 135 | 0 | if (value instanceof AccountType){ |
| 136 | 0 | AccountType at = (AccountType) value; |
| 137 | 0 | this.setText(PrefsModel.getInstance().getTranslator().get(at.getName())); |
| 138 | |
|
| 139 | 0 | if (isSelected) |
| 140 | 0 | this.setForeground(Color.WHITE); |
| 141 | |
else |
| 142 | 0 | this.setForeground((at.isCredit() ? Color.RED : Const.COLOR_JLIST_UNSELECTED_TEXT)); |
| 143 | |
|
| 144 | 0 | } |
| 145 | |
else |
| 146 | 0 | this.setText(" "); |
| 147 | |
|
| 148 | 0 | return this; |
| 149 | |
} |
| 150 | |
}); |
| 151 | |
|
| 152 | 0 | name.addKeyListener(new KeyAdapter(){ |
| 153 | |
@Override |
| 154 | |
public void keyReleased(KeyEvent e) { |
| 155 | 0 | super.keyReleased(e); |
| 156 | |
|
| 157 | 0 | updateButtons(); |
| 158 | 0 | } |
| 159 | |
}); |
| 160 | |
|
| 161 | 0 | FocusListener focusListener = new FocusListener(){ |
| 162 | 0 | public void focusGained(FocusEvent e) {} |
| 163 | |
public void focusLost(FocusEvent e) { |
| 164 | 0 | updateButtons(); |
| 165 | 0 | } |
| 166 | |
}; |
| 167 | |
|
| 168 | 0 | ok.addFocusListener(focusListener); |
| 169 | 0 | cancel.addFocusListener(focusListener); |
| 170 | 0 | name.addFocusListener(focusListener); |
| 171 | 0 | type.addFocusListener(focusListener); |
| 172 | 0 | startingBalance.addFocusListener(focusListener); |
| 173 | 0 | notes.addFocusListener(focusListener); |
| 174 | |
|
| 175 | 0 | JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
| 176 | 0 | if (OperatingSystemUtil.isMac()){ |
| 177 | 0 | buttonPanel.add(cancel); |
| 178 | 0 | buttonPanel.add(ok); |
| 179 | |
} |
| 180 | |
else { |
| 181 | 0 | buttonPanel.add(ok); |
| 182 | 0 | buttonPanel.add(cancel); |
| 183 | |
} |
| 184 | |
|
| 185 | 0 | this.getRootPane().setDefaultButton(ok); |
| 186 | 0 | this.setLayout(new BorderLayout()); |
| 187 | 0 | this.add(textPanel, BorderLayout.CENTER); |
| 188 | 0 | this.add(buttonPanel, BorderLayout.SOUTH); |
| 189 | 0 | } |
| 190 | |
|
| 191 | |
public void updateButtons() { |
| 192 | 0 | super.updateButtons(); |
| 193 | |
|
| 194 | 0 | ok.setEnabled(name.getText() != null && name.getText().length() > 0); |
| 195 | |
|
| 196 | 0 | interestRate.setVisible(PrefsModel.getInstance().isShowInterestRates()); |
| 197 | 0 | interestRateLabel.setVisible(PrefsModel.getInstance().isShowInterestRates()); |
| 198 | |
|
| 199 | 0 | if (((AccountType) type.getSelectedItem()).isCredit()){ |
| 200 | 0 | if (PrefsModel.getInstance().isShowCreditRemaining()){ |
| 201 | 0 | overdraftCreditLimitLabel.setText(TextFormatter.getTranslation(BuddiKeys.CREDIT_LIMIT)); |
| 202 | |
|
| 203 | 0 | overdraftCreditLimit.setVisible(true); |
| 204 | 0 | overdraftCreditLimitLabel.setVisible(true); |
| 205 | |
} |
| 206 | |
else { |
| 207 | 0 | overdraftCreditLimit.setVisible(false); |
| 208 | 0 | overdraftCreditLimitLabel.setVisible(false); |
| 209 | |
} |
| 210 | |
} |
| 211 | |
else { |
| 212 | 0 | if (PrefsModel.getInstance().isShowOverdraft()){ |
| 213 | 0 | overdraftCreditLimitLabel.setText(TextFormatter.getTranslation(BuddiKeys.OVERDRAFT_LIMIT)); |
| 214 | |
|
| 215 | 0 | overdraftCreditLimit.setVisible(true); |
| 216 | 0 | overdraftCreditLimitLabel.setVisible(true); |
| 217 | |
} |
| 218 | |
else { |
| 219 | 0 | overdraftCreditLimit.setVisible(false); |
| 220 | 0 | overdraftCreditLimitLabel.setVisible(false); |
| 221 | |
} |
| 222 | |
} |
| 223 | 0 | } |
| 224 | |
|
| 225 | |
public void updateContent() { |
| 226 | 0 | super.updateContent(); |
| 227 | |
|
| 228 | 0 | if (selected == null){ |
| 229 | 0 | name.setText(""); |
| 230 | |
|
| 231 | 0 | startingBalance.setValue(0l); |
| 232 | 0 | notes.setText(""); |
| 233 | 0 | overdraftCreditLimit.setValue(0l); |
| 234 | 0 | interestRate.setValue(0l); |
| 235 | |
} |
| 236 | |
else { |
| 237 | 0 | name.setText(PrefsModel.getInstance().getTranslator().get(selected.getName())); |
| 238 | 0 | type.setSelectedItem(selected.getAccountType()); |
| 239 | 0 | startingBalance.setValue(selected.getStartingBalance() * (selected.getAccountType().isCredit() ? -1 : 1)); |
| 240 | 0 | notes.setText(PrefsModel.getInstance().getTranslator().get(selected.getNotes())); |
| 241 | 0 | overdraftCreditLimit.setValue(selected.getOverdraftCreditLimit()); |
| 242 | 0 | interestRate.setValue(selected.getInterestRate()); |
| 243 | |
} |
| 244 | 0 | } |
| 245 | |
|
| 246 | |
public void actionPerformed(ActionEvent e) { |
| 247 | 0 | if (e.getSource().equals(ok)){ |
| 248 | |
|
| 249 | 0 | for (Account oldAccount : model.getAccounts()) { |
| 250 | 0 | Object[] options = new Object[2]; |
| 251 | 0 | options[0] = TextFormatter.getTranslation(ButtonKeys.BUTTON_OK); |
| 252 | 0 | options[1] = TextFormatter.getTranslation(ButtonKeys.BUTTON_CANCEL); |
| 253 | |
|
| 254 | 0 | if (oldAccount.getName().equals(name.getText()) |
| 255 | |
&& (selected == null |
| 256 | |
|| !selected.equals(oldAccount))){ |
| 257 | 0 | int reply = JOptionPane.showOptionDialog( |
| 258 | |
this, |
| 259 | |
TextFormatter.getTranslation(BuddiKeys.DUPLICATE_ACCOUNT_NAMES), |
| 260 | |
TextFormatter.getTranslation(BuddiKeys.WARNING), |
| 261 | |
JOptionPane.YES_NO_OPTION, |
| 262 | |
JOptionPane.WARNING_MESSAGE, |
| 263 | |
null, |
| 264 | |
options, |
| 265 | |
options[0]); |
| 266 | |
|
| 267 | |
|
| 268 | 0 | if (reply == JOptionPane.NO_OPTION) |
| 269 | 0 | return; |
| 270 | 0 | else if (reply ==JOptionPane.YES_OPTION) |
| 271 | 0 | break; |
| 272 | |
} |
| 273 | 0 | } |
| 274 | |
|
| 275 | |
Account a; |
| 276 | |
try { |
| 277 | 0 | if (selected == null){ |
| 278 | 0 | a = ModelFactory.createAccount(name.getText(), (AccountType) type.getSelectedItem()); |
| 279 | 0 | a.setStartingBalance(startingBalance.getValue() * (((AccountType) type.getSelectedItem()).isCredit() ? -1 : 1)); |
| 280 | 0 | a.setNotes(notes.getText()); |
| 281 | 0 | a.setOverdraftCreditLimit(overdraftCreditLimit.getValue()); |
| 282 | 0 | a.setInterestRate(interestRate.getValue()); |
| 283 | 0 | Logger.getLogger(this.getClass().getName()).finest("Created new Account " + a); |
| 284 | |
|
| 285 | 0 | model.addAccount(a); |
| 286 | |
} |
| 287 | |
else { |
| 288 | 0 | a = selected; |
| 289 | 0 | a.setName(name.getText()); |
| 290 | 0 | a.setAccountType((AccountType) type.getSelectedItem()); |
| 291 | 0 | a.setStartingBalance(startingBalance.getValue() * (a.getAccountType().isCredit() ? -1 : 1)); |
| 292 | 0 | a.setNotes(notes.getText()); |
| 293 | 0 | a.setOverdraftCreditLimit(overdraftCreditLimit.getValue()); |
| 294 | 0 | a.setInterestRate(interestRate.getValue()); |
| 295 | |
} |
| 296 | 0 | a.updateBalance(); |
| 297 | |
|
| 298 | 0 | closeWindow(); |
| 299 | |
} |
| 300 | 0 | catch (ModelException me){ |
| 301 | 0 | String[] options = new String[1]; |
| 302 | 0 | options[0] = TextFormatter.getTranslation(ButtonKeys.BUTTON_OK); |
| 303 | |
|
| 304 | 0 | JOptionPane.showOptionDialog(this, |
| 305 | |
TextFormatter.getTranslation(BuddiKeys.ACCOUNT_EDITOR_ERROR_UPDATING_ACCOUNT), |
| 306 | |
TextFormatter.getTranslation(BuddiKeys.ERROR), |
| 307 | |
JOptionPane.OK_CANCEL_OPTION, |
| 308 | |
JOptionPane.INFORMATION_MESSAGE, |
| 309 | |
null, |
| 310 | |
options, |
| 311 | |
options[0]); |
| 312 | 0 | } |
| 313 | 0 | } |
| 314 | 0 | else if (e.getSource().equals(cancel)){ |
| 315 | 0 | closeWindow(); |
| 316 | |
} |
| 317 | 0 | else if (e.getSource().equals(type)){ |
| 318 | 0 | updateButtons(); |
| 319 | |
} |
| 320 | 0 | } |
| 321 | |
} |