View Javadoc

1   /*
2    * Fsgrep is a simple Java application which allows a user to
3    * search all files in a directory structure for lines matching
4    * a given pattern.  Its functionality is a combination of the
5    * Unix 'find' and 'grep' utilities.
6    * Visit [http://fsgrep.sourceforge.net/] for more information.
7    * 
8    * Copyright (C) 2003-2006 Murali Krishnan [murali_ca_us@users.sourceforge.net]
9    * 
10   * Fsgrep is free software; you can redistribute it and/or modify
11   * it under the terms of version 2 of the GNU General Public
12   * License as published by the Free Software Foundation.
13   * 
14   * Fsgrep is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public License
20   * along with Fsgrep (see the file named LICENSE.txt); if not, write
21   * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22   * Boston, MA  02111-1307  USA
23   */
24  
25  package mk.fsgrep.gui;
26  
27  
28  import java.awt.*;
29  import java.awt.event.ActionEvent;
30  import java.awt.event.ActionListener;
31  import java.io.File;
32  import java.util.*;
33  
34  import javax.swing.*;
35  
36  import mk.fsgrep.find.ScanProfile;
37  import mk.fsgrep.util.TargetFile;
38  
39  
40  /***
41   * A dialog for selecting new scan profiles.
42   * 
43   * @author  Murali Krishnan
44   *
45   */
46  public class ProfileSettings {
47  
48      //------------------------------------------------------------
49      //- Class Variables
50  
51  
52  
53      //------------------------------------------------------------
54      //- Class Functions
55  
56  
57  
58      //------------------------------------------------------------
59      //- Instance Variables
60  
61      private App _app = null;
62      private JDialog _dialog = null;
63      private JTextField _fieldDir = null;
64      private JTextField _fieldSuffix = null;
65      private JButton _buttonChoose = null;
66      private JButton _buttonOk = null;
67      private JButton _buttonCancel = null;
68  
69  
70  
71      //------------------------------------------------------------
72      //- Constructors
73  
74      ProfileSettings(App pApp) {
75          _app = pApp;
76  
77          createComponents();
78      }
79  
80  
81      //------------------------------------------------------------
82      //- Accessors
83  
84      protected App getApp() {return _app;}
85      protected JDialog getDialog() {return _dialog;}
86      protected JTextField getFieldDir() {return _fieldDir;}
87      protected JTextField getFieldSuffix() {return _fieldSuffix;}
88      protected JButton getButtonChoose() {return _buttonChoose;}
89      protected JButton getButtonOk() {return _buttonOk;}
90      protected JButton getButtonCancel() {return _buttonCancel;}
91  
92  
93  
94      //------------------------------------------------------------
95      //- Settors
96  
97  
98  
99      //------------------------------------------------------------
100     //- Private/Protected Utility Functions
101 
102     protected void createComponents() {
103         _dialog = new JDialog(getApp().getFrame(), "Scan Profile", true);
104 
105         ScanProfile profile = getApp().getModel().getFinder().getProfile();
106         TargetFile dir = new TargetFile(profile.getRoot());
107         _fieldDir = new JTextField(dir.getName(), 30);
108         _fieldSuffix = new JTextField(profile.getFilenameSpec(), 30);
109 
110         _buttonChoose = new JButton("Choose");
111         _buttonOk = new JButton("OK");
112         _buttonCancel = new JButton("Cancel");
113 
114         defineActions();
115         makeFrame();
116     }
117 
118 
119     protected void defineActions() {
120         getButtonChoose().addActionListener(new ActionListener() {
121                 public void actionPerformed (ActionEvent e) {
122                     useFileChooser();
123                 }
124             });
125 
126         getButtonOk().addActionListener(new ActionListener() {
127                 public void actionPerformed (ActionEvent e) {
128                     if (validateDirectory()) {
129                         useGivenValues();
130                     }
131                 }
132             });
133 
134         getButtonCancel().addActionListener(new ActionListener() {
135                 public void actionPerformed (ActionEvent e) {
136                     getDialog().dispose();
137                 }
138             });
139 
140         getFieldDir().addActionListener(new ActionListener() {
141                 public void actionPerformed (ActionEvent aEvent) {
142                     if (validateDirectory()) {
143                         getFieldSuffix().selectAll();
144                         getFieldSuffix().grabFocus();
145                     }
146                 }
147             });
148 
149         getFieldSuffix().addActionListener(new ActionListener() {
150                 public void actionPerformed (ActionEvent aEvent) {
151                     getButtonOk().grabFocus();
152                 }
153             });
154     }
155 
156 
157     protected boolean validateDirectory() {
158         File file = new File(getFieldDir().getText());
159         boolean result = file.isDirectory();
160 
161         if (!result) {
162             String title = "No such directory";
163             String content = "Directory [" + getFieldDir().getText() + "] does not exist.";
164             JOptionPane.showMessageDialog(getDialog(), content, title, JOptionPane.ERROR_MESSAGE);
165             getFieldDir().grabFocus();
166         }
167 
168         return result;
169     }
170 
171 
172     protected void makeFrame() {
173       getDialog().getContentPane().setLayout(new BorderLayout());
174 
175       getDialog().getContentPane().add(makeMainPanel(), BorderLayout.CENTER);
176       getDialog().getContentPane().add(makeButtonPanel(), BorderLayout.SOUTH);
177 
178       getDialog().pack();
179       getDialog().setLocation(30, 30);
180       getDialog().setVisible(true);
181 
182       getFieldDir().grabFocus();
183     }
184 
185 
186     protected JPanel makeMainPanel() {
187         JPanel result = new JPanel(new GridBagLayout());
188 
189         GridBagConstraints gbc = new GridBagConstraints();
190         gbc.anchor = GridBagConstraints.NORTH;
191         gbc.ipadx = 4;
192         gbc.ipady = 4;
193         gbc.insets = new Insets(2, 4, 2, 4);
194         gbc.fill = GridBagConstraints.HORIZONTAL;
195         gbc.gridx = 0;
196         gbc.gridy = 0;
197         gbc.weightx = 0;
198         gbc.weighty = 1;
199 
200         result.add(new JLabel("Top Directory:"), gbc);
201 
202         gbc.gridy = 1;
203         result.add(new JLabel("Suffix(es)*:"), gbc);
204 
205         gbc.gridx = 1;
206         gbc.gridy = 0;
207         gbc.weightx = 1;
208         result.add(getFieldDir(), gbc);
209 
210         gbc.gridy = 1;
211         result.add(getFieldSuffix(), gbc);
212 
213         gbc.gridx = 2;
214         gbc.gridy = 0;
215         gbc.weightx = 0;
216         gbc.fill = GridBagConstraints.NONE;
217         result.add(getButtonChoose(), gbc);
218 
219         return result;
220     }
221 
222 
223     protected JPanel makeButtonPanel() {
224         JPanel result = new JPanel(new BorderLayout());
225 
226         ArrayList<JButton> buttons = new ArrayList<JButton>();
227         {
228             buttons.add(getButtonOk());
229             buttons.add(getButtonCancel());
230         }
231 
232         result.add(App.wrapButtons(buttons), BorderLayout.EAST);
233         result.add(new JLabel("*(can be a comma-separated list, or * for all text files.)"), BorderLayout.WEST);
234 
235         return result;
236     }
237 
238 
239     protected void useFileChooser() {
240         JFileChooser chooser = new JFileChooser();
241         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
242         chooser.setFileHidingEnabled(false);
243 
244         File current = new File(getFieldDir().getText());
245         if (!current.isDirectory()) {
246             current = getApp().getModel().getFinder().getRoot();
247         }
248         chooser.setCurrentDirectory(current);
249 
250         int result = chooser.showDialog(getDialog(), "Select");
251         if (result == JFileChooser.APPROVE_OPTION) {
252             TargetFile selected = new TargetFile(chooser.getSelectedFile());
253             getFieldDir().setText(selected.getName());
254         }
255     }
256 
257 
258     protected void useGivenValues() {
259         getApp().handleProfileSettings(getFieldDir().getText(), getFieldSuffix().getText());
260 
261         getDialog().dispose();
262     }
263 
264 
265     //------------------------------------------------------------
266     //- Public Interface Functions
267 
268 
269 
270     //------------------------------------------------------------
271     //- Class Interface Functions
272 
273 
274 
275     //------------------------------------------------------------
276     //- Inner Classes
277 
278 
279 
280     //------------------------------------------------------------
281     //- Main
282 
283 
284 
285 }