1 11 package org.eclipse.pde.ui.templates; 12 import org.eclipse.swt.SWT; 13 import org.eclipse.swt.events.ModifyEvent; 14 import org.eclipse.swt.events.ModifyListener; 15 import org.eclipse.swt.layout.GridData; 16 import org.eclipse.swt.widgets.Composite; 17 import org.eclipse.swt.widgets.Label; 18 import org.eclipse.swt.widgets.Text; 19 25 public class StringOption extends TemplateOption { 26 private Text text; 27 private Label labelControl; 28 private boolean ignoreListener; 29 private int fStyle; 30 31 private final static int F_DEFAULT_STYLE = SWT.SINGLE | SWT.BORDER; 32 42 public StringOption(BaseOptionTemplateSection section, String name, 43 String label) { 44 super(section, name, label); 45 fStyle = F_DEFAULT_STYLE; 46 setRequired(true); 47 } 48 49 54 public void setReadOnly(boolean readOnly) { 55 if (readOnly) { 56 fStyle = F_DEFAULT_STYLE | SWT.READ_ONLY; 57 } else { 58 fStyle = F_DEFAULT_STYLE; 59 } 60 } 61 62 68 public String getText() { 69 if (getValue() != null) 70 return getValue().toString(); 71 return null; 72 } 73 81 public void setText(String newText) { 82 setValue(newText); 83 } 84 91 public void setValue(Object value) { 92 super.setValue(value); 93 if (text != null) { 94 ignoreListener = true; 95 String textValue = getText(); 96 text.setText(textValue != null ? textValue : ""); ignoreListener = false; 98 } 99 } 100 108 public void createControl(Composite parent, int span) { 109 labelControl = createLabel(parent, 1); 110 labelControl.setEnabled(isEnabled()); 111 text = new Text(parent, fStyle); 112 if (getValue() != null) 113 text.setText(getValue().toString()); 114 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 115 gd.horizontalSpan = span - 1; 116 text.setLayoutData(gd); 117 text.setEnabled(isEnabled()); 118 text.addModifyListener(new ModifyListener() { 119 public void modifyText(ModifyEvent e) { 120 if (ignoreListener) 121 return; 122 StringOption.super.setValue(text.getText()); 123 getSection().validateOptions(StringOption.this); 124 } 125 }); 126 } 127 132 public boolean isEmpty() { 133 return getValue() == null || getValue().toString().length() == 0; 134 } 135 141 public void setEnabled(boolean enabled) { 142 super.setEnabled(enabled); 143 if (labelControl != null) { 144 labelControl.setEnabled(enabled); 145 text.setEnabled(enabled); 146 } 147 } 148 } 149 | Popular Tags |