1 11 package org.eclipse.jdt.internal.debug.ui.launcher; 12 13 14 import org.eclipse.jface.preference.FieldEditor; 15 import org.eclipse.jface.util.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 27 public class ComboFieldEditor extends FieldEditor { 28 29 32 private Combo fCombo; 33 34 37 private String fValue; 38 39 43 private String [][] fEntryNamesAndValues; 44 45 public ComboFieldEditor(String name, String labelText, String [][] entryNamesAndValues, Composite parent) { 46 init(name, labelText); 47 Assert.isTrue(checkArray(entryNamesAndValues)); 48 fEntryNamesAndValues = entryNamesAndValues; 49 createControl(parent); 50 } 51 52 58 private boolean checkArray(String [][] table) { 59 if (table == null) { 60 return false; 61 } 62 for (int i = 0; i < table.length; i++) { 63 String [] array = table[i]; 64 if (array == null || array.length != 2) { 65 return false; 66 } 67 } 68 return true; 69 } 70 71 74 protected void adjustForNumColumns(int numColumns) { 75 if (numColumns > 1) { 76 Control control = getLabelControl(); 77 int left = numColumns; 78 if (control != null) { 79 ((GridData)control.getLayoutData()).horizontalSpan = 1; 80 left = left - 1; 81 } 82 ((GridData)fCombo.getLayoutData()).horizontalSpan = left; 83 } else { 84 Control control = getLabelControl(); 85 if (control != null) { 86 ((GridData)control.getLayoutData()).horizontalSpan = 1; 87 } 88 ((GridData)fCombo.getLayoutData()).horizontalSpan = 1; 89 } 90 } 91 92 95 protected void doFillIntoGrid(Composite parent, int numColumns) { 96 int comboC = 1; 97 if (numColumns > 1) { 98 comboC = numColumns - 1; 99 } 100 Control control = getLabelControl(parent); 101 GridData gd = new GridData(); 102 gd.horizontalSpan = 1; 103 control.setLayoutData(gd); 104 control = getComboBoxControl(parent); 105 gd = new GridData(); 106 gd.horizontalSpan = comboC; 107 gd.horizontalAlignment = GridData.FILL; 108 control.setLayoutData(gd); 109 control.setFont(parent.getFont()); 110 } 111 112 115 protected void doLoad() { 116 updateComboForValue(getPreferenceStore().getString(getPreferenceName())); 117 } 118 119 122 protected void doLoadDefault() { 123 updateComboForValue(getPreferenceStore().getDefaultString(getPreferenceName())); 124 } 125 126 129 protected void doStore() { 130 if (fValue == null) { 131 getPreferenceStore().setToDefault(getPreferenceName()); 132 return; 133 } 134 135 getPreferenceStore().setValue(getPreferenceName(), fValue); 136 } 137 138 141 public int getNumberOfControls() { 142 return 1; 143 } 144 145 148 public Combo getComboBoxControl(Composite parent) { 149 if (fCombo == null) { 150 fCombo = new Combo(parent, SWT.READ_ONLY); 151 fCombo.setFont(parent.getFont()); 152 for (int i = 0; i < fEntryNamesAndValues.length; i++) { 153 fCombo.add(fEntryNamesAndValues[i][0], i); 154 } 155 156 fCombo.addSelectionListener(new SelectionAdapter() { 157 public void widgetSelected(SelectionEvent evt) { 158 String oldValue = fValue; 159 String name = fCombo.getText(); 160 fValue = getValueForName(name); 161 setPresentsDefaultValue(false); 162 fireValueChanged(VALUE, oldValue, fValue); 163 } 164 }); 165 } 166 return fCombo; 167 } 168 169 172 protected String getValueForName(String name) { 173 for (int i = 0; i < fEntryNamesAndValues.length; i++) { 174 String [] entry = fEntryNamesAndValues[i]; 175 if (name.equals(entry[0])) { 176 return entry[1]; 177 } 178 } 179 return fEntryNamesAndValues[0][0]; 180 } 181 182 185 protected void updateComboForValue(String value) { 186 fValue = value; 187 for (int i = 0; i < fEntryNamesAndValues.length; i++) { 188 if (value.equals(fEntryNamesAndValues[i][1])) { 189 fCombo.setText(fEntryNamesAndValues[i][0]); 190 return; 191 } 192 } 193 if (fEntryNamesAndValues.length > 0) { 194 fValue = fEntryNamesAndValues[0][1]; 195 } 196 } 197 } 198 | Popular Tags |