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.io.File;
6   import java.io.FilenameFilter;
7   import java.io.IOException;
8   
9   import javax.swing.ComboBoxModel;
10  import javax.swing.DefaultComboBoxModel;
11  import javax.swing.ImageIcon;
12  import javax.swing.JButton;
13  import javax.swing.JCheckBox;
14  import javax.swing.JComboBox;
15  import javax.swing.JFileChooser;
16  import javax.swing.JFrame;
17  import javax.swing.JLabel;
18  import javax.swing.JScrollPane;
19  import javax.swing.JSeparator;
20  import javax.swing.JTextArea;
21  import javax.swing.JTextField;
22  import javax.swing.JTextPane;
23  import javax.swing.WindowConstants;
24  import javax.swing.filechooser.FileNameExtensionFilter;
25  
26  import sk.stuba.fiit.foo07.genex.beans.Test;
27  import sk.stuba.fiit.foo07.genex.common.ConnectionHelper;
28  import sk.stuba.fiit.foo07.genex.common.ResourceHelper;
29  import sk.stuba.fiit.foo07.genex.export.ExportLatex;
30  
31  /**
32   * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
33   * Builder, which is free for non-commercial use. If Jigloo is being used
34   * commercially (ie, by a corporation, company or business for any purpose
35   * whatever) then you should purchase a license for each developer using Jigloo.
36   * Please visit www.cloudgarden.com for details. Use of Jigloo implies
37   * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
38   * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
39   * ANY CORPORATE OR COMMERCIAL PURPOSE.
40   */
41  public class ExportTestDialog extends javax.swing.JDialog {
42      /**
43       * 
44       */
45      private static final long serialVersionUID = -8342991578393987116L;
46  
47      private Test toExport;
48  
49      private ResourceHelper resHelp;
50      private JScrollPane jSrcHint;
51      private JTextPane jTxtHint;
52      private JSeparator jSeparator2;
53      private JLabel jLabel4;
54      private JTextField jTxtSkupina;
55      private JButton jBtnOK;
56      private JButton jBtnCancel;
57      private JTextField jTxtDate;
58      private JLabel jLabel5;
59      private JComboBox jComboBox1;
60      private JCheckBox jChbPermutate;
61      private JButton jBtnFile;
62      private JLabel jLabel2;
63      private JTextArea jTxtInstructions;
64      private JScrollPane jSrcInstructions;
65      private JLabel jLabel3;
66      private JTextField jTxtName;
67      private JLabel jLabel1;
68      private JSeparator jSeparator1;
69      public boolean cancel = false;
70  
71      public ExportTestDialog(JFrame frame, Test toExport) {
72          super(frame);
73          resHelp = new ResourceHelper();
74          this.toExport = toExport;
75          initGUI();
76      }
77  
78      private void initGUI() {
79          try {
80              {
81                  this.setTitle("Exportova\u0165 test");
82                  this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
83                  this.setLocationByPlatform(true);
84                  this.setResizable(false);
85                  getContentPane().setLayout(null);
86                  {
87                      jSrcHint = new JScrollPane();
88                      getContentPane().add(jSrcHint);
89                      jSrcHint.setBounds(12, 12, 512, 75);
90                      {
91                          jTxtHint = new JTextPane();
92                          jSrcHint.setViewportView(jTxtHint);
93                          jTxtHint.setEditable(false);
94                          jTxtHint.setBackground(GenexGUI.COMB1_BACKGROUND_COLOR);
95                          jTxtHint.setForeground(GenexGUI.COMB1_FOREGROUND_COLOR);
96                      }
97                  }
98                  {
99                      jSeparator1 = new JSeparator();
100                     getContentPane().add(jSeparator1);
101                     jSeparator1.setBounds(12, 93, 512, 10);
102                 }
103                 {
104                     jSeparator2 = new JSeparator();
105                     getContentPane().add(jSeparator2);
106                     jSeparator2.setBounds(12, 314, 512, 10);
107                 }
108                 {
109                     jLabel1 = new JLabel();
110                     getContentPane().add(jLabel1);
111                     jLabel1.setText("Meno testu");
112                     jLabel1.setBounds(12, 109, 106, 14);
113                     jLabel1.setForeground(new java.awt.Color(100, 0, 0));
114                 }
115                 {
116                     jTxtName = new JTextField();
117                     getContentPane().add(jTxtName);
118                     jTxtName.setBounds(124, 106, 115, 21);
119                     jTxtName.setBackground(new java.awt.Color(255, 255, 240));
120                     jTxtName.setForeground(new java.awt.Color(0, 0, 255));
121                 }
122                 {
123                     jBtnFile = new JButton();
124                     getContentPane().add(jBtnFile);
125                     jBtnFile.setText("...");
126                     jBtnFile.setBounds(251, 106, 31, 21);
127                     jBtnFile.addActionListener(new ActionListener() {
128                         public void actionPerformed(ActionEvent evt) {
129                             jBtnFileActionPerformed(evt);
130                         }
131                     });
132                 }
133                 {
134                     jLabel2 = new JLabel();
135                     getContentPane().add(jLabel2);
136                     jLabel2.setText("\u0160ablóna");
137                     jLabel2.setBounds(303, 109, 106, 14);
138                     jLabel2.setForeground(new java.awt.Color(100, 0, 0));
139                 }
140                 {
141                     jLabel3 = new JLabel();
142                     getContentPane().add(jLabel3);
143                     jLabel3.setText("In\u0161trukcie k testu");
144                     jLabel3.setBounds(12, 136, 112, 14);
145                     jLabel3.setForeground(new java.awt.Color(100, 0, 0));
146                 }
147                 {
148                     jSrcInstructions = new JScrollPane();
149                     getContentPane().add(jSrcInstructions);
150                     jSrcInstructions.setBounds(12, 156, 512, 114);
151                     {
152                         jTxtInstructions = new JTextArea();
153                         jSrcInstructions.setViewportView(jTxtInstructions);
154                         jTxtInstructions.setBackground(new java.awt.Color(255,
155                                 255, 240));
156                         jTxtInstructions.setForeground(new java.awt.Color(0, 0,
157                                 255));
158                     }
159                 }
160                 {
161                     jChbPermutate = new JCheckBox();
162                     getContentPane().add(jChbPermutate);
163                     jChbPermutate
164                             .setText("Zamie\u0161a\u0165 poradie odpovedí");
165                     jChbPermutate.setBounds(12, 282, 181, 18);
166                     jChbPermutate.setForeground(new java.awt.Color(100, 0, 0));
167                 }
168                 {
169                     jLabel4 = new JLabel();
170                     getContentPane().add(jLabel4);
171                     jLabel4.setText("Skupina");
172                     jLabel4.setBounds(434, 284, 53, 14);
173                     jLabel4.setForeground(new java.awt.Color(100, 0, 0));
174                 }
175                 {
176                     jTxtSkupina = new JTextField();
177                     getContentPane().add(jTxtSkupina);
178                     jTxtSkupina.setBounds(493, 281, 31, 21);
179                     jTxtSkupina
180                             .setBackground(new java.awt.Color(255, 255, 240));
181                     jTxtSkupina.setForeground(new java.awt.Color(0, 0, 255));
182                 }
183                 {
184                     jLabel5 = new JLabel();
185                     getContentPane().add(jLabel5);
186                     jLabel5.setText("Dátum");
187                     jLabel5.setBounds(239, 284, 52, 14);
188                     jLabel5.setForeground(new java.awt.Color(100, 0, 0));
189                 }
190                 {
191                     jTxtDate = new JTextField();
192                     getContentPane().add(jTxtDate);
193                     jTxtDate.setBounds(303, 281, 82, 21);
194                     jTxtDate.setBackground(new java.awt.Color(255, 255, 240));
195                     jTxtDate.setForeground(new java.awt.Color(0, 0, 255));
196                 }
197                 {
198                     jBtnCancel = new JButton();
199                     getContentPane().add(jBtnCancel);
200                     jBtnCancel.setText("Cancel");
201                     jBtnCancel.setBounds(434, 330, 90, 21);
202                     jBtnCancel.setMnemonic(java.awt.event.KeyEvent.VK_C);
203                     jBtnCancel.addActionListener(new ActionListener() {
204                         public void actionPerformed(ActionEvent evt) {
205                             jBtnCancelActionPerformed(evt);
206                         }
207                     });
208                 }
209                 {
210                     jBtnOK = new JButton();
211                     getContentPane().add(jBtnOK);
212                     jBtnOK.setText("OK");
213                     jBtnOK.setBounds(338, 330, 90, 21);
214                     jBtnOK.setMnemonic(java.awt.event.KeyEvent.VK_O);
215                     jBtnOK.addActionListener(new ActionListener() {
216                         public void actionPerformed(ActionEvent evt) {
217                             jBtnOKActionPerformed(evt);
218                         }
219                     });
220                 }
221                 {
222                     FilenameFilter filter = new FilenameFilter() {
223                         public boolean accept(File dir, String name) {
224                             return !name.startsWith(".")
225                                     && name.endsWith(".vm");
226                         }
227                     };
228 
229                     jComboBox1 = new JComboBox();
230                     File templateDir = new File("templates");
231                     ComboBoxModel jComboBox1Model = new DefaultComboBoxModel(
232                             templateDir.list(filter));
233 
234                     getContentPane().add(jComboBox1);
235                     jComboBox1.setModel(jComboBox1Model);
236                     jComboBox1.setBounds(392, 105, 125, 22);
237                 }
238             }
239             this.setSize(544, 398);
240         } catch (Exception e) {
241             e.printStackTrace();
242         }
243     }
244 
245     private void jBtnFileActionPerformed(ActionEvent evt) {
246         JFileChooser jfc = new JFileChooser(".");
247         jfc.setFileFilter(new FileNameExtensionFilter("LaTeX súbory", "tex",
248                 "ltx"));
249         int state = jfc.showSaveDialog(this);
250         if (state == JFileChooser.CANCEL_OPTION)
251             return;
252 
253         File f = jfc.getSelectedFile();
254         if (f == null)
255             return;
256 
257         jTxtName.setText(f.getAbsolutePath());
258 
259     }
260 
261     private void jBtnOKActionPerformed(ActionEvent evt) {
262         if (jTxtName.getText().trim().length() == 0) {
263             hint("Warning: Názov testu je povinný údaj.");
264             return;
265         }
266 
267         ExportLatex EL = new ExportLatex();
268         EL.setGroup(jTxtSkupina.getText());
269         EL.setInstructions(jTxtInstructions.getText());
270         EL.setOutputFilename(jTxtName.getText());
271         EL.setPermutateAnswers(jChbPermutate.isEnabled());
272         EL.setTemplate((String) jComboBox1.getSelectedItem());
273         EL.setDate(jTxtDate.getText());
274         try {
275             System.out.print(toExport.getTestID());
276             EL.exportTest(toExport.getTestID(), ConnectionHelper
277                     .getConnection());
278         } catch (IOException e) {
279             e.printStackTrace();
280         }
281 
282         setVisible(false);
283     }
284 
285     private void hint(String text) {
286         if (jTxtHint.getBackground() == GenexGUI.COMB1_BACKGROUND_COLOR) {
287             jTxtHint.setBackground(GenexGUI.COMB2_BACKGROUND_COLOR);
288             jTxtHint.setForeground(GenexGUI.COMB2_FOREGROUND_COLOR);
289         } else {
290             jTxtHint.setBackground(GenexGUI.COMB1_BACKGROUND_COLOR);
291             jTxtHint.setForeground(GenexGUI.COMB1_FOREGROUND_COLOR);
292         }
293         if (text.startsWith("Error:")) {
294             jTxtHint.setText(text.substring(6));
295             jTxtHint.setCaretPosition(0);
296             jTxtHint.insertIcon(new ImageIcon(resHelp
297                     .getResourceBytes("/icons/error.png")));
298         } else if (text.startsWith("Warning:")) {
299             jTxtHint.setText(text.substring(8));
300             jTxtHint.setCaretPosition(0);
301             jTxtHint.insertIcon(new ImageIcon(resHelp
302                     .getResourceBytes("/icons/warning.png")));
303         } else if (text.startsWith("Info:")) {
304             jTxtHint.setText(text.substring(5));
305             jTxtHint.setCaretPosition(0);
306             jTxtHint.insertIcon(new ImageIcon(resHelp
307                     .getResourceBytes("/icons/info.png")));
308         } else {
309             jTxtHint.setText(text);
310         }
311     }
312 
313     private void jBtnCancelActionPerformed(ActionEvent evt) {
314         cancel = true;
315         setVisible(false);
316     }
317 
318     public void showDialog() {
319         this.setModalityType(ModalityType.DOCUMENT_MODAL);
320         setVisible(true);
321     }
322 
323 }