1 11 package org.eclipse.jface.preference; 12 13 import org.eclipse.jface.resource.JFaceResources; 14 import org.eclipse.swt.widgets.Composite; 15 import org.eclipse.swt.widgets.Text; 16 17 20 public class IntegerFieldEditor extends StringFieldEditor { 21 private int minValidValue = 0; 22 23 private int maxValidValue = Integer.MAX_VALUE; 24 25 private static final int DEFAULT_TEXT_LIMIT = 10; 26 27 30 protected IntegerFieldEditor() { 31 } 32 33 40 public IntegerFieldEditor(String name, String labelText, Composite parent) { 41 this(name, labelText, parent, DEFAULT_TEXT_LIMIT); 42 } 43 44 52 public IntegerFieldEditor(String name, String labelText, Composite parent, 53 int textLimit) { 54 init(name, labelText); 55 setTextLimit(textLimit); 56 setEmptyStringAllowed(false); 57 setErrorMessage(JFaceResources 58 .getString("IntegerFieldEditor.errorMessage")); createControl(parent); 60 } 61 62 68 public void setValidRange(int min, int max) { 69 minValidValue = min; 70 maxValidValue = max; 71 setErrorMessage(JFaceResources.format( 72 "IntegerFieldEditor.errorMessageRange", new Object [] { new Integer (min), new Integer (max) })); 74 } 75 76 80 protected boolean checkState() { 81 82 Text text = getTextControl(); 83 84 if (text == null) { 85 return false; 86 } 87 88 String numberString = text.getText(); 89 try { 90 int number = Integer.valueOf(numberString).intValue(); 91 if (number >= minValidValue && number <= maxValidValue) { 92 clearErrorMessage(); 93 return true; 94 } 95 96 showErrorMessage(); 97 return false; 98 99 } catch (NumberFormatException e1) { 100 showErrorMessage(); 101 } 102 103 return false; 104 } 105 106 109 protected void doLoad() { 110 Text text = getTextControl(); 111 if (text != null) { 112 int value = getPreferenceStore().getInt(getPreferenceName()); 113 text.setText("" + value); } 115 116 } 117 118 121 protected void doLoadDefault() { 122 Text text = getTextControl(); 123 if (text != null) { 124 int value = getPreferenceStore().getDefaultInt(getPreferenceName()); 125 text.setText("" + value); } 127 valueChanged(); 128 } 129 130 133 protected void doStore() { 134 Text text = getTextControl(); 135 if (text != null) { 136 Integer i = new Integer (text.getText()); 137 getPreferenceStore().setValue(getPreferenceName(), i.intValue()); 138 } 139 } 140 141 148 public int getIntValue() throws NumberFormatException { 149 return new Integer (getStringValue()).intValue(); 150 } 151 } 152 | Popular Tags |