~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/preferences/MacroProposalsPreferencePage.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
8
 *
 
9
 * Contributors:
 
10
 *    Alphonse Van Assche - initial API and implementation
 
11
 *    Wind River Systems, Inc - compile fixes
 
12
 *******************************************************************************/
 
13
 
 
14
package org.eclipse.linuxtools.internal.rpm.ui.editor.preferences;
 
15
 
 
16
import java.util.ArrayList;
 
17
import java.util.StringTokenizer;
 
18
 
 
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;
 
43
 
 
44
/**
 
45
 * RPM macro proposals and hover preference page class.
 
46
 * 
 
47
 */
 
48
public class MacroProposalsPreferencePage extends FieldEditorPreferencePage
 
49
                implements IWorkbenchPreferencePage {
 
50
 
 
51
        public MacroProposalsPreferencePage() {
 
52
                super(FLAT);
 
53
                setPreferenceStore(Activator.getDefault().getPreferenceStore());
 
54
        }
 
55
 
 
56
        public void init(IWorkbench workbench) {
 
57
        }
 
58
 
 
59
        @Override
 
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,
 
68
                                1,
 
69
                                new String[][] {
 
70
                                                {
 
71
                                                                Messages.MacroProposalsPreferencePage_2,
 
72
                                                                PreferenceConstants.P_MACRO_HOVER_CONTENT_VIEWDESCRIPTION },
 
73
                                                {
 
74
                                                                Messages.MacroProposalsPreferencePage_3,
 
75
                                                                PreferenceConstants.P_MACRO_HOVER_CONTENT_VIEWCONTENT } },
 
76
                                getFieldEditorParent(), true);
 
77
                addField(macroHoverListEditor);
 
78
        }
 
79
 
 
80
        class MacroListEditor extends ListEditor {
 
81
 
 
82
                /**
 
83
                 * The list widget; <code>null</code> if none (before creation or
 
84
                 * after disposal).
 
85
                 */
 
86
                private List list;
 
87
 
 
88
                /**
 
89
                 * The button box containing the Add, Remove, Up, and Down buttons;
 
90
                 * <code>null</code> if none (before creation or after disposal).
 
91
                 */
 
92
                private Composite buttonBox;
 
93
 
 
94
                /**
 
95
                 * The Add file button.
 
96
                 */
 
97
                private Button addFileButton;
 
98
                
 
99
                /**
 
100
                 * The Add button.
 
101
                 */
 
102
                private Button addDirButton;
 
103
 
 
104
                /**
 
105
                 * The Remove button.
 
106
                 */
 
107
                private Button removeButton;
 
108
 
 
109
                /**
 
110
                 * The Up button.
 
111
                 */
 
112
                private Button upButton;
 
113
 
 
114
                /**
 
115
                 * The Down button.
 
116
                 */
 
117
                private Button downButton;
 
118
 
 
119
                /**
 
120
                 * The selection listener.
 
121
                 */
 
122
                private SelectionListener selectionListener;
 
123
 
 
124
                public MacroListEditor(String name, String labelText, Composite parent) {
 
125
                        super();
 
126
                        init(name, labelText);
 
127
                        createControl(parent);
 
128
                        list = getListControl(parent);
 
129
                }
 
130
 
 
131
                @Override
 
132
                protected String createList(String[] items) {
 
133
                        StringBuilder path = new StringBuilder(""); //$NON-NLS-1$
 
134
                        for (String item:items) {
 
135
                                path.append(item);
 
136
                                path.append(";"); //$NON-NLS-1$
 
137
                        }
 
138
                        return path.toString();
 
139
                }
 
140
 
 
141
                @Override
 
142
                protected String getNewInputObject() {
 
143
                        FileDialog dialog = new FileDialog(getShell());
 
144
                        return dialog.open();
 
145
                }
 
146
                
 
147
                protected String getNewDirInputObject() {
 
148
                        DirectoryDialog dialog = new DirectoryDialog(getShell());
 
149
                        return dialog.open();
 
150
                }
 
151
 
 
152
                @Override
 
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());
 
158
                        }
 
159
                        return v.toArray(new String[v.size()]);
 
160
                }
 
161
 
 
162
                /**
 
163
                 * Notifies that the Add button has been pressed.
 
164
                 */
 
165
                private void addFilePressed() {
 
166
                        setPresentsDefaultValue(false);
 
167
                        String input = getNewInputObject();
 
168
 
 
169
                        if (input != null) {
 
170
                                int index = list.getSelectionIndex();
 
171
                                if (index >= 0) {
 
172
                                        list.add(input, index + 1);
 
173
                                } else {
 
174
                                        list.add(input, 0);
 
175
                                }
 
176
                                selectionChanged();
 
177
                        }
 
178
                }
 
179
                
 
180
                /**
 
181
                 * Notifies that the Add button has been pressed.
 
182
                 */
 
183
                private void addDirPressed() {
 
184
                        setPresentsDefaultValue(false);
 
185
                        String input = getNewDirInputObject();
 
186
 
 
187
                        if (input != null) {
 
188
                                int index = list.getSelectionIndex();
 
189
                                if (index >= 0) {
 
190
                                        list.add(input, index + 1);
 
191
                                } else {
 
192
                                        list.add(input, 0);
 
193
                                }
 
194
                                selectionChanged();
 
195
                        }
 
196
                }
 
197
 
 
198
                /**
 
199
                 * Creates the Add, Remove, Up, and Down button in the given button box.
 
200
                 * 
 
201
                 * @param box
 
202
                 *            the box for the buttons
 
203
                 */
 
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$
 
210
                }
 
211
 
 
212
                /**
 
213
                 * Helper method to create a push button.
 
214
                 * 
 
215
                 * @param parent
 
216
                 *            the parent control
 
217
                 * @param key
 
218
                 *            the resource name used to supply the button's label text
 
219
                 * @return Button
 
220
                 */
 
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());
 
232
                        return button;
 
233
                }
 
234
 
 
235
                /**
 
236
                 * Creates a selection listener.
 
237
                 */
 
238
                @Override
 
239
                public void createSelectionListener() {
 
240
                        selectionListener = new SelectionAdapter() {
 
241
                                @Override
 
242
                                public void widgetSelected(SelectionEvent event) {
 
243
                                        Widget widget = event.widget;
 
244
                                        if (widget == addFileButton) {
 
245
                                                addFilePressed();
 
246
                                        } else if (widget == addDirButton) {
 
247
                                                addDirPressed();
 
248
                                        }else if (widget == removeButton) {
 
249
                                                removePressed();
 
250
                                        } else if (widget == upButton) {
 
251
                                                upPressed();
 
252
                                        } else if (widget == downButton) {
 
253
                                                downPressed();
 
254
                                        } else if (widget == list) {
 
255
                                                selectionChanged();
 
256
                                        }
 
257
                                }
 
258
                        };
 
259
                }
 
260
 
 
261
                /**
 
262
                 * Notifies that the Down button has been pressed.
 
263
                 */
 
264
                private void downPressed() {
 
265
                        swap(false);
 
266
                }
 
267
 
 
268
                /**
 
269
                 * Returns this field editor's button box containing the Add, Remove,
 
270
                 * Up, and Down button.
 
271
                 * 
 
272
                 * @param parent The parent control
 
273
                 * @return the button box
 
274
                 */
 
275
                @Override
 
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;
 
286
                                                addDirButton = null;
 
287
                                                removeButton = null;
 
288
                                                upButton = null;
 
289
                                                downButton = null;
 
290
                                                buttonBox = null;
 
291
                                        }
 
292
                                });
 
293
 
 
294
                        } else {
 
295
                                checkParent(buttonBox, parent);
 
296
                        }
 
297
 
 
298
                        selectionChanged();
 
299
                        return buttonBox;
 
300
                }
 
301
 
 
302
                /**
 
303
                 * Returns this field editor's list control.
 
304
                 * 
 
305
                 * @param parent The parent control
 
306
                 * @return the list control
 
307
                 */
 
308
                @Override
 
309
                public final List getListControl(Composite parent) {
 
310
                        if (list == null) {
 
311
                                list = new List(parent, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL
 
312
                                                | SWT.H_SCROLL);
 
313
                                list.setFont(parent.getFont());
 
314
                                list.addSelectionListener(getSelectionListener());
 
315
                                list.addDisposeListener(new DisposeListener() {
 
316
                                        public void widgetDisposed(DisposeEvent event) {
 
317
                                                list = null;
 
318
                                        }
 
319
                                });
 
320
                        } else {
 
321
                                checkParent(list, parent);
 
322
                        }
 
323
                        return list;
 
324
                }
 
325
 
 
326
                /**
 
327
                 * Returns this field editor's selection listener. The listener is
 
328
                 * created if nessessary.
 
329
                 * 
 
330
                 * @return the selection listener
 
331
                 */
 
332
                private SelectionListener getSelectionListener() {
 
333
                        if (selectionListener == null) {
 
334
                                createSelectionListener();
 
335
                        }
 
336
                        return selectionListener;
 
337
                }
 
338
 
 
339
                /**
 
340
                 * Returns this field editor's shell.
 
341
                 * <p>
 
342
                 * This method is internal to the framework; subclassers should not call
 
343
                 * this method.
 
344
                 * </p>
 
345
                 * 
 
346
                 * @return the shell
 
347
                 */
 
348
                @Override
 
349
                protected Shell getShell() {
 
350
                        if (addFileButton == null) {
 
351
                                return null;
 
352
                        }
 
353
                        return addFileButton.getShell();
 
354
                }
 
355
 
 
356
                /**
 
357
                 * Notifies that the Remove button has been pressed.
 
358
                 */
 
359
                private void removePressed() {
 
360
                        setPresentsDefaultValue(false);
 
361
                        int index = list.getSelectionIndex();
 
362
                        if (index >= 0) {
 
363
                                list.remove(index);
 
364
                                selectionChanged();
 
365
                        }
 
366
                }
 
367
 
 
368
                /**
 
369
                 * Notifies that the list selection has changed.
 
370
                 */
 
371
                @Override
 
372
                protected void selectionChanged() {
 
373
 
 
374
                        int index = list.getSelectionIndex();
 
375
                        int size = list.getItemCount();
 
376
 
 
377
                        removeButton.setEnabled(index >= 0);
 
378
                        upButton.setEnabled(size > 1 && index > 0);
 
379
                        downButton.setEnabled(size > 1 && index >= 0 && index < size - 1);
 
380
                }
 
381
 
 
382
                /**
 
383
                 * Moves the currently selected item up or down.
 
384
                 * 
 
385
                 * @param up
 
386
                 *            <code>true</code> if the item should move up, and
 
387
                 *            <code>false</code> if it should move down
 
388
                 */
 
389
                private void swap(boolean up) {
 
390
                        setPresentsDefaultValue(false);
 
391
                        int index = list.getSelectionIndex();
 
392
                        int target = up ? index - 1 : index + 1;
 
393
 
 
394
                        if (index >= 0) {
 
395
                                String[] selection = list.getSelection();
 
396
                                Assert.isTrue(selection.length == 1);
 
397
                                list.remove(index);
 
398
                                list.add(selection[0], target);
 
399
                                list.setSelection(target);
 
400
                        }
 
401
                        selectionChanged();
 
402
                }
 
403
 
 
404
                /**
 
405
                 * Notifies that the Up button has been pressed.
 
406
                 */
 
407
                private void upPressed() {
 
408
                        swap(true);
 
409
                }
 
410
 
 
411
                /*
 
412
                 * @see FieldEditor.setEnabled(boolean,Composite).
 
413
                 */
 
414
                @Override
 
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);
 
423
                }
 
424
 
 
425
        }
 
426
 
 
427
}