1 package kalenteriswing;
2
3 import javax.swing.Box;
4 import javax.swing.JList;
5 import javax.swing.JTextArea;
6 import javax.swing.event.ListSelectionEvent;
7 import javax.swing.event.ListSelectionListener;
8
9 import kalenteri.Sisalto;
10 import capocalGUI.Ck_about;
11 import capocalGUI.Ck_asktrainingdate;
12 import capocalGUI.Ck_printwindow;
13 import capocalGUI.Ck_startscreen;
14 import capocalGUI.Ck_uusisisalto;
15 import fi.jyu.mit.gui.EditPanel;
16
17
23 @SuppressWarnings("rawtypes")
24 public class KalenteriSwing {
25
26 private Kalenteri k;
27 private String fileName;
28
29 private EditPanel nimiPanel;
30 private JList pvmList;
31 private JList harjSisList;
32 private JList sisList;
33 private JTextArea lisatieto;
34 private Box harjPerustiedotBox;
35
36 private EditPanel[] editPanelPerustiedot;
37
38
41 public KalenteriSwing() {
42 k = new Kalenteri();
43 }
44
45
47
50 public void setNimiPanel(EditPanel e) {
51 this.nimiPanel = e;
52 }
53
54
57 public void setPvmList (JList j) {
58 this.pvmList = j;
59 }
60
61
64 public void setHarjSisList(JList j) {
65 this.harjSisList = j;
66 }
67
68
71 public void setSisList(JList j) {
72 this.sisList = j;
73 }
74
75
78 public void setLisatieto(JTextArea txtrHyvtReenitLopuksi) {
79 this.lisatieto = txtrHyvtReenitLopuksi;
80 }
81
82
85 public void setHarjPerusTiedotBox(Box verticalBoxPerusTiedot) {
86 this.harjPerustiedotBox = verticalBoxPerusTiedot;
87 }
88
89
91
95 @SuppressWarnings("unchecked")
96 public void uusiKayttaja() {
97 k = new Kalenteri();
98 Ck_startscreen startscreen = new Ck_startscreen();
99 startscreen.setKs(this);
100 startscreen.setVisible(true);
101 nimiPanel.setText(getLoginName());
102 sisList.setListData(getSisallot());
103 pvmList.setListData(getHarjoitukset());
104 }
105
106
107
111 public void login(String loginName) {
112 k.loginName = loginName;
113 String fileName = convertToFileName(loginName);
114 this.fileName = fileName;
115 asetaPerustietoKentat();
116 alusta();
117 k.lueTiedostosta(fileName);
118 }
119
120
123 public void uusiHarjoitus() {
124 String pvmSyote;
125 do {
126 pvmSyote = Ck_asktrainingdate.asktrainingdate();
127 if (pvmSyote == null)
128 break;
129 } while (!k.tarkistaPvm(pvmSyote));
130 if (!(pvmSyote == null))
131 k.uusiHarjoitus(pvmSyote);
132 }
133
134
137 public void tallenna() {
138 if (pvmList.getSelectedIndex() != -1) {
140 k.setHarjAlkuaika(editPanelPerustiedot[0].getText());
141 k.setHarjLoppuaika(editPanelPerustiedot[1].getText());
142 k.setPaikkakunta(editPanelPerustiedot[2].getText());
143 k.setSeura(editPanelPerustiedot[3].getText());
144 k.setOpettaja(editPanelPerustiedot[4].getText());
145 k.setLisatieto(lisatieto.getText());
146 k.kirjoitaTiedostoon(fileName);
147 }
148 }
149
150
153 @SuppressWarnings("unchecked")
154 public void lisaaUusiSisalto() {
155 String uusiSis = Ck_uusisisalto.uusisisalto();
156 if (uusiSis.equals("") || uusiSis.equals(" "))
157 return;
158 k.lisaaSisalto(uusiSis);
159 sisList.setListData(getSisallot());
160 }
161
162
165 public void ohjeita() {
166 about();
168 }
169
170
173 @SuppressWarnings("unchecked")
174 public void poistaHarjoitus() {
175 k.poistaHarjoitus(pvmList.getSelectedIndex());
176 pvmList.setListData(getHarjoitukset());
177 }
178
179
182 @SuppressWarnings("unchecked")
183 public void poistaSisalto() {
184 k.poistaSisalto(sisList.getSelectedIndex());
185 sisList.setListData(getSisallot());
186 }
187
188
191 public void tulosta() {
192 Ck_printwindow printWindow = new Ck_printwindow();
193 printWindow.setTextArea(k.tulosta(pvmList.getSelectedIndex()));
194 printWindow.setVisible(true);
195 }
196
197
200 public void about() {
201 Ck_about frame = new Ck_about();
202 frame.setVisible(true);
203 }
204
205
220 public static String convertToFileName(String s) {
221 String temp = s;
222 temp = temp.toLowerCase();
223 temp = temp.replace(' ', '_');
224 temp = temp.replace('.', '_');
225 temp = temp.replace('\'', '_');
226 temp = temp.replace('å', 'o');
227 temp = temp.replace('ä', 'a');
228 temp = temp.replace('ö', 'o');
229 temp = temp + ".dat";
230 return temp;
231 }
232
233
237 public String getLoginName() {
238 return k.loginName;
239 }
240
241
244 public Sisalto[] getSisallot() {
245 return k.SisallotToString();
246 }
247
248
251 public String[] getHarjoitukset() {
252 return k.getHarjoitukset();
253 }
254
255
258 @SuppressWarnings({ "synthetic-access", "unchecked" })
259 public void alusta() {
260
261 pvmList.addListSelectionListener(new ListSelectionListener() {
262
263 @Override
264 public void valueChanged(ListSelectionEvent e) {
265 if(pvmList.getSelectedIndex() != -1) {
266 haeHarjoitus(pvmList.getSelectedIndex());
267 } else {
268 nollaaKentat();
269 harjSisList.setListData(new Object[] {});
270 lisatieto.setText(null);
271 }
272 }
273 });
274 harjSisList.addListSelectionListener(new ListSelectionListener() {
275
276 @Override
277 public void valueChanged(ListSelectionEvent e) {
278 k.poistaHarjoituksenSisallot((String)harjSisList.getSelectedValue());
279 harjSisList.setListData(k.getHarjoituksenSisallot(pvmList.getSelectedIndex()));
280 }
281 });
282 sisList.addListSelectionListener(new ListSelectionListener() {
283
284 @Override
285 public void valueChanged(ListSelectionEvent e) {
286 if (pvmList.getSelectedIndex() != -1)
287 k.lisaaHarjoituksenSisallot(sisList.getSelectedIndex());
288 harjSisList.setListData(k.getHarjoituksenSisallot(pvmList.getSelectedIndex()));
289 }
290 });
291 }
292
293
297 @SuppressWarnings("unchecked")
298 public void haeHarjoitus(int selectedIndex) {
299
300 int id = k.setValittuHarjoitus(selectedIndex).getId();
301
302 editPanelPerustiedot[0].setText(k.getHarjAloitusaika(id));
303 editPanelPerustiedot[1].setText(k.getHarjLopetusaika(id));
304 editPanelPerustiedot[2].setText(k.getPaikkakunta(id));
305 editPanelPerustiedot[3].setText(k.getSeura(id));
306 editPanelPerustiedot[4].setText(k.getOpettaja(id));
307
308 harjSisList.setListData(k.getHarjoituksenSisallot(id));
309
310 lisatieto.setText(k.getLisatieto(id));
311 }
312
313
316 public void asetaPerustietoKentat() {
317 harjPerustiedotBox.removeAll();
318 editPanelPerustiedot = new EditPanel[5];
319
320 EditPanel e = new EditPanel();
321 e.setCaption("Alkamisaika");
322 editPanelPerustiedot[0] = e;
323 harjPerustiedotBox.add(e);
324 e = new EditPanel();
325 e.setCaption("Loppumisaika");
326 editPanelPerustiedot[1] = e;
327 harjPerustiedotBox.add(e);
328 e = new EditPanel();
329 e.setCaption("Paikkakunta");
330 editPanelPerustiedot[2] = e;
331 harjPerustiedotBox.add(e);
332 e = new EditPanel();
333 e.setCaption("Seura");
334 editPanelPerustiedot[3] = e;
335 harjPerustiedotBox.add(e);
336 e = new EditPanel();
337 e.setCaption("Opettaja");
338 editPanelPerustiedot[4] = e;
339 harjPerustiedotBox.add(e);
340 }
341
342
345 private void nollaaKentat() {
346 for (int i=0;i<5;i++) {
347 editPanelPerustiedot[i].setText(null);
348 }
349 }
350
351
352
353
358 @SuppressWarnings("unchecked")
359 public void search(String hakusana) {
360 Object[] pvmTaulu = k.search(hakusana);
361 if (pvmTaulu.length == 0)
362 pvmList.setListData(getHarjoitukset());
363 pvmList.setListData(pvmTaulu);
364 }
365 }
366