-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopPanel.java
163 lines (153 loc) · 6.75 KB
/
topPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import javax.swing.*;
//Used SingleTon because all four frames use the same topPanel
class TopPanel{
protected Font regFont = new Font("Serif", Font.BOLD, 30);
private Color backgroundPanelColor = new Color(0, 128, 255);
protected static ResultSet resultSetFacade;
protected JPanel returnPanel;
private DecimalFormat decimalFormat=new DecimalFormat("###,###.##");
protected GridBagConstraints constraintsToManipulate = new GridBagConstraints();
private String wealthChange,wealth;
private long daysLeft;
private Calendar startDate = Calendar.getInstance();
private Box wealthBox;
private static TopPanel singleInstance;
private static FrameFacade individualFac;
private JLabel wealthChangeLabel;
private TopPanel(){}
//Only returns one instance
public static TopPanel getInstance(FrameFacade individualFacade){
if(singleInstance==null){
individualFac=individualFacade;
singleInstance=new TopPanel();
}
return singleInstance;
}
public JPanel makePanel(){
if(returnPanel==null){
returnPanel=new JPanel();
makeTopPanel();
}
return returnPanel;
}
public void setWealth(double wealth){
((JLabel)wealthBox.getComponent(1)).setText("Wealth : "+decimalFormat.format(wealth));
}
public void setChange(double wealthChange){
wealthChangeLabel.setText("Wealth Change : "+decimalFormat.format(wealthChange));
}
// Calls the methods that manipulate the returnPanel and returns the
// returnPanel
private JPanel makeTopPanel() {
getConInfo();
createTopComponents();
return returnPanel;
}
// Gets the connection from the FactoryFacade
private void getConInfo() {
resultSetFacade = individualFac.getConnectionInfo("SELECT * From personel_data");
try {
if (resultSetFacade.next()) {
Calendar endCal=Calendar.getInstance();
endCal.setTime(resultSetFacade.getDate(6));
daysLeft=ChronoUnit.DAYS.between(startDate.toInstant(), endCal.toInstant());
wealth =decimalFormat.format(resultSetFacade.getDouble(3));
wealthChange =decimalFormat.format(resultSetFacade.getDouble(2));
}
} catch (SQLException e) {}
}
// Creates the components for the topPanel
private void createTopComponents() {
returnPanel.setLayout(new GridBagLayout());
returnPanel.setBackground(backgroundPanelColor);
wealthBox=createEditBoxes("Wealth : $" + wealth+"|");
addListenerToWealth((JButton)wealthBox.getComponent(0),(JLabel)wealthBox.getComponent(1));
wealthChangeLabel = FactoryFacade.makeLabels("Change : $" + wealthChange+"|", regFont);
//moneyRatioLabel = FactoryFacade.makeLabels("$" + moneySpent + " / $" + totalMoney+"|", regFont);
Box daysLeftBox=createEditBoxes(daysLeft + " Days Left");
addListenerToDaysLeft((JButton)daysLeftBox.getComponent(0),(JLabel)daysLeftBox.getComponent(1));
createTopPanel(wealthBox, wealthChangeLabel,daysLeftBox,constraintsToManipulate);
}
// Puts the topPanel together
private void createTopPanel(Box wealthBox, JLabel wealthChangeLabel,Box daysLeftBox, GridBagConstraints c) {
FactoryFacade.addWithGridBag(1, 1, 1, 1, 50, 50, GridBagConstraints.NONE, GridBagConstraints.CENTER, c,
returnPanel, wealthBox);
FactoryFacade.addWithGridBag(2, 1, 1, 1, 50, 50, GridBagConstraints.NONE, GridBagConstraints.CENTER, c,
returnPanel, wealthChangeLabel);
//FactoryFacade.addWithGridBag(3, 1, 1, 1, 50, 50, GridBagConstraints.NONE, GridBagConstraints.CENTER, c,
// returnPanel, moneyRatio);
FactoryFacade.addWithGridBag(4, 1, 1, 1, 50, 50, GridBagConstraints.NONE, GridBagConstraints.CENTER, c,
returnPanel, daysLeftBox);
}
//Creates the Boxes that contain the edit button and a JLabel created with the String parameter
private Box createEditBoxes(String labelName){
JButton button = FactoryFacade.makeIconButtons(
"C:/Users/Kyle Peters/Documents/picturesForFinance/editButton.png", backgroundPanelColor,20,30);
JLabel label = FactoryFacade.makeLabels(labelName,regFont);
Box returnBox=Box.createHorizontalBox();
returnBox.add(button);
returnBox.add(label);
return returnBox;
}
//Enables the person's wealth to be changed
private void addListenerToWealth(JButton wealthButton,JLabel wealthLabel){
wealthButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try{
String wealthString=JOptionPane.showInputDialog(null,"You don't have to fill in this field if you prefer not to","Set Wealth",JOptionPane.INFORMATION_MESSAGE);
wealthString=wealthString.replaceAll(",", "");
double wealthDouble=Double.parseDouble(wealthString);
updateDataBase(wealthDouble);
updateLabels(wealthDouble);
}catch(NumberFormatException | NullPointerException | SQLException exception){
}
}
private void updateDataBase(Double wealthDouble) throws SQLException{
resultSetFacade.absolute(1);
resultSetFacade.updateDouble(3,wealthDouble);
resultSetFacade.updateRow();
}
private void updateLabels(Double wealthDouble) throws SQLException{
String formatString=decimalFormat.format(wealthDouble);
wealthLabel.setText("Wealth : $" + formatString+" | ");
wealth=formatString;
}
});//End of ActionListener
}
//Enables the person to change the end of the Budget Month
private void addListenerToDaysLeft(JButton daysLeftButton,JLabel daysLeftLabel){
daysLeftButton.addActionListener(new ActionListener(){
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
try{
int hitYes=JOptionPane.showConfirmDialog(null,"Do you wish to start a new Month","Set Date",JOptionPane.INFORMATION_MESSAGE);
if(hitYes==0){
int month=(startDate.get(Calendar.MONTH)==12)?1:startDate.get(Calendar.MONTH)+1;
java.sql.Date endOfNewMonth=new java.sql.Date(startDate.get(Calendar.YEAR)-1900,month,startDate.get(Calendar.DAY_OF_MONTH));
updateDataBase(endOfNewMonth);
updateLabels();
}
}catch(NullPointerException |SQLException exception){}
}
private void updateDataBase(java.sql.Date endOfNewMonth) throws SQLException{
resultSetFacade.absolute(1);
resultSetFacade.updateDate(6, endOfNewMonth);
resultSetFacade.updateRow();
}
private void updateLabels() throws SQLException{
Calendar endCal=Calendar.getInstance();
endCal.setTime(resultSetFacade.getDate(6));
daysLeft=ChronoUnit.DAYS.between(startDate.toInstant(), endCal.toInstant());
daysLeftLabel.setText(daysLeft + " Days Left");
}
});//End of Buttonistener
}
}// End of BudgetTop