Search in sources :

Example 11 with DefaultFormatterFactory

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);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) DefaultFormatter(javax.swing.text.DefaultFormatter) ParseException(java.text.ParseException) DefaultFormatterFactory(javax.swing.text.DefaultFormatterFactory)

Example 12 with DefaultFormatterFactory

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;
    }
}
Also used : SearchDefinitionType(net.sf.mzmine.util.SearchDefinitionType) DefaultFormatterFactory(javax.swing.text.DefaultFormatterFactory) SearchDefinition(net.sf.mzmine.util.SearchDefinition) PatternSyntaxException(java.util.regex.PatternSyntaxException) NumberFormat(java.text.NumberFormat) NumberFormatter(javax.swing.text.NumberFormatter)

Example 13 with DefaultFormatterFactory

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);
}
Also used : DefaultFormatterFactory(javax.swing.text.DefaultFormatterFactory) NumberFormatter(javax.swing.text.NumberFormatter)

Aggregations

DefaultFormatterFactory (javax.swing.text.DefaultFormatterFactory)13 NumberFormatter (javax.swing.text.NumberFormatter)9 DecimalFormat (java.text.DecimalFormat)5 NumberFormat (java.text.NumberFormat)3 ParseException (java.text.ParseException)3 JFormattedTextField (javax.swing.JFormattedTextField)2 RegExFormatter (com.intellij.codeInspection.ui.RegExFormatter)1 DocumentAdapter (com.intellij.ui.DocumentAdapter)1 RegexFormatter (io.github.satr.idea.plugin.connector.la.ui.components.RegexFormatter)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 DocumentEvent (javax.swing.event.DocumentEvent)1 DefaultFormatter (javax.swing.text.DefaultFormatter)1 Document (javax.swing.text.Document)1 SearchDefinition (net.sf.mzmine.util.SearchDefinition)1 SearchDefinitionType (net.sf.mzmine.util.SearchDefinitionType)1 NotNull (org.jetbrains.annotations.NotNull)1