View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2006 Christian W. Hargraves (engrean@hotmail.com)
4   
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.ui;
20  
21  import java.awt.FlowLayout;
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  import java.io.File;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.io.OutputStream;
28  import java.util.Properties;
29  
30  import javax.swing.JButton;
31  import javax.swing.JDialog;
32  import javax.swing.JFrame;
33  import javax.swing.JPanel;
34  import javax.swing.JTabbedPane;
35  
36  import net.sf.jameleon.util.Configurator;
37  
38  public class ConfigDialog extends JDialog{
39  	private static final long serialVersionUID = 1L;
40      protected JTabbedPane tabbedPane;
41      protected GeneralConfigPanel generalPanel;
42      protected EnvironmentConfigPanel envPanel;
43      protected UIConfigPanel uiConfigPanel;
44      protected static final String ENV_PANEL = "Environment Settings";
45      protected static final String GENERAL_PANEL = "General Settings";
46      protected static final String UI_PANEL = "UI Settings";
47  
48      public ConfigDialog(JFrame rootFrame){
49          super(rootFrame, "Jameleon Settings", true);
50          init();
51          setVisible(true);
52      }
53  
54      private void init(){
55          tabbedPane = new JTabbedPane(JTabbedPane.TOP);
56          generalPanel = new GeneralConfigPanel();
57          envPanel = new EnvironmentConfigPanel();
58          uiConfigPanel = new UIConfigPanel();
59          tabbedPane.add(ENV_PANEL, envPanel);
60          tabbedPane.add(GENERAL_PANEL, generalPanel);
61          tabbedPane.add(UI_PANEL, uiConfigPanel);
62          getContentPane().add(tabbedPane, "Center");
63  
64          JPanel p2 = new JPanel(new FlowLayout());
65          JButton ok = new JButton("Ok");
66          p2.add(ok);
67          JButton cancel = new JButton("Cancel");
68          p2.add(cancel);
69          getContentPane().add(p2, "South");
70      
71          ok.addActionListener(new ActionListener() {
72            public void actionPerformed(ActionEvent evt) {
73                envPanel.updateProperties();
74                generalPanel.updateProperties();
75                uiConfigPanel.updateProperties();
76                Configurator config = Configurator.getInstance();
77                OutputStream os = null;
78                try{
79                    Properties props = config.getProperties();
80                    File f = new File(config.getConfigName());
81                    if (!f.exists()) {
82                        f.createNewFile();
83                    }
84                    os = new FileOutputStream(f);
85                    props.store(os, null);
86                }catch(IOException ioe){
87                    ioe.printStackTrace();
88                }finally{
89                    if (os != null) {
90                        try{
91                            os.close();
92                        }catch(IOException ioe){
93                            //So what, we couldn't close the file?
94                        }
95                    }
96                }
97                Configurator.clearInstance();
98                setVisible(false);
99                dispose();
100           }
101         });
102         cancel.addActionListener(new ActionListener(){
103                 public void actionPerformed(ActionEvent e) {
104                     setVisible(false);
105                     dispose();
106                 }
107                 });
108         pack();
109         //setSize(400, 300);
110     }
111 
112 }