1 11 package org.eclipse.jface.preference; 12 13 14 import org.eclipse.core.runtime.Assert; 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.events.SelectionAdapter; 17 import org.eclipse.swt.events.SelectionEvent; 18 import org.eclipse.swt.layout.GridData; 19 import org.eclipse.swt.widgets.Combo; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Control; 22 23 29 public class ComboFieldEditor extends FieldEditor { 30 31 34 private Combo fCombo; 35 36 39 private String fValue; 40 41 45 private String [][] fEntryNamesAndValues; 46 47 56 public ComboFieldEditor(String name, String labelText, String [][] entryNamesAndValues, Composite parent) { 57 init(name, labelText); 58 Assert.isTrue(checkArray(entryNamesAndValues)); 59 fEntryNamesAndValues = entryNamesAndValues; 60 createControl(parent); 61 } 62 63 69 private boolean checkArray(String [][] table) { 70 if (table == null) { 71 return false; 72 } 73 for (int i = 0; i < table.length; i++) { 74 String [] array = table[i]; 75 if (array == null || array.length != 2) { 76 return false; 77 } 78 } 79 return true; 80 } 81 82 85 protected void adjustForNumColumns(int numColumns) { 86 if (numColumns > 1) { 87 Control control = getLabelControl(); 88 int left = numColumns; 89 if (control != null) { 90 ((GridData)control.getLayoutData()).horizontalSpan = 1; 91 left = left - 1; 92 } 93 ((GridData)fCombo.getLayoutData()).horizontalSpan = left; 94 } else { 95 Control control = getLabelControl(); 96 if (control != null) { 97 ((GridData)control.getLayoutData()).horizontalSpan = 1; 98 } 99 ((GridData)fCombo.getLayoutData()).horizontalSpan = 1; 100 } 101 } 102 103 106 protected void doFillIntoGrid(Composite parent, int numColumns) { 107 int comboC = 1; 108 if (numColumns > 1) { 109 comboC = numColumns - 1; 110 } 111 Control control = getLabelControl(parent); 112 GridData gd = new GridData(); 113 gd.horizontalSpan = 1; 114 control.setLayoutData(gd); 115 control = getComboBoxControl(parent); 116 gd = new GridData(); 117 gd.horizontalSpan = comboC; 118 gd.horizontalAlignment = GridData.FILL; 119 control.setLayoutData(gd); 120 control.setFont(parent.getFont()); 121 } 122 123 126 protected void doLoad() { 127 updateComboForValue(getPreferenceStore().getString(getPreferenceName())); 128 } 129 130 133 protected void doLoadDefault() { 134 updateComboForValue(getPreferenceStore().getDefaultString(getPreferenceName())); 135 } 136 137 140 protected void doStore() { 141 if (fValue == null) { 142 getPreferenceStore().setToDefault(getPreferenceName()); 143 return; 144 } 145 getPreferenceStore().setValue(getPreferenceName(), fValue); 146 } 147 148 151 public int getNumberOfControls() { 152 return 2; 153 } 154 155 158 private Combo getComboBoxControl(Composite parent) { 159 if (fCombo == null) { 160 fCombo = new Combo(parent, SWT.READ_ONLY); 161 fCombo.setFont(parent.getFont()); 162 for (int i = 0; i < fEntryNamesAndValues.length; i++) { 163 fCombo.add(fEntryNamesAndValues[i][0], i); 164 } 165 166 fCombo.addSelectionListener(new SelectionAdapter() { 167 public void widgetSelected(SelectionEvent evt) { 168 String oldValue = fValue; 169 String name = fCombo.getText(); 170 fValue = getValueForName(name); 171 setPresentsDefaultValue(false); 172 fireValueChanged(VALUE, oldValue, fValue); 173 } 174 }); 175 } 176 return fCombo; 177 } 178 179 182 private String getValueForName(String name) { 183 for (int i = 0; i < fEntryNamesAndValues.length; i++) { 184 String [] entry = fEntryNamesAndValues[i]; 185 if (name.equals(entry[0])) { 186 return entry[1]; 187 } 188 } 189 return fEntryNamesAndValues[0][0]; 190 } 191 192 195 private void updateComboForValue(String value) { 196 fValue = value; 197 for (int i = 0; i < fEntryNamesAndValues.length; i++) { 198 if (value.equals(fEntryNamesAndValues[i][1])) { 199 fCombo.setText(fEntryNamesAndValues[i][0]); 200 return; 201 } 202 } 203 if (fEntryNamesAndValues.length > 0) { 204 fValue = fEntryNamesAndValues[0][1]; 205 fCombo.setText(fEntryNamesAndValues[0][0]); 206 } 207 } 208 } 209 | Popular Tags |