blob: dbd5f9fd9ff2ec3326d5be34ce52bf1619b7f8b6 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
/*******************************************************************************
* Copyright (c) 2000, 2005 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.mylar.internal.tasks.ui;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
/**
* General purpose dialog for selecting an item from a combo box.
*
* @author This was (essentially) copied from Eclipse's internal implementation
* by Wesley Coelho
*/
public class ComboSelectionDialog extends Dialog {
private String fSelection = null;
private final String fShellTitle;
private final String fLabelText;
private final String[] fAllowedStrings;
private final int fInitialSelectionIndex;
private int fSelectedIndex = -1;
public ComboSelectionDialog(Shell parentShell, String shellTitle, String labelText, String[] comboStrings,
int initialSelectionIndex) {
super(parentShell);
fShellTitle = shellTitle;
fLabelText = labelText;
fAllowedStrings = comboStrings;
fInitialSelectionIndex = initialSelectionIndex;
}
public String getSelectedString() {
return fSelection;
}
/*
* @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
*/
@Override
protected Control createDialogArea(Composite parent) {
getShell().setText(fShellTitle);
Composite composite = (Composite) super.createDialogArea(parent);
Composite innerComposite = new Composite(composite, SWT.NONE);
innerComposite.setLayoutData(new GridData());
GridLayout gl = new GridLayout();
gl.numColumns = 2;
innerComposite.setLayout(gl);
Label label = new Label(innerComposite, SWT.NONE);
label.setText(fLabelText);
label.setLayoutData(new GridData());
final Combo combo = new Combo(innerComposite, SWT.READ_ONLY);
for (int i = 0; i < fAllowedStrings.length; i++) {
combo.add(fAllowedStrings[i]);
}
combo.select(fInitialSelectionIndex);
fSelection = combo.getItem(combo.getSelectionIndex());
GridData gd = new GridData();
gd.widthHint = convertWidthInCharsToPixels(getMaxStringLength());
combo.setLayoutData(gd);
combo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
fSelection = combo.getItem(combo.getSelectionIndex());
fSelectedIndex = combo.getSelectionIndex();
}
});
applyDialogFont(composite);
return composite;
}
/**
* Returns the array index of the selected string or -1 if no string was
* selected.
*/
public int getSelectedIndex() {
return fSelectedIndex;
}
private int getMaxStringLength() {
int max = 0;
for (int i = 0; i < fAllowedStrings.length; i++) {
max = Math.max(max, fAllowedStrings[i].length());
}
return max;
}
}
|