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.*;
30  import java.util.*;
31  import javax.swing.*;
32  
33  
34  /***
35   * A dialog that allows the user to select an item from the list of recently
36   * used search patterns.
37   * 
38   * @author  Murali Krishnan
39   *
40   */
41  public class RecentPatternSelector {
42  
43      //------------------------------------------------------------
44      //- Class Variables
45  
46  
47  
48      //------------------------------------------------------------
49      //- Class Functions
50  
51  
52  
53      //------------------------------------------------------------
54      //- Instance Variables
55  
56      private App _app = null;
57      private JDialog _dialog = null;
58      private JList _list = new JList();
59      private JButton _buttonOk = new JButton("Select");
60      private JButton _buttonCancel = new JButton("Cancel");
61  
62  
63      //------------------------------------------------------------
64      //- Constructors
65  
66      RecentPatternSelector(App pApp) {
67          _app = pApp;
68  
69          initialize();
70      }
71  
72  
73      //------------------------------------------------------------
74      //- Accessors
75  
76      protected App getApp() {return _app;}
77      protected JDialog getDialog() {return _dialog;}
78      protected JList getList() {return _list;}
79      protected JButton getButtonOk() {return _buttonOk;}
80      protected JButton getButtonCancel() {return _buttonCancel;}
81  
82  
83      //------------------------------------------------------------
84      //- Settors
85  
86      protected void setDialog(JDialog val) {_dialog=val;}
87  
88  
89      //------------------------------------------------------------
90      //- Private/Protected Utility Functions
91  
92      public void initialize() {
93          createComponents();
94          defineActions();
95          makeFrame();
96      }
97  
98  
99      public void createComponents() {
100         getList().setListData(new Vector(getApp().getRecentSearches()));
101         getList().setBorder(BorderFactory.createTitledBorder("Recent searches:"));
102 
103         String title = "Recent Search Patterns";
104         _dialog = new JDialog(getApp().getFrame(), title, true);
105     }
106 
107 
108     public void defineActions() {
109         getButtonOk().addActionListener(new ActionListener() {
110                 public void actionPerformed (ActionEvent e) {
111                     handleItemSelection();
112                 }
113             });
114 
115         getButtonCancel().addActionListener(new ActionListener() {
116                 public void actionPerformed (ActionEvent e) {
117                     getDialog().dispose();
118                 }
119             });
120 
121 
122         getList().addMouseListener(new MouseAdapter() {
123                 public void mouseClicked(MouseEvent e) {
124                     if (e.getClickCount() == 2) {
125                         handleItemSelection();
126                     }
127                 }
128             });
129     }
130 
131 
132     public void makeFrame() {
133       getDialog().getContentPane().setLayout(new BorderLayout());
134 
135       getDialog().getContentPane().add(makeMainPanel(), BorderLayout.CENTER);
136       getDialog().getContentPane().add(makeButtonPanel(), BorderLayout.SOUTH);
137 
138       getDialog().pack();
139       Point location = getApp().getFrame().getLocation();
140       Point buttonLocation = getApp().findButtonSelectPattern().getLocation();
141       location.translate(buttonLocation.x, 50);
142       getDialog().setLocation(location);
143       getDialog().setVisible(true);
144     }
145 
146 
147     protected JPanel makeMainPanel() {
148         JPanel result = new JPanel(new BorderLayout());
149 
150         result.add(getList(), BorderLayout.CENTER);
151 
152         return result;
153     }
154 
155 
156     protected JPanel makeButtonPanel() {
157         JPanel result = new JPanel(new BorderLayout());
158 
159         ArrayList<JButton> buttons = new ArrayList<JButton>();
160         {
161             buttons.add(getButtonOk());
162             buttons.add(getButtonCancel());
163         }
164 
165         result.add(App.wrapButtons(buttons), BorderLayout.EAST);
166 
167         return result;
168     }
169 
170 
171     //------------------------------------------------------------
172     //- Protected Action Handling
173 
174     protected void handleItemSelection() {
175         String selection = (String) getList().getSelectedValue();
176         if (selection == null) {
177             System.err.println("Nothing selected.");
178         } else {
179             getApp().setSearchText(selection);
180             JTextField field = getApp().findSearchField();
181             field.grabFocus();
182             getDialog().dispose();
183         }
184     }
185 
186 
187     //------------------------------------------------------------
188     //- Public Interface Functions
189 
190 
191 
192     //------------------------------------------------------------
193     //- Class Interface Functions
194 
195 
196 
197     //------------------------------------------------------------
198     //- Inner Classes
199 
200 
201 
202     //------------------------------------------------------------
203     //- Main
204 
205 
206 
207 }