1
/*******************************************************************************
2
* Copyright (c) 2007 Alphonse Van Assche.
3
* Copyright (c) 2008, 2009 Red Hat, Inc.
4
* All rights reserved. This program and the accompanying materials
5
* are made available under the terms of the Eclipse Public License v1.0
6
* which accompanies this distribution, and is available at
7
* http://www.eclipse.org/legal/epl-v10.html
10
* Alphonse Van Assche - initial API and implementation
11
* Wind River Systems, Inc - compile fixes
12
*******************************************************************************/
14
package org.eclipse.linuxtools.internal.rpm.ui.editor.preferences;
16
import java.util.ArrayList;
17
import java.util.StringTokenizer;
19
import org.eclipse.core.runtime.Assert;
20
import org.eclipse.jface.dialogs.IDialogConstants;
21
import org.eclipse.jface.preference.FieldEditorPreferencePage;
22
import org.eclipse.jface.preference.ListEditor;
23
import org.eclipse.jface.preference.RadioGroupFieldEditor;
24
import org.eclipse.jface.resource.JFaceResources;
25
import org.eclipse.linuxtools.internal.rpm.ui.editor.Activator;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.events.DisposeEvent;
28
import org.eclipse.swt.events.DisposeListener;
29
import org.eclipse.swt.events.SelectionAdapter;
30
import org.eclipse.swt.events.SelectionEvent;
31
import org.eclipse.swt.events.SelectionListener;
32
import org.eclipse.swt.layout.GridData;
33
import org.eclipse.swt.layout.GridLayout;
34
import org.eclipse.swt.widgets.Button;
35
import org.eclipse.swt.widgets.Composite;
36
import org.eclipse.swt.widgets.DirectoryDialog;
37
import org.eclipse.swt.widgets.FileDialog;
38
import org.eclipse.swt.widgets.List;
39
import org.eclipse.swt.widgets.Shell;
40
import org.eclipse.swt.widgets.Widget;
41
import org.eclipse.ui.IWorkbench;
42
import org.eclipse.ui.IWorkbenchPreferencePage;
45
* RPM macro proposals and hover preference page class.
48
public class MacroProposalsPreferencePage extends FieldEditorPreferencePage
49
implements IWorkbenchPreferencePage {
51
public MacroProposalsPreferencePage() {
53
setPreferenceStore(Activator.getDefault().getPreferenceStore());
56
public void init(IWorkbench workbench) {
60
protected void createFieldEditors() {
61
ListEditor macroListEditor = new MacroListEditor(
62
PreferenceConstants.P_MACRO_PROPOSALS_FILESPATH,
63
Messages.MacroProposalsPreferencePage_0, getFieldEditorParent());
64
addField(macroListEditor);
65
RadioGroupFieldEditor macroHoverListEditor = new RadioGroupFieldEditor(
66
PreferenceConstants.P_MACRO_HOVER_CONTENT,
67
Messages.MacroProposalsPreferencePage_1,
71
Messages.MacroProposalsPreferencePage_2,
72
PreferenceConstants.P_MACRO_HOVER_CONTENT_VIEWDESCRIPTION },
74
Messages.MacroProposalsPreferencePage_3,
75
PreferenceConstants.P_MACRO_HOVER_CONTENT_VIEWCONTENT } },
76
getFieldEditorParent(), true);
77
addField(macroHoverListEditor);
80
class MacroListEditor extends ListEditor {
83
* The list widget; <code>null</code> if none (before creation or
89
* The button box containing the Add, Remove, Up, and Down buttons;
90
* <code>null</code> if none (before creation or after disposal).
92
private Composite buttonBox;
95
* The Add file button.
97
private Button addFileButton;
102
private Button addDirButton;
107
private Button removeButton;
112
private Button upButton;
117
private Button downButton;
120
* The selection listener.
122
private SelectionListener selectionListener;
124
public MacroListEditor(String name, String labelText, Composite parent) {
126
init(name, labelText);
127
createControl(parent);
128
list = getListControl(parent);
132
protected String createList(String[] items) {
133
StringBuilder path = new StringBuilder(""); //$NON-NLS-1$
134
for (String item:items) {
136
path.append(";"); //$NON-NLS-1$
138
return path.toString();
142
protected String getNewInputObject() {
143
FileDialog dialog = new FileDialog(getShell());
144
return dialog.open();
147
protected String getNewDirInputObject() {
148
DirectoryDialog dialog = new DirectoryDialog(getShell());
149
return dialog.open();
153
protected String[] parseString(String stringList) {
154
StringTokenizer st = new StringTokenizer(stringList, ";\n\r"); //$NON-NLS-1$
155
ArrayList<String> v = new ArrayList<String>();
156
while (st.hasMoreTokens()) {
157
v.add(st.nextToken());
159
return v.toArray(new String[v.size()]);
163
* Notifies that the Add button has been pressed.
165
private void addFilePressed() {
166
setPresentsDefaultValue(false);
167
String input = getNewInputObject();
170
int index = list.getSelectionIndex();
172
list.add(input, index + 1);
181
* Notifies that the Add button has been pressed.
183
private void addDirPressed() {
184
setPresentsDefaultValue(false);
185
String input = getNewDirInputObject();
188
int index = list.getSelectionIndex();
190
list.add(input, index + 1);
199
* Creates the Add, Remove, Up, and Down button in the given button box.
202
* the box for the buttons
204
private void createButtons(Composite box) {
205
addFileButton = createPushButton(box, Messages.MacroProposalsPreferencePage_4);
206
addDirButton = createPushButton(box, Messages.MacroProposalsPreferencePage_5);
207
removeButton = createPushButton(box, "ListEditor.remove");//$NON-NLS-1$
208
upButton = createPushButton(box, "ListEditor.up");//$NON-NLS-1$
209
downButton = createPushButton(box, "ListEditor.down");//$NON-NLS-1$
213
* Helper method to create a push button.
218
* the resource name used to supply the button's label text
221
private Button createPushButton(Composite parent, String key) {
222
Button button = new Button(parent, SWT.PUSH);
223
button.setText(JFaceResources.getString(key));
224
button.setFont(parent.getFont());
225
GridData data = new GridData(GridData.FILL_HORIZONTAL);
226
int widthHint = convertHorizontalDLUsToPixels(button,
227
IDialogConstants.BUTTON_WIDTH);
228
data.widthHint = Math.max(widthHint, button.computeSize(
229
SWT.DEFAULT, SWT.DEFAULT, true).x);
230
button.setLayoutData(data);
231
button.addSelectionListener(getSelectionListener());
236
* Creates a selection listener.
239
public void createSelectionListener() {
240
selectionListener = new SelectionAdapter() {
242
public void widgetSelected(SelectionEvent event) {
243
Widget widget = event.widget;
244
if (widget == addFileButton) {
246
} else if (widget == addDirButton) {
248
}else if (widget == removeButton) {
250
} else if (widget == upButton) {
252
} else if (widget == downButton) {
254
} else if (widget == list) {
262
* Notifies that the Down button has been pressed.
264
private void downPressed() {
269
* Returns this field editor's button box containing the Add, Remove,
270
* Up, and Down button.
272
* @param parent The parent control
273
* @return the button box
276
public Composite getButtonBoxControl(Composite parent) {
277
if (buttonBox == null) {
278
buttonBox = new Composite(parent, SWT.NULL);
279
GridLayout layout = new GridLayout();
280
layout.marginWidth = 0;
281
buttonBox.setLayout(layout);
282
createButtons(buttonBox);
283
buttonBox.addDisposeListener(new DisposeListener() {
284
public void widgetDisposed(DisposeEvent event) {
285
addFileButton = null;
295
checkParent(buttonBox, parent);
303
* Returns this field editor's list control.
305
* @param parent The parent control
306
* @return the list control
309
public final List getListControl(Composite parent) {
311
list = new List(parent, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL
313
list.setFont(parent.getFont());
314
list.addSelectionListener(getSelectionListener());
315
list.addDisposeListener(new DisposeListener() {
316
public void widgetDisposed(DisposeEvent event) {
321
checkParent(list, parent);
327
* Returns this field editor's selection listener. The listener is
328
* created if nessessary.
330
* @return the selection listener
332
private SelectionListener getSelectionListener() {
333
if (selectionListener == null) {
334
createSelectionListener();
336
return selectionListener;
340
* Returns this field editor's shell.
342
* This method is internal to the framework; subclassers should not call
349
protected Shell getShell() {
350
if (addFileButton == null) {
353
return addFileButton.getShell();
357
* Notifies that the Remove button has been pressed.
359
private void removePressed() {
360
setPresentsDefaultValue(false);
361
int index = list.getSelectionIndex();
369
* Notifies that the list selection has changed.
372
protected void selectionChanged() {
374
int index = list.getSelectionIndex();
375
int size = list.getItemCount();
377
removeButton.setEnabled(index >= 0);
378
upButton.setEnabled(size > 1 && index > 0);
379
downButton.setEnabled(size > 1 && index >= 0 && index < size - 1);
383
* Moves the currently selected item up or down.
386
* <code>true</code> if the item should move up, and
387
* <code>false</code> if it should move down
389
private void swap(boolean up) {
390
setPresentsDefaultValue(false);
391
int index = list.getSelectionIndex();
392
int target = up ? index - 1 : index + 1;
395
String[] selection = list.getSelection();
396
Assert.isTrue(selection.length == 1);
398
list.add(selection[0], target);
399
list.setSelection(target);
405
* Notifies that the Up button has been pressed.
407
private void upPressed() {
412
* @see FieldEditor.setEnabled(boolean,Composite).
415
public void setEnabled(boolean enabled, Composite parent) {
416
super.setEnabled(enabled, parent);
417
getListControl(parent).setEnabled(enabled);
418
addFileButton.setEnabled(enabled);
419
addDirButton.setEnabled(enabled);
420
removeButton.setEnabled(enabled);
421
upButton.setEnabled(enabled);
422
downButton.setEnabled(enabled);