Source code: org/relayirc/chatengine/ChannelSearch.java
1
2 /*
3 * FILE: ChannelSearch.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.chatengine;
24 import org.relayirc.util.*;
25
26 import java.util.Vector;
27
28 /**
29 * Channel searcher. Works closely with a chat engine
30 * to search for channels that match the search criteria, notify
31 * listeners of each such matching channel as it is found and build a
32 * collection of matching channels.
33 */
34 public class ChannelSearch {
35
36 private IChatEngine _engine = null;
37 private String _name = null;
38 private int _minUsers = Integer.MIN_VALUE;
39 private int _maxUsers = Integer.MAX_VALUE;
40 private Vector _results = new Vector();
41 private boolean _complete = false;
42 private Vector _listeners = new Vector();
43
44 /** Channel search needs a chat engine. */
45 public ChannelSearch(IChatEngine engine) {
46 _engine = engine;
47 }
48
49 /** Get channel name search string. */
50 public String getName() {return _name;}
51
52 /** Set channel name search string. */
53 public void setName(String name) {_name=name;}
54
55 /** Get minimum user-count criteria. */
56 public int getMinUsers() {return _minUsers;}
57
58 /** Set minimum user-count criteria. */
59 public void setMinUsers(int min) {_minUsers=min;}
60
61 /** Get maximum user-count criteria. */
62 public int getMaxUsers() {return _maxUsers;}
63
64 /** Set maximum user-count criteria. */
65 public void setMaxUsers(int min) {_maxUsers=min;}
66
67 /** Internal use. */
68 void setComplete(boolean complete) {_complete=complete;}
69
70 /** True if seach has completed. */
71 public boolean isComplete() {return _complete;}
72
73 /** Number of channels found in most recent search, or -1 on error. */
74 public int getChannelCount() {
75 if (_results == null) return -1;
76 return _results.size();
77 }
78
79 /** Number of channels found in most recent search, or null on error. */
80 public Channel getChannelAt(int index) {
81 if (_results == null) return null;
82 return (Channel)_results.elementAt(index);
83 }
84
85 /** Start the channel search with the current criteria. */
86 public void start() {
87 _results = new Vector();
88 _engine.startChannelSearch(this);
89 }
90
91 /** Add search listener. */
92 public void addChannelSearchListener(ChannelSearchListener listener) {
93 _listeners.addElement(listener);
94 }
95
96 /** Remove search listener. */
97 public void removeChannelSearchListener(ChannelSearchListener listener) {
98 _listeners.removeElement(listener);
99 }
100
101 /** Internal use. */
102 void processChannel(Channel chan) {
103 if (chan.getUserCount()>_minUsers && chan.getUserCount()<_maxUsers) {
104 _results.addElement(chan);
105 for (int i=0; i<_listeners.size(); i++) {
106 ((ChannelSearchListener)_listeners.elementAt(i)).searchFound(chan);
107 }
108 }
109 }
110
111 /** Internal use. */
112 void searchStarted(int channels) {
113 for (int i=0; i<_listeners.size(); i++) {
114 ((ChannelSearchListener)_listeners.elementAt(i)).searchStarted(channels);
115 }
116 }
117
118 /** Internal use. */
119 void searchEnded() {
120 for (int i=0; i<_listeners.size(); i++) {
121 ((ChannelSearchListener)_listeners.elementAt(i)).searchEnded();
122 }
123 }
124 }
125
126