1 package com.jbidwatcher.ui;
2 /*
3  * Copyright (c) 2000-2007, CyberFOX Software, Inc. All Rights Reserved.
4  *
5  * Developed by mrs (Morgan Schweers)
6  */
7 
8 import com.cyberfox.util.platform.Path;
9 import com.jbidwatcher.util.config.JConfig;
10 import com.jbidwatcher.util.queue.MQFactory;
11 import com.jbidwatcher.search.Searcher;
12 import com.jbidwatcher.util.Constants;
13 import com.jbidwatcher.ui.table.TableSorter;
14 import com.jbidwatcher.search.SearchManager;
15 
16 import javax.swing.*;
17 import javax.swing.event.*;
18 import java.awt.*;
19 import java.awt.event.*;
20 import java.util.ArrayList;
21 
22 public class JSearchContext extends JBidTableContext {
23   private static SearchInfoDialog _searchDetail = null;
24 
addMenu(JPopupMenu p, String name, String cmd)25   private void addMenu(JPopupMenu p, String name, String cmd) {
26     p.add(makeMenuItem(name, cmd)).addActionListener(this);
27   }
28 
addMenu(JPopupMenu p, String name)29   private void addMenu(JPopupMenu p, String name) {
30     p.add(makeMenuItem(name)).addActionListener(this);
31   }
32 
internalDoubleClick(MouseEvent e)33   protected void internalDoubleClick(MouseEvent e) {
34     if(!(e.getComponent() instanceof JComponent)) return;
35     JComponent inComponent = (JComponent) e.getComponent();
36 
37     if(inComponent instanceof JTable) {
38       JTable thisTable = (JTable) inComponent;
39       int rowPoint = thisTable.rowAtPoint(new Point(e.getX(), e.getY()));
40       Searcher whichSearch = (Searcher) thisTable.getValueAt(rowPoint, -1);
41 
42       whichSearch.execute();
43     }
44   }
45 
actionPerformed(ActionEvent ae)46   public void actionPerformed(ActionEvent ae) {
47     String actionString = ae.getActionCommand();
48     Searcher whichSearch = null;
49     if(actionString.startsWith(Constants.NO_CONTEXT)) {
50       actionString = actionString.substring(Constants.NO_CONTEXT.length());
51     } else {
52       if(_inTable != null) {
53         int rowPoint = _inTable.rowAtPoint(new Point(getPopupX(), getPopupY()));
54         whichSearch = (Searcher)_inTable.getValueAt(rowPoint, -1);
55       }
56     }
57 
58     DoAction(actionString, whichSearch);
59   }
60 
constructTablePopup()61   protected final JPopupMenu constructTablePopup() {
62     JPopupMenu pop = new JPopupMenu();
63 
64     addMenu(pop, "Do Search", "Execute");
65     addMenu(pop, "Load Searches");
66     addMenu(pop, "Edit Search");
67     pop.add(new JPopupMenu.Separator());
68     addMenu(pop, "Enable");
69     addMenu(pop, "Disable");
70     pop.add(new JPopupMenu.Separator());
71     addMenu(pop, "Delete");
72 
73     return pop;
74   }
75 
changeTable()76   private void changeTable() {
77     TableSorter tm = (TableSorter)_inTable.getModel();
78 
79     tm.tableChanged(new TableModelEvent(tm));
80   }
81 
handleSave(boolean success)82   private void handleSave(boolean success) {
83     if(success) {
84       JOptionPane.showMessageDialog(null, "Searches Saved!", "Save Complete", JOptionPane.INFORMATION_MESSAGE);
85     } else {
86       String saveFile = JConfig.queryConfiguration("search.savefile", "searches.xml");
87       saveFile = Path.getCanonicalFile(saveFile, "jbidwatcher", false);
88 
89       MQFactory.getConcrete("Swing").enqueue("ERROR Failed to save searches.  Check that the directory for\n" + saveFile + " exists, and is writable.");
90     }
91   }
92 
93   private static final int EXECUTE=0, ENABLE=1, DISABLE=2, EDIT=3, NEW=4;
94 
showEdit(Searcher s)95   private void showEdit(Searcher s) {
96     if(_searchDetail == null) {
97       _searchDetail = new SearchInfoDialog();
98     }
99     _searchDetail.prepare(s);
100     _searchDetail.pack();
101     _searchDetail.setVisible(true);
102     changeTable();
103   }
104 
doSingle(Searcher s, int cmd)105   private void doSingle(Searcher s, int cmd) {
106     switch(cmd) {
107       case EXECUTE:
108         s.execute();
109         break;
110       case ENABLE:
111         s.enable();
112         break;
113       case DISABLE:
114         s.disable();
115         break;
116       case EDIT:
117         showEdit(s);
118         break;
119       case NEW:
120         showEdit(s);
121         break;
122       default:
123         break;
124     }
125   }
126 
doCommand(Searcher s, int cmd, String no_items_msg)127   private void doCommand(Searcher s, int cmd, String no_items_msg) {
128     int[] rows = getPossibleRows();
129 
130     if(rows.length == 0 && s != null) {
131       doSingle(s, cmd);
132     } else {
133       if(rows.length == 0) {
134         JOptionPane.showMessageDialog(_inTable, no_items_msg, "No search(es) chosen", JOptionPane.INFORMATION_MESSAGE);
135       } else {
136         for (int row : rows) {
137           doSingle((Searcher) _inTable.getValueAt(row, -1), cmd);
138         }
139       }
140     }
141     changeTable();
142   }
143 
DoExecute(Searcher s)144   private void DoExecute(Searcher s) {
145     doCommand(s, EXECUTE, "You must select at least one search to execute first.");
146   }
147 
DoEnable(Searcher s)148   private void DoEnable(Searcher s) {
149     doCommand(s, ENABLE, "You must select at least one search to enable first.");
150   }
151 
DoDisable(Searcher s)152   private void DoDisable(Searcher s) {
153     doCommand(s, DISABLE, "You must select at least one search to disable first.");
154   }
155 
DoEdit(Searcher s)156   private void DoEdit(Searcher s) {
157     doCommand(s, EDIT, "You must select at least one search to edit first.");
158   }
159 
DoNew()160   private void DoNew() {
161     doSingle(null, NEW);
162   }
163 
confirmDeletion(Component src, String prompt)164   protected boolean confirmDeletion(Component src, String prompt) {
165     int endResult = JOptionPane.showOptionDialog(src, prompt,
166         "Confirm", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
167         null, null, null);
168 
169     return !(endResult == JOptionPane.CANCEL_OPTION ||
170         endResult == JOptionPane.CLOSED_OPTION);
171   }
172 
DoDelete(Searcher chosenSearch)173   private void DoDelete(Searcher chosenSearch) {
174     Searcher s = chosenSearch;
175     int[] rows = getPossibleRows();
176     String prompt;
177 
178     if( (rows.length <= 1) && s != null) {
179       if(rows.length == 1) s = (Searcher)_inTable.getValueAt(rows[0], -1);
180 
181       prompt = "<HTML><BODY>Are you sure you want to remove this search?<br><b>" + s.getName() + "</b></body></html>";
182       //  Use the right parent!  FIXME -- mrs: 17-February-2003 23:53
183       if(confirmDeletion(null, prompt)) {
184         SearchManager.getInstance().deleteSearch(s);
185       }
186     } else {
187       if(rows.length == 0) {
188         JOptionPane.showMessageDialog(_inTable, "You must select what searches to delete first.", "No search", JOptionPane.INFORMATION_MESSAGE);
189       } else {
190         prompt = "Are you sure you want to remove all selected searches?";
191         //  Use the right parent!  FIXME -- mrs: 17-February-2003 23:53
192         if(confirmDeletion(null, prompt)) {
193           ArrayList<Searcher> delList = new ArrayList<Searcher>();
194           for (int row : rows) {
195             s = (Searcher) _inTable.getValueAt(row, -1);
196             delList.add(s);
197           }
198           for (Searcher del : delList) {
199             SearchManager.getInstance().deleteSearch(del);
200           }
201         }
202       }
203     }
204     changeTable();
205   }
206 
DoAction(String cmd, Object param)207   public void DoAction(String cmd, Object param) {
208     Searcher search = (Searcher)param;
209 
210     if(cmd.equals("Execute")) DoExecute(search);
211     else if(cmd.equals("Edit Search")) DoEdit(search);
212     else if(cmd.equals("New")) DoNew();
213     else if(cmd.equals("Delete")) DoDelete(search);
214     else if(cmd.equals("Enable")) DoEnable(search);
215     else if(cmd.equals("Disable")) DoDisable(search);
216     else if(cmd.equals("Save All")) handleSave(SearchManager.getInstance().saveSearches());
217     else if(cmd.equals("Load Searches")) { SearchManager.getInstance().loadSearches(); changeTable(); }
218     else System.out.println("Cannot figure out what '" + cmd + "'.");
219   }
220 
JSearchContext()221   public JSearchContext() {
222     localPopup = constructTablePopup();
223   }
224 
DoAction(Object src, String actionString, Object whichEntry)225   protected void DoAction(Object src, String actionString, Object whichEntry) {
226     DoAction(actionString, whichEntry);
227   }
228 }
229