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.Arrays; |
19 | |
import java.util.logging.Logger; |
20 | |
|
21 | |
import javax.swing.ButtonGroup; |
22 | |
import javax.swing.DefaultListCellRenderer; |
23 | |
import javax.swing.JButton; |
24 | |
import javax.swing.JComboBox; |
25 | |
import javax.swing.JLabel; |
26 | |
import javax.swing.JList; |
27 | |
import javax.swing.JOptionPane; |
28 | |
import javax.swing.JPanel; |
29 | |
import javax.swing.JRadioButton; |
30 | |
import javax.swing.JScrollPane; |
31 | |
|
32 | |
import org.homeunix.thecave.buddi.Const; |
33 | |
import org.homeunix.thecave.buddi.i18n.BuddiKeys; |
34 | |
import org.homeunix.thecave.buddi.i18n.keys.BudgetCategoryTypes; |
35 | |
import org.homeunix.thecave.buddi.i18n.keys.ButtonKeys; |
36 | |
import org.homeunix.thecave.buddi.model.BudgetCategory; |
37 | |
import org.homeunix.thecave.buddi.model.Document; |
38 | |
import org.homeunix.thecave.buddi.model.impl.ModelFactory; |
39 | |
import org.homeunix.thecave.buddi.model.prefs.PrefsModel; |
40 | |
import org.homeunix.thecave.buddi.plugin.api.exception.ModelException; |
41 | |
import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter; |
42 | |
import org.homeunix.thecave.buddi.util.InternalFormatter; |
43 | |
import org.homeunix.thecave.buddi.view.MainFrame; |
44 | |
import org.homeunix.thecave.buddi.view.swing.TranslatorListCellRenderer; |
45 | |
|
46 | |
import ca.digitalcave.moss.collections.CompositeList; |
47 | |
import ca.digitalcave.moss.common.OperatingSystemUtil; |
48 | |
import ca.digitalcave.moss.swing.MossDialog; |
49 | |
import ca.digitalcave.moss.swing.MossHintTextArea; |
50 | |
import ca.digitalcave.moss.swing.MossHintTextField; |
51 | |
import ca.digitalcave.moss.swing.MossScrollingComboBox; |
52 | |
import ca.digitalcave.moss.swing.model.BackedComboBoxModel; |
53 | |
|
54 | |
public class BudgetCategoryEditorDialog extends MossDialog implements ActionListener { |
55 | |
|
56 | |
public static final long serialVersionUID = 0; |
57 | |
|
58 | |
private final MossHintTextField name; |
59 | |
private final JComboBox parent; |
60 | |
private final JComboBox budgetCategoryType; |
61 | |
private final JRadioButton income; |
62 | |
private final JRadioButton expense; |
63 | |
private final MossHintTextArea notes; |
64 | |
|
65 | |
private final JButton ok; |
66 | |
private final JButton cancel; |
67 | |
|
68 | |
private final BudgetCategory selected; |
69 | |
private final BudgetCategory newParent; |
70 | |
|
71 | |
private final Document model; |
72 | |
|
73 | |
@SuppressWarnings("unchecked") |
74 | |
public BudgetCategoryEditorDialog(MainFrame frame, Document model, BudgetCategory selected, BudgetCategory newParent) { |
75 | 0 | super(frame); |
76 | |
|
77 | 0 | this.selected = selected; |
78 | 0 | this.newParent = newParent; |
79 | 0 | this.model = model; |
80 | |
|
81 | 0 | name = new MossHintTextField(PrefsModel.getInstance().getTranslator().get(BuddiKeys.HINT_NAME)); |
82 | 0 | parent = new MossScrollingComboBox(new BackedComboBoxModel<BudgetCategory>(new CompositeList<BudgetCategory>(true, true, Arrays.asList(new BudgetCategory[]{null}), model.getBudgetCategories()))); |
83 | 0 | if (parent.getModel().getSize() > 0) |
84 | 0 | parent.setSelectedIndex(0); |
85 | 0 | budgetCategoryType = new JComboBox(BudgetCategoryTypes.values()); |
86 | 0 | income = new JRadioButton(PrefsModel.getInstance().getTranslator().get(BuddiKeys.BUDGET_EDITOR_INCOME)); |
87 | 0 | expense = new JRadioButton(PrefsModel.getInstance().getTranslator().get(BuddiKeys.BUDGET_EDITOR_EXPENSE)); |
88 | 0 | notes = new MossHintTextArea(PrefsModel.getInstance().getTranslator().get(BuddiKeys.HINT_NOTES)); |
89 | |
|
90 | 0 | ok = new JButton(PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_OK)); |
91 | 0 | cancel = new JButton(PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_CANCEL)); |
92 | 0 | } |
93 | |
|
94 | |
public void init() { |
95 | 0 | super.init(); |
96 | 0 | JPanel textPanel = new JPanel(new BorderLayout()); |
97 | 0 | JPanel textPanelLeft = new JPanel(new GridLayout(0, 1)); |
98 | 0 | JPanel textPanelRight = new JPanel(new GridLayout(0, 1)); |
99 | 0 | textPanel.add(textPanelLeft, BorderLayout.WEST); |
100 | 0 | textPanel.add(textPanelRight, BorderLayout.EAST); |
101 | |
|
102 | 0 | textPanelLeft.add(new JLabel(PrefsModel.getInstance().getTranslator().get(BuddiKeys.BUDGET_EDITOR_NAME))); |
103 | 0 | textPanelLeft.add(new JLabel(PrefsModel.getInstance().getTranslator().get(BuddiKeys.BUDGET_EDITOR_PARENT))); |
104 | 0 | textPanelLeft.add(new JLabel(PrefsModel.getInstance().getTranslator().get(BuddiKeys.BUDGET_EDITOR_BUDGET_PERIOD_TYPE))); |
105 | 0 | textPanelLeft.add(new JLabel(PrefsModel.getInstance().getTranslator().get(BuddiKeys.BUDGET_EDITOR_TYPE))); |
106 | 0 | textPanelLeft.add(new JLabel("")); |
107 | |
|
108 | |
|
109 | 0 | textPanelRight.add(name); |
110 | 0 | textPanelRight.add(parent); |
111 | 0 | textPanelRight.add(budgetCategoryType); |
112 | 0 | textPanelRight.add(income); |
113 | 0 | textPanelRight.add(expense); |
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 | |
|
121 | 0 | ButtonGroup group = new ButtonGroup(); |
122 | 0 | group.add(income); |
123 | 0 | group.add(expense); |
124 | |
|
125 | 0 | ok.setPreferredSize(InternalFormatter.getButtonSize(ok)); |
126 | 0 | cancel.setPreferredSize(InternalFormatter.getButtonSize(cancel)); |
127 | |
|
128 | 0 | ok.addActionListener(this); |
129 | 0 | cancel.addActionListener(this); |
130 | 0 | parent.addActionListener(this); |
131 | |
|
132 | 0 | budgetCategoryType.setRenderer(new TranslatorListCellRenderer()); |
133 | |
|
134 | 0 | name.addKeyListener(new KeyAdapter(){ |
135 | |
@Override |
136 | |
public void keyReleased(KeyEvent e) { |
137 | 0 | super.keyReleased(e); |
138 | |
|
139 | 0 | updateButtons(); |
140 | 0 | } |
141 | |
}); |
142 | |
|
143 | 0 | FocusListener focusListener = new FocusListener(){ |
144 | 0 | public void focusGained(FocusEvent e) {} |
145 | |
public void focusLost(FocusEvent e) { |
146 | 0 | updateButtons(); |
147 | 0 | } |
148 | |
}; |
149 | |
|
150 | 0 | ok.addFocusListener(focusListener); |
151 | 0 | cancel.addFocusListener(focusListener); |
152 | 0 | name.addFocusListener(focusListener); |
153 | 0 | parent.addFocusListener(focusListener); |
154 | 0 | budgetCategoryType.addFocusListener(focusListener); |
155 | 0 | income.addFocusListener(focusListener); |
156 | 0 | expense.addFocusListener(focusListener); |
157 | 0 | notes.addFocusListener(focusListener); |
158 | |
|
159 | |
|
160 | 0 | parent.setRenderer(new DefaultListCellRenderer(){ |
161 | |
private static final long serialVersionUID = 0; |
162 | |
|
163 | |
@Override |
164 | |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
165 | 0 | super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
166 | |
|
167 | 0 | if (value == null) |
168 | 0 | this.setText(PrefsModel.getInstance().getTranslator().get(BuddiKeys.NO_PARENT)); |
169 | 0 | else if (value instanceof BudgetCategory){ |
170 | 0 | BudgetCategory bc = (BudgetCategory) value; |
171 | 0 | this.setText(PrefsModel.getInstance().getTranslator().get(bc.getFullName())); |
172 | |
|
173 | 0 | if (isSelected) |
174 | 0 | this.setForeground(Color.WHITE); |
175 | |
else |
176 | 0 | this.setForeground((bc.isIncome() ? Const.COLOR_JLIST_UNSELECTED_TEXT : Color.RED)); |
177 | 0 | } |
178 | |
else |
179 | 0 | this.setText(" "); |
180 | |
|
181 | 0 | return this; |
182 | |
} |
183 | |
}); |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | 0 | JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
196 | 0 | if (OperatingSystemUtil.isMac()){ |
197 | 0 | buttonPanel.add(cancel); |
198 | 0 | buttonPanel.add(ok); |
199 | |
} |
200 | |
else { |
201 | 0 | buttonPanel.add(ok); |
202 | 0 | buttonPanel.add(cancel); |
203 | |
} |
204 | |
|
205 | 0 | this.getRootPane().setDefaultButton(ok); |
206 | 0 | this.setLayout(new BorderLayout()); |
207 | 0 | this.add(textPanel, BorderLayout.CENTER); |
208 | 0 | this.add(buttonPanel, BorderLayout.SOUTH); |
209 | 0 | } |
210 | |
|
211 | |
public void updateButtons() { |
212 | 0 | super.updateButtons(); |
213 | |
|
214 | 0 | ok.setEnabled(name.getText() != null && name.getText().length() > 0); |
215 | |
|
216 | 0 | budgetCategoryType.setEnabled(parent.getSelectedItem() == null); |
217 | 0 | income.setEnabled(parent.getSelectedItem() == null); |
218 | 0 | expense.setEnabled(parent.getSelectedItem() == null); |
219 | |
|
220 | 0 | if (parent.getSelectedItem() != null){ |
221 | 0 | BudgetCategory bc = (BudgetCategory) parent.getSelectedItem(); |
222 | 0 | budgetCategoryType.setSelectedItem(bc.getBudgetPeriodType()); |
223 | 0 | income.setSelected(bc.isIncome()); |
224 | 0 | expense.setSelected(!bc.isIncome()); |
225 | |
} |
226 | 0 | } |
227 | |
|
228 | |
public void updateContent() { |
229 | 0 | super.updateContent(); |
230 | |
|
231 | 0 | if (selected == null){ |
232 | 0 | name.setText(""); |
233 | 0 | expense.setSelected(true); |
234 | 0 | parent.setSelectedItem(newParent); |
235 | 0 | budgetCategoryType.setSelectedItem(ModelFactory.getBudgetCategoryType(BudgetCategoryTypes.BUDGET_CATEGORY_TYPE_MONTH)); |
236 | 0 | notes.setText(""); |
237 | |
} |
238 | |
else { |
239 | 0 | name.setText(PrefsModel.getInstance().getTranslator().get(selected.getName())); |
240 | 0 | if (selected.isIncome()) |
241 | 0 | income.setSelected(true); |
242 | |
else |
243 | 0 | expense.setSelected(true); |
244 | 0 | budgetCategoryType.setSelectedItem(BudgetCategoryTypes.valueOf(selected.getBudgetPeriodType().getName())); |
245 | 0 | parent.setSelectedItem(selected.getParent()); |
246 | 0 | notes.setText(PrefsModel.getInstance().getTranslator().get(selected.getNotes())); |
247 | |
} |
248 | 0 | } |
249 | |
|
250 | |
public void actionPerformed(ActionEvent e) { |
251 | 0 | if (e.getSource().equals(ok)){ |
252 | |
|
253 | 0 | for (BudgetCategory oldBudgetCategory : model.getBudgetCategories()) { |
254 | 0 | Object[] options = new Object[2]; |
255 | 0 | options[0] = TextFormatter.getTranslation(ButtonKeys.BUTTON_OK); |
256 | 0 | options[1] = TextFormatter.getTranslation(ButtonKeys.BUTTON_CANCEL); |
257 | |
|
258 | 0 | if (oldBudgetCategory.getName().equals(name.getText()) |
259 | |
&& (selected == null |
260 | |
|| !selected.equals(oldBudgetCategory))){ |
261 | 0 | int reply = JOptionPane.showOptionDialog( |
262 | |
this, |
263 | |
TextFormatter.getTranslation(BuddiKeys.DUPLICATE_BUDGET_CATEGORY_NAMES), |
264 | |
TextFormatter.getTranslation(BuddiKeys.WARNING), |
265 | |
JOptionPane.YES_NO_OPTION, |
266 | |
JOptionPane.WARNING_MESSAGE, |
267 | |
null, |
268 | |
options, |
269 | |
options[0]); |
270 | |
|
271 | |
|
272 | 0 | if (reply == JOptionPane.NO_OPTION) |
273 | 0 | return; |
274 | 0 | else if (reply ==JOptionPane.YES_OPTION) |
275 | 0 | break; |
276 | |
} |
277 | 0 | } |
278 | |
|
279 | |
BudgetCategory bc; |
280 | |
try { |
281 | 0 | if (selected == null){ |
282 | 0 | bc = ModelFactory.createBudgetCategory(name.getText(), ModelFactory.getBudgetCategoryType(budgetCategoryType.getSelectedItem().toString()), income.isSelected()); |
283 | 0 | bc.setParent((BudgetCategory) parent.getSelectedItem()); |
284 | 0 | bc.setNotes(notes.getText()); |
285 | 0 | model.addBudgetCategory(bc); |
286 | |
|
287 | 0 | Logger.getLogger(this.getClass().getName()).finest("Created new BudgetCategory " + bc); |
288 | |
} |
289 | |
else { |
290 | 0 | bc = selected; |
291 | 0 | bc.setName(name.getText()); |
292 | 0 | bc.setParent((BudgetCategory) parent.getSelectedItem()); |
293 | 0 | bc.setPeriodType(ModelFactory.getBudgetCategoryType(budgetCategoryType.getSelectedItem().toString())); |
294 | 0 | bc.setIncome(income.isSelected()); |
295 | 0 | bc.setNotes(notes.getText()); |
296 | |
|
297 | 0 | Logger.getLogger(this.getClass().getName()).finest("Updated BudgetCategory " + bc); |
298 | |
} |
299 | |
|
300 | 0 | closeWindow(); |
301 | |
} |
302 | 0 | catch (ModelException me){ |
303 | 0 | String[] options = new String[1]; |
304 | 0 | options[0] = TextFormatter.getTranslation(ButtonKeys.BUTTON_OK); |
305 | |
|
306 | 0 | JOptionPane.showOptionDialog(this, |
307 | |
TextFormatter.getTranslation(BuddiKeys.BUDGET_EDITOR_ERROR_UPDATING_CATEGORY), |
308 | |
TextFormatter.getTranslation(BuddiKeys.ERROR), |
309 | |
JOptionPane.OK_CANCEL_OPTION, |
310 | |
JOptionPane.INFORMATION_MESSAGE, |
311 | |
null, |
312 | |
options, |
313 | |
options[0]); |
314 | 0 | } |
315 | |
|
316 | 0 | } |
317 | 0 | else if (e.getSource().equals(cancel)){ |
318 | 0 | closeWindow(); |
319 | |
} |
320 | 0 | else if (e.getSource().equals(parent)){ |
321 | 0 | updateButtons(); |
322 | |
} |
323 | 0 | } |
324 | |
} |