package composants;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;
/**
*
* @author nelhassani
*
*/
public final class JSpinnerNumber extends JSpinner {
// Logger log = Logger.getLogger(JSpinnerNumber.class);
private JSpinner.NumberEditor spinnerEditor;
private SpinnerNumberModel model;
private boolean isDouble = false;
private void configEditor() {
spinnerEditor = new NumberEditor(this);
super.setModel(model);
final Document jsDoc = spinnerEditor.getTextField().getDocument();
if (jsDoc instanceof PlainDocument) {
AbstractDocument doc = new PlainDocument() {
private static final long serialVersionUID = 1L;
@Override
public void setDocumentFilter(DocumentFilter filter) {
if (filter instanceof NumericDocumentFilter) {
super.setDocumentFilter(filter);
}
}
};
doc.setDocumentFilter(new NumericDocumentFilter());
spinnerEditor.getTextField().setDocument(doc);
}
this.setEditor(spinnerEditor);
}
// -------------------------------------------------------------------------------
public JSpinnerNumber() {
// log.info("initialisation du JTextNumber");
try {
this.setSize(200, 200);
model = new SpinnerNumberModel(0, 0, 9999, 1);
super.setModel(model);
configEditor();
} catch (Exception e) {
// log.error("Erreur d'initialisation du JTextNumber :", e);
System.err.println("Erreur d'initialisation du JTextNumber :"
+ e.getStackTrace());
}
}
/**
* @param min
* le premier num�ro dans la s�quence
* @param max
* le dernier num�ro dans la s�quence
* @param value
* la valeur actuelle du mod�le
* @param step
* la diff�rence entre les �l�ments de la s�quence
*/
public JSpinnerNumber(int min, int max, int value, int step) {
isDouble = false;
model = new SpinnerNumberModel(value, min, max, step);
super.setModel(model);
configEditor();
}
public JSpinnerNumber(double min, double max, double value, double step) {
model = new SpinnerNumberModel(value, min, max, step);
isDouble = true;
super.setModel(model);
configEditor();
}
private class NumericDocumentFilter extends DocumentFilter {
public boolean checkint(String s) {
try {
int d = Integer.parseInt(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public boolean checkdouble(String s) {
try {
Double.parseDouble(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
@Override
public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {
if (stringContainsOnlyDigits(string))
super.insertString(fb, offset, string, attr);
}
@Override
public void remove(FilterBypass fb, int offset, int length)
throws BadLocationException {
// TODO Auto-generated method stub
super.remove(fb, offset, length);
}
@Override
public void replace(FilterBypass fb, int offset, int length,
String text, AttributeSet attrs) throws BadLocationException {
if (stringContainsOnlyDigits(text))
super.replace(fb, offset, length, text, attrs);
}
private boolean stringContainsOnlyDigits(String text) {
for (int i = 0; i < text.length(); i++) {
if (!Character.isDigit(text.charAt(i))) {
if (isDouble) {
if (text.charAt(i) != ',')
return false;
} else
return false;
}
}
return true;
}
}
}