1 11 package org.eclipse.search.internal.ui.util; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.events.SelectionAdapter; 15 import org.eclipse.swt.events.SelectionEvent; 16 import org.eclipse.swt.layout.GridData; 17 import org.eclipse.swt.widgets.Combo; 18 import org.eclipse.swt.widgets.Composite; 19 import org.eclipse.swt.widgets.Control; 20 21 import org.eclipse.jface.preference.FieldEditor; 22 import org.eclipse.jface.util.Assert; 23 24 32 public class ComboFieldEditor extends FieldEditor { 33 34 37 private Combo fCombo; 38 39 42 private String fValue; 43 44 48 private String [][] fEntryNamesAndValues; 49 50 public ComboFieldEditor(String name, String labelText, String [][] entryNamesAndValues, Composite parent) { 51 init(name, labelText); 52 Assert.isTrue(checkArray(entryNamesAndValues)); 53 fEntryNamesAndValues= entryNamesAndValues; 54 createControl(parent); 55 } 56 57 63 private boolean checkArray(String [][] table) { 64 if (table == null) { 65 return false; 66 } 67 for (int i= 0; i < table.length; i++) { 68 String [] array= table[i]; 69 if (array == null || array.length != 2) { 70 return false; 71 } 72 } 73 return true; 74 } 75 76 79 protected void adjustForNumColumns(int numColumns) { 80 Control control= getLabelControl(); 81 if (control != null) { 82 ((GridData)control.getLayoutData()).horizontalSpan= numColumns-1; 83 numColumns=1; 84 } 85 ((GridData)fCombo.getLayoutData()).horizontalSpan= numColumns; 86 } 87 88 91 protected void doFillIntoGrid(Composite parent, int numColumns) { 92 Control control= getLabelControl(parent); 93 GridData gd= new GridData(); 94 gd.horizontalSpan= numColumns; 95 control.setLayoutData(gd); 96 control= getComboBoxControl(parent); 97 gd= new GridData(); 98 gd.horizontalSpan= numColumns; 99 control.setLayoutData(gd); 100 } 101 102 105 protected void doLoad() { 106 updateComboForValue(getPreferenceStore().getString(getPreferenceName())); 107 } 108 109 112 protected void doLoadDefault() { 113 updateComboForValue(getPreferenceStore().getDefaultString(getPreferenceName())); 114 } 115 116 119 protected void doStore() { 120 if (fValue == null) { 121 getPreferenceStore().setToDefault(getPreferenceName()); 122 return; 123 } 124 125 getPreferenceStore().setValue(getPreferenceName(), fValue); 126 } 127 128 131 public int getNumberOfControls() { 132 return 2; 133 } 134 135 138 public Combo getComboBoxControl(Composite parent) { 139 if (fCombo == null) { 140 fCombo= new Combo(parent, SWT.READ_ONLY); 141 for (int i= 0; i < fEntryNamesAndValues.length; i++) { 142 fCombo.add(fEntryNamesAndValues[i][0], i); 143 } 144 fCombo.setFont(parent.getFont()); 145 fCombo.addSelectionListener(new SelectionAdapter() { 146 public void widgetSelected(SelectionEvent evt) { 147 String oldValue= fValue; 148 String name= fCombo.getText(); 149 fValue= getValueForName(name); 150 setPresentsDefaultValue(false); 151 fireValueChanged(VALUE, oldValue, fValue); 152 } 153 }); 154 } 155 return fCombo; 156 } 157 158 161 protected String getValueForName(String name) { 162 for (int i= 0; i < fEntryNamesAndValues.length; i++) { 163 String [] entry= fEntryNamesAndValues[i]; 164 if (name.equals(entry[0])) { 165 return entry[1]; 166 } 167 } 168 return fEntryNamesAndValues[0][0]; 169 } 170 171 174 protected void updateComboForValue(String value) { 175 fValue= value; 176 for (int i= 0; i < fEntryNamesAndValues.length; i++) { 177 if (value.equals(fEntryNamesAndValues[i][1])) { 178 fCombo.setText(fEntryNamesAndValues[i][0]); 179 return; 180 } 181 } 182 if (fEntryNamesAndValues.length > 0) { 183 fValue= fEntryNamesAndValues[0][1]; 184 fCombo.setText(fEntryNamesAndValues[0][0]); 185 } 186 } 187 } 188 | Popular Tags |