View Javadoc

1   package sk.stuba.fiit.foo07.genex.gui;
2   
3   import java.awt.Dimension;
4   import java.awt.event.ActionEvent;
5   import java.awt.event.ActionListener;
6   import java.awt.event.MouseAdapter;
7   import java.awt.event.MouseEvent;
8   import java.io.File;
9   import java.io.FileInputStream;
10  import java.nio.ByteBuffer;
11  import java.nio.channels.FileChannel;
12  import java.sql.Connection;
13  import java.sql.SQLException;
14  import java.util.ArrayList;
15  
16  import javax.activation.MimetypesFileTypeMap;
17  import javax.swing.DefaultListModel;
18  import javax.swing.ImageIcon;
19  import javax.swing.JButton;
20  import javax.swing.JCheckBox;
21  import javax.swing.JDialog;
22  import javax.swing.JFileChooser;
23  import javax.swing.JLabel;
24  import javax.swing.JList;
25  import javax.swing.JScrollPane;
26  import javax.swing.JSeparator;
27  import javax.swing.JTextArea;
28  import javax.swing.JTextField;
29  import javax.swing.JTextPane;
30  import javax.swing.SwingConstants;
31  import javax.swing.WindowConstants;
32  import javax.swing.event.ChangeEvent;
33  import javax.swing.event.ChangeListener;
34  import javax.swing.event.ListSelectionEvent;
35  import javax.swing.event.ListSelectionListener;
36  import javax.swing.filechooser.FileNameExtensionFilter;
37  
38  import sk.stuba.fiit.foo07.genex.beans.Picture;
39  import sk.stuba.fiit.foo07.genex.common.ResourceHelper;
40  import sk.stuba.fiit.foo07.genex.dao.PictureDao;
41  import sk.stuba.fiit.foo07.genex.dao.PictureDaoDerby;
42  
43  /**
44   * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
45   * Builder, which is free for non-commercial use. If Jigloo is being used
46   * commercially (ie, by a corporation, company or business for any purpose
47   * whatever) then you should purchase a license for each developer using Jigloo.
48   * Please visit www.cloudgarden.com for details. Use of Jigloo implies
49   * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
50   * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
51   * ANY CORPORATE OR COMMERCIAL PURPOSE.
52   */
53  public class NewPictureDialog extends javax.swing.JDialog {
54      /**
55       * 
56       */
57      private static final long serialVersionUID = 7855093533185184721L;
58  
59      private DefaultListModel pictures;
60      private PictureDao pDao;
61      private Picture[] ret;
62      private Picture lastSel;
63      private JLabel jLabel5;
64      private JTextField jTxtMIME;
65      private JTextField jTxtSettings;
66      private JLabel jLabel4;
67      private JScrollPane jScrollPane2;
68      private JTextArea jTxtDesciption;
69      private JLabel jLabel3;
70      private ResourceHelper resHelp;
71  
72      private JLabel jLabel1;
73      private JScrollPane jScrPictures;
74      private JScrollPane jScrollPane1;
75      private JButton jBtnOK;
76      private PictureComponent jPreview;
77      private JCheckBox jChbResize;
78      private JButton jBtnCancel;
79      private JSeparator jSeparator2;
80      private JButton jBtnAddPicture;
81      private JList jLstPictures;
82      private JLabel jLabel2;
83      private JScrollPane jScrPreview;
84      private JSeparator jSeparator1;
85      private JTextPane jTxtHint;
86  
87      public NewPictureDialog(JDialog frame, Connection c) {
88          super(frame);
89  
90          resHelp = new ResourceHelper();
91          pictures = new DefaultListModel();
92          pDao = new PictureDaoDerby(c);
93          ArrayList<Picture> dbpixs = null;
94          //TODO
95          try {
96              dbpixs = pDao.getAllPictures();
97              for (Picture p : dbpixs) {
98                  pictures.addElement(p);
99              }
100         } catch (Exception e) {
101         }
102         initGUI();
103     }
104 
105     private void initGUI() {
106         try {
107             {
108                 getContentPane().setLayout(null);
109                 this.setResizable(false);
110                 this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
111                 this.setTitle("Prida\u0165 obrázky");
112                 setLocationByPlatform(true);
113                 {
114                     jLabel1 = new JLabel();
115                     getContentPane().add(jLabel1);
116                     jLabel1.setText("Obrázky v databáze");
117                     jLabel1.setBounds(12, 103, 167, 14);
118                     jLabel1.setForeground(new java.awt.Color(100, 0, 0));
119                 }
120                 {
121                     jScrPictures = new JScrollPane();
122                     getContentPane().add(jScrPictures);
123                     jScrPictures.setBounds(12, 123, 291, 174);
124                     {
125                         jLstPictures = new JList(pictures);
126                         jScrPictures.setViewportView(jLstPictures);
127                         jLstPictures.setBackground(new java.awt.Color(255, 255,
128                                 240));
129                         jLstPictures
130                                 .setForeground(new java.awt.Color(0, 0, 255));
131                         jLstPictures.addMouseListener(new MouseAdapter() {
132                             @Override
133                             public void mouseClicked(MouseEvent evt) {
134                                 if (evt.getClickCount() == 2)
135                                     jBtnOKActionPerformed(new ActionEvent(
136                                             jBtnOK, 0, null));
137                             }
138                         });
139                         jLstPictures
140                                 .addListSelectionListener(new ListSelectionListener() {
141                                     public void valueChanged(
142                                             ListSelectionEvent evt) {
143                                         jLstPicturesValueChanged(evt);
144                                     }
145                                 });
146                     }
147                 }
148                 {
149                     jScrollPane1 = new JScrollPane();
150                     getContentPane().add(jScrollPane1);
151                     jScrollPane1.setBounds(12, 12, 592, 75);
152                     {
153                         jTxtHint = new JTextPane();
154                         jScrollPane1.setViewportView(jTxtHint);
155                         jTxtHint.setBackground(GenexGUI.COMB1_BACKGROUND_COLOR);
156                         jTxtHint.setForeground(GenexGUI.COMB1_FOREGROUND_COLOR);
157                         jTxtHint.setEditable(false);
158                         hint("Info: Mô\u017eete prida\u0165 aj viacero obrázkov nazaz pou\u017eitím klávesov shift, control a my\u0161i.");
159                     }
160                 }
161                 {
162                     jSeparator1 = new JSeparator();
163                     getContentPane().add(jSeparator1);
164                     jSeparator1.setBounds(12, 93, 592, 10);
165                 }
166                 {
167                     jScrPreview = new JScrollPane();
168                     getContentPane().add(jScrPreview);
169                     jScrPreview.setBounds(309, 123, 295, 218);
170                     {
171                         jPreview = new PictureComponent();
172                         jPreview.setMinimumSize(new Dimension(jScrPreview
173                                 .getSize().width - 3,
174                                 jScrPreview.getSize().height - 3));
175                         jScrPreview.setViewportView(jPreview);
176                     }
177                 }
178                 {
179                     jLabel2 = new JLabel();
180                     getContentPane().add(jLabel2);
181                     jLabel2.setText("Náh\u013ead");
182                     jLabel2.setBounds(309, 103, 75, 14);
183                     jLabel2.setForeground(new java.awt.Color(100, 0, 0));
184                 }
185                 {
186                     jBtnAddPicture = new JButton();
187                     getContentPane().add(jBtnAddPicture);
188                     jBtnAddPicture.setText("Zo súboru");
189                     jBtnAddPicture.setBounds(194, 309, 109, 21);
190                     jBtnAddPicture.setMnemonic(java.awt.event.KeyEvent.VK_Z);
191                     jBtnAddPicture.addActionListener(new ActionListener() {
192                         public void actionPerformed(ActionEvent evt) {
193                             jBtnAddPictureActionPerformed(evt);
194                         }
195                     });
196                 }
197                 {
198                     jChbResize = new JCheckBox();
199                     getContentPane().add(jChbResize);
200                     jChbResize.setText("v okne");
201                     jChbResize.setBounds(508, 345, 96, 18);
202                     jChbResize.setForeground(new java.awt.Color(100, 0, 0));
203                     jChbResize.setMnemonic(java.awt.event.KeyEvent.VK_K);
204                     jChbResize.setSelected(true);
205                     jChbResize.setHorizontalTextPosition(SwingConstants.LEFT);
206                     jChbResize.setHorizontalAlignment(SwingConstants.RIGHT);
207                     jChbResize.addChangeListener(new ChangeListener() {
208                         public void stateChanged(ChangeEvent evt) {
209                             jChbResizeStateChanged(evt);
210                         }
211                     });
212                 }
213                 {
214                     jSeparator2 = new JSeparator();
215                     getContentPane().add(jSeparator2);
216                     jSeparator2.setBounds(14, 432, 592, 10);
217                 }
218                 {
219                     jBtnCancel = new JButton();
220                     getContentPane().add(jBtnCancel);
221                     jBtnCancel.setText("Cancel");
222                     jBtnCancel.setBounds(515, 443, 92, 21);
223                     jBtnCancel.setMnemonic(java.awt.event.KeyEvent.VK_C);
224                     jBtnCancel.addActionListener(new ActionListener() {
225                         public void actionPerformed(ActionEvent evt) {
226                             jBtnCancelActionPerformed(evt);
227                         }
228                     });
229                 }
230                 {
231                     jBtnOK = new JButton();
232                     getContentPane().add(jBtnOK);
233                     jBtnOK.setText("OK");
234                     jBtnOK.setBounds(418, 443, 92, 21);
235                     jBtnOK.setMnemonic(java.awt.event.KeyEvent.VK_O);
236                     jBtnOK.addActionListener(new ActionListener() {
237                         public void actionPerformed(ActionEvent evt) {
238                             jBtnOKActionPerformed(evt);
239                         }
240                     });
241                 }
242                 {
243                     jLabel3 = new JLabel();
244                     getContentPane().add(jLabel3);
245                     jLabel3.setText("Popis");
246                     jLabel3.setBounds(12, 321, 109, 14);
247                     jLabel3.setForeground(new java.awt.Color(100, 0, 0));
248                 }
249                 {
250                     jScrollPane2 = new JScrollPane();
251                     getContentPane().add(jScrollPane2);
252                     jScrollPane2.setBounds(12, 341, 291, 79);
253                     {
254                         jTxtDesciption = new JTextArea();
255                         jScrollPane2.setViewportView(jTxtDesciption);
256                         jTxtDesciption.setBackground(new java.awt.Color(255,
257                                 255, 240));
258                         jTxtDesciption.setForeground(new java.awt.Color(0, 0,
259                                 255));
260                         jTxtDesciption.setPreferredSize(new java.awt.Dimension(
261                                 288, 27));
262                     }
263                 }
264                 {
265                     jLabel4 = new JLabel();
266                     getContentPane().add(jLabel4);
267                     jLabel4.setText("Parametre zobrazenia");
268                     jLabel4.setBounds(311, 353, 222, 14);
269                     jLabel4.setForeground(new java.awt.Color(100, 0, 0));
270                 }
271                 {
272                     jTxtSettings = new JTextField();
273                     getContentPane().add(jTxtSettings);
274                     jTxtSettings.setBounds(311, 373, 293, 21);
275                     jTxtSettings
276                             .setBackground(new java.awt.Color(255, 255, 240));
277                     jTxtSettings.setForeground(new java.awt.Color(0, 0, 255));
278                 }
279                 {
280                     jLabel5 = new JLabel();
281                     getContentPane().add(jLabel5);
282                     jLabel5.setText("MIME typ");
283                     jLabel5.setBounds(311, 406, 107, 14);
284                     jLabel5.setForeground(new java.awt.Color(100, 0, 0));
285                 }
286                 {
287                     jTxtMIME = new JTextField();
288                     getContentPane().add(jTxtMIME);
289                     jTxtMIME.setBounds(430, 403, 174, 21);
290                     jTxtMIME.setBackground(new java.awt.Color(255, 255, 240));
291                     jTxtMIME.setForeground(new java.awt.Color(0, 0, 255));
292                     jTxtMIME.setEditable(false);
293                 }
294             }
295             this.setSize(628, 508);
296         } catch (Exception e) {
297             e.printStackTrace();
298         }
299     }
300 
301     public Picture[] showDialog() {
302         setModalityType(ModalityType.DOCUMENT_MODAL);
303         setVisible(true);
304         return ret;
305     }
306 
307     private void jBtnCancelActionPerformed(ActionEvent evt) {
308         jLstPicturesValueChanged(null);
309         ret = null;
310         setVisible(false);
311     }
312 
313     private void jBtnOKActionPerformed(ActionEvent evt) {
314         jLstPicturesValueChanged(null);
315         int[] indices = jLstPictures.getSelectedIndices();
316         ret = new Picture[indices.length];
317         for (int i = 0; i < ret.length; i++)
318             ret[i] = (Picture) pictures.get(indices[i]);
319 
320         setVisible(false);
321     }
322 
323     private void jChbResizeStateChanged(ChangeEvent evt) {
324         jPreview.setScaled(jChbResize.isSelected());
325         jScrPreview.setViewportView(jPreview);
326     }
327 
328     private void jBtnAddPictureActionPerformed(ActionEvent evt) {
329         JFileChooser jfc = new JFileChooser(".");
330         jfc.setFileFilter(new FileNameExtensionFilter("Podporované formáty",
331                 "jpg", "jpeg", "bmp", "gif", "png"));
332         int state = jfc.showOpenDialog(this);
333         if (state == JFileChooser.CANCEL_OPTION)
334             return;
335 
336         File f = jfc.getSelectedFile();
337         if (f == null)
338             return;
339 
340         byte[] data = null;
341         try {
342             data = read(f.getAbsolutePath());
343         } catch (Exception e) {
344             e.printStackTrace();
345             return;
346         }
347 
348         String name = f.getName();
349         StringBuilder newName = new StringBuilder();
350 
351         for (int i = 0; i < name.lastIndexOf("."); i++) {
352 
353             if (((97 <= name.charAt(i)) && (name.charAt(i) <= 122))
354                     || ((65 <= name.charAt(i)) && (name.charAt(i) <= 90))
355                     || Character.isDigit(name.charAt(i)))
356                 newName.append(name.charAt(i));
357             else
358                 newName.append("-");
359 
360         }
361         for (int i = name.lastIndexOf("."); i < name.length(); i++)
362             newName.append(name.charAt(i));
363         String nazov = newName.toString();
364 
365         int i = 1;
366         StringBuilder name2 = new StringBuilder();
367         name2.append(nazov);
368         try {
369             while (pDao.existsPictureByName(name2.toString())) {
370                 System.out.println("som tu");
371                 name2 = new StringBuilder();
372                 name2.append(nazov);
373                 name2.insert(name2.lastIndexOf("."), i);
374 
375                 i++;
376             }
377         } catch (SQLException e1) {
378             e1.printStackTrace();
379         }
380 
381         Picture p = new Picture(name2.toString(), "", "",
382                 new MimetypesFileTypeMap(getClass().getResourceAsStream(
383                         "/mime.types")).getContentType(f.getAbsolutePath()),
384                 data, null);
385         pictures.addElement(p);
386         //TODO
387         try {
388             pDao.addPicture(p);
389 
390         } catch (Exception e) {
391             e.printStackTrace();
392         }
393 
394     }
395 
396     private void jLstPicturesValueChanged(ListSelectionEvent evt) {
397         if (lastSel != null) {
398             lastSel.setDescription(jTxtDesciption.getText());
399             lastSel.setSettings(jTxtSettings.getText());
400             try {
401                 pDao.updatePicture(lastSel);
402             } catch (SQLException e) {
403                 hint("Error: " + e.getMessage());
404             }
405         }
406         Picture sel = (Picture) jLstPictures.getSelectedValue();
407         if (sel == null)
408             return;
409         lastSel = sel;
410         jPreview.setImage(sel.getContent());
411         jScrPreview.setViewportView(jPreview);
412         jTxtDesciption.setText(sel.getDescription());
413         jTxtSettings.setText(sel.getSettings());
414         jTxtMIME.setText(sel.getMimeType());
415     }
416 
417     private void hint(String text) {
418         if (jTxtHint.getBackground() == GenexGUI.COMB1_BACKGROUND_COLOR) {
419             jTxtHint.setBackground(GenexGUI.COMB2_BACKGROUND_COLOR);
420             jTxtHint.setForeground(GenexGUI.COMB2_FOREGROUND_COLOR);
421         } else {
422             jTxtHint.setBackground(GenexGUI.COMB1_BACKGROUND_COLOR);
423             jTxtHint.setForeground(GenexGUI.COMB1_FOREGROUND_COLOR);
424         }
425         if (text.startsWith("Error:")) {
426             jTxtHint.setText(text.substring(6));
427             jTxtHint.setCaretPosition(0);
428             jTxtHint.insertIcon(new ImageIcon(resHelp
429                     .getResourceBytes("/icons/error.png")));
430         } else if (text.startsWith("Warning:")) {
431             jTxtHint.setText(text.substring(8));
432             jTxtHint.setCaretPosition(0);
433             jTxtHint.insertIcon(new ImageIcon(resHelp
434                     .getResourceBytes("/icons/warning.png")));
435         } else if (text.startsWith("Info:")) {
436             jTxtHint.setText(text.substring(5));
437             jTxtHint.setCaretPosition(0);
438             jTxtHint.insertIcon(new ImageIcon(resHelp
439                     .getResourceBytes("/icons/info.png")));
440         } else {
441             jTxtHint.setText(text);
442         }
443     }
444 
445     public byte[] read(String file) throws Exception {
446         FileInputStream fis = new FileInputStream(file);
447         FileChannel fc = fis.getChannel();
448         byte[] data = new byte[(int) fc.size()];
449         ByteBuffer bb = ByteBuffer.wrap(data);
450         fc.read(bb);
451         return data;
452     }
453 
454 }