1 | |
|
2 | |
|
3 | |
|
4 | |
package org.homeunix.thecave.buddi.view.dialogs; |
5 | |
|
6 | |
import java.awt.BorderLayout; |
7 | |
import java.awt.Dimension; |
8 | |
import java.awt.FlowLayout; |
9 | |
import java.awt.event.ActionEvent; |
10 | |
import java.awt.event.ActionListener; |
11 | |
import java.text.ParseException; |
12 | |
import java.text.SimpleDateFormat; |
13 | |
import java.util.Date; |
14 | |
import java.util.logging.Logger; |
15 | |
|
16 | |
import javax.swing.BorderFactory; |
17 | |
import javax.swing.BoxLayout; |
18 | |
import javax.swing.JButton; |
19 | |
import javax.swing.JFormattedTextField; |
20 | |
import javax.swing.JLabel; |
21 | |
import javax.swing.JOptionPane; |
22 | |
import javax.swing.JPanel; |
23 | |
|
24 | |
import org.homeunix.thecave.buddi.Const; |
25 | |
import org.homeunix.thecave.buddi.i18n.BuddiKeys; |
26 | |
import org.homeunix.thecave.buddi.i18n.keys.BudgetCategoryTypes; |
27 | |
import org.homeunix.thecave.buddi.i18n.keys.ButtonKeys; |
28 | |
import org.homeunix.thecave.buddi.i18n.keys.PluginReportDateRangeChoices; |
29 | |
import org.homeunix.thecave.buddi.model.BudgetCategoryType; |
30 | |
import org.homeunix.thecave.buddi.model.impl.ModelFactory; |
31 | |
import org.homeunix.thecave.buddi.model.prefs.PrefsModel; |
32 | |
import org.homeunix.thecave.buddi.plugin.BuddiPluginHelper; |
33 | |
import org.homeunix.thecave.buddi.plugin.api.BuddiReportPlugin; |
34 | |
import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter; |
35 | |
import org.homeunix.thecave.buddi.view.MainFrame; |
36 | |
import org.jdesktop.swingx.JXDatePicker; |
37 | |
|
38 | |
import ca.digitalcave.moss.common.DateUtil; |
39 | |
import ca.digitalcave.moss.swing.MossDialog; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public class CustomDateDialog extends MossDialog implements ActionListener { |
50 | |
public static final long serialVersionUID = 0; |
51 | |
|
52 | |
protected final JButton okButton; |
53 | |
protected final JButton cancelButton; |
54 | |
|
55 | |
protected final JLabel mainLabel; |
56 | |
protected final JLabel middleLabel; |
57 | |
|
58 | |
protected final JXDatePicker startDateChooser; |
59 | |
protected final JXDatePicker endDateChooser; |
60 | |
|
61 | |
protected final BuddiReportPlugin report; |
62 | |
private final MainFrame reportFrame; |
63 | |
|
64 | 0 | private static Date startDate = null; |
65 | 0 | private static Date endDate = new Date(); |
66 | |
|
67 | |
public CustomDateDialog(MainFrame reportFrame, BuddiReportPlugin plugin){ |
68 | 0 | super(reportFrame); |
69 | |
|
70 | 0 | this.reportFrame = reportFrame; |
71 | 0 | this.report = plugin; |
72 | |
|
73 | 0 | okButton = new JButton(TextFormatter.getTranslation(ButtonKeys.BUTTON_OK)); |
74 | 0 | cancelButton = new JButton(TextFormatter.getTranslation(ButtonKeys.BUTTON_CANCEL)); |
75 | |
|
76 | 0 | Dimension buttonSize = new Dimension(100, okButton.getPreferredSize().height); |
77 | 0 | okButton.setPreferredSize(buttonSize); |
78 | 0 | cancelButton.setPreferredSize(buttonSize); |
79 | |
|
80 | 0 | startDateChooser = new JXDatePicker(); |
81 | 0 | endDateChooser = new JXDatePicker(); |
82 | |
|
83 | 0 | startDateChooser.setEditor(new JFormattedTextField(new SimpleDateFormat(PrefsModel.getInstance().getDateFormat()))); |
84 | 0 | endDateChooser.setEditor(new JFormattedTextField(new SimpleDateFormat(PrefsModel.getInstance().getDateFormat()))); |
85 | |
|
86 | 0 | Dimension textFieldSize = new Dimension(180, startDateChooser.getPreferredSize().height); |
87 | |
|
88 | 0 | startDateChooser.setPreferredSize(textFieldSize); |
89 | 0 | endDateChooser.setPreferredSize(textFieldSize); |
90 | |
|
91 | 0 | JPanel r1 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
92 | 0 | JPanel r2 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
93 | |
|
94 | 0 | mainLabel = new JLabel(); |
95 | 0 | middleLabel = new JLabel(); |
96 | |
|
97 | 0 | r1.add(mainLabel); |
98 | |
|
99 | 0 | r2.add(startDateChooser); |
100 | 0 | r2.add(middleLabel); |
101 | 0 | r2.add(endDateChooser); |
102 | |
|
103 | 0 | JPanel textPanelSpacer = new JPanel(); |
104 | 0 | textPanelSpacer.setLayout(new BoxLayout(textPanelSpacer, BoxLayout.Y_AXIS)); |
105 | 0 | textPanelSpacer.setBorder(BorderFactory.createEmptyBorder(7, 17, 17, 17)); |
106 | 0 | textPanelSpacer.add(r1); |
107 | 0 | textPanelSpacer.add(r2); |
108 | |
|
109 | 0 | JPanel mainBorderPanel = new JPanel(); |
110 | 0 | mainBorderPanel.setLayout(new BorderLayout()); |
111 | 0 | mainBorderPanel.setBorder(BorderFactory.createTitledBorder("")); |
112 | 0 | mainBorderPanel.add(textPanelSpacer); |
113 | |
|
114 | 0 | JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
115 | 0 | buttonPanel.add(cancelButton); |
116 | 0 | buttonPanel.add(okButton); |
117 | |
|
118 | 0 | JPanel mainPanel = new JPanel(); |
119 | 0 | mainPanel.setLayout(new BorderLayout()); |
120 | 0 | mainPanel.setBorder(BorderFactory.createEmptyBorder(7, 12, 12, 12)); |
121 | |
|
122 | 0 | mainPanel.add(mainBorderPanel, BorderLayout.CENTER); |
123 | 0 | mainPanel.add(buttonPanel, BorderLayout.SOUTH); |
124 | |
|
125 | 0 | this.setLayout(new BorderLayout()); |
126 | 0 | this.add(mainPanel); |
127 | 0 | this.getRootPane().setDefaultButton(okButton); |
128 | 0 | this.setTitle(TextFormatter.getTranslation(BuddiKeys.CHOOSE_DATE_INTERVAL)); |
129 | |
|
130 | 0 | if (startDate == null){ |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | 0 | BudgetCategoryType period = ModelFactory.getBudgetCategoryType(BudgetCategoryTypes.BUDGET_CATEGORY_TYPE_MONTH); |
141 | 0 | startDate = period.getStartOfBudgetPeriod(new Date()); |
142 | |
} |
143 | |
|
144 | 0 | startDateChooser.setDate(startDate); |
145 | 0 | endDateChooser.setDate(endDate); |
146 | |
|
147 | 0 | setVisibility(plugin.getDateRangeChoice()); |
148 | 0 | } |
149 | |
|
150 | |
private void setVisibility(PluginReportDateRangeChoices rangeChoiceType){ |
151 | 0 | if (rangeChoiceType.equals(PluginReportDateRangeChoices.INTERVAL)){ |
152 | 0 | mainLabel.setText(TextFormatter.getTranslation(BuddiKeys.REPORT_BETWEEN)); |
153 | |
|
154 | 0 | middleLabel.setText(" - "); |
155 | |
} |
156 | |
else{ |
157 | 0 | mainLabel.setText(TextFormatter.getTranslation(BuddiKeys.REPORT_AS_OF_DATE)); |
158 | 0 | middleLabel.setVisible(false); |
159 | |
|
160 | 0 | if (rangeChoiceType.equals(PluginReportDateRangeChoices.START_ONLY)){ |
161 | 0 | endDateChooser.setVisible(false); |
162 | |
} |
163 | 0 | else if (rangeChoiceType.equals(PluginReportDateRangeChoices.END_ONLY)){ |
164 | 0 | startDateChooser.setVisible(false); |
165 | |
} |
166 | |
} |
167 | 0 | } |
168 | |
|
169 | |
|
170 | |
public void init() { |
171 | 0 | okButton.addActionListener(this); |
172 | 0 | cancelButton.addActionListener(this); |
173 | 0 | } |
174 | |
|
175 | |
public void actionPerformed(ActionEvent e) { |
176 | |
|
177 | 0 | if (e.getSource().equals(okButton)){ |
178 | |
try { |
179 | 0 | startDateChooser.commitEdit(); |
180 | 0 | endDateChooser.commitEdit(); |
181 | 0 | } catch (ParseException e1) { |
182 | 0 | String[] options = new String[1]; |
183 | 0 | options[0] = TextFormatter.getTranslation(ButtonKeys.BUTTON_OK); |
184 | |
|
185 | 0 | JOptionPane.showOptionDialog( |
186 | |
null, |
187 | |
TextFormatter.getTranslation(BuddiKeys.CANNOT_PARSE_DATE), |
188 | |
TextFormatter.getTranslation(BuddiKeys.REPORT_DATE_ERROR), |
189 | |
JOptionPane.DEFAULT_OPTION, |
190 | |
JOptionPane.ERROR_MESSAGE, |
191 | |
null, |
192 | |
options, |
193 | |
options[0] |
194 | |
); |
195 | 0 | return; |
196 | 0 | } |
197 | |
|
198 | 0 | System.out.println(startDateChooser.getDate() + ", " + endDateChooser.getDate()); |
199 | |
|
200 | 0 | startDate = DateUtil.getStartOfDay(startDateChooser.getDate()); |
201 | 0 | endDate = DateUtil.getEndOfDay(endDateChooser.getDate()); |
202 | |
|
203 | 0 | System.out.println(startDate + ", " + endDate); |
204 | |
|
205 | 0 | if (report.getDateRangeChoice().equals(PluginReportDateRangeChoices.INTERVAL) |
206 | |
&& endDate.before(startDate)){ |
207 | 0 | String[] options = new String[1]; |
208 | 0 | options[0] = TextFormatter.getTranslation(ButtonKeys.BUTTON_OK); |
209 | |
|
210 | 0 | JOptionPane.showOptionDialog( |
211 | |
null, |
212 | |
TextFormatter.getTranslation(BuddiKeys.START_DATE_AFTER_END_DATE), |
213 | |
TextFormatter.getTranslation(BuddiKeys.REPORT_DATE_ERROR), |
214 | |
JOptionPane.DEFAULT_OPTION, |
215 | |
JOptionPane.ERROR_MESSAGE, |
216 | |
null, |
217 | |
options, |
218 | |
options[0] |
219 | |
); |
220 | 0 | return; |
221 | |
} |
222 | |
|
223 | 0 | if (Const.DEVEL) Logger.getLogger(this.getClass().getName()).finest("Getting transactions between " + startDate + " and " + endDate); |
224 | |
|
225 | 0 | CustomDateDialog.this.setVisible(false); |
226 | |
|
227 | 0 | BuddiPluginHelper.openReport(reportFrame, report, startDate, endDate); |
228 | |
} |
229 | 0 | else if (e.getSource().equals(cancelButton)){ |
230 | 0 | this.closeWindow(); |
231 | |
} |
232 | 0 | } |
233 | |
|
234 | |
} |