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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
| import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.StringTokenizer;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class Pit
extends JFrame
implements ActionListener
{
private String[] opisZrodel = { "Źródło 1", "Źródło 2", "Źródło 3", "Suma ze wszystkich źródeł" };
private final int N = this.opisZrodel.length;
private final int PRZYCH = 0;
private final int KUPROC = 1;
private final int KU = 2;
private final int DOCH = 3;
private final int ZAL = 4;
private String[] opisKol = { "Przychód", "Koszty uzyskania w %", "Koszty uzyskania przychodu", "Dochód", "Zaliczki" };
private String[] sKoszt = { " 0", "20", "50" };
private JTextField[][] tf = new JTextField[this.N][this.opisKol.length];
private JLabel lWynik = createResultLabel(" ");
private JLabel statusLab = new JLabel(" ");
JButton bCalc = new JButton("Oblicz");
JCheckBox autoFill = new JCheckBox("Wiersza");
JCheckBox autoCalc = new JCheckBox("Podatku");
private JPopupMenu popupMenu = createPopupMenu();
private Border blueLineBorder = BorderFactory.createLineBorder(Color.blue);
private Border raisedBorder = BorderFactory.createRaisedBevelBorder();
private Border etchedRed = BorderFactory.createEtchedBorder(Color.orange, Color.red);
private Border etchedBlue = BorderFactory.createEtchedBorder(Color.cyan, Color.blue);
private Border defBorder;
private Color defColor;
public Pit()
{
Container localContainer = getContentPane();
JPanel localJPanel1 = new JPanel();
JLabel[] arrayOfJLabel = makeHeaders();
for (int i = 0; i < this.opisKol.length; i++) {
localJPanel1.add(createColumnPanel(i, arrayOfJLabel[i]));
}
localJPanel1.setBorder(this.raisedBorder);
localContainer.add(localJPanel1, "Center");
this.defBorder = this.tf[0][0].getBorder();
this.defColor = this.tf[0][0].getBackground();
JPanel localJPanel2 = new JPanel();
localJPanel2.setLayout(new BoxLayout(localJPanel2, 1));
JPanel localJPanel3 = new JPanel();
localJPanel3.setBorder(BorderFactory.createLoweredBevelBorder());
this.bCalc.addActionListener(this);
localJPanel3.add(this.bCalc);
localJPanel3.add(this.lWynik);
JPanel localJPanel4 = new JPanel();
localJPanel4.setBorder(BorderFactory.createTitledBorder("Automatyczne przeliczenia"));
localJPanel4.add(this.autoFill);
localJPanel4.add(this.autoCalc);
localJPanel3.add(localJPanel4);
localJPanel2.add(localJPanel3);
JPanel localJPanel5 = new JPanel(new FlowLayout(0));
localJPanel5.setBorder(this.raisedBorder);
localJPanel5.add(this.statusLab);
localJPanel2.add(localJPanel5);
localContainer.add(localJPanel2, "South");
setDefaultCloseOperation(3);
pack();
show();
}
private JLabel[] makeHeaders()
{
JLabel[] arrayOfJLabel = new JLabel[this.opisKol.length];
int i = 0;
for (int j = 0; j < this.opisKol.length; j++)
{
StringTokenizer localStringTokenizer = new StringTokenizer(this.opisKol[j]);
String str = "<html><center><b>";
while (localStringTokenizer.hasMoreTokens()) {
str = str + localStringTokenizer.nextToken() + "<br>";
}
str = str + "</b></center></html>";
arrayOfJLabel[j] = new JLabel(str);
arrayOfJLabel[j].setHorizontalAlignment(0);
arrayOfJLabel[j].setVerticalAlignment(0);
int k = arrayOfJLabel[j].getPreferredSize().height;
if (k > i) {
i = k;
}
}
for (int j = 0; j < this.opisKol.length; j++) {
arrayOfJLabel[j].setPreferredSize(new Dimension(arrayOfJLabel[j].getPreferredSize().width, i));
}
return arrayOfJLabel;
}
private JPanel createColumnPanel(int paramInt, JLabel paramJLabel)
{
JPanel localJPanel = new JPanel();
localJPanel.setLayout(new BoxLayout(localJPanel, 1));
localJPanel.setBorder(this.blueLineBorder);
localJPanel.add(paramJLabel);
for (int i = 0; i < this.N; i++)
{
int j = paramInt == 1 ? 5 : 10;
JTextField localJTextField = this.tf[i][paramInt] = new JTextField(j);
localJTextField.putClientProperty("Position", new int[] { i, paramInt });
localJTextField.putClientProperty("Description", this.opisKol[paramInt] + " ze źródła: " + this.opisZrodel[i]);
localJTextField.setHorizontalAlignment(4);
localJTextField.setInputVerifier(new InputVerifier()
{
public boolean verify(JComponent paramAnonymousJComponent)
{
return Pit.this.validateData(paramAnonymousJComponent);
}
});
localJTextField.addFocusListener(new FocusAdapter()
{
String oldValue = "";
public void focusLost(FocusEvent paramAnonymousFocusEvent)
{
JTextField localJTextField = (JTextField)paramAnonymousFocusEvent.getSource();
String str = localJTextField.getText();
if (localJTextField.getBackground().equals(Pit.this.defColor))
{
if (!str.equals(this.oldValue)) {
if (Pit.this.autoCalc.isSelected())
{
Pit.this.doCalc();
}
else if (Pit.this.autoFill.isSelected())
{
int[] arrayOfInt = (int[])localJTextField.getClientProperty("Position");
if (arrayOfInt[1] != 4) {
Pit.this.recalc(arrayOfInt[0], 3);
}
}
}
}
else if (localJTextField.getBackground() == Color.yellow) {
localJTextField.setBackground(Pit.this.defColor);
}
localJTextField.setBorder(Pit.this.defBorder);
Pit.this.statusLab.setText(" ");
this.oldValue = str;
}
public void focusGained(FocusEvent paramAnonymousFocusEvent)
{
Pit.this.bCalc.setEnabled(true);
JTextField localJTextField = (JTextField)paramAnonymousFocusEvent.getSource();
int[] arrayOfInt = (int[])localJTextField.getClientProperty("Position");
if ((arrayOfInt[0] == Pit.this.N - 1) && (arrayOfInt[1] == 1)) {
return;
}
Pit.this.statusLab.setText((String)localJTextField.getClientProperty("Description"));
if (localJTextField.getBackground() == Pit.this.dtc) {
localJTextField.setBorder(Pit.this.etchedBlue);
} else {
localJTextField.setBorder(Pit.this.etchedRed);
}
Pit.this.recalc(arrayOfInt[0], arrayOfInt[1]);
}
});
if (i == this.N - 1) {
setAsDisabledField(localJTextField);
}
if (paramInt == 1)
{
if (i < this.N - 1) {
localJTextField.addMouseListener(new MouseAdapter()
{
public void mouseReleased(MouseEvent paramAnonymousMouseEvent)
{
if (paramAnonymousMouseEvent.isPopupTrigger())
{
Pit.this.popupMenu.setInvoker(paramAnonymousMouseEvent.getComponent());
Pit.this.popupMenu.show(paramAnonymousMouseEvent.getComponent(), paramAnonymousMouseEvent.getX(), paramAnonymousMouseEvent.getY());
}
}
});
} else {
localJTextField.setBackground(Color.gray);
}
}
else if (paramInt == 3) {
setAsDisabledField(localJTextField);
}
localJPanel.add(localJTextField);
}
return localJPanel;
}
private Color dtc = new Color(240, 240, 240);
private void setAsDisabledField(JTextField paramJTextField)
{
paramJTextField.setEditable(false);
paramJTextField.setBackground(this.dtc);
paramJTextField.setForeground(Color.blue);
paramJTextField.setDisabledTextColor(this.dtc);
}
JLabel createResultLabel(String paramString)
{
JLabel localJLabel = new JLabel(paramString);
localJLabel.setBorder(BorderFactory.createCompoundBorder(new LineBorder(Color.red, 3), new TitledBorder(new LineBorder(Color.red, 1), "Wyliczony podatek")));
FontMetrics localFontMetrics = localJLabel.getFontMetrics(localJLabel.getFont());
int i = localFontMetrics.stringWidth("88888888888888888888888");
Dimension localDimension = localJLabel.getPreferredSize();
localJLabel.setPreferredSize(new Dimension(i, localDimension.height));
localJLabel.setOpaque(true);
localJLabel.setBackground(new Color(250, 250, 231));
localJLabel.setHorizontalAlignment(4);
return localJLabel;
}
private JPopupMenu createPopupMenu()
{
JPopupMenu localJPopupMenu = new JPopupMenu();
for (int i = 0; i < this.sKoszt.length; i++)
{
JMenuItem localJMenuItem = new JMenuItem(this.sKoszt[i]);
localJMenuItem.addActionListener(new ActionListener()
{
private final JPopupMenu val$popupMenu;
public void actionPerformed(ActionEvent paramAnonymousActionEvent)
{
JTextField localJTextField = (JTextField)this.val$popupMenu.getInvoker();
String str1 = localJTextField.getText();
String str2 = paramAnonymousActionEvent.getActionCommand();
if (!str1.equals(str2))
{
localJTextField.setText(paramAnonymousActionEvent.getActionCommand());
if (Pit.this.autoCalc.isSelected())
{
Pit.this.doCalc();
}
else if (Pit.this.autoFill.isSelected())
{
int[] arrayOfInt = (int[])localJTextField.getClientProperty("Position");
Pit.this.recalc(arrayOfInt[0], 3);
}
}
}
});
localJPopupMenu.add(localJMenuItem);
}
return localJPopupMenu;
}
private boolean validateData(JComponent paramJComponent)
{
JTextField localJTextField = (JTextField)paramJComponent;
String str = localJTextField.getText();
boolean bool = true;
if (!str.equals("")) {
try
{
double d = Double.parseDouble(str);
}
catch (NumberFormatException localNumberFormatException)
{
this.lWynik.setText("???");
localJTextField.setBackground(Color.yellow);
this.bCalc.setEnabled(false);
bool = false;
}
}
return bool;
}
private double wyliczKU(int paramInt)
{
double d1 = getValue(paramInt, 0);
double d2 = getValue(paramInt, 1);
double d3 = getValue(paramInt, 2);
double d4 = d1 * d2 / 100.0D;
if (d4 != d3)
{
this.tf[paramInt][2].setText("" + d4);
double d5 = 0.0D;
for (int i = 0; i < this.N - 1; i++) {
d5 += getValue(i, 2);
}
this.tf[(this.N - 1)][2].setText("" + d5);
}
return d4;
}
private double wyliczDoch(int paramInt)
{
double d1 = wyliczKU(paramInt);
double d2 = getValue(paramInt, 3);
double d3 = getValue(paramInt, 0) - d1;
if (d2 != d3)
{
this.tf[paramInt][3].setText("" + d3);
double d4 = 0.0D;
for (int i = 0; i < this.N - 1; i++) {
d4 += getValue(i, 3);
}
this.tf[(this.N - 1)][3].setText("" + d4);
}
return d3;
}
private void recalc(int paramInt1, int paramInt2)
{
JTextField localJTextField = this.tf[paramInt1][paramInt2];
if ((paramInt1 == this.N - 1) && (paramInt2 != 1))
{
double d = 0.0D;
for (int i = 0; i < this.N - 1; i++) {
d += getValue(i, paramInt2);
}
localJTextField.setText("" + d);
return;
}
if (paramInt2 == 2) {
wyliczKU(paramInt1);
} else if (paramInt2 == 3) {
wyliczDoch(paramInt1);
}
}
public void actionPerformed(ActionEvent paramActionEvent)
{
doCalc();
}
private void doCalc()
{
double d1 = 0.0D;
double d2 = 0.0D;
double d3 = 0.0D;
double d4 = 0.0D;
double d5 = 0.0D;
int i = this.N - 1;
for (int j = 0; j < i; j++)
{
double d6 = getValue(j, 0);
double d7 = getValue(j, 1) / 100.0D;
double d8 = d6 * d7;
double d9 = d6 - d8;
double d10 = getValue(j, 4);
this.tf[j][2].setText("" + d8);
this.tf[j][3].setText("" + d9);
d1 += d6;
d2 += d8;
d3 += d9;
d4 += d10;
}
this.tf[i][0].setText(d1 + "");
this.tf[i][2].setText(d2 + "");
this.tf[i][3].setText(d3 + "");
this.tf[i][4].setText(d4 + "");
d5 = 0.15D * d3 - d4;
this.lWynik.setText(d5 + "");
}
double getValue(int paramInt1, int paramInt2)
{
double d = 0.0D;
try
{
d = Double.parseDouble(this.tf[paramInt1][paramInt2].getText());
}
catch (NumberFormatException localNumberFormatException) {}
return d;
}
public static void main(String[] paramArrayOfString)
{
new Pit();
}
} |