Search in sources :

Example 6 with MaskFormatter

use of javax.swing.text.MaskFormatter in project asl-sensor-suite by usgs.

the class InputPanel method loadMetadata.

private void loadMetadata(int panelLoadingRespFrom, JButton respButton) {
    // this section produces a selection box to make sure FDSN loading is user preference
    {
        JRadioButton local = new JRadioButton("Load from local RESP or XML file");
        JRadioButton fdsn = new JRadioButton("Load from FDSN");
        ButtonGroup group = new ButtonGroup();
        group.add(local);
        group.add(fdsn);
        local.setSelected(true);
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(local);
        panel.add(fdsn);
        int result = JOptionPane.showConfirmDialog(this, panel, "Select load method", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.CANCEL_OPTION) {
            return;
        }
        if (local.isSelected()) {
            loadLocalMetadata(panelLoadingRespFrom, respButton);
            return;
        }
    }
    try {
        MaskFormatter networkFormatter = new MaskFormatter("AA");
        // MaskFormatter stationFormatter = new MaskFormatter("UUUUU");
        MaskFormatter locationFormatter = new MaskFormatter("##");
        MaskFormatter channelFormatter = new MaskFormatter("UUA");
        JFormattedTextField networkField = new JFormattedTextField(networkFormatter);
        networkField.setText("");
        networkField.setMinimumSize(networkField.getPreferredSize());
        JTextField stationField = new JTextField();
        stationField.setText("");
        stationField.setMinimumSize(stationField.getPreferredSize());
        JFormattedTextField locationField = new JFormattedTextField(locationFormatter);
        locationField.setText("");
        locationField.setMinimumSize(locationField.getPreferredSize());
        JFormattedTextField channelField = new JFormattedTextField(channelFormatter);
        channelField.setText("");
        channelField.setMinimumSize(channelField.getPreferredSize());
        if (dataStore.blockIsSet(panelLoadingRespFrom)) {
            String name = dataStore.getBlock(panelLoadingRespFrom).getName();
            String[] fields = name.split("_");
            networkField.setText(fields[0]);
            stationField.setText(fields[1]);
            locationField.setText(fields[2]);
            channelField.setText(fields[3]);
        }
        Date start = null;
        // set initial start and end times to be 2 days ago and 1 day ago at day start
        // such that there should be data for the full day's length that already exists
        Date defaultStartValue = Date.from(LocalDate.now().minusDays(2).atStartOfDay(ZoneOffset.UTC).toInstant());
        // if there is any data, enforce range restriction on that limit
        if (dataStore.areAnyBlocksSet(activePlots)) {
            Pair<Long, Long> startAndEnd = dataStore.getCommonTime(activePlots);
            start = Date.from(Instant.ofEpochMilli(startAndEnd.getFirst()));
            defaultStartValue = start;
        }
        JSpinner startPicker = timePickerFactory(start);
        SimpleDateFormat format = ((JSpinner.DateEditor) startPicker.getEditor()).getFormat();
        format.setTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC));
        startPicker.setValue(defaultStartValue);
        JPanel queryPanel = new JPanel();
        queryPanel.setLayout(new GridLayout(7, 2));
        queryPanel.add(new JLabel("NOTE:"));
        queryPanel.add(new JLabel("Wildcards are not supported!"));
        queryPanel.add(new JLabel("Network: (ex: IU)"));
        queryPanel.add(networkField);
        queryPanel.add(new JLabel("Station: (ex: ANMO)"));
        queryPanel.add(stationField);
        queryPanel.add(new JLabel("Location: (ex: 00)"));
        queryPanel.add(locationField);
        queryPanel.add(new JLabel("Channel: (ex: LHZ)"));
        queryPanel.add(channelField);
        queryPanel.add(new JLabel("Time within expected epoch (UTC):"));
        queryPanel.add(startPicker);
        int result = JOptionPane.showConfirmDialog(this, queryPanel, "Set FDSN query parameters", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String net = networkField.getText().toUpperCase();
            String sta = stationField.getText().replaceAll("\\s", "").toUpperCase();
            String loc = locationField.getText().replaceAll("\\s", "").toUpperCase();
            String cha = channelField.getText().toUpperCase();
            Date startDate = (Date) startPicker.getValue();
            long startMillis = startDate.toInstant().toEpochMilli();
            threadedMetaFromFDSN(panelLoadingRespFrom, net, sta, loc, cha, startMillis);
        }
    } catch (ParseException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(this, "ERROR CREATING FDSN QUERY DIALOG BOX", "FDSN ERROR", JOptionPane.ERROR_MESSAGE);
    }
}
Also used : JPanel(javax.swing.JPanel) JRadioButton(javax.swing.JRadioButton) BoxLayout(javax.swing.BoxLayout) JFormattedTextField(javax.swing.JFormattedTextField) JLabel(javax.swing.JLabel) MaskFormatter(javax.swing.text.MaskFormatter) JTextField(javax.swing.JTextField) Date(java.util.Date) LocalDate(java.time.LocalDate) GridLayout(java.awt.GridLayout) ButtonGroup(javax.swing.ButtonGroup) JSpinner(javax.swing.JSpinner) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 7 with MaskFormatter

use of javax.swing.text.MaskFormatter in project servoy-client by Servoy.

the class JSUtils method js_stringFormat.

/**
 * Format a string using mask.
 *
 * @sample
 * var formattedString = utils.stringFormat('0771231231', '(###)####-###'); //returns (077)1231-231
 *
 * @param text the string to format
 * @param format the format
 * @return the resulting text
 */
@JSFunction
public String js_stringFormat(String text, String mask) {
    MaskFormatter mf;
    try {
        mf = new MaskFormatter(mask);
        mf.setValueContainsLiteralCharacters(false);
        return mf.valueToString(text);
    } catch (ParseException e1) {
        // $NON-NLS-1$
        Debug.error("Error when formmating string", e1);
    }
    return null;
}
Also used : MaskFormatter(javax.swing.text.MaskFormatter) ParseException(java.text.ParseException) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 8 with MaskFormatter

use of javax.swing.text.MaskFormatter in project servoy-client by Servoy.

the class DayButton method showDialog.

public int showDialog(String pattern) {
    String pat = getDisplayPattern(pattern);
    // $NON-NLS-1$
    int tmp = pat.toLowerCase().indexOf("hh:mm");
    if (tmp == -1) {
        // $NON-NLS-1$
        tmp = pat.toLowerCase().indexOf("hhmm");
    }
    if (tmp != -1) {
        // $NON-NLS-1$
        int space = pat.indexOf(" ", tmp);
        if (space == -1)
            space = pat.length();
        timePattern = pat.substring(tmp, space);
        pat = pat.substring(0, tmp) + pat.substring(space);
        sdf.applyPattern(timePattern);
        StringBuilder sb = new StringBuilder(timePattern.length());
        for (int i = 0; i < timePattern.length(); i++) {
            char ch = timePattern.charAt(i);
            if (Character.isLetter(ch)) {
                sb.append('#');
            } else {
                sb.append(ch);
            }
        }
        DefaultFormatter defaultFormatter = new DefaultFormatter();
        DefaultFormatter maskFormatter;
        try {
            maskFormatter = new MaskFormatter(sb.toString());
            maskFormatter.setOverwriteMode(true);
        } catch (ParseException e) {
            Debug.error(e);
            maskFormatter = defaultFormatter;
        }
        timeField.setFormatterFactory(new DefaultFormatterFactory(defaultFormatter, defaultFormatter, maskFormatter));
        timeField.setVisible(true);
        timeField.setValue(sdf.format(calendar.getTime()));
    } else {
        timeField.setVisible(false);
        timePattern = null;
    }
    sdf.applyPattern(pat.trim());
    updateLabel();
    returnValue = ERROR_OPTION;
    setVisible(true);
    return returnValue;
}
Also used : DefaultFormatter(javax.swing.text.DefaultFormatter) MaskFormatter(javax.swing.text.MaskFormatter) ParseException(java.text.ParseException) DefaultFormatterFactory(javax.swing.text.DefaultFormatterFactory)

Example 9 with MaskFormatter

use of javax.swing.text.MaskFormatter in project servoy-client by Servoy.

the class J2DBClient method updateInsertModeIcon.

/**
 * updates the insert mode icon for the given display
 *
 * @param display
 */
public void updateInsertModeIcon(IDisplay display) {
    Icon icon = empty;
    if (display instanceof DataField) {
        DataField field = (DataField) display;
        AbstractFormatterFactory formatterFactory = field.getFormatterFactory();
        if (formatterFactory instanceof DefaultFormatterFactory) {
            DefaultFormatterFactory factory = ((DefaultFormatterFactory) formatterFactory);
            AbstractFormatter editFormatter = factory.getEditFormatter();
            if (editFormatter == null)
                editFormatter = factory.getDefaultFormatter();
            if (editFormatter instanceof DefaultFormatter && !(editFormatter instanceof MaskFormatter)) {
                if (((DefaultFormatter) editFormatter).getOverwriteMode()) {
                    icon = overwrite;
                } else {
                    icon = insert;
                }
            }
        }
    } else if (display instanceof DataTextArea) {
        if (((DataTextArea) display).getOverwriteMode()) {
            icon = overwrite;
        } else {
            icon = insert;
        }
    }
    insertModeLabel.setIcon(icon);
}
Also used : DataField(com.servoy.j2db.smart.dataui.DataField) DataTextArea(com.servoy.j2db.smart.dataui.DataTextArea) DefaultFormatter(javax.swing.text.DefaultFormatter) MaskFormatter(javax.swing.text.MaskFormatter) ImageIcon(javax.swing.ImageIcon) Icon(javax.swing.Icon) DefaultFormatterFactory(javax.swing.text.DefaultFormatterFactory) AbstractFormatterFactory(javax.swing.JFormattedTextField.AbstractFormatterFactory) AbstractFormatter(javax.swing.JFormattedTextField.AbstractFormatter)

Example 10 with MaskFormatter

use of javax.swing.text.MaskFormatter in project astroimagej by keastrid.

the class Data_Processor method createFormatter.

protected MaskFormatter createFormatter(String s) {
    MaskFormatter formatter = null;
    try {
        formatter = new MaskFormatter(s);
        formatter.setOverwriteMode(true);
    } catch (java.text.ParseException exc) {
        System.err.println("formatter is bad: " + exc.getMessage());
        System.exit(-1);
    }
    return formatter;
}
Also used : MaskFormatter(javax.swing.text.MaskFormatter)

Aggregations

MaskFormatter (javax.swing.text.MaskFormatter)15 ParseException (java.text.ParseException)10 JFormattedTextField (javax.swing.JFormattedTextField)5 JTextField (javax.swing.JTextField)4 JLabel (javax.swing.JLabel)3 JPanel (javax.swing.JPanel)3 DefaultFormatter (javax.swing.text.DefaultFormatter)3 DefaultFormatterFactory (javax.swing.text.DefaultFormatterFactory)3 Dimension (java.awt.Dimension)2 GridLayout (java.awt.GridLayout)2 SimpleDateFormat (java.text.SimpleDateFormat)2 LocalDate (java.time.LocalDate)2 Date (java.util.Date)2 BoxLayout (javax.swing.BoxLayout)2 ButtonGroup (javax.swing.ButtonGroup)2 JRadioButton (javax.swing.JRadioButton)2 JSpinner (javax.swing.JSpinner)2 ElementListener (com.ramussoft.common.event.ElementListener)1 SerialCheker (com.ramussoft.pb.frames.components.SerialCheker)1 DemoChecker (com.ramussoft.pb.idef.elements.DemoChecker)1