misc
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.news / src / net / sourceforge / phpeclipse / news / pref / SiteListEditor.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *     J�r�me N�gre    - adaptation of ListEditor to add the search button
11  *******************************************************************************/
12 package net.sourceforge.phpeclipse.news.pref;
13
14 import java.util.ArrayList;
15
16 import net.sourceforge.phpeclipse.news.Channel;
17 import net.sourceforge.phpeclipse.news.Plugin;
18 import net.sourceforge.phpeclipse.news.search.SearchDialog;
19
20 import org.eclipse.jface.dialogs.IDialogConstants;
21 import org.eclipse.jface.dialogs.InputDialog;
22 import org.eclipse.jface.preference.FieldEditor;
23 import org.eclipse.jface.resource.JFaceResources;
24 import org.eclipse.jface.util.Assert;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.DisposeEvent;
27 import org.eclipse.swt.events.DisposeListener;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.events.SelectionListener;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.List;
37 import org.eclipse.swt.widgets.Shell;
38 import org.eclipse.swt.widgets.Widget;
39
40 public class SiteListEditor extends FieldEditor {
41         /**
42          * The list widget; <code>null</code> if none
43          * (before creation or after disposal).
44          */
45         private List list;
46         private ArrayList channels;
47         /**
48          * The button box containing the Add, Remove, Up, and Down buttons;
49          * <code>null</code> if none (before creation or after disposal).
50          */
51         private Composite buttonBox;
52         /**
53          * The Add button.
54          */
55         private Button addButton;
56         /**
57          * The Search button.
58          */
59         private Button searchButton;
60         /**
61          * The Remove button.
62          */
63         private Button removeButton;
64         /**
65          * The Up button.
66          */
67         private Button upButton;
68         /**
69          * The Down button.
70          */
71         private Button downButton;
72         /**
73          * The selection listener.
74          */
75         private SelectionListener selectionListener;
76         /**
77          * Notifies that the Add button has been pressed.
78          */
79         private void addPressed() {
80                 setPresentsDefaultValue(false);
81                 Channel input = getNewInputChannel();
82                 if (input != null) {
83                         int index = list.getSelectionIndex();
84                         if (index >= 0) {
85                                 list.add(input.getTitle(), index + 1);
86                                 channels.add(index + 1, input);
87                         } else {
88                                 list.add(input.getTitle(), 0);
89                                 channels.add(0, input);
90                         }
91                         selectionChanged();
92                 }
93         }
94
95         /**
96          * Notifies that the Search button has been pressed.
97          */
98         private void searchPressed() {
99                 setPresentsDefaultValue(false);
100                 SearchDialog sd = new SearchDialog(SiteListEditor.this
101                                 .getShell());
102                 sd.open();
103                 Channel[] inputs = sd.getChannels();
104                 for(int i=0; i<inputs.length; i++) {
105                         int index = list.getSelectionIndex();
106                         if (index >= 0) {
107                                 list.add(inputs[i].getTitle(), index + 1);
108                                 channels.add(index + 1, inputs[i]);
109                         } else {
110                                 list.add(inputs[i].getTitle(), 0);
111                                 channels.add(0, inputs[i]);
112                         }
113                 }
114                 selectionChanged();
115         }
116
117         
118         /* (non-Javadoc)
119          * Method declared on FieldEditor.
120          */
121         protected void adjustForNumColumns(int numColumns) {
122                 Control control = getLabelControl();
123                 ((GridData) control.getLayoutData()).horizontalSpan = numColumns;
124                 ((GridData) list.getLayoutData()).horizontalSpan = numColumns - 1;
125         }
126         /**
127          * Creates the Add, Remove, Up, and Down button in the given button box.
128          *
129          * @param buttonBox the box for the buttons
130          */
131         private void createButtons(Composite buttonBox) {
132                 addButton = createPushButton(buttonBox, "ListEditor.add");//$NON-NLS-1$
133                 //TODO use my bundle ?
134                 searchButton = createPushButton(buttonBox, "Search (experimental)");
135                 removeButton = createPushButton(buttonBox, "ListEditor.remove");//$NON-NLS-1$
136                 upButton = createPushButton(buttonBox, "ListEditor.up");//$NON-NLS-1$
137                 downButton = createPushButton(buttonBox, "ListEditor.down");//$NON-NLS-1$
138         }
139         /**
140          * Helper method to create a push button.
141          * 
142          * @param parent the parent control
143          * @param key the resource name used to supply the button's label text
144          */
145         private Button createPushButton(Composite parent, String key) {
146                 Button button = new Button(parent, SWT.PUSH);
147                 button.setText(JFaceResources.getString(key));
148                 button.setFont(parent.getFont());
149                 GridData data = new GridData(GridData.FILL_HORIZONTAL);
150                 data.heightHint = convertVerticalDLUsToPixels(button,
151                                 IDialogConstants.BUTTON_HEIGHT);
152                 int widthHint = convertHorizontalDLUsToPixels(button,
153                                 IDialogConstants.BUTTON_WIDTH);
154                 data.widthHint = Math.max(widthHint, button.computeSize(SWT.DEFAULT,
155                                 SWT.DEFAULT, true).x);
156                 button.setLayoutData(data);
157                 button.addSelectionListener(getSelectionListener());
158                 return button;
159         }
160         /**
161          * Creates a selection listener.
162          */
163         public void createSelectionListener() {
164                 selectionListener = new SelectionAdapter() {
165                         public void widgetSelected(SelectionEvent event) {
166                                 Widget widget = event.widget;
167                                 if (widget == addButton) {
168                                         addPressed();
169                                 } else if (widget == searchButton) {
170                                         searchPressed();
171                                 } else if (widget == removeButton) {
172                                         removePressed();
173                                 } else if (widget == upButton) {
174                                         upPressed();
175                                 } else if (widget == downButton) {
176                                         downPressed();
177                                 } else if (widget == list) {
178                                         selectionChanged();
179                                 }
180                         }
181                 };
182         }
183         /* (non-Javadoc)
184          * Method declared on FieldEditor.
185          */
186         protected void doFillIntoGrid(Composite parent, int numColumns) {
187                 Control control = getLabelControl(parent);
188                 GridData gd = new GridData();
189                 gd.horizontalSpan = numColumns;
190                 control.setLayoutData(gd);
191                 list = getListControl(parent);
192                 gd = new GridData(GridData.FILL_HORIZONTAL);
193                 gd.verticalAlignment = GridData.FILL;
194                 gd.horizontalSpan = numColumns - 1;
195                 gd.grabExcessHorizontalSpace = true;
196                 list.setLayoutData(gd);
197                 buttonBox = getButtonBoxControl(parent);
198                 gd = new GridData();
199                 gd.verticalAlignment = GridData.BEGINNING;
200                 buttonBox.setLayoutData(gd);
201         }
202         /* (non-Javadoc)
203          * Method declared on FieldEditor.
204          */
205         protected void doLoad() {
206                 if (list != null) {
207                         channels = ChannelStore.getChannels();
208                         for (int i = 0; i < channels.size(); i++) {
209                                 list.add(((Channel)channels.get(i)).getTitle());
210                         }
211                 }
212         }
213         /* (non-Javadoc)
214          * Method declared on FieldEditor.
215          */
216         protected void doLoadDefault() {
217                 if (list != null) {
218                         list.removeAll();
219                         channels = ChannelStore.getDefaultChannels();
220                         for (int i = 0; i < channels.size(); i++) {
221                                 list.add(((Channel)channels.get(i)).getTitle());
222                         }
223                         setPresentsDefaultValue(false);
224                 }
225         }
226         /* (non-Javadoc)
227          * Method declared on FieldEditor.
228          */
229         protected void doStore() {
230                 ChannelStore.saveReadStatus(Plugin.getDefault().getChannelList());
231                 ChannelStore.setChannels(channels);
232                 Plugin.getDefault().updateChannelList();
233         }
234         /**
235          * Notifies that the Down button has been pressed.
236          */
237         private void downPressed() {
238                 swap(false);
239         }
240         /**
241          * Returns this field editor's button box containing the Add, Remove,
242          * Up, and Down button.
243          *
244          * @param parent the parent control
245          * @return the button box
246          */
247         public Composite getButtonBoxControl(Composite parent) {
248                 if (buttonBox == null) {
249                         buttonBox = new Composite(parent, SWT.NULL);
250                         GridLayout layout = new GridLayout();
251                         layout.marginWidth = 0;
252                         buttonBox.setLayout(layout);
253                         createButtons(buttonBox);
254                         buttonBox.addDisposeListener(new DisposeListener() {
255                                 public void widgetDisposed(DisposeEvent event) {
256                                         addButton = null;
257                                         searchButton = null;
258                                         removeButton = null;
259                                         upButton = null;
260                                         downButton = null;
261                                         buttonBox = null;
262                                 }
263                         });
264                 } else {
265                         checkParent(buttonBox, parent);
266                 }
267                 selectionChanged();
268                 return buttonBox;
269         }
270         /**
271          * Returns this field editor's list control.
272          *
273          * @param parent the parent control
274          * @return the list control
275          */
276         public List getListControl(Composite parent) {
277                 if (list == null) {
278                         list = new List(parent, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL
279                                         | SWT.H_SCROLL);
280                         list.setFont(parent.getFont());
281                         list.addSelectionListener(getSelectionListener());
282                         list.addDisposeListener(new DisposeListener() {
283                                 public void widgetDisposed(DisposeEvent event) {
284                                         list = null;
285                                 }
286                         });
287                 } else {
288                         checkParent(list, parent);
289                 }
290                 return list;
291         }
292         /* (non-Javadoc)
293          * Method declared on FieldEditor.
294          */
295         public int getNumberOfControls() {
296                 return 2;
297         }
298         /**
299          * Returns this field editor's selection listener.
300          * The listener is created if nessessary.
301          *
302          * @return the selection listener
303          */
304         private SelectionListener getSelectionListener() {
305                 if (selectionListener == null)
306                         createSelectionListener();
307                 return selectionListener;
308         }
309         /**
310          * Returns this field editor's shell.
311          * <p>
312          * This method is internal to the framework; subclassers should not call
313          * this method.
314          * </p>
315          *
316          * @return the shell
317          */
318         protected Shell getShell() {
319                 if (addButton == null)
320                         return null;
321                 return addButton.getShell();
322         }
323         /**
324          * Notifies that the Remove button has been pressed.
325          */
326         private void removePressed() {
327                 setPresentsDefaultValue(false);
328                 int index = list.getSelectionIndex();
329                 if (index >= 0) {
330                         list.remove(index);
331                         channels.remove(index);
332                         selectionChanged();
333                 }
334         }
335         /**
336          * Notifies that the list selection has changed.
337          */
338         private void selectionChanged() {
339                 int index = list.getSelectionIndex();
340                 int size = list.getItemCount();
341                 removeButton.setEnabled(index >= 0);
342                 upButton.setEnabled(size > 1 && index > 0);
343                 downButton.setEnabled(size > 1 && index >= 0 && index < size - 1);
344         }
345         /* (non-Javadoc)
346          * Method declared on FieldEditor.
347          */
348         public void setFocus() {
349                 if (list != null) {
350                         list.setFocus();
351                 }
352         }
353         /**
354          * Moves the currently selected item up or down.
355          *
356          * @param up <code>true</code> if the item should move up,
357          *  and <code>false</code> if it should move down
358          */
359         private void swap(boolean up) {
360                 setPresentsDefaultValue(false);
361                 int index = list.getSelectionIndex();
362                 int target = up ? index - 1 : index + 1;
363                 if (index >= 0) {
364                         //list widget
365                         String[] selection = list.getSelection();
366                         Assert.isTrue(selection.length == 1);
367                         list.remove(index);
368                         list.add(selection[0], target);
369                         list.setSelection(target);
370                         //channels arrayList
371                         Object obj = channels.remove(index);
372                         channels.add(target, obj);
373                 }
374                 selectionChanged();
375         }
376         /**
377          * Notifies that the Up button has been pressed.
378          */
379         private void upPressed() {
380                 swap(true);
381         }
382         /*
383          * @see FieldEditor.setEnabled(boolean,Composite).
384          */
385         public void setEnabled(boolean enabled, Composite parent) {
386                 super.setEnabled(enabled, parent);
387                 getListControl(parent).setEnabled(enabled);
388                 addButton.setEnabled(enabled);
389                 removeButton.setEnabled(enabled);
390                 upButton.setEnabled(enabled);
391                 downButton.setEnabled(enabled);
392         }
393
394         /*
395          * 
396          * 
397          * 
398          * 
399          * 
400          * 
401          * 
402          * 
403          * 
404          * 
405          * 
406          * 
407          * 
408          */
409         
410         /**
411          * Creates a site list field editor.
412          * 
413          * @param name the name of the preference this field editor works on
414          * @param labelText the label text of the field editor
415          * @param parent the parent of the field editor's control
416          */
417         protected SiteListEditor(String name, String labelText, Composite parent) {
418                 init(name, labelText);
419                 createControl(parent);
420         }
421         /**
422          * Creates and returns a new item for the list.
423          *
424          * @return a new item
425          */
426         protected Channel getNewInputChannel() {
427                 InputDialog dialog;
428                 dialog = new InputDialog(this.getShell(), "All The News",
429                                 "Enter new site name", "", null);
430                 dialog.open();
431                 if ("".equals(dialog.getValue()) || dialog.getValue() == null)
432                         return null;
433                 String title = dialog.getValue();
434                 dialog = new InputDialog(this.getShell(), "All The News",
435                                 "Enter new site URL", "", null);
436                 dialog.open();
437                 if ("".equals(dialog.getValue()) || dialog.getValue() == null)
438                         return null;
439                 String url = dialog.getValue();
440                 return new Channel(title, url);
441         }
442 }