View Javadoc

1   package sk.stuba.fiit.foo07.genex.gui;
2   
3   import java.awt.event.ActionEvent;
4   import java.awt.event.ActionListener;
5   import java.awt.event.KeyEvent;
6   import java.awt.event.MouseAdapter;
7   import java.awt.event.MouseEvent;
8   import java.sql.Connection;
9   import java.util.ArrayList;
10  
11  import javax.swing.DefaultListModel;
12  import javax.swing.JButton;
13  import javax.swing.JDialog;
14  import javax.swing.JLabel;
15  import javax.swing.JList;
16  import javax.swing.JOptionPane;
17  import javax.swing.JScrollPane;
18  import javax.swing.JSeparator;
19  import javax.swing.JTextField;
20  import javax.swing.WindowConstants;
21  import javax.swing.event.CaretEvent;
22  import javax.swing.event.CaretListener;
23  
24  import sk.stuba.fiit.foo07.genex.beans.Keyword;
25  import sk.stuba.fiit.foo07.genex.dao.KeywordDao;
26  import sk.stuba.fiit.foo07.genex.dao.KeywordDaoDerby;
27  
28  /**
29   * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
30   * Builder, which is free for non-commercial use. If Jigloo is being used
31   * commercially (ie, by a corporation, company or business for any purpose
32   * whatever) then you should purchase a license for each developer using Jigloo.
33   * Please visit www.cloudgarden.com for details. Use of Jigloo implies
34   * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
35   * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
36   * ANY CORPORATE OR COMMERCIAL PURPOSE.
37   */
38  public class NewKeywordDialog extends javax.swing.JDialog {
39  
40      /**
41       * 
42       */
43      private static final long serialVersionUID = 3299084890496830893L;
44      private DefaultListModel keywords;
45      private KeywordDao kDao;
46      private Keyword[] ret;
47      private JLabel jLabel2;
48      private JButton jBtnNew;
49  
50      private JTextField jTxtKeyword;
51      private JButton jBtnOK;
52      private JButton jBtnCancel;
53      private JSeparator jSeparator1;
54      private JList jLstKeywords;
55      private JScrollPane jScrKeywords;
56      private JLabel jLabel1;
57  
58      public NewKeywordDialog(JDialog frame, Connection c) {
59          super(frame);
60  
61          keywords = new DefaultListModel();
62          try {
63              kDao = new KeywordDaoDerby(c);
64          } catch (Exception e) {
65          }
66  
67          ArrayList<Keyword> keys = null;
68          //TODO
69          //pozret
70          try {
71              keys = kDao.getAllKeywords();
72          } catch (Exception e) {
73          }
74          for (Keyword k : keys)
75              keywords.addElement(k);
76  
77          initGUI();
78      }
79  
80      private void initGUI() {
81          try {
82              {
83                  this.setTitle("Prida\u0165 k\u013eú\u010dové slová");
84                  getContentPane().setLayout(null);
85                  this.setResizable(false);
86                  this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
87                  setLocationByPlatform(true);
88                  {
89                      jLabel1 = new JLabel();
90                      getContentPane().add(jLabel1);
91                      jLabel1.setText("Nové");
92                      jLabel1.setBounds(12, 12, 55, 14);
93                      jLabel1.setForeground(new java.awt.Color(100, 0, 0));
94                  }
95                  {
96                      jTxtKeyword = new JTextField();
97                      getContentPane().add(jTxtKeyword);
98                      jTxtKeyword.setBounds(79, 9, 161, 21);
99                      jTxtKeyword
100                             .setBackground(new java.awt.Color(255, 255, 240));
101                     jTxtKeyword.addCaretListener(new CaretListener() {
102                         public void caretUpdate(CaretEvent evt) {
103                             jTxtKeywordCaretUpdate(evt);
104                         }
105                     });
106                 }
107                 {
108                     jScrKeywords = new JScrollPane();
109                     getContentPane().add(jScrKeywords);
110                     jScrKeywords.setBounds(12, 59, 228, 159);
111                     {
112                         jLstKeywords = new JList();
113                         jScrKeywords.setViewportView(jLstKeywords);
114                         jLstKeywords.setModel(keywords);
115                         jLstKeywords.setBackground(new java.awt.Color(255, 255,
116                                 240));
117                         jLstKeywords.addMouseListener(new MouseAdapter() {
118                             @Override
119                             public void mouseClicked(MouseEvent evt) {
120                                 if (evt.getClickCount() == 2)
121                                     jBtnOKActionPerformed(new ActionEvent(
122                                             jBtnOK, 0, null));
123                             }
124                         });
125                     }
126                 }
127                 {
128                     jSeparator1 = new JSeparator();
129                     getContentPane().add(jSeparator1);
130                     jSeparator1.setBounds(12, 224, 228, 10);
131                 }
132                 {
133                     jBtnCancel = new JButton();
134                     getContentPane().add(jBtnCancel);
135                     jBtnCancel.setText("Cancel");
136                     jBtnCancel.setBounds(155, 232, 85, 21);
137                     jBtnCancel.setMnemonic(KeyEvent.VK_C);
138                     jBtnCancel.addActionListener(new ActionListener() {
139                         public void actionPerformed(ActionEvent evt) {
140                             jBtnCancelActionPerformed(evt);
141                         }
142                     });
143                 }
144                 {
145                     jBtnOK = new JButton();
146                     getContentPane().add(jBtnOK);
147                     jBtnOK.setText("OK");
148                     jBtnOK.setBounds(59, 232, 85, 21);
149                     jBtnOK.setMnemonic(KeyEvent.VK_O);
150                     jBtnOK.addActionListener(new ActionListener() {
151                         public void actionPerformed(ActionEvent evt) {
152                             jBtnOKActionPerformed(evt);
153                         }
154                     });
155                 }
156                 {
157                     jBtnNew = new JButton();
158                     getContentPane().add(jBtnNew);
159                     jBtnNew.setText("v");
160                     jBtnNew.setBounds(200, 34, 40, 21);
161                     jBtnNew.setMnemonic(java.awt.event.KeyEvent.VK_V);
162                     jBtnNew.addActionListener(new ActionListener() {
163                         public void actionPerformed(ActionEvent evt) {
164                             jBtnNewActionPerformed(evt);
165                         }
166                     });
167                 }
168                 {
169                     jLabel2 = new JLabel();
170                     getContentPane().add(jLabel2);
171                     jLabel2.setText("Výber k\u013eú\u010dových slov");
172                     jLabel2.setBounds(12, 36, 164, 17);
173                     jLabel2.setForeground(new java.awt.Color(100, 0, 0));
174                 }
175             }
176             this.setSize(264, 300);
177         } catch (Exception e) {
178             e.printStackTrace();
179         }
180     }
181 
182     public Keyword[] showDialog() {
183         this.setModalityType(ModalityType.DOCUMENT_MODAL);
184         setVisible(true);
185         return ret;
186     }
187 
188     private void jBtnOKActionPerformed(ActionEvent evt) {
189         int[] ind = jLstKeywords.getSelectedIndices();
190         ret = new Keyword[ind.length];
191         for (int i = 0; i < ind.length; i++)
192             ret[i] = (Keyword) keywords.elementAt(ind[i]);
193 
194         setVisible(false);
195     }
196 
197     private void jBtnCancelActionPerformed(ActionEvent evt) {
198         ret = null;
199         setVisible(false);
200     }
201 
202     private void jTxtKeywordCaretUpdate(CaretEvent evt) {
203         jLstKeywords.clearSelection();
204         jLstKeywords.ensureIndexIsVisible(0);
205         if (jTxtKeyword.getText().length() == 0)
206             return;
207 
208         int i, size = keywords.getSize();
209         for (i = 0; i < size; i++) {
210             if (keywords.get(i).toString().startsWith(jTxtKeyword.getText())) {
211                 jLstKeywords.addSelectionInterval(i, i);
212             }
213         }
214         jLstKeywords.ensureIndexIsVisible(jLstKeywords.getSelectedIndex());
215     }
216 
217     private boolean hasKeyword() {
218         for (int i = 0; i < keywords.getSize(); i++)
219             if (jTxtKeyword.getText().equals(keywords.get(i).toString()))
220                 return true;
221 
222         return false;
223     }
224 
225     private void jBtnNewActionPerformed(ActionEvent evt) {
226         if (jTxtKeyword.getText().trim().length() == 0)
227             return;
228         if (jTxtKeyword.getText().trim().length() > 50) {
229             JOptionPane.showMessageDialog(this,
230                     "Kľúčové slovo môže obsahovať maximálne 50 znakov.",
231                     "Chyba", JOptionPane.ERROR_MESSAGE);
232             return;
233         }
234 
235         if (hasKeyword()) {
236             JOptionPane.showMessageDialog(this,
237                     "Dané kľúčové slovo sa už v databáze nachádza.", "Chyba",
238                     JOptionPane.ERROR_MESSAGE);
239             return;
240         }
241 
242         keywords.addElement(new Keyword(jTxtKeyword.getText().trim(), null));
243         jTxtKeyword.requestFocusInWindow();
244         jTxtKeywordCaretUpdate(null);
245     }
246 
247     public void deactivateNewKeywordAdding() {
248         jBtnNew.setEnabled(false);
249     }
250 }