1 11 package org.eclipse.team.internal.ui.preferences; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.jface.preference.FieldEditor; 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 31 public class ComboFieldEditor extends FieldEditor { 32 33 36 private Combo fCombo; 37 38 41 private String fValue; 42 43 47 private String [][] fEntryNamesAndValues; 48 49 public ComboFieldEditor(String name, String labelText, String [][] entryNamesAndValues, Composite parent) { 50 init(name, labelText); 51 Assert.isTrue(checkArray(entryNamesAndValues)); 52 fEntryNamesAndValues= entryNamesAndValues; 53 createControl(parent); 54 } 55 56 62 private boolean checkArray(String [][] table) { 63 if (table == null) { 64 return false; 65 } 66 for (int i= 0; i < table.length; i++) { 67 String [] array= table[i]; 68 if (array == null || array.length != 2) { 69 return false; 70 } 71 } 72 return true; 73 } 74 75 78 protected void adjustForNumColumns(int numColumns) { 79 Control control= getLabelControl(); 80 if (control != null) { 81 ((GridData)control.getLayoutData()).horizontalSpan= numColumns; 82 } 83 ((GridData)fCombo.getLayoutData()).horizontalSpan= numColumns; 84 } 85 86 89 protected void doFillIntoGrid(Composite parent, int numColumns) { 90 Control control= getLabelControl(parent); 91 GridData gd= new GridData(); 92 gd.horizontalSpan= numColumns; 93 control.setLayoutData(gd); 94 control= getComboBoxControl(parent); 95 gd= new GridData(); 96 gd.horizontalSpan= numColumns; 97 control.setLayoutData(gd); 98 } 99 100 103 protected void doLoad() { 104 updateComboForValue(getPreferenceStore().getString(getPreferenceName())); 105 } 106 107 110 protected void doLoadDefault() { 111 updateComboForValue(getPreferenceStore().getDefaultString(getPreferenceName())); 112 } 113 114 117 protected void doStore() { 118 if (fValue == null) { 119 getPreferenceStore().setToDefault(getPreferenceName()); 120 return; 121 } 122 123 getPreferenceStore().setValue(getPreferenceName(), fValue); 124 } 125 126 129 public int getNumberOfControls() { 130 return 1; 131 } 132 133 136 public Combo getComboBoxControl(Composite parent) { 137 if (fCombo == null) { 138 fCombo= new Combo(parent, SWT.READ_ONLY); 139 for (int i= 0; i < fEntryNamesAndValues.length; i++) { 140 fCombo.add(fEntryNamesAndValues[i][0], i); 141 } 142 fCombo.setFont(parent.getFont()); 143 fCombo.addSelectionListener(new SelectionAdapter() { 144 public void widgetSelected(SelectionEvent evt) { 145 String oldValue= fValue; 146 String name= fCombo.getText(); 147 fValue= getValueForName(name); 148 setPresentsDefaultValue(false); 149 fireValueChanged(VALUE, oldValue, fValue); 150 } 151 }); 152 } 153 return fCombo; 154 } 155 156 159 protected String getValueForName(String name) { 160 for (int i= 0; i < fEntryNamesAndValues.length; i++) { 161 String [] entry= fEntryNamesAndValues[i]; 162 if (name.equals(entry[0])) { 163 return entry[1]; 164 } 165 } 166 return fEntryNamesAndValues[0][0]; 167 } 168 169 172 protected void updateComboForValue(String value) { 173 fValue= value; 174 for (int i= 0; i < fEntryNamesAndValues.length; i++) { 175 if (value.equals(fEntryNamesAndValues[i][1])) { 176 fCombo.setText(fEntryNamesAndValues[i][0]); 177 return; 178 } 179 } 180 if (fEntryNamesAndValues.length > 0) { 181 fValue= fEntryNamesAndValues[0][1]; 182 fCombo.setText(fEntryNamesAndValues[0][0]); 183 } 184 } 185 } 186 | Popular Tags |