1 11 12 package org.eclipse.pde.internal.ui.editor.validation; 13 14 import org.eclipse.core.resources.IProject; 15 import org.eclipse.swt.events.ModifyEvent; 16 import org.eclipse.swt.events.ModifyListener; 17 import org.eclipse.swt.widgets.Text; 18 import org.eclipse.ui.forms.IManagedForm; 19 20 24 public abstract class TextValidator extends AbstractControlValidator { 25 26 private ModifyListener fModifyListener; 27 28 private boolean fAutoValidate; 29 30 private String fCurrentText; 31 32 38 public TextValidator(IManagedForm managedForm, Text control, 39 IProject project, boolean autoValidate) { 40 super(managedForm, control, project); 41 fAutoValidate = autoValidate; 45 intialize(); 47 } 48 49 52 private void intialize() { 53 fCurrentText = getText().getText(); 55 if (fAutoValidate == false) { 57 return; 58 } 59 createListeners(); 61 if (getEnabled()) { 63 addListeners(); 64 } 65 } 66 67 70 public void setEnabled(boolean enabled) { 71 if (getEnabled() == enabled) { 73 return; 74 } 75 super.setEnabled(enabled); 77 if (fAutoValidate == false) { 79 return; 80 } 81 if (getEnabled()) { 83 addListeners(); 84 } else { 85 removeListeners(); 86 } 87 } 88 89 92 protected void createListeners() { 93 fModifyListener = new ModifyListener() { 94 public void modifyText(ModifyEvent e) { 95 handleModifyTextEvent(e); 96 } 97 }; 98 } 99 100 103 protected void handleModifyTextEvent(ModifyEvent e) { 104 String newText = getText().getText(); 107 if (newText.equals(fCurrentText)) { 108 return; 109 } 110 fCurrentText = newText; 112 validate(); 114 } 115 116 119 protected void addListeners() { 120 getText().addModifyListener(fModifyListener); 121 } 122 123 126 protected void removeListeners() { 127 getText().removeModifyListener(fModifyListener); 128 } 129 130 133 protected Text getText() { 134 return (Text)getControl(); 135 } 136 137 140 protected boolean autoEnable() { 141 if (getText().getEditable() == false) { 143 return false; 144 } 145 return super.autoEnable(); 146 } 147 } 148 | Popular Tags |