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
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;
14 import java.util.ArrayList;
16 import net.sourceforge.phpeclipse.news.Channel;
17 import net.sourceforge.phpeclipse.news.Plugin;
18 import net.sourceforge.phpeclipse.news.search.SearchDialog;
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;
40 public class SiteListEditor extends FieldEditor {
42 * The list widget; <code>null</code> if none
43 * (before creation or after disposal).
46 private ArrayList channels;
48 * The button box containing the Add, Remove, Up, and Down buttons;
49 * <code>null</code> if none (before creation or after disposal).
51 private Composite buttonBox;
55 private Button addButton;
59 private Button searchButton;
63 private Button removeButton;
67 private Button upButton;
71 private Button downButton;
73 * The selection listener.
75 private SelectionListener selectionListener;
77 * Notifies that the Add button has been pressed.
79 private void addPressed() {
80 setPresentsDefaultValue(false);
81 Channel input = getNewInputChannel();
83 int index = list.getSelectionIndex();
85 list.add(input.getTitle(), index + 1);
86 channels.add(index + 1, input);
88 list.add(input.getTitle(), 0);
89 channels.add(0, input);
96 * Notifies that the Search button has been pressed.
98 private void searchPressed() {
99 setPresentsDefaultValue(false);
100 SearchDialog sd = new SearchDialog(SiteListEditor.this
103 Channel[] inputs = sd.getChannels();
104 for(int i=0; i<inputs.length; i++) {
105 int index = list.getSelectionIndex();
107 list.add(inputs[i].getTitle(), index + 1);
108 channels.add(index + 1, inputs[i]);
110 list.add(inputs[i].getTitle(), 0);
111 channels.add(0, inputs[i]);
119 * Method declared on FieldEditor.
121 protected void adjustForNumColumns(int numColumns) {
122 Control control = getLabelControl();
123 ((GridData) control.getLayoutData()).horizontalSpan = numColumns;
124 ((GridData) list.getLayoutData()).horizontalSpan = numColumns - 1;
127 * Creates the Add, Remove, Up, and Down button in the given button box.
129 * @param buttonBox the box for the buttons
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$
140 * Helper method to create a push button.
142 * @param parent the parent control
143 * @param key the resource name used to supply the button's label text
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());
161 * Creates a selection listener.
163 public void createSelectionListener() {
164 selectionListener = new SelectionAdapter() {
165 public void widgetSelected(SelectionEvent event) {
166 Widget widget = event.widget;
167 if (widget == addButton) {
169 } else if (widget == searchButton) {
171 } else if (widget == removeButton) {
173 } else if (widget == upButton) {
175 } else if (widget == downButton) {
177 } else if (widget == list) {
184 * Method declared on FieldEditor.
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);
199 gd.verticalAlignment = GridData.BEGINNING;
200 buttonBox.setLayoutData(gd);
203 * Method declared on FieldEditor.
205 protected void doLoad() {
207 channels = ChannelStore.getChannels();
208 for (int i = 0; i < channels.size(); i++) {
209 list.add(((Channel)channels.get(i)).getTitle());
214 * Method declared on FieldEditor.
216 protected void doLoadDefault() {
219 channels = ChannelStore.getDefaultChannels();
220 for (int i = 0; i < channels.size(); i++) {
221 list.add(((Channel)channels.get(i)).getTitle());
223 setPresentsDefaultValue(false);
227 * Method declared on FieldEditor.
229 protected void doStore() {
230 ChannelStore.saveReadStatus(Plugin.getDefault().getChannelList());
231 ChannelStore.setChannels(channels);
232 Plugin.getDefault().updateChannelList();
235 * Notifies that the Down button has been pressed.
237 private void downPressed() {
241 * Returns this field editor's button box containing the Add, Remove,
242 * Up, and Down button.
244 * @param parent the parent control
245 * @return the button box
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) {
265 checkParent(buttonBox, parent);
271 * Returns this field editor's list control.
273 * @param parent the parent control
274 * @return the list control
276 public List getListControl(Composite parent) {
278 list = new List(parent, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL
280 list.setFont(parent.getFont());
281 list.addSelectionListener(getSelectionListener());
282 list.addDisposeListener(new DisposeListener() {
283 public void widgetDisposed(DisposeEvent event) {
288 checkParent(list, parent);
293 * Method declared on FieldEditor.
295 public int getNumberOfControls() {
299 * Returns this field editor's selection listener.
300 * The listener is created if nessessary.
302 * @return the selection listener
304 private SelectionListener getSelectionListener() {
305 if (selectionListener == null)
306 createSelectionListener();
307 return selectionListener;
310 * Returns this field editor's shell.
312 * This method is internal to the framework; subclassers should not call
318 protected Shell getShell() {
319 if (addButton == null)
321 return addButton.getShell();
324 * Notifies that the Remove button has been pressed.
326 private void removePressed() {
327 setPresentsDefaultValue(false);
328 int index = list.getSelectionIndex();
331 channels.remove(index);
336 * Notifies that the list selection has changed.
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);
346 * Method declared on FieldEditor.
348 public void setFocus() {
354 * Moves the currently selected item up or down.
356 * @param up <code>true</code> if the item should move up,
357 * and <code>false</code> if it should move down
359 private void swap(boolean up) {
360 setPresentsDefaultValue(false);
361 int index = list.getSelectionIndex();
362 int target = up ? index - 1 : index + 1;
365 String[] selection = list.getSelection();
366 Assert.isTrue(selection.length == 1);
368 list.add(selection[0], target);
369 list.setSelection(target);
371 Object obj = channels.remove(index);
372 channels.add(target, obj);
377 * Notifies that the Up button has been pressed.
379 private void upPressed() {
383 * @see FieldEditor.setEnabled(boolean,Composite).
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);
411 * Creates a site list field editor.
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
417 protected SiteListEditor(String name, String labelText, Composite parent) {
418 init(name, labelText);
419 createControl(parent);
422 * Creates and returns a new item for the list.
426 protected Channel getNewInputChannel() {
428 dialog = new InputDialog(this.getShell(), "All The News",
429 "Enter new site name", "", null);
431 if ("".equals(dialog.getValue()) || dialog.getValue() == null)
433 String title = dialog.getValue();
434 dialog = new InputDialog(this.getShell(), "All The News",
435 "Enter new site URL", "", null);
437 if ("".equals(dialog.getValue()) || dialog.getValue() == null)
439 String url = dialog.getValue();
440 return new Channel(title, url);