Added externaltools plugin
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.news / src / net / sourceforge / phpeclipse / news / search / SearchDialog.java
1 /*
2  * Created on 25 janv. 2004
3  *
4  * (c)2004 Jérôme Nègre - http://www.jnegre.org/
5  */
6
7 package net.sourceforge.phpeclipse.news.search;
8
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;
14
15 import net.sourceforge.phpeclipse.news.Channel;
16
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;
34
35 /**
36  * @author Jérôme Nègre
37  *
38  */
39 public class SearchDialog extends Dialog {
40
41     private Label statusBar;
42     private Text searchText;
43     private List list;
44     private Text name;
45     private Text siteUrl;
46     private Text feedUrl;
47     private Text version;
48     private Text description;
49     
50     private ArrayList resultChannels = new ArrayList();
51     
52     private XmlRpcClient xmlRpcClient;
53     
54     /**
55      * @param parentShell
56      */
57     public SearchDialog(Shell parentShell) {
58         super(parentShell);
59         this.setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MODELESS);
60     }
61
62     protected void configureShell(Shell newShell) {
63         super.configureShell(newShell);
64         newShell.setText("Search using http://www.syndic8.com/");
65     }
66
67     /**
68      * Adds the controls to the dialog
69      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
70      */
71     protected Control createDialogArea(Composite parent) {
72         Composite composite = (Composite)super.createDialogArea(parent);
73         GridLayout gl = (GridLayout)composite.getLayout();
74         gl.numColumns = 4;
75         
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);
81         //Button "search!"
82         Button searchButton = new Button(composite,0);
83         searchButton.setText("Search!");
84         searchButton.addSelectionListener(new SelectionAdapter() {
85             public void widgetSelected(SelectionEvent e) {
86                 //TODO use a worker
87                 Cursor waitCursor = new Cursor(SearchDialog.this.getContents().getDisplay(), SWT.CURSOR_WAIT);
88                 try {
89                         SearchDialog.this.getContents().setCursor(waitCursor);
90                     SearchDialog.this.searchNow();
91                 } catch (Exception x) {
92                     setStatusMessage("Error: "+x.getMessage());
93                 } finally {
94                         SearchDialog.this.getContents().setCursor(null);
95                         waitCursor.dispose();
96                 }
97             }
98         });
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()));
106             }
107         });
108         
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));
116         //name
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));
120         //site url
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));
124         //feed url
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));
128         //RSS version
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));
132         //description
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));
136
137         //status bar
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.");
143         return composite;
144     }
145
146     private XmlRpcClient getXmlRpcClient() throws MalformedURLException {
147         if(this.xmlRpcClient == null) {
148             this.xmlRpcClient = new XmlRpcClient("http://www.syndic8.com/xmlrpc.php");
149         }
150         return this.xmlRpcClient;
151     }
152
153     /**
154      * 
155      */
156     protected void searchNow() throws Exception {
157       clearFeedList();
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");
174       args.clear();
175       args.add(ids);
176       args.add(fields);
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();
182           addFeedInList(info);
183       }
184       setStatusMessage("Ready.");
185     }
186     
187     protected void clearFeedList() {
188         list.removeAll();
189         showFieldDetails(null);
190         list.setData(new ArrayList());
191     }
192     
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"));
199     }
200     
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)) {
205             //skip it
206             return;
207         }
208         ArrayList al = (ArrayList)list.getData();
209         al.add(info);
210         list.add(name);
211     }
212     
213     protected void setStatusMessage(String message) {
214         statusBar.setText(message);
215         //TODO remove next line
216         System.out.println(message);
217     }
218
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);
222     }
223
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));
231                         }
232                 }
233         }
234         
235         public Channel[] getChannels() {
236                 return (Channel[])resultChannels.toArray(new Channel[]{});
237         }
238 }