1 11 package org.eclipse.jdt.internal.ui.wizards.dialogfields; 12 13 import org.eclipse.swt.events.ModifyEvent; 14 import org.eclipse.swt.events.ModifyListener; 15 import org.eclipse.swt.events.SelectionEvent; 16 import org.eclipse.swt.events.SelectionListener; 17 import org.eclipse.swt.layout.GridData; 18 import org.eclipse.swt.widgets.Combo; 19 import org.eclipse.swt.widgets.Composite; 20 import org.eclipse.swt.widgets.Control; 21 import org.eclipse.swt.widgets.Label; 22 23 26 public class ComboDialogField extends DialogField { 27 28 private String fText; 29 private int fSelectionIndex; 30 private String [] fItems; 31 private Combo fComboControl; 32 private ModifyListener fModifyListener; 33 private int fFlags; 34 35 public ComboDialogField(int flags) { 36 super(); 37 fText= ""; fItems= new String [0]; 39 fFlags= flags; 40 fSelectionIndex= -1; 41 } 42 43 45 48 public Control[] doFillIntoGrid(Composite parent, int nColumns) { 49 assertEnoughColumns(nColumns); 50 51 Label label= getLabelControl(parent); 52 label.setLayoutData(gridDataForLabel(1)); 53 Combo combo= getComboControl(parent); 54 combo.setLayoutData(gridDataForCombo(nColumns - 1)); 55 56 return new Control[] { label, combo }; 57 } 58 59 62 public int getNumberOfControls() { 63 return 2; 64 } 65 66 protected static GridData gridDataForCombo(int span) { 67 GridData gd= new GridData(); 68 gd.horizontalAlignment= GridData.FILL; 69 gd.grabExcessHorizontalSpace= false; 70 gd.horizontalSpan= span; 71 return gd; 72 } 73 74 76 79 public boolean setFocus() { 80 if (isOkToUse(fComboControl)) { 81 fComboControl.setFocus(); 82 } 83 return true; 84 } 85 86 88 93 public Combo getComboControl(Composite parent) { 94 if (fComboControl == null) { 95 assertCompositeNotNull(parent); 96 fModifyListener= new ModifyListener() { 97 public void modifyText(ModifyEvent e) { 98 doModifyText(e); 99 } 100 }; 101 SelectionListener selectionListener= new SelectionListener() { 102 public void widgetSelected(SelectionEvent e) { 103 doSelectionChanged(e); 104 } 105 106 public void widgetDefaultSelected(SelectionEvent e) { } 107 }; 108 109 fComboControl= new Combo(parent, fFlags); 110 fComboControl.setItems(fItems); 112 if (fSelectionIndex != -1) { 113 fComboControl.select(fSelectionIndex); 114 } else { 115 fComboControl.setText(fText); 116 } 117 fComboControl.setFont(parent.getFont()); 118 fComboControl.addModifyListener(fModifyListener); 119 fComboControl.addSelectionListener(selectionListener); 120 fComboControl.setEnabled(isEnabled()); 121 } 122 return fComboControl; 123 } 124 125 private void doModifyText(ModifyEvent e) { 126 if (isOkToUse(fComboControl)) { 127 fText= fComboControl.getText(); 128 fSelectionIndex= fComboControl.getSelectionIndex(); 129 } 130 dialogFieldChanged(); 131 } 132 133 private void doSelectionChanged(SelectionEvent e) { 134 if (isOkToUse(fComboControl)) { 135 fItems= fComboControl.getItems(); 136 fText= fComboControl.getText(); 137 fSelectionIndex= fComboControl.getSelectionIndex(); 138 } 139 dialogFieldChanged(); 140 } 141 142 144 147 protected void updateEnableState() { 148 super.updateEnableState(); 149 if (isOkToUse(fComboControl)) { 150 fComboControl.setEnabled(isEnabled()); 151 } 152 } 153 154 156 159 public String [] getItems() { 160 return fItems; 161 } 162 163 166 public void setItems(String [] items) { 167 fItems= items; 168 if (isOkToUse(fComboControl)) { 169 fComboControl.setItems(items); 170 } 171 dialogFieldChanged(); 172 } 173 174 177 public String getText() { 178 return fText; 179 } 180 181 184 public void setText(String text) { 185 fText= text; 186 if (isOkToUse(fComboControl)) { 187 fComboControl.setText(text); 188 } else { 189 dialogFieldChanged(); 190 } 191 } 192 193 196 public boolean selectItem(int index) { 197 boolean success= false; 198 if (isOkToUse(fComboControl)) { 199 fComboControl.select(index); 200 success= fComboControl.getSelectionIndex() == index; 201 } else { 202 if (index >= 0 && index < fItems.length) { 203 fText= fItems[index]; 204 fSelectionIndex= index; 205 success= true; 206 } 207 } 208 if (success) { 209 dialogFieldChanged(); 210 } 211 return success; 212 } 213 214 217 public boolean selectItem(String name) { 218 for (int i= 0; i < fItems.length; i++) { 219 if (fItems[i].equals(name)) { 220 return selectItem(i); 221 } 222 } 223 return false; 224 } 225 226 227 public int getSelectionIndex() { 228 return fSelectionIndex; 229 } 230 231 232 235 public void setTextWithoutUpdate(String text) { 236 fText= text; 237 if (isOkToUse(fComboControl)) { 238 fComboControl.removeModifyListener(fModifyListener); 239 fComboControl.setText(text); 240 fComboControl.addModifyListener(fModifyListener); 241 } 242 } 243 244 247 public void refresh() { 248 super.refresh(); 249 setTextWithoutUpdate(fText); 250 } 251 252 } 253 | Popular Tags |