| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2000, 2008 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | * Remy Chi Jian Suen <remy.suen@gmail.com> - Bug 214392 missing implementation of ComboFieldEditor.setEnabled |
| 11 | *******************************************************************************/ |
| 12 | package org.eclipse.jface.preference; |
| 13 | |
| 14 | |
| 15 | import org.eclipse.core.runtime.Assert; |
| 16 | import org.eclipse.swt.SWT; |
| 17 | import org.eclipse.swt.events.SelectionAdapter; |
| 18 | import org.eclipse.swt.events.SelectionEvent; |
| 19 | import org.eclipse.swt.layout.GridData; |
| 20 | import org.eclipse.swt.widgets.Combo; |
| 21 | import org.eclipse.swt.widgets.Composite; |
| 22 | import org.eclipse.swt.widgets.Control; |
| 23 | |
| 24 | /** |
| 25 | * A field editor for a combo box that allows the drop-down selection of one of |
| 26 | * a list of items. |
| 27 | * |
| 28 | * @since 3.3 |
| 29 | */ |
| 30 | public class ComboFieldEditor extends FieldEditor { |
| 31 | |
| 32 | /** |
| 33 | * The <code>Combo</code> widget. |
| 34 | */ |
| 35 | private Combo fCombo; |
| 36 | |
| 37 | /** |
| 38 | * The value (not the name) of the currently selected item in the Combo widget. |
| 39 | */ |
| 40 | private String fValue; |
| 41 | |
| 42 | /** |
| 43 | * The names (labels) and underlying values to populate the combo widget. These should be |
| 44 | * arranged as: { {name1, value1}, {name2, value2}, ...} |
| 45 | */ |
| 46 | private String[][] fEntryNamesAndValues; |
| 47 | |
| 48 | /** |
| 49 | * Create the combo box field editor. |
| 50 | * |
| 51 | * @param name the name of the preference this field editor works on |
| 52 | * @param labelText the label text of the field editor |
| 53 | * @param entryNamesAndValues the names (labels) and underlying values to populate the combo widget. These should be |
| 54 | * arranged as: { {name1, value1}, {name2, value2}, ...} |
| 55 | * @param parent the parent composite |
| 56 | */ |
| 57 | public ComboFieldEditor(String name, String labelText, String[][] entryNamesAndValues, Composite parent) { |
| 58 | init(name, labelText); |
| 59 | Assert.isTrue(checkArray(entryNamesAndValues)); |
| 60 | fEntryNamesAndValues = entryNamesAndValues; |
| 61 | createControl(parent); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Checks whether given <code>String[][]</code> is of "type" |
| 66 | * <code>String[][2]</code>. |
| 67 | * |
| 68 | * @return <code>true</code> if it is ok, and <code>false</code> otherwise |
| 69 | */ |
| 70 | private boolean checkArray(String[][] table) { |
| 71 | if (table == null) { |
| 72 | return false; |
| 73 | } |
| 74 | for (int i = 0; i < table.length; i++) { |
| 75 | String[] array = table[i]; |
| 76 | if (array == null || array.length != 2) { |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | /* (non-Javadoc) |
| 84 | * @see org.eclipse.jface.preference.FieldEditor#adjustForNumColumns(int) |
| 85 | */ |
| 86 | protected void adjustForNumColumns(int numColumns) { |
| 87 | if (numColumns > 1) { |
| 88 | Control control = getLabelControl(); |
| 89 | int left = numColumns; |
| 90 | if (control != null) { |
| 91 | ((GridData)control.getLayoutData()).horizontalSpan = 1; |
| 92 | left = left - 1; |
| 93 | } |
| 94 | ((GridData)fCombo.getLayoutData()).horizontalSpan = left; |
| 95 | } else { |
| 96 | Control control = getLabelControl(); |
| 97 | if (control != null) { |
| 98 | ((GridData)control.getLayoutData()).horizontalSpan = 1; |
| 99 | } |
| 100 | ((GridData)fCombo.getLayoutData()).horizontalSpan = 1; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /* (non-Javadoc) |
| 105 | * @see org.eclipse.jface.preference.FieldEditor#doFillIntoGrid(org.eclipse.swt.widgets.Composite, int) |
| 106 | */ |
| 107 | protected void doFillIntoGrid(Composite parent, int numColumns) { |
| 108 | int comboC = 1; |
| 109 | if (numColumns > 1) { |
| 110 | comboC = numColumns - 1; |
| 111 | } |
| 112 | Control control = getLabelControl(parent); |
| 113 | GridData gd = new GridData(); |
| 114 | gd.horizontalSpan = 1; |
| 115 | control.setLayoutData(gd); |
| 116 | control = getComboBoxControl(parent); |
| 117 | gd = new GridData(); |
| 118 | gd.horizontalSpan = comboC; |
| 119 | gd.horizontalAlignment = GridData.FILL; |
| 120 | control.setLayoutData(gd); |
| 121 | control.setFont(parent.getFont()); |
| 122 | } |
| 123 | |
| 124 | /* (non-Javadoc) |
| 125 | * @see org.eclipse.jface.preference.FieldEditor#doLoad() |
| 126 | */ |
| 127 | protected void doLoad() { |
| 128 | updateComboForValue(getPreferenceStore().getString(getPreferenceName())); |
| 129 | } |
| 130 | |
| 131 | /* (non-Javadoc) |
| 132 | * @see org.eclipse.jface.preference.FieldEditor#doLoadDefault() |
| 133 | */ |
| 134 | protected void doLoadDefault() { |
| 135 | updateComboForValue(getPreferenceStore().getDefaultString(getPreferenceName())); |
| 136 | } |
| 137 | |
| 138 | /* (non-Javadoc) |
| 139 | * @see org.eclipse.jface.preference.FieldEditor#doStore() |
| 140 | */ |
| 141 | protected void doStore() { |
| 142 | if (fValue == null) { |
| 143 | getPreferenceStore().setToDefault(getPreferenceName()); |
| 144 | return; |
| 145 | } |
| 146 | getPreferenceStore().setValue(getPreferenceName(), fValue); |
| 147 | } |
| 148 | |
| 149 | /* (non-Javadoc) |
| 150 | * @see org.eclipse.jface.preference.FieldEditor#getNumberOfControls() |
| 151 | */ |
| 152 | public int getNumberOfControls() { |
| 153 | return 2; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Lazily create and return the Combo control. |
| 158 | */ |
| 159 | private Combo getComboBoxControl(Composite parent) { |
| 160 | if (fCombo == null) { |
| 161 | fCombo = new Combo(parent, SWT.READ_ONLY); |
| 162 | fCombo.setFont(parent.getFont()); |
| 163 | for (int i = 0; i < fEntryNamesAndValues.length; i++) { |
| 164 | fCombo.add(fEntryNamesAndValues[i][0], i); |
| 165 | } |
| 166 | |
| 167 | fCombo.addSelectionListener(new SelectionAdapter() { |
| 168 | public void widgetSelected(SelectionEvent evt) { |
| 169 | String oldValue = fValue; |
| 170 | String name = fCombo.getText(); |
| 171 | fValue = getValueForName(name); |
| 172 | setPresentsDefaultValue(false); |
| 173 | fireValueChanged(VALUE, oldValue, fValue); |
| 174 | } |
| 175 | }); |
| 176 | } |
| 177 | return fCombo; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Given the name (label) of an entry, return the corresponding value. |
| 182 | */ |
| 183 | private String getValueForName(String name) { |
| 184 | for (int i = 0; i < fEntryNamesAndValues.length; i++) { |
| 185 | String[] entry = fEntryNamesAndValues[i]; |
| 186 | if (name.equals(entry[0])) { |
| 187 | return entry[1]; |
| 188 | } |
| 189 | } |
| 190 | return fEntryNamesAndValues[0][0]; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Set the name in the combo widget to match the specified value. |
| 195 | */ |
| 196 | private void updateComboForValue(String value) { |
| 197 | fValue = value; |
| 198 | for (int i = 0; i < fEntryNamesAndValues.length; i++) { |
| 199 | if (value.equals(fEntryNamesAndValues[i][1])) { |
| 200 | fCombo.setText(fEntryNamesAndValues[i][0]); |
| 201 | return; |
| 202 | } |
| 203 | } |
| 204 | if (fEntryNamesAndValues.length > 0) { |
| 205 | fValue = fEntryNamesAndValues[0][1]; |
| 206 | fCombo.setText(fEntryNamesAndValues[0][0]); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * (non-Javadoc) |
| 212 | * |
| 213 | * @see org.eclipse.jface.preference.FieldEditor#setEnabled(boolean, |
| 214 | * org.eclipse.swt.widgets.Composite) |
| 215 | */ |
| 216 | public void setEnabled(boolean enabled, Composite parent) { |
| 217 | super.setEnabled(enabled, parent); |
| 218 | getComboBoxControl(parent).setEnabled(enabled); |
| 219 | } |
| 220 | } |