use of javax.swing.text.DefaultFormatterFactory in project java-swing-tips by aterai.
the class MainPanel method makeFFactory.
private static DefaultFormatterFactory makeFFactory() {
DefaultFormatter formatter = new DefaultFormatter() {
@Override
public Object stringToValue(String text) throws ParseException {
Pattern pattern = Pattern.compile("^\\s*(\\p{XDigit}{1,6})\\s*$");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
Integer iv = Integer.valueOf(text, 16);
if (iv <= Character.MAX_CODE_POINT) {
return iv;
}
}
Toolkit.getDefaultToolkit().beep();
throw new ParseException(text, 0);
// try {
// return Integer.valueOf(text, 16);
// } catch (NumberFormatException ex) {
// Toolkit.getDefaultToolkit().beep();
// ParseException wrap = new ParseException(text, 0);
// wrap.initCause(ex);
// throw wrap;
// }
}
// private static final String MASK = "000000";
@Override
public String valueToString(Object value) {
// String str = MASK + Integer.toHexString((Integer) value).toUpperCase(Locale.ENGLISH);
// int i = str.length() - MASK.length();
// return str.substring(i);
// String s = Integer.toHexString((Integer) value);
// return String.format("%6S", s).replaceAll(" ", "0");
Integer iv = (Integer) value;
return String.format("%06X", iv);
}
};
formatter.setValueClass(Integer.class);
formatter.setOverwriteMode(true);
return new DefaultFormatterFactory(formatter);
}
use of javax.swing.text.DefaultFormatterFactory in project mzmine2 by mzmine.
the class ScatterPlotBottomPanel method actionPerformed.
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("DATA_CHANGE")) {
ScatterPlotAxisSelection optionX = (ScatterPlotAxisSelection) comboX.getSelectedItem();
ScatterPlotAxisSelection optionY = (ScatterPlotAxisSelection) comboY.getSelectedItem();
if ((optionX == null) || (optionY == null))
return;
String foldText = foldXvalues[comboFold.getSelectedIndex()];
int foldValue = Integer.parseInt(foldText);
if (foldValue <= 0)
foldValue = 2;
chart.setDisplayedAxes(optionX, optionY, foldValue);
return;
}
if (command.equals("LABEL_ITEMS")) {
chart.setItemLabels(labeledItems.isSelected());
}
if (command.equals("SEARCH")) {
SearchDefinitionType searchType = (SearchDefinitionType) comboSearchDataType.getSelectedItem();
String searchRegex = txtSearchField.getText();
Number minValue = ((Number) minSearchField.getValue());
if (minValue == null)
minValue = 0;
Number maxValue = ((Number) maxSearchField.getValue());
if (maxValue == null)
maxValue = 0;
Range<Double> searchRange = Range.closed(minValue.doubleValue(), maxValue.doubleValue());
try {
SearchDefinition newSearch = new SearchDefinition(searchType, searchRegex, searchRange);
chart.updateSearchDefinition(newSearch);
} catch (PatternSyntaxException pe) {
MZmineCore.getDesktop().displayErrorMessage(window, "The regular expression's syntax is invalid: " + pe);
}
return;
}
if (command.equals("SEARCH_DATA_TYPE")) {
SearchDefinitionType searchType = (SearchDefinitionType) comboSearchDataType.getSelectedItem();
switch(searchType) {
case MASS:
minSearchField.setVisible(true);
maxSearchField.setVisible(true);
labelRange.setVisible(true);
txtSearchField.setVisible(false);
NumberFormat mzFormatter = MZmineCore.getConfiguration().getMZFormat();
Range<Double> mzRange = peakList.getRowsMZRange();
DefaultFormatterFactory mzFormatFac = new DefaultFormatterFactory(new NumberFormatter(mzFormatter));
minSearchField.setFormatterFactory(mzFormatFac);
minSearchField.setValue(mzRange.lowerEndpoint());
maxSearchField.setFormatterFactory(mzFormatFac);
maxSearchField.setValue(mzRange.upperEndpoint());
break;
case RT:
minSearchField.setVisible(true);
maxSearchField.setVisible(true);
labelRange.setVisible(true);
txtSearchField.setVisible(false);
NumberFormat rtFormatter = MZmineCore.getConfiguration().getRTFormat();
Range<Double> rtRange = peakList.getRowsRTRange();
DefaultFormatterFactory rtFormatFac = new DefaultFormatterFactory(new NumberFormatter(rtFormatter));
minSearchField.setFormatterFactory(rtFormatFac);
minSearchField.setValue(rtRange.lowerEndpoint());
maxSearchField.setFormatterFactory(rtFormatFac);
maxSearchField.setValue(rtRange.upperEndpoint());
break;
case NAME:
minSearchField.setVisible(false);
maxSearchField.setVisible(false);
labelRange.setVisible(false);
txtSearchField.setVisible(true);
break;
}
return;
}
}
use of javax.swing.text.DefaultFormatterFactory in project mzmine2 by mzmine.
the class RangeComponent method setNumberFormat.
public void setNumberFormat(NumberFormat format) {
DefaultFormatterFactory fac = new DefaultFormatterFactory(new NumberFormatter(format));
minTxtField.setFormatterFactory(fac);
maxTxtField.setFormatterFactory(fac);
}
Aggregations