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.Dimension;
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  import java.io.File;
25  import java.util.Iterator;
26  
27  import javax.swing.BoxLayout;
28  import javax.swing.DefaultListModel;
29  import javax.swing.JButton;
30  import javax.swing.JFileChooser;
31  import javax.swing.JLabel;
32  import javax.swing.JList;
33  import javax.swing.JPanel;
34  import javax.swing.JScrollPane;
35  import javax.swing.JTextField;
36  import javax.swing.SpringLayout;
37  
38  import net.sf.jameleon.util.Configurator;
39  import net.sf.jameleon.util.JameleonDefaultValues;
40  
41  public class UIConfigPanel extends JPanel{
42  
43      private JTextField scriptDirF = new JTextField();
44      private DefaultListModel cpEntriesM = new DefaultListModel();
45      private JList cpEntriesF = new JList(cpEntriesM);
46  
47      public UIConfigPanel(){
48          super(new SpringLayout());
49          init();
50      }
51  
52      public void init(){
53          JLabel jl = new JLabel("Scripts Directory:");
54          add(jl);
55          scriptDirF.setMaximumSize(new Dimension(Integer.MAX_VALUE, 5));
56          jl.setLabelFor(scriptDirF);
57          scriptDirF.setToolTipText("<html>The directory containing the test scripts</html>");
58          
59          add(scriptDirF);
60          scriptDirF.setText(Configurator.getInstance().getValue(JameleonDefaultValues.SCRIPT_DIR_CONFIG_NAME,
61                                                                  JameleonDefaultValues.SCRIPT_DIR.getPath()));
62          add(new JLabel("Classpath:"));
63          add(new JLabel(""));
64          JPanel cpPanel = new JPanel();
65          cpPanel.setLayout(new BoxLayout(cpPanel, BoxLayout.Y_AXIS));
66          populateClassPathEntries();
67          JScrollPane scroll = new JScrollPane(cpEntriesF);
68          cpPanel.add(scroll);
69          add(cpPanel);
70          setUpButtons();
71  
72          SpringUtilities.makeCompactGrid(this,
73                                          3, 2,   //rows, cols
74                                          6, 6,   //initX, initY
75                                          6, 6);  //xPad, yPad
76      }
77  
78      private void setUpButtons(){
79          JPanel buttonsPanel = new JPanel();
80          buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.Y_AXIS));
81          JButton add = new JButton("Add");
82          final UIConfigPanel owner = this;
83          add.addActionListener(new ActionListener(){
84                  public void actionPerformed(ActionEvent e) {
85                      JFileChooser fileChooser = new JFileChooser(".");
86                      fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
87                      
88                      int returnVal = fileChooser.showDialog(owner, "Choose File/Directory");
89                      if (returnVal == JFileChooser.APPROVE_OPTION) {
90                          File f = fileChooser.getSelectedFile();
91                          if (f.exists()) {
92                              String absolutePath = f.getAbsolutePath();
93                              String relativePath = new File(".").getAbsolutePath();
94                              if (relativePath.endsWith(".")) {
95                                  relativePath = relativePath.substring(0,relativePath.length()-2);
96                              }
97                              if (absolutePath.startsWith(relativePath) && 
98                                  absolutePath.length() > relativePath.length()) {
99                                  relativePath = absolutePath.substring(relativePath.length()+1);
100                             }else{
101                                 relativePath = absolutePath;
102                             }
103                             if (!cpEntriesM.contains(relativePath)) {
104                                 cpEntriesM.addElement(relativePath.replaceAll("////", "/"));
105                             }
106                         }
107                     }
108                 }
109                 });
110         JButton remove = new JButton("Remove");
111         remove.addActionListener(new ActionListener(){
112                 public void actionPerformed(ActionEvent e) {
113                     int[] indices = cpEntriesF.getSelectedIndices();
114                     for (int i = indices.length - 1; i >= 0; i--) {
115                         if (indices[i] > -1) {
116                             cpEntriesM.remove(indices[i]);
117                         }
118                     }
119                 }
120                 });
121         buttonsPanel.add(add);
122         buttonsPanel.add(remove);
123         add(buttonsPanel);
124     }
125 
126     private void populateClassPathEntries(){
127         Configurator config = Configurator.getInstance();
128         Iterator it = config.getKeysStartingWith(JameleonDefaultValues.ENTRIES_CONFIG_NAME).iterator();
129         while (it.hasNext()) {
130             cpEntriesM.addElement(config.getValue((String)it.next()));
131         }
132     }
133 
134     protected void updateProperties(){
135         String scriptDir = scriptDirF.getText();
136         Configurator config = Configurator.getInstance();
137         if (JameleonDefaultValues.SCRIPT_DIR.getPath().equals(scriptDir)) {
138             scriptDir = null;
139         }
140         config.setValue(JameleonDefaultValues.SCRIPT_DIR_CONFIG_NAME, scriptDir);
141         Iterator it = config.getKeysStartingWith(JameleonDefaultValues.ENTRIES_CONFIG_NAME).iterator();
142         while (it.hasNext()) {
143             config.setValue((String)it.next(), null);
144         }
145         int size = cpEntriesM.size();
146         for (int i = 0; i < size; i++) {
147             config.setValue(JameleonDefaultValues.ENTRIES_CONFIG_NAME+padNumber(i+1, size), (String)cpEntriesM.get(i));
148         }
149     }
150 
151     protected String padNumber(int num, int maxNum){
152         String numS = ""+num;
153         String maxNumS = ""+maxNum;
154         int numOfZeros = maxNumS.length() - numS.length();
155         for (int i = 0; i < numOfZeros; i++) {
156             numS = 0 + numS;
157         }
158         return numS;
159     }
160 
161 }