Source code: org/relayirc/swingui/ChannelSearchPanel.java
1
2 /*
3 * FILE: ChannelSearchPanel.java
4 *
5 * The contents of this file are subject to the Mozilla Public License
6 * Version 1.0 (the "License"); you may not use this file except in
7 * compliance with the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS"
11 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
12 * License for the specific language governing rights and limitations
13 * under the License.
14 *
15 * The Original Code is Relay IRC chat client.
16 *
17 * The Initial Developer of the Original Code is David M. Johnson.
18 * Portions created by David M. Johnson are Copyright (C) 1998.
19 * All Rights Reserved.
20 *
21 * Contributor(s): No contributors to this file.
22 */
23 package org.relayirc.swingui;
24 import org.relayirc.chatengine.*;
25 import org.relayirc.swingutil.*;
26
27 import java.awt.*;
28 import java.awt.event.*;
29 import javax.swing.*;
30 import javax.swing.border.*;
31 import javax.swing.table.*;
32 import javax.swing.event.*;
33
34 ///////////////////////////////////////////////////////////////////////
35
36 /**
37 * A panel containing fields for specifying a channel search, a search
38 * button to start the search and a table in which to display the
39 * search results.
40 */
41 public class ChannelSearchPanel extends JPanel
42 implements MDIClientPanel, ChannelSearchListener {
43
44 private ChannelSearch _search;
45 private String _dockState = MDIPanel.DOCK_NONE;
46 private ChannelTable _table;
47 private JLabel _status;
48 private JProgressBar _progress;
49 private int _progressValue = 0;
50
51 /** Construct a search panel for a specified channel search object. */
52 public ChannelSearchPanel(ChannelSearch search) {
53 _search = search;
54 _search.addChannelSearchListener(this);
55
56 setLayout(new BorderLayout());
57
58 // Query panel to specify search parameters
59 add(new QueryPanel(this),BorderLayout.NORTH);
60
61 // Channel table to show results
62 _table = new ChannelTable(search);
63 add(new JScrollPane(_table),BorderLayout.CENTER);
64
65 // Status bar and progress meter
66 JPanel statusPanel = new JPanel();
67 statusPanel.setLayout(new BorderLayout());
68
69 _status = new JLabel();
70 _status.setBorder(new BevelBorder(BevelBorder.LOWERED));
71 statusPanel.add(_status,BorderLayout.CENTER);
72
73 _progress = new JProgressBar();
74 _progress.setBorder(new BevelBorder(BevelBorder.LOWERED));
75 statusPanel.add(_progress,BorderLayout.EAST);
76
77 add(statusPanel,BorderLayout.SOUTH);
78 }
79 //-----------------------------------------------------------------
80 /** Returns panel's search object. */
81 public ChannelSearch getChannelSearch() {
82 return _search;
83 }
84 //------------------------------------------------------------------
85 /** Get dock-state of this panel. @see org.relayirc.swingutil.MDIClientPanel */
86 public String getDockState() {
87 return _dockState;
88 }
89 //------------------------------------------------------------------
90 /** Set dock-state of this panel. @see org.relayirc.swingutil.MDIClientPanel */
91 public void setDockState(String dockState) {
92 _dockState = dockState;
93 }
94 //------------------------------------------------------------------
95 /** Get panel for this MDI client. @see org.relayirc.swingutil.MDIClientPanel */
96 public JPanel getPanel() {
97 return this;
98 }
99 //----------------------------------------------------------------------
100 /** Called when the search is started. */
101 public void searchStarted(int channels) {
102 _status.setText("Searching...");
103 _progressValue = 0;
104 _progress.setMinimum(0);
105 _progress.setMaximum(channels);
106 }
107 //----------------------------------------------------------------------
108 /** Called with a channel has been found by the search. */
109 public void searchFound(Channel chan) {
110 _progress.setValue(++_progressValue);
111 }
112 //----------------------------------------------------------------------
113 /** Called when the search is complete. */
114 public void searchEnded() {
115 _status.setText("Search Complete ("+_table.getRowCount()+" channels found)");
116 _progressValue = 0;
117 _progress.setValue(0);
118 }
119 }
120
121 ///////////////////////////////////////////////////////////////////////////////////
122
123 /**
124 * Panel of text fields for entering search criteria such as Name,
125 * Min Users, Max Users, etc. Designed to be used within a
126 * ChannelSearchPanel.
127 * @see ChannelSearchPanel
128 */
129 class QueryPanel extends JPanel {
130 private JTextField _nameField = new JTextField(10);
131 private JTextField _minField = new JTextField(3);
132 private JTextField _maxField = new JTextField(3);
133 private ChannelSearchPanel _searchPanel;
134
135 public QueryPanel(ChannelSearchPanel searchPanel) {
136 _searchPanel = searchPanel;
137
138 setBorder(new EmptyBorder(10,10,10,10));
139 setLayout(new GridBagLayout());
140
141
142 add(new JLabel("Name",SwingConstants.RIGHT),
143 new GridBagConstraints2(0,0,1,1, 0.0,0.0,
144 GridBagConstraints.EAST,GridBagConstraints.NONE,new Insets(1,1,1,1),0,0));
145 add(_nameField,
146 new GridBagConstraints2(1,0,1,1, 1.0,0.0,
147 GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(1,1,1,1),0,0));
148
149
150 // Min users field
151 add(new JLabel("Min Users",SwingConstants.RIGHT),
152 new GridBagConstraints2(0,1,1,1, 0.0,0.0,
153 GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(1,1,1,1),0,0));
154 add(_minField,
155 new GridBagConstraints2(1,1,1,1, 0.0,0.0,
156 GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(1,1,1,1),0,0));
157 // If min users is set, then put value in min field
158 if (_searchPanel.getChannelSearch().getMinUsers() != Integer.MIN_VALUE) {
159 _minField.setText(Integer.toString(_searchPanel.getChannelSearch().getMinUsers()));
160 }
161
162
163 // Max users field
164 add(new JLabel("Max Users",SwingConstants.RIGHT),
165 new GridBagConstraints2(0,2,1,1, 0.0,0.0,
166 GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(1,1,1,1),0,0));
167 add(_maxField,
168 new GridBagConstraints2(1,2,1,1, 0.0,0.0,
169 GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(1,1,1,1),0,0));
170 // If max users is set, then put value in max field
171 if (_searchPanel.getChannelSearch().getMaxUsers() != Integer.MAX_VALUE) {
172 _maxField.setText(Integer.toString(_searchPanel.getChannelSearch().getMaxUsers()));
173 }
174
175
176 //add(new JCheckBox("Save Results"),
177 //new GridBagConstraints2(2,2,1,1,0.0,0.0,
178 //GridBagConstraints.EAST,GridBagConstraints.HORIZONTAL,new Insets(1,1,1,1),0,0));
179
180
181 // Search button starts search
182 JButton searchButton = new JButton("Search");
183 add(searchButton,
184 new GridBagConstraints2(2,0,1,1,0.0,0.0,
185 GridBagConstraints.EAST,GridBagConstraints.HORIZONTAL,new Insets(1,1,1,1),0,0));
186
187 searchButton.addActionListener(new ActionListener() {
188 public void actionPerformed(ActionEvent event) {
189
190 int ret = JOptionPane.showConfirmDialog(ChatApp.getChatApp(),
191 "Some servers do not support channel searches and may \n"
192 +"disconnect. Are you sure you want to run this search?");
193
194 if (ret==JOptionPane.YES_OPTION || ret==JOptionPane.OK_OPTION) {
195 int min = Integer.MIN_VALUE;
196 int max = Integer.MAX_VALUE;
197 try {min = Integer.parseInt(_minField.getText())-1;} catch (Exception e) {}
198 try {max = Integer.parseInt(_maxField.getText())+1;} catch (Exception e) {}
199 _searchPanel.getChannelSearch().setMinUsers(min);
200 _searchPanel.getChannelSearch().setMaxUsers(max);
201 _searchPanel.getChannelSearch().start();
202 }
203 }
204 });
205
206 //add(new JButton("Save"),
207 //new GridBagConstraints2(2,1,1,1,0.0,0.0,
208 //GridBagConstraints.EAST,GridBagConstraints.HORIZONTAL,new Insets(1,1,1,1),0,0));
209 }
210 }
211
212 ///////////////////////////////////////////////////////////////////////////////////
213
214 /**
215 * Table of channels that met search criteria. Uses ChannelTableModel
216 * to allow editing of channel information right in the table.
217 * @see ChannelTableModel
218 */
219 class ChannelTable extends JTable {
220 private ChannelSearch _search;
221
222 public ChannelTable(ChannelSearch search) {
223 super(new ChannelTableModel(search));
224 _search = search;
225
226 setRowHeight(20);
227 setShowGrid(false);
228 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
229 getColumn(new String("Name")).setCellRenderer(new GuiTableCellRenderer());
230
231 getColumn(new String("Users")).setCellRenderer(new TableCellRenderer() {
232 public Component getTableCellRendererComponent(JTable table, Object value,
233 boolean isSelected, boolean hasFocus, int row, int column) {
234 JLabel comp = new JLabel(value.toString(),SwingConstants.CENTER);
235 comp.setOpaque(true);
236 if (isSelected) {
237 comp.setForeground(table.getSelectionForeground());
238 comp.setBackground(table.getSelectionBackground());
239 } else {
240 comp.setForeground(table.getForeground());
241 comp.setBackground(table.getBackground());
242 }
243 return comp;
244 }
245 });
246
247 // Listen for double-clicks and right-clicks
248 addMouseListener(new MouseAdapter() {
249
250 public void mouseClicked(MouseEvent event) {
251 org.relayirc.util.RCTest.println("CLICK!!");
252
253 final MouseEvent me = event;
254
255 // Double-click?
256 if (me.getClickCount()==2) {
257
258 // Yes, join that channel!
259 int row = rowAtPoint(me.getPoint());
260 if (row!=-1) {
261 String chan = getModel().getValueAt(row,0).toString();
262 ChatApp.getChatApp().getEngine().sendJoin(chan);
263 }
264 }
265 // Right-click?
266 else if (me.isPopupTrigger()) {
267 org.relayirc.util.RCTest.println("TRIGGER");
268
269 // Yes, show popup menu so that channel may be added to favorites
270 final JPopupMenu popup = new JPopupMenu();
271 JMenuItem addFaveItem = new JMenuItem("Add to favorites");
272 addFaveItem.addActionListener( new ActionListener() {
273 public void actionPerformed(ActionEvent ae) {
274
275 // Add channel to favorites
276 int row = rowAtPoint(me.getPoint());
277 if (row!=-1) {
278 String chanName = getModel().getValueAt(row,0).toString();
279 Channel chan = new Channel(chanName);
280 ChatApp.getChatApp().getOptions().addChannel(chan);
281 }
282 }
283 });
284 popup.add(addFaveItem);
285
286 Point pt2 = SwingUtilities.convertPoint(
287 (Component)me.getSource(),
288 new Point(me.getX(),me.getY()),ChannelTable.this);
289
290 popup.show(ChannelTable.this,pt2.x,pt2.y);
291 }
292 }
293 });
294 }
295 //-----------------------------------------------------------------
296 /**
297 * Present pop-up menu for tree nodes.
298 */
299 public void processMouseEvent( MouseEvent e ) {
300 if (e.isPopupTrigger()) {
301 org.relayirc.util.RCTest.println("TRIGGER2");
302 }
303 }
304 }
305
306
307 ///////////////////////////////////////////////////////////////////////////////////
308
309 /**
310 * Table model for displaying channel search results with each row
311 * represents a channel.
312 */
313 class ChannelTableModel extends DefaultTableModel implements ChannelSearchListener {
314 private ChannelSearch _search;
315
316 //----------------------------------------------------------------------
317 public ChannelTableModel(ChannelSearch search) {
318 _search = search;
319 _search.addChannelSearchListener(this);
320 }
321
322 //----------------------------------------------------------------------
323 public void searchStarted(int channels) {
324 }
325
326 //----------------------------------------------------------------------
327 public void searchFound(Channel chan) {
328 fireTableDataChanged();
329 }
330
331 //----------------------------------------------------------------------
332 public void searchEnded() {
333 }
334
335 //----------------------------------------------------------------------
336 public int getRowCount() {
337 if (_search!=null)
338 return _search.getChannelCount();
339 else
340 return 0;
341 }
342
343 //----------------------------------------------------------------------
344 public int getColumnCount() {
345 return 3; // Name, User Count and Topic
346 }
347
348 //----------------------------------------------------------------------
349 public String getColumnName(int column) {
350 switch (column) {
351 case 0:
352 return new String("Name");
353 case 1:
354 return new String("Users");
355 case 2:
356 return new String("Topic");
357 default:
358 return new String("");
359 }
360 }
361
362 //----------------------------------------------------------------------
363 public Object getValueAt(int row, int column) {
364 if (_search!=null) {
365 switch (column) {
366 case 0:
367 return new GuiObject(
368 _search.getChannelAt(row).getName(),_search.getChannelAt(row),
369 new ImageIcon(getClass().getResource("images/Users.gif")));
370 case 1:
371 return new Integer(_search.getChannelAt(row).getUserCount());
372 case 2:
373 return _search.getChannelAt(row).getTopic();
374 default:
375 return new String("");
376 }
377 }
378 else return null;
379 }
380 //----------------------------------------------------------------------
381 public boolean isCellEditable(int rowIndex,int columnIndex) {
382 return false;
383 }
384 }
385
386