1 11 package org.eclipse.jdt.internal.ui.wizards.dialogfields; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.events.ModifyEvent; 15 import org.eclipse.swt.events.ModifyListener; 16 import org.eclipse.swt.layout.GridData; 17 import org.eclipse.swt.widgets.Composite; 18 import org.eclipse.swt.widgets.Control; 19 import org.eclipse.swt.widgets.Label; 20 import org.eclipse.swt.widgets.Text; 21 22 import org.eclipse.jface.text.contentassist.IContentAssistProcessor; 23 24 import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper; 25 26 29 public class StringDialogField extends DialogField { 30 31 private String fText; 32 private Text fTextControl; 33 private ModifyListener fModifyListener; 34 private IContentAssistProcessor fContentAssistProcessor; 35 36 public StringDialogField() { 37 super(); 38 fText= ""; } 40 41 public void setContentAssistProcessor(IContentAssistProcessor processor) { 42 fContentAssistProcessor= processor; 43 if (fContentAssistProcessor != null && isOkToUse(fTextControl)) { 44 ControlContentAssistHelper.createTextContentAssistant(fTextControl, fContentAssistProcessor); 45 } 46 } 47 48 public IContentAssistProcessor getContentAssistProcessor() { 49 return fContentAssistProcessor; 50 } 51 52 54 57 public Control[] doFillIntoGrid(Composite parent, int nColumns) { 58 assertEnoughColumns(nColumns); 59 60 Label label= getLabelControl(parent); 61 label.setLayoutData(gridDataForLabel(1)); 62 Text text= getTextControl(parent); 63 text.setLayoutData(gridDataForText(nColumns - 1)); 64 65 return new Control[] { label, text }; 66 } 67 68 71 public int getNumberOfControls() { 72 return 2; 73 } 74 75 protected static GridData gridDataForText(int span) { 76 GridData gd= new GridData(); 77 gd.horizontalAlignment= GridData.FILL; 78 gd.grabExcessHorizontalSpace= false; 79 gd.horizontalSpan= span; 80 return gd; 81 } 82 83 85 88 public boolean setFocus() { 89 if (isOkToUse(fTextControl)) { 90 fTextControl.setFocus(); 91 fTextControl.setSelection(0, fTextControl.getText().length()); 92 } 93 return true; 94 } 95 96 98 103 public Text getTextControl(Composite parent) { 104 if (fTextControl == null) { 105 assertCompositeNotNull(parent); 106 fModifyListener= new ModifyListener() { 107 public void modifyText(ModifyEvent e) { 108 doModifyText(e); 109 } 110 }; 111 112 fTextControl= new Text(parent, SWT.SINGLE | SWT.BORDER); 113 fTextControl.setText(fText); 115 fTextControl.setFont(parent.getFont()); 116 fTextControl.addModifyListener(fModifyListener); 117 118 fTextControl.setEnabled(isEnabled()); 119 if (fContentAssistProcessor != null) { 120 ControlContentAssistHelper.createTextContentAssistant(fTextControl, fContentAssistProcessor); 121 } 122 } 123 return fTextControl; 124 } 125 126 private void doModifyText(ModifyEvent e) { 127 if (isOkToUse(fTextControl)) { 128 fText= fTextControl.getText(); 129 } 130 dialogFieldChanged(); 131 } 132 133 135 138 protected void updateEnableState() { 139 super.updateEnableState(); 140 if (isOkToUse(fTextControl)) { 141 fTextControl.setEnabled(isEnabled()); 142 } 143 } 144 145 147 150 public String getText() { 151 return fText; 152 } 153 154 157 public void setText(String text) { 158 fText= text; 159 if (isOkToUse(fTextControl)) { 160 fTextControl.setText(text); 161 } else { 162 dialogFieldChanged(); 163 } 164 } 165 166 169 public void setTextWithoutUpdate(String text) { 170 fText= text; 171 if (isOkToUse(fTextControl)) { 172 fTextControl.removeModifyListener(fModifyListener); 173 fTextControl.setText(text); 174 fTextControl.addModifyListener(fModifyListener); 175 } 176 } 177 178 181 public void refresh() { 182 super.refresh(); 183 if (isOkToUse(fTextControl)) { 184 setTextWithoutUpdate(fText); 185 } 186 } 187 188 } 189 | Popular Tags |