1
1
package pl .bator .lso_list_generator .controller ;
2
2
3
- import pl .bator .lso_list_generator .model .Group ;
4
3
import pl .bator .lso_list_generator .repository .GroupJSONRepository ;
5
- import pl .bator .lso_list_generator .service .ApplicationService ;
6
- import pl .bator .lso_list_generator .service .PDFService ;
7
4
8
5
import javax .swing .*;
9
6
import java .awt .*;
10
7
import java .io .IOException ;
11
- import java .text .DateFormatSymbols ;
12
- import java .time .Month ;
13
- import java .time .Year ;
14
- import java .util .Arrays ;
15
- import java .util .Comparator ;
16
- import java .util .Locale ;
17
8
import java .util .Objects ;
18
9
19
10
public class ApplicationController extends JFrame {
20
- private JComboBox <String > monthComboBox ;
21
- private JComboBox <Integer > yearComboBox ;
22
- private JScrollPane scrollPane ;
23
- private JButton generatePdfButton ;
24
- private JButton addGroupButton ;
25
- private JPanel groupTilesPanel ;
26
- private JLabel pathLabel ;
27
- private String defaultSavePath = System .getProperty ("user.home" ) + "\\ Desktop" ;
11
+ private final NavbarController navbarController ;
12
+ private final GroupsPanelController groupsPanelController ;
28
13
private GroupJSONRepository groupJSONRepository ;
29
- private ApplicationService applicationService ;
30
14
31
15
public ApplicationController () {
32
16
try {
33
- this .groupJSONRepository = new GroupJSONRepository ();
34
- this .applicationService = new ApplicationService (groupJSONRepository );
35
- initWindow ();
17
+ groupJSONRepository = new GroupJSONRepository ();
36
18
} catch (IOException e ) {
37
- showErrorAndExit ("Błąd podczas inicjalizacji aplikacji: " + e . getMessage () );
19
+ showErrorAndExit ("Nie można otworzyć pliku JSON." );
38
20
}
39
- }
40
-
41
- private void showErrorAndExit (String message ) {
42
- javax .swing .JOptionPane .showMessageDialog (null , message , "Błąd krytyczny" , javax .swing .JOptionPane .ERROR_MESSAGE );
43
- System .exit (1 );
21
+ navbarController = new NavbarController (groupJSONRepository , this );
22
+ groupsPanelController = new GroupsPanelController (groupJSONRepository );
23
+ initWindow ();
44
24
}
45
25
46
26
private void initWindow () {
@@ -52,138 +32,26 @@ private void initWindow() {
52
32
setResizable (false );
53
33
54
34
JPanel mainPanel = new JPanel (new BorderLayout ());
55
- JPanel monthAndYearSelector = new JPanel (new FlowLayout ());
56
- JPanel pathSelector = new JPanel (new FlowLayout ());
35
+ JPanel monthAndYearSelectorPanel = new JPanel (new FlowLayout ());
36
+ JPanel pathSelectorPanel = new JPanel (new FlowLayout ());
57
37
JPanel buttonPanel = new JPanel (new FlowLayout ());
38
+ JPanel groupsPanel = new JPanel (new FlowLayout ());
39
+ JScrollPane scrollPane = new JScrollPane (groupsPanel );
58
40
59
- initializeGroupTilesPanel ();
60
- initializeMonthComboBox (monthAndYearSelector );
61
- initializeYearComboBox (monthAndYearSelector );
62
- initializeButtons ();
63
-
64
- displayCurrentSavePath (pathSelector );
65
- addChangePathButton (pathSelector );
66
-
67
- buttonPanel .add (addGroupButton );
68
- buttonPanel .add (generatePdfButton );
41
+ navbarController .initView (monthAndYearSelectorPanel , pathSelectorPanel , buttonPanel );
42
+ groupsPanelController .initView (groupsPanel , scrollPane , buttonPanel );
69
43
70
44
mainPanel .setLayout (new BoxLayout (mainPanel , BoxLayout .Y_AXIS ));
71
- mainPanel .add (monthAndYearSelector );
72
- mainPanel .add (pathSelector );
45
+ mainPanel .add (monthAndYearSelectorPanel );
46
+ mainPanel .add (pathSelectorPanel );
73
47
mainPanel .add (buttonPanel );
74
-
75
48
mainPanel .add (scrollPane );
76
49
77
50
add (mainPanel );
78
51
}
79
52
80
- // events
81
-
82
- public void handleChangePathClick () {
83
- JFileChooser fileChooser = new JFileChooser (defaultSavePath );
84
- fileChooser .setDialogTitle ("Wybierz folder zapisu" );
85
- fileChooser .setFileSelectionMode (JFileChooser .DIRECTORIES_ONLY );
86
- int userSelection = fileChooser .showSaveDialog (this );
87
-
88
- if (userSelection == JFileChooser .APPROVE_OPTION ) {
89
- defaultSavePath = fileChooser .getSelectedFile ().getAbsolutePath ();
90
- pathLabel .setText ("Aktualna ścieżka: " + defaultSavePath );
91
- }
92
- }
93
-
94
-
95
- public void handleGenerateClick () {
96
- try {
97
- String selectedMonth = (String ) monthComboBox .getSelectedItem ();
98
- Integer selectedYear = (Integer ) yearComboBox .getSelectedItem ();
99
-
100
- int monthIndex = -1 ;
101
- String [] polishMonths = new DateFormatSymbols (new Locale ("pl" )).getMonths ();
102
- for (int i = 0 ; i < polishMonths .length ; i ++) {
103
- assert selectedMonth != null ;
104
- if (selectedMonth .equals (polishMonths [i ])) {
105
- monthIndex = i ;
106
- break ;
107
- }
108
- }
109
-
110
- if (monthIndex == -1 ) {
111
- JOptionPane .showMessageDialog (this , "Błąd: Nie można odnaleźć wybranego miesiąca." , "Błąd" , JOptionPane .ERROR_MESSAGE );
112
- return ;
113
- }
114
-
115
- Month month = Month .of (monthIndex + 1 );
116
-
117
- PDFService pdfService = new PDFService ();
118
- for (int i = 0 ; i < groupJSONRepository .getGroups ().size (); i ++) {
119
- pdfService .generatePdf (groupJSONRepository .getGroups ().get (i ), month , Year .of (selectedYear ), defaultSavePath );
120
- }
121
- JOptionPane .showMessageDialog (this , "Wygenerowano pomyślnie!" );
122
- } catch (IOException e ) {
123
- JOptionPane .showMessageDialog (this , "Błąd generowania: " + e .getMessage (), "Error" , JOptionPane .ERROR_MESSAGE );
124
- }
125
- }
126
-
127
-
128
- //components
129
-
130
-
131
- private void initializeGroupTilesPanel () {
132
- groupTilesPanel = new JPanel ();
133
- groupTilesPanel .setLayout (new BoxLayout (groupTilesPanel , BoxLayout .Y_AXIS ));
134
- groupTilesPanel .setBorder (BorderFactory .createTitledBorder ("Grupy" ));
135
- groupTilesPanel .setMinimumSize (new Dimension (600 , 400 ));
136
-
137
- scrollPane = new JScrollPane (groupTilesPanel );
138
- scrollPane .setPreferredSize (new Dimension (600 , 400 ));
139
- scrollPane .setHorizontalScrollBarPolicy (JScrollPane .HORIZONTAL_SCROLLBAR_NEVER );
140
- scrollPane .setVerticalScrollBarPolicy (JScrollPane .VERTICAL_SCROLLBAR_AS_NEEDED );
141
-
142
- var groups = groupJSONRepository .getGroups ().stream ().sorted (Comparator .comparingInt (Group ::getNumber )).toList ();
143
- for (Group group : groups ) {
144
- JPanel tilePanel = applicationService .createGroupTile (group );
145
- groupTilesPanel .add (tilePanel );
146
- }
147
-
148
- applicationService .setGroupTilesPanel (groupTilesPanel );
149
- }
150
-
151
- private void initializeMonthComboBox (JPanel selectionPanel1 ) {
152
- int currentMonth = java .time .LocalDate .now ().getMonthValue ();
153
- String [] polishMonths = new DateFormatSymbols (new Locale ("pl" )).getMonths ();
154
- monthComboBox = new JComboBox <>(Arrays .copyOf (polishMonths , polishMonths .length - 1 ));
155
- monthComboBox .setSelectedItem (polishMonths [currentMonth - 1 ]);
156
- selectionPanel1 .add (new JLabel ("Generuj dla miesiąca: " ));
157
- selectionPanel1 .add (monthComboBox );
158
- }
159
-
160
- private void initializeYearComboBox (JPanel selectionPanel1 ) {
161
- int currentYear = Year .now ().getValue ();
162
- Integer [] years = new Integer [10 ];
163
- for (int i = 0 ; i < years .length ; i ++) {
164
- years [i ] = currentYear - 5 + i ;
165
- }
166
- yearComboBox = new JComboBox <>(years );
167
- yearComboBox .setSelectedItem (currentYear );
168
- selectionPanel1 .add (new JLabel ("roku: " ));
169
- selectionPanel1 .add (yearComboBox );
170
- }
171
-
172
- private void displayCurrentSavePath (JPanel selectionPanel2 ) {
173
- pathLabel = new JLabel ("Aktualna ścieżka: " + defaultSavePath );
174
- selectionPanel2 .add (pathLabel );
175
- }
176
-
177
- private void addChangePathButton (JPanel selectionPanel2 ) {
178
- JButton changePathButton = new JButton ("Zmień ścieżkę" );
179
- changePathButton .addActionListener (e -> handleChangePathClick ());
180
- selectionPanel2 .add (changePathButton );
181
- }
182
-
183
- private void initializeButtons () {
184
- generatePdfButton = new JButton ("Generuj" );
185
- generatePdfButton .addActionListener (e -> handleGenerateClick ());
186
- addGroupButton = new JButton ("Dodaj nową grupę" );
187
- addGroupButton .addActionListener (e -> applicationService .handleAddNewGroup (groupTilesPanel ));
53
+ public void showErrorAndExit (String message ) {
54
+ JOptionPane .showMessageDialog (null , message , "Błąd krytyczny" , javax .swing .JOptionPane .ERROR_MESSAGE );
55
+ System .exit (1 );
188
56
}
189
57
}
0 commit comments