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  /**
18   * Luokka jossa käsitellään kalenteria swing-komponenteilla
19   * @author Timo Koski
20   * @version 17.2.2012
21   * @version 11.4.2012
22   */
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      /**
39       * muodostaja
40       */
41      public KalenteriSwing() {
42          k = new Kalenteri();
43      }
44  
45      // Swing-komponenttien setterit ja getterit
46  
47      /**
48       * @param e
49       */
50      public void setNimiPanel(EditPanel e) {
51          this.nimiPanel = e;
52      }
53  
54      /**
55       * @param j
56       */
57      public void setPvmList (JList j) {
58          this.pvmList = j;
59      }
60  
61      /**
62       * @param j
63       */
64      public void setHarjSisList(JList j) {
65          this.harjSisList = j;
66      }
67  
68      /**
69       * @param j
70       */
71      public void setSisList(JList j) {
72          this.sisList = j;
73      }
74  
75      /**
76       * @param txtrHyvtReenitLopuksi
77       */
78      public void setLisatieto(JTextArea txtrHyvtReenitLopuksi) {
79          this.lisatieto = txtrHyvtReenitLopuksi;
80      }
81  
82      /**
83       * @param verticalBoxPerusTiedot
84       */
85      public void setHarjPerusTiedotBox(Box verticalBoxPerusTiedot) {
86          this.harjPerustiedotBox = verticalBoxPerusTiedot;
87      }
88  
89      // Metodit alkavat
90  
91      /**
92       * Avaa dialogin, joka kysyy käyttäjää.
93       * 
94       */
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     /**
108      * Startscreenin (tai käyttäjänvaihdon) login
109      * @param loginName 
110      */
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     /**
121      * Luodaan uusi Harjoitus olio. Tarkastetaan myös sen päivämäärä.
122      */
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     /**
135      * tallentamismetodi
136      */
137     public void tallenna() {
138         // TODO: Jos tallennetaan ilman käyttäjää
139         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     /**
151      * 
152      */
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     /**
163      * 
164      */
165     public void ohjeita() {
166         // TODO: jotain muuta kuin about()
167         about();
168     }
169 
170     /**
171      * 
172      */
173     @SuppressWarnings("unchecked")
174     public void poistaHarjoitus() {
175         k.poistaHarjoitus(pvmList.getSelectedIndex());
176         pvmList.setListData(getHarjoitukset());
177     }
178 
179     /**
180      * 
181      */
182     @SuppressWarnings("unchecked")
183     public void poistaSisalto() {
184         k.poistaSisalto(sisList.getSelectedIndex());
185         sisList.setListData(getSisallot());
186     }
187 
188     /**
189      * 
190      */
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     /**
198      * Tiedot-dialogi
199      */
200     public void about() {
201         Ck_about frame = new Ck_about();
202         frame.setVisible(true);
203     }
204 
205     /**
206      * @param s
207      * @return s
208      * @example
209      * <pre name="test">
210      * String p = "äiti";
211      * convertToFileName(p) === "aiti.dat";
212      * p = "MÖTTÖNEN";
213      * convertToFileName(p) === "mottonen.dat";
214      * p = "åke äKäslompolo";
215      * convertToFileName(p) === "oke_akaslompolo.dat";
216      * p = "dmi'troz ÅÅ''pqÄÄ";
217      * convertToFileName(p) === "dmi_troz_oo__pqaa.dat";
218      * </pre>
219      */
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     /**
234      * Palautetaan kalenterista sisäänkirjautumisnimi.
235      * @return loginName
236      */
237     public String getLoginName() {
238         return k.loginName;
239     }
240 
241     /**
242      * @return k.SisallotToString()
243      */
244     public Sisalto[] getSisallot() {
245         return k.SisallotToString();
246     }
247 
248     /**
249      * @return k.getHarjoitukset()
250      */
251     public String[] getHarjoitukset() {
252         return k.getHarjoitukset();
253     }
254 
255     /**
256      * Alustetaan komponenteille kuuntelijat.
257      */
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     /**
294      * Hakee harjoituksen, jonka kohdalle päivämäärälistassa on siirrytty.
295      * @param selectedIndex 
296      */
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     /**
314      * Perustietokenttien asettaminen käyttöliittymään - keskelle ylös.
315      */
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     /**
343      * Nollaa perustietokentät
344      */
345     private void nollaaKentat() {
346         for (int i=0;i<5;i++) {
347             editPanelPerustiedot[i].setText(null);
348         }
349     }
350 
351     /* HAKUTOIMINTO */
352     
353     /**
354      * Asetetaan päivämäärälistaksi kalenteri-luokan metodin palauttama
355      * String-taulukko, joka on karsittu hakusana-parametrilla.
356      * @param hakusana
357      */
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