2 * Created on 25 janv. 2004
4 * (c)2004 Jérôme Nègre - http://www.jnegre.org/
7 package net.sourceforge.phpeclipse.news.search;
9 import java.net.MalformedURLException;
10 import java.util.ArrayList;
11 import java.util.Hashtable;
12 import java.util.Iterator;
13 import java.util.Vector;
15 import net.sourceforge.phpeclipse.news.Channel;
17 import org.apache.xmlrpc.XmlRpcClient;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.graphics.Cursor;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Group;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.List;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.swt.widgets.Text;
36 * @author Jérôme Nègre
39 public class SearchDialog extends Dialog {
41 private Label statusBar;
42 private Text searchText;
48 private Text description;
50 private ArrayList resultChannels = new ArrayList();
52 private XmlRpcClient xmlRpcClient;
57 public SearchDialog(Shell parentShell) {
59 this.setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MODELESS);
62 protected void configureShell(Shell newShell) {
63 super.configureShell(newShell);
64 newShell.setText("Search using http://www.syndic8.com/");
68 * Adds the controls to the dialog
69 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
71 protected Control createDialogArea(Composite parent) {
72 Composite composite = (Composite)super.createDialogArea(parent);
73 GridLayout gl = (GridLayout)composite.getLayout();
76 //Text to enter the searched words
77 searchText = new Text(composite,SWT.BORDER);
78 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
79 gd.horizontalSpan = 3;
80 searchText.setLayoutData(gd);
82 Button searchButton = new Button(composite,0);
83 searchButton.setText("Search!");
84 searchButton.addSelectionListener(new SelectionAdapter() {
85 public void widgetSelected(SelectionEvent e) {
87 Cursor waitCursor = new Cursor(SearchDialog.this.getContents().getDisplay(), SWT.CURSOR_WAIT);
89 SearchDialog.this.getContents().setCursor(waitCursor);
90 SearchDialog.this.searchNow();
91 } catch (Exception x) {
92 setStatusMessage("Error: "+x.getMessage());
94 SearchDialog.this.getContents().setCursor(null);
99 //List for the titles of the feeds
100 list = new List(composite,SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL|SWT.SINGLE);
101 gd = new GridData(GridData.FILL_BOTH);
102 list.setLayoutData(gd);
103 list.addSelectionListener(new SelectionAdapter() {
104 public void widgetSelected(SelectionEvent e) {
105 SearchDialog.this.showFieldDetails((Hashtable)((ArrayList)list.getData()).get(list.getSelectionIndex()));
109 //Description of the selected feed
110 Group group = new Group(composite,0);
111 group.setText("Selected Feed");
112 gd = new GridData(GridData.FILL_BOTH);
113 gd.horizontalSpan = 3;
114 group.setLayoutData(gd);
115 group.setLayout(new GridLayout(2,false));
117 new Label(group,0).setText("Name:");
118 name = new Text(group,SWT.BORDER|SWT.READ_ONLY);
119 name.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
121 new Label(group,0).setText("Site URL:");
122 siteUrl = new Text(group,SWT.BORDER|SWT.READ_ONLY);
123 siteUrl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
125 new Label(group,0).setText("Feed URL:");
126 feedUrl = new Text(group,SWT.BORDER|SWT.READ_ONLY);
127 feedUrl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
129 new Label(group,0).setText("RSS version:");
130 version = new Text(group,SWT.BORDER|SWT.READ_ONLY);
131 version.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
133 new Label(group,0).setText("Description:");
134 description = new Text(group,SWT.BORDER|SWT.READ_ONLY|SWT.MULTI|SWT.H_SCROLL|SWT.V_SCROLL|SWT.WRAP);
135 description.setLayoutData(new GridData(GridData.FILL_BOTH));
138 statusBar = new Label(composite,SWT.NONE);
139 gd = new GridData(GridData.FILL_HORIZONTAL);
140 gd.horizontalSpan = 4;
141 statusBar.setLayoutData(gd);
142 setStatusMessage("Ready.");
146 private XmlRpcClient getXmlRpcClient() throws MalformedURLException {
147 if(this.xmlRpcClient == null) {
148 this.xmlRpcClient = new XmlRpcClient("http://www.syndic8.com/xmlrpc.php");
150 return this.xmlRpcClient;
156 protected void searchNow() throws Exception {
158 setStatusMessage("Connecting...");
159 XmlRpcClient client = getXmlRpcClient();
160 //Get the list of ids
161 Vector args = new Vector();
162 args.add(searchText.getText());
163 args.add("sitename");
164 //args.add(new Integer(30));
165 Vector ids = (Vector)client.execute("syndic8.FindFeeds",args);
166 setStatusMessage("Found "+ids.size()+" result(s), asking for details...");
167 //Get the descriptions of the feeds
168 Vector fields = new Vector();
169 fields.add("sitename");
170 fields.add("siteurl");
171 fields.add("dataurl");
172 fields.add("rss_version");
173 fields.add("description");
177 Vector infos = (Vector)client.execute("syndic8.GetFeedInfo",args);
178 setStatusMessage("Showing details...");
179 Iterator iterator = infos.iterator();
180 while(iterator.hasNext()) {
181 Hashtable info = (Hashtable)iterator.next();
184 setStatusMessage("Ready.");
187 protected void clearFeedList() {
189 showFieldDetails(null);
190 list.setData(new ArrayList());
193 protected void showFieldDetails(Hashtable info) {
194 name.setText(info==null?"":(String)info.get("sitename"));
195 siteUrl.setText(info==null?"":(String)info.get("siteurl"));
196 feedUrl.setText(info==null?"":(String)info.get("dataurl"));
197 version.setText(info==null?"":(String)info.get("rss_version"));
198 description.setText(info==null?"":(String)info.get("description"));
201 protected void addFeedInList(Hashtable info) {
202 String name = (String)info.get("sitename");
203 String dataurl = (String)info.get("dataurl");
204 if("".equals(name) || "".equals(dataurl)) {
208 ArrayList al = (ArrayList)list.getData();
213 protected void setStatusMessage(String message) {
214 statusBar.setText(message);
215 //TODO remove next line
216 System.out.println(message);
219 protected void createButtonsForButtonBar(Composite parent) {
220 createButton(parent,IDialogConstants.OPEN_ID,"Add Selected",false);
221 createButton(parent,IDialogConstants.OK_ID,IDialogConstants.OK_LABEL,false);
224 protected void buttonPressed(int buttonId) {
225 super.buttonPressed(buttonId);
226 if(buttonId == IDialogConstants.OPEN_ID) {
227 String name = this.name.getText();
228 String url = this.feedUrl.getText();
229 if(!"".equals(name) && !"".equals(url)) {
230 resultChannels.add(new Channel(name, url));
235 public Channel[] getChannels() {
236 return (Channel[])resultChannels.toArray(new Channel[]{});