1
|
package gui;
|
2
|
|
3
|
import java.awt.BorderLayout;
|
4
|
import java.awt.FlowLayout;
|
5
|
import java.awt.GridLayout;
|
6
|
import java.awt.event.ActionEvent;
|
7
|
import java.awt.event.ActionListener;
|
8
|
|
9
|
import java.io.File;
|
10
|
import java.text.DateFormat;
|
11
|
import java.text.ParseException;
|
12
|
import java.text.SimpleDateFormat;
|
13
|
import java.util.Calendar;
|
14
|
import java.util.Date;
|
15
|
import java.util.Properties;
|
16
|
|
17
|
import javax.swing.JButton;
|
18
|
import javax.swing.JFileChooser;
|
19
|
import javax.swing.JFormattedTextField.AbstractFormatter;
|
20
|
import javax.swing.JFrame;
|
21
|
import javax.swing.JLabel;
|
22
|
import javax.swing.JOptionPane;
|
23
|
import javax.swing.JPanel;
|
24
|
import javax.swing.JScrollPane;
|
25
|
import javax.swing.JTextArea;
|
26
|
import javax.swing.JTextField;
|
27
|
|
28
|
import org.jdatepicker.impl.JDatePanelImpl;
|
29
|
import org.jdatepicker.impl.JDatePickerImpl;
|
30
|
import org.jdatepicker.impl.UtilDateModel;
|
31
|
|
32
|
/**
|
33
|
*
|
34
|
* Zakladni okno aplikace, zprostredkovava vstupy od uzivatele
|
35
|
*
|
36
|
* @author Marek Sobota, Daniel Stus, Lukas Scurko, Jan Jirman
|
37
|
* @version 1.0
|
38
|
*
|
39
|
*/
|
40
|
public class Window extends JFrame{
|
41
|
|
42
|
/**zakladni verze serializace*/
|
43
|
private static final long serialVersionUID = 1L;
|
44
|
|
45
|
/**konstanta pro oznaceni souboru se spoji*/
|
46
|
private static final int SPOJ_FILE = 0;
|
47
|
|
48
|
/**konstanta pro oznaceni slozky JDF*/
|
49
|
private static final int JDF_DIR = 1;
|
50
|
|
51
|
/**konstanta pro oznaceni dialogu pro vyber zacinajiciho data*/
|
52
|
private static final int DATE_PICKER_FROM = 2;
|
53
|
|
54
|
/**konstanta pro oznaceni dialogu pro vyber konciciho data*/
|
55
|
private static final int DATE_PICKER_TO = 3;
|
56
|
|
57
|
/**spolecna delka pro vsechny {@code JTextField}*/
|
58
|
private static final int TEXT_FIELD_LENGTH = 15;
|
59
|
|
60
|
/**konstanta pro nazev souboru caskody*/
|
61
|
private static final String CASKODY = "caskody.txt";
|
62
|
|
63
|
/**konstanta pro nazev souboru linky*/
|
64
|
private static final String LINKY = "linky.txt";
|
65
|
|
66
|
/**konstanta pro nazev souboru pevnykod*/
|
67
|
private static final String PEVNY_KOD = "pevnykod.txt";
|
68
|
|
69
|
/**konstanta pro nazev souboru spoje*/
|
70
|
private static final String SPOJE = "spoje.txt";
|
71
|
|
72
|
/**pouzivany datovy format*/
|
73
|
public static final DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
|
74
|
|
75
|
/**atribut se souborem se spoji*/
|
76
|
private File spojFile = null;
|
77
|
|
78
|
/**atribut se slozkou JDF*/
|
79
|
private File jdfDir = null;
|
80
|
|
81
|
/**den zacatku obdobi*/
|
82
|
private Calendar dateFrom = null;
|
83
|
|
84
|
/**dden konce obdobi*/
|
85
|
private Calendar dateTo = null;
|
86
|
|
87
|
/**panel pro zadani vstupnich dat*/
|
88
|
private JPanel inputP;
|
89
|
|
90
|
/**tlacitko pro odeslani zadanych dat*/
|
91
|
private JButton confirmBT;
|
92
|
|
93
|
/**tlacitko pro vypnuti programu*/
|
94
|
private JButton endBT;
|
95
|
|
96
|
/**Dialog pro vybrani cesty k souborum*/
|
97
|
private JFileChooser fileChooser;
|
98
|
|
99
|
/**Dialog pro vybrani data zacatku*/
|
100
|
private JDatePickerImpl datePickerFrom;
|
101
|
|
102
|
/**Dialog pro vybrani data konce*/
|
103
|
private JDatePickerImpl datePickerTo;
|
104
|
|
105
|
/**TextField pro zobrazeni cesty k souboru se spojema*/
|
106
|
private JTextField spojPathTF;
|
107
|
|
108
|
/**TextField pro zobrazeni cesty k slozce JDF*/
|
109
|
private JTextField jdfPathTF;
|
110
|
|
111
|
/**TextArea pro výpis informací o běhu programu*/
|
112
|
private JTextArea infoTA;
|
113
|
|
114
|
/**ScrollPane pro obaleni infoTA*/
|
115
|
private JScrollPane scrollPane;
|
116
|
|
117
|
/**
|
118
|
* inicializuje okno, jmenovite jeho panely, komponenty, priradi potrebne listenery a prida je do zobrazeni
|
119
|
*/
|
120
|
public Window(){
|
121
|
super("POVED");
|
122
|
this.setLayout(new BorderLayout());
|
123
|
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
124
|
|
125
|
//nastaveni JPanelu
|
126
|
inputP = new JPanel();
|
127
|
JPanel dateFromP = new JPanel();
|
128
|
JPanel dateToP = new JPanel();
|
129
|
JPanel spojPathP = new JPanel();
|
130
|
JPanel jdfPathP = new JPanel();
|
131
|
inputP.setLayout(new GridLayout(4, 1));
|
132
|
dateFromP.setLayout(new FlowLayout());
|
133
|
dateToP.setLayout(new FlowLayout());
|
134
|
spojPathP.setLayout(new FlowLayout());
|
135
|
jdfPathP.setLayout(new FlowLayout());
|
136
|
|
137
|
//inicializace kompoment
|
138
|
JLabel povedLB = new JLabel("POVED", JLabel.CENTER);
|
139
|
JLabel dateFromLB = new JLabel("Obdobi od: ");
|
140
|
JLabel dateToLB = new JLabel ("Obdobi do: ");
|
141
|
JLabel spojPathLB = new JLabel("Spoje: ");
|
142
|
JLabel jdfPathLB = new JLabel("JDF: ");
|
143
|
confirmBT = new JButton("Potvrdit");
|
144
|
endBT = new JButton("Ukoncit");
|
145
|
JButton spojBT = new JButton("Vybrat spoj soubor");
|
146
|
JButton jdfBT = new JButton("Vybrat JDF slozku");
|
147
|
spojPathTF = new JTextField(TEXT_FIELD_LENGTH);
|
148
|
jdfPathTF = new JTextField(TEXT_FIELD_LENGTH);
|
149
|
fileChooser = new JFileChooser();
|
150
|
infoTA = new JTextArea();
|
151
|
scrollPane = new JScrollPane(infoTA);
|
152
|
initDatePicker();
|
153
|
|
154
|
//nastaveni komponent
|
155
|
spojPathTF.setEditable(false);
|
156
|
jdfPathTF.setEditable(false);
|
157
|
infoTA.setEditable(false);
|
158
|
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
|
159
|
|
160
|
|
161
|
//pridani listeneru k buttonum
|
162
|
confirmBT.addActionListener(new ConfirmListener());
|
163
|
endBT.addActionListener(new ActionListener(){
|
164
|
//@Override
|
165
|
/**
|
166
|
* ukonci program
|
167
|
*
|
168
|
* @param arg0 kod udalosti
|
169
|
*/
|
170
|
public void actionPerformed(ActionEvent arg0) {
|
171
|
printMessage("Exiting...");
|
172
|
System.exit(0);
|
173
|
}
|
174
|
});
|
175
|
spojBT.addActionListener(new FileChooserListener(SPOJ_FILE));
|
176
|
jdfBT.addActionListener(new FileChooserListener(JDF_DIR));
|
177
|
|
178
|
//pridani komponent do panelu
|
179
|
inputP.add(dateFromP);
|
180
|
inputP.add(dateToP);
|
181
|
inputP.add(spojPathP);
|
182
|
inputP.add(jdfPathP);
|
183
|
dateFromP.add(dateFromLB);
|
184
|
dateFromP.add(datePickerFrom);
|
185
|
dateToP.add(dateToLB);
|
186
|
dateToP.add(datePickerTo);
|
187
|
spojPathP.add(spojPathLB);
|
188
|
spojPathP.add(spojPathTF);
|
189
|
spojPathP.add(spojBT);
|
190
|
jdfPathP.add(jdfPathLB);
|
191
|
jdfPathP.add(jdfPathTF);
|
192
|
jdfPathP.add(jdfBT);
|
193
|
|
194
|
//pridani panelu do okna
|
195
|
this.add(povedLB, BorderLayout.NORTH);
|
196
|
this.add(inputP, BorderLayout.CENTER);
|
197
|
this.add(confirmBT, BorderLayout.SOUTH);
|
198
|
|
199
|
this.pack();
|
200
|
|
201
|
}
|
202
|
|
203
|
/**
|
204
|
*
|
205
|
* inicializuje dve na sobe nezavisle instance {@code DatePickerImpl}
|
206
|
*/
|
207
|
private void initDatePicker(){
|
208
|
UtilDateModel modelFrom = new UtilDateModel();
|
209
|
Properties pFrom = new Properties();
|
210
|
pFrom.put("text.today", "Today");
|
211
|
pFrom.put("text.month", "Month");
|
212
|
pFrom.put("text.year", "Year");
|
213
|
UtilDateModel modelTo = new UtilDateModel();
|
214
|
Properties pTo = new Properties();
|
215
|
pTo.put("text.today", "Today");
|
216
|
pTo.put("text.month", "Month");
|
217
|
pTo.put("text.year", "Year");
|
218
|
JDatePanelImpl datePanelFrom = new JDatePanelImpl(modelFrom, pFrom);
|
219
|
JDatePanelImpl datePanelTo = new JDatePanelImpl(modelTo, pTo);
|
220
|
datePickerFrom = new JDatePickerImpl(datePanelFrom, new DateLabelFormatter());
|
221
|
datePickerTo = new JDatePickerImpl(datePanelTo, new DateLabelFormatter());
|
222
|
datePickerFrom.addActionListener(new DatePickerListener(DATE_PICKER_FROM));
|
223
|
datePickerTo.addActionListener(new DatePickerListener(DATE_PICKER_TO));
|
224
|
|
225
|
}
|
226
|
|
227
|
/**
|
228
|
* zmeni okno ze vstupoveho pohledu, aby od ted zobrazovalo prubezne informace
|
229
|
*/
|
230
|
private void changeToInfo(){
|
231
|
this.remove(inputP);
|
232
|
this.remove(confirmBT);
|
233
|
|
234
|
this.add(scrollPane, BorderLayout.CENTER);
|
235
|
this.add(endBT, BorderLayout.SOUTH);
|
236
|
this.revalidate();
|
237
|
}
|
238
|
|
239
|
|
240
|
/**
|
241
|
* metoda pro pridani zpravy do vypisu uzivateli
|
242
|
*
|
243
|
* @param message zprava, kterou chceme uzivateli sdelit
|
244
|
*/
|
245
|
public void printMessage(String message){
|
246
|
infoTA.append(message + "\n");
|
247
|
}
|
248
|
|
249
|
/**
|
250
|
*
|
251
|
* Listener pro potvrzovaci tlacitko, kontroluje, jestli vsechna zadana data jsou platna
|
252
|
*
|
253
|
* @author Marek Sobota, Daniel Stus, Lukas Scurko, Jan Jirman
|
254
|
* @version 1.0
|
255
|
*
|
256
|
*/
|
257
|
class ConfirmListener implements ActionListener{
|
258
|
|
259
|
//@Override
|
260
|
/**
|
261
|
* zkontroluje, zda-li zadana data jsou platna, pote zmeni panely aby okno vypisovalo informace o prubehu generace
|
262
|
* a odesle data na zpracovani
|
263
|
*
|
264
|
* @param arg0 kod udalosti
|
265
|
*/
|
266
|
public void actionPerformed(ActionEvent arg0) {
|
267
|
//kontrola jestli jsou obdobi zadana a ve spravnem poradi a jestli jsou cesty vyplněny
|
268
|
if(spojFile == null || jdfDir == null){
|
269
|
JOptionPane.showMessageDialog(null, "Nejsou vybrany potrebne soubory",
|
270
|
"Varovani", JOptionPane.WARNING_MESSAGE);
|
271
|
return;
|
272
|
}
|
273
|
if(dateTo == null || dateFrom == null){
|
274
|
JOptionPane.showMessageDialog(null, "Neni zadano datum",
|
275
|
"Varovani", JOptionPane.WARNING_MESSAGE);
|
276
|
return;
|
277
|
}
|
278
|
if(dateTo.compareTo(dateFrom) < 0){
|
279
|
JOptionPane.showMessageDialog(null, "Zacatek obdobi je po konci obdobi",
|
280
|
"Varovani", JOptionPane.WARNING_MESSAGE);
|
281
|
return;
|
282
|
}
|
283
|
|
284
|
changeToInfo();
|
285
|
}
|
286
|
|
287
|
}
|
288
|
|
289
|
/**
|
290
|
* Listener pro {@code JFileChooser}, zpracovava dialog pro vybrani souboru a kontrolu jeho platnosti
|
291
|
*
|
292
|
* @author Marek Sobota, Daniel Stus, Lukas Scurko, Jan Jirman
|
293
|
* @version 1.0
|
294
|
*
|
295
|
*/
|
296
|
class FileChooserListener implements ActionListener{
|
297
|
|
298
|
/**
|
299
|
* cislo oznacujici, jestli se jedna o soubor se spoji nebo o slozku JDF
|
300
|
*/
|
301
|
private int fileType;
|
302
|
|
303
|
/**
|
304
|
* konstruktor ulozi do atributu typ souboru, ktery ma vybirat
|
305
|
*
|
306
|
* @param fileType ktery soubor se vybral (0 = spoj, 1 = jdf slozka)
|
307
|
*/
|
308
|
public FileChooserListener(int fileType) {
|
309
|
this.fileType = fileType;
|
310
|
}
|
311
|
|
312
|
//@Override
|
313
|
/**
|
314
|
* otevre dialog pro vybrani souboru nebo slozky, overi, zda-li typ souboru vyhovuje
|
315
|
* pokud ano, ulozi ho do prislusne promene, jinak vypise varovani
|
316
|
*
|
317
|
*/
|
318
|
public void actionPerformed(ActionEvent arg0) {
|
319
|
int option = fileChooser.showOpenDialog(null);
|
320
|
if(option == JFileChooser.APPROVE_OPTION){
|
321
|
File selectedFile = fileChooser.getSelectedFile();
|
322
|
|
323
|
if(selectedFile.isFile() && fileType == SPOJ_FILE && selectedFile.getName().endsWith(".txt")){
|
324
|
spojFile = selectedFile;
|
325
|
spojPathTF.setText(spojFile.getAbsolutePath());
|
326
|
}
|
327
|
else if(selectedFile.isDirectory() && fileType == JDF_DIR){
|
328
|
//overi existenci potrebnych souboru ve slozce
|
329
|
File caskody = new File(selectedFile.getAbsolutePath() + File.separator + CASKODY);
|
330
|
File linky = new File(selectedFile.getAbsolutePath() + File.separator + LINKY);
|
331
|
File pevnyKod = new File(selectedFile.getAbsolutePath() + File.separator + PEVNY_KOD);
|
332
|
File spoje = new File(selectedFile.getAbsolutePath() + File.separator + SPOJE);
|
333
|
if(!caskody.exists()){
|
334
|
JOptionPane.showMessageDialog(null, "Ve slozce se nenachazi soubor " + CASKODY,
|
335
|
"Varovani", JOptionPane.WARNING_MESSAGE);
|
336
|
return;
|
337
|
}
|
338
|
if(!linky.exists()){
|
339
|
JOptionPane.showMessageDialog(null, "Ve slozce se nenachazi soubor " + LINKY,
|
340
|
"Varovani", JOptionPane.WARNING_MESSAGE);
|
341
|
return;
|
342
|
}
|
343
|
if(!pevnyKod.exists()){
|
344
|
JOptionPane.showMessageDialog(null, "Ve slozce se nenachazi soubor " + PEVNY_KOD,
|
345
|
"Varovani", JOptionPane.WARNING_MESSAGE);
|
346
|
return;
|
347
|
}
|
348
|
if(!spoje.exists()){
|
349
|
JOptionPane.showMessageDialog(null, "Ve slozce se nenachazi soubor " + SPOJE,
|
350
|
"Varovani", JOptionPane.WARNING_MESSAGE);
|
351
|
return;
|
352
|
}
|
353
|
|
354
|
jdfDir = selectedFile;
|
355
|
jdfPathTF.setText(jdfDir.getAbsolutePath());
|
356
|
}
|
357
|
else{
|
358
|
JOptionPane.showMessageDialog(null, "Vybran nespravny typ souboru",
|
359
|
"Varovani", JOptionPane.WARNING_MESSAGE);
|
360
|
}
|
361
|
}
|
362
|
}
|
363
|
|
364
|
}
|
365
|
|
366
|
/**
|
367
|
*
|
368
|
* Listener pro {@code JDatePicker}, zpracovava dialog pro vybrani data
|
369
|
*
|
370
|
* @author Marek Sobota, Daniel Stus, Lukas Scurko, Jan Jirman
|
371
|
* @version 1.0
|
372
|
*
|
373
|
*/
|
374
|
class DatePickerListener implements ActionListener{
|
375
|
|
376
|
/**cislo oznacujici zda-li se jedna o zacatek nebo konec obdobi*/
|
377
|
private int datePickerType;
|
378
|
|
379
|
/**
|
380
|
* konstruktor ulozi do atributu typ souboru, ktery ma vybirat
|
381
|
*
|
382
|
* @param datePickerType typ data, ktery se vybral (2 = od, 3 = do)
|
383
|
*/
|
384
|
public DatePickerListener(int datePickerType){
|
385
|
this.datePickerType = datePickerType;
|
386
|
}
|
387
|
|
388
|
//@Override
|
389
|
/**
|
390
|
* ulozi vybrane datum z dialogu do odpovidajiciho data
|
391
|
*
|
392
|
* @param arg0 kod udalosti
|
393
|
*/
|
394
|
public void actionPerformed(ActionEvent arg0) {
|
395
|
if(datePickerType == DATE_PICKER_FROM){
|
396
|
dateFrom = Calendar.getInstance();
|
397
|
dateFrom.setTime((Date) datePickerFrom.getModel().getValue());
|
398
|
}
|
399
|
else if (datePickerType == DATE_PICKER_TO){
|
400
|
dateTo = Calendar.getInstance();
|
401
|
dateTo.setTime((Date) datePickerTo.getModel().getValue());
|
402
|
}
|
403
|
}
|
404
|
|
405
|
|
406
|
}
|
407
|
|
408
|
class DateLabelFormatter extends AbstractFormatter {
|
409
|
|
410
|
private static final long serialVersionUID = 1L;
|
411
|
|
412
|
private String datePattern = "yyyy-MM-dd";
|
413
|
private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern);
|
414
|
|
415
|
@Override
|
416
|
public Object stringToValue(String text) throws ParseException {
|
417
|
return dateFormatter.parseObject(text);
|
418
|
}
|
419
|
|
420
|
@Override
|
421
|
public String valueToString(Object value) throws ParseException {
|
422
|
if (value != null) {
|
423
|
Calendar cal = (Calendar) value;
|
424
|
return dateFormatter.format(cal.getTime());
|
425
|
}
|
426
|
|
427
|
return "";
|
428
|
}
|
429
|
|
430
|
}
|
431
|
|
432
|
/**
|
433
|
*
|
434
|
* hlavni metoda pro ozkouseni funkcnosti {@code Window}
|
435
|
*
|
436
|
* @param args parametry programu
|
437
|
*/
|
438
|
public static void main(String[] args){
|
439
|
Window w = new Window();
|
440
|
w.setVisible(true);
|
441
|
}
|
442
|
}
|