KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > search > DeleteFilterGUI


1 package com.ca.directory.jxplorer.search;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.*;
6 import java.util.logging.Logger JavaDoc;
7 import javax.swing.*;
8
9 import com.ca.directory.jxplorer.*;
10 import com.ca.commons.cbutil.*;
11
12 /**
13 * This class sets up a dialog that displays a list of saved filters (from search_filters.txt).
14 * It allows the user to delete one filter at a time. For each filter being deleted it checks if it
15 * is a subfilter of any other filter. If so it checks if that filter is a subfilter and so on.
16 * (A filter is unusable if a subfilter is deleted). After all of the dependant filters are determined,
17 * a prompt is displayed asking if they really want to delete this filter and all its dependant filters.
18 * @author Trudi.
19 */

20 public class DeleteFilterGUI extends CBDialog
21 {
22     SearchModel searchModel = new SearchModel();
23     JList list;
24     MainMenu mainMenu;
25     ArrayList filterNames;
26     JXplorer jxplorer = null;
27
28     private static Logger JavaDoc log = Logger.getLogger(DeleteFilterGUI.class.getName());
29
30    /**
31     * Constructor that sets up a dialog with a scrollable list which is used to display
32     * all of the filters that can be deleted.
33     * @param jxplorer JXplorer.
34     */

35     public DeleteFilterGUI(JXplorer jxplorer)
36     {
37         super(jxplorer, CBIntText.get("Delete Search Filter"), HelpIDs.SEARCH_DELETE_FILTER);
38         this.jxplorer = jxplorer;
39         mainMenu = jxplorer.getMainMenu();
40         
41         filterNames = searchModel.getFilterNames(SearchModel.ALLFILTERS);
42                 
43         display.addln(new JLabel(CBIntText.get("Select a Search Filter to Delete:")));
44
45         display.makeWide();
46         display.add(getScrollList(filterNames.toArray(), filterNames.toArray()));
47
48         CBButton btnDelete = new CBButton(CBIntText.get("Delete"), CBIntText.get("Delete the selected filters."));
49         btnDelete.addActionListener(new ActionListener(){
50                 public void actionPerformed(ActionEvent e){
51                     delete();
52         }});
53
54         // Clear out the buttons...
55
buttonPanel.removeAll();
56
57         // Add the buttons we need...
58
buttonPanel.add(btnDelete);
59         buttonPanel.add(Cancel);
60         buttonPanel.add(Help);
61
62         btnDelete.setToolTipText(CBIntText.get("Click here to delete the selected filter."));
63         Cancel.setToolTipText(CBIntText.get("Click here to exit."));
64         Help.setToolTipText(CBIntText.get("Click here for Help."));
65
66         setSize(300, 200);
67
68         CBUtility.center(this, jxplorer);
69     }
70
71    /**
72     * Sets up a scrollable list with given items and tooltips.
73     * @param items an array of things (usually strings that are added to the list).
74     * @param toolTips and array of strings used as the tooltips for the items (i.e. tooltip[x] goes with item[x]).
75     * @return the scroll pane with the list and listeners added.
76     */

77     protected JScrollPane getScrollList(Object JavaDoc[] items, Object JavaDoc[] toolTips)
78     {
79         final Object JavaDoc[] names = items;
80         final Object JavaDoc[] toolTps = toolTips;
81         
82         list = new JList(names)
83         {
84             public String JavaDoc getToolTipText(MouseEvent e) //TE: add a tool tip!
85
{
86                 int index = locationToIndex(e.getPoint());
87                 if (-1 < index)
88                     return toolTps[index].toString();
89                 else
90                     return null;
91             }
92         };
93         
94         list.setSelectionMode(0);
95         list.setSelectionModel(new CBSingleSelectionModel(list)); //TE: tries to ensure that the selected item is visible.
96
list.setToolTipText("");
97                 
98         JScrollPane sp = new JScrollPane(list);
99         sp.setPreferredSize(new Dimension(250, 100));
100         sp.setMinimumSize(new Dimension(250, 100));
101         sp.setAlignmentX(LEFT_ALIGNMENT);
102
103         return sp;
104     }
105
106    /**
107     * Gets the filter that the user has chosen to delete and displays a list of dependant filters.
108     * These dependant filters will have to be deleted as well or they will not work. It displays a
109     * list of these filters (via a JOptionPane message) so the user can decide if they really want
110     * to delete the filters. If they do the method goes ahead and deletes them and updates the
111     * search menu before exiting.
112     */

113     public void delete()
114     {
115         if(filterNames.size()<=0)
116             return;
117
118         if(list.getSelectedIndex() ==-1)
119         {
120             int response = JOptionPane.showConfirmDialog(this, CBIntText.get("No filter selected. Try again?"), CBIntText.get("No Filter Selected"), JOptionPane.YES_NO_OPTION);
121
122             if (response == JOptionPane.NO_OPTION) //TE: the user hasn't made a selection do they want to try again or exit the dialog?
123
{
124                 setVisible(false);
125                 dispose();
126                 return;
127             }
128             else
129             {
130                 return;
131             }
132         }
133             
134         Object JavaDoc value = list.getSelectedValue();
135         
136         int response;
137         
138         boolean text = false;
139         
140         if (searchModel.isTextFilter(value.toString()))
141             text = true;
142
143         if (!text)
144         {
145             ArrayList dependantFilters = searchModel.getDependants(value); //TE: get all of the filters that depend on the filter that the user wants to blow away.
146

147             String JavaDoc[] names = (String JavaDoc[])dependantFilters.toArray(new String JavaDoc[dependantFilters.size()]);
148             StringBuffer JavaDoc buffy = new StringBuffer JavaDoc();
149             
150             for(int i=1;i<names.length;i++) //TE: remove the 'JXFilter.' from the filter names.
151
{
152                 buffy.append(names[i].substring(names[i].indexOf(".")+1));
153                 buffy.append("\n");
154             }
155             
156             if (names.length==1) //TE: if there are no dependant filters...
157
{
158                 response = JOptionPane.showConfirmDialog(this, CBIntText.get("Are you sure you want to delete: ''{0}'' ?",
159                                 new String JavaDoc[] {value.toString()}),
160                                 CBIntText.get("Delete Information"), JOptionPane.YES_NO_OPTION);
161             }
162             else //TE: if there are dependand filters display them and wait for user confirmation before deleting them...
163
{
164                 response = JOptionPane.showConfirmDialog(this, CBIntText.get("Deleting ''{0}'' will also delete these dependant filters: ''{1}''\nDo you want to continue?\n\n",
165                                 new String JavaDoc[] {value.toString(), buffy.toString()}),
166                                 CBIntText.get("Delete Information"), JOptionPane.YES_NO_OPTION);
167             }
168         
169             if (response == JOptionPane.YES_OPTION)
170             {
171                 searchModel.removeFilters(names);
172             
173                 mainMenu.updateSearchMenu(); //TE: update the search menu.
174

175                 try
176                 {
177                     for(int i=0; i<names.length; i++)
178                     {
179                         filterNames.remove(names[i].substring(names[i].indexOf(".")+1));
180                         log.info("Deleted search filter: " + names[i].substring(names[i].indexOf(".")+1));
181                     }
182
183                     list.setListData(filterNames.toArray());
184                 }
185                 catch(Exception JavaDoc e)
186                 {
187                     log.warning("No selection to remove.");
188                 }
189             }
190         }
191         else
192         {
193             response = JOptionPane.showConfirmDialog(this, CBIntText.get("Are you sure you want to delete: ''{0}'' ?", new String JavaDoc[] {value.toString()}),
194                             CBIntText.get("Delete Information"), JOptionPane.YES_NO_OPTION);
195                     
196             if (response == JOptionPane.YES_OPTION)
197             {
198                 searchModel.removeFilter("JXTextFilter."+value.toString());
199             
200                 mainMenu.updateSearchMenu(); //TE: update the search menu.
201

202                 try
203                 {
204                     filterNames.remove(value.toString().substring(value.toString().indexOf(".")+1));
205                     log.info("Deleted search filter: " + value.toString().substring(value.toString().indexOf(".")+1));
206
207                     list.setListData(filterNames.toArray());
208                 }
209                 catch(Exception JavaDoc e)
210                 {
211                     log.warning("No selection to remove.");
212                 }
213             }
214         }
215
216         // Set the search GUI to null so that it is forced to re-read it's config so it gets updated filter list...
217
jxplorer.getTree().setSearchGUI(null);
218         jxplorer.getSearchTree().setSearchGUI(null);
219         jxplorer.getSchemaTree().setSearchGUI(null);
220     }
221 }
222
Popular Tags