Search in sources :

Example 26 with BadLocationException

use of javax.swing.text.BadLocationException in project adempiere by adempiere.

the class MDocDate method insertString.

/**
	 *	Insert String
	 *  @param offset offset
	 *  @param string string
	 *  @param attr attributes
	 *  @throws BadLocationException
	 */
@Override
public void insertString(int offset, String string, AttributeSet attr) throws BadLocationException {
    log.finest("Offset=" + offset + ",String=" + string + ",Attr=" + attr + ",OldText=" + getText() + ",OldLength=" + getText().length());
    //	DBTextDataBinder.updateText sends stuff at once - length=8
    if (string != null && string.length() == 1) {
        //	ignore if too long
        if (offset >= m_mask.length())
            return;
        //	is it an empty field?
        int length = getText().length();
        if (offset == 0 && length == 0) {
            Date today = new Date(System.currentTimeMillis());
            String dateStr = m_format.format(today);
            super.insertString(0, string + dateStr.substring(1), attr);
            m_tc.setCaretPosition(1);
            return;
        }
        //	is it a digit ?
        try {
            Integer.parseInt(string);
        } catch (Exception pe) {
            //hengsin, [ 1660175 ] Date field - anoying popup
            //startDateDialog();
            ADialog.beep();
            return;
        }
        //	positioned before the delimiter - jump over delimiter
        if (offset != m_mask.length() - 1 && m_mask.charAt(offset + 1) == DELIMITER)
            m_tc.setCaretPosition(offset + 2);
        //	positioned at the delimiter
        if (m_mask.charAt(offset) == DELIMITER) {
            offset++;
            m_tc.setCaretPosition(offset + 1);
        }
        //	replace current position
        super.remove(offset, 1);
    }
    //	Set new character
    super.insertString(offset, string, attr);
    //	New value set Cursor
    if (offset == 0 && string != null && string.length() > 1)
        m_tc.setCaretPosition(0);
}
Also used : Date(java.util.Date) BadLocationException(javax.swing.text.BadLocationException)

Example 27 with BadLocationException

use of javax.swing.text.BadLocationException in project intellij-community by JetBrains.

the class RegExHelpPopup method createRegExLinkRunnable.

@NotNull
public static Runnable createRegExLinkRunnable(@Nullable final Component owner, @Nullable final Logger logger) {
    return new Runnable() {

        JBPopup helpPopup;

        @Override
        public void run() {
            try {
                if (helpPopup != null && !helpPopup.isDisposed() && helpPopup.isVisible()) {
                    return;
                }
                helpPopup = createRegExHelpPopup();
                Disposer.register(helpPopup, new Disposable() {

                    @Override
                    public void dispose() {
                        destroyPopup();
                    }
                });
                if (owner != null) {
                    helpPopup.showInCenterOf(owner);
                } else {
                    helpPopup.showInFocusCenter();
                }
            } catch (BadLocationException ex) {
                if (logger != null)
                    logger.info(ex);
            }
        }

        private void destroyPopup() {
            helpPopup = null;
        }
    };
}
Also used : Disposable(com.intellij.openapi.Disposable) JBPopup(com.intellij.openapi.ui.popup.JBPopup) BadLocationException(javax.swing.text.BadLocationException) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with BadLocationException

use of javax.swing.text.BadLocationException in project intellij-community by JetBrains.

the class ActionTracer method afterActionPerformed.

@Override
public void afterActionPerformed(AnAction action, DataContext dataContext, AnActionEvent event) {
    StringBuilder out = new StringBuilder(String.format("%1$tF %1$tT,%1$tL ", System.currentTimeMillis()));
    final ActionManager actionManager = ActionManager.getInstance();
    final String id = actionManager.getId(action);
    out.append("id=").append(id);
    if (id != null) {
        out.append("; shortcuts:");
        final Shortcut[] shortcuts = KeymapManager.getInstance().getActiveKeymap().getShortcuts(id);
        for (int i = 0; i < shortcuts.length; i++) {
            Shortcut shortcut = shortcuts[i];
            out.append(shortcut);
            if (i < shortcuts.length - 1) {
                out.append(",");
            }
        }
    }
    out.append("; class: ").append(action.getClass().getName());
    out.append("\n");
    final Document doc = myText.getDocument();
    try {
        doc.insertString(doc.getLength(), out.toString(), null);
        SwingUtilities.invokeLater(() -> {
            final int y = (int) myText.getBounds().getMaxY();
            myText.scrollRectToVisible(new Rectangle(0, y, myText.getBounds().width, 0));
        });
    } catch (BadLocationException e) {
        LOG.error(e);
    }
}
Also used : Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException)

Example 29 with BadLocationException

use of javax.swing.text.BadLocationException in project JMRI by JMRI.

the class AbstractMonFrame method nextLine.

public void nextLine(String line, String raw) {
    // handle display of traffic
    // line is the traffic in 'normal form', raw is the "raw form"
    // Both should be one or more well-formed lines, e.g. end with \n
    StringBuffer sb = new StringBuffer(120);
    // display the timestamp if requested
    if (timeCheckBox.isSelected()) {
        // NOI18N
        sb.append(df.format(new Date())).append(": ");
    }
    // display the raw data if requested
    if (rawCheckBox.isSelected()) {
        // NOI18N
        sb.append('[').append(raw).append("]  ");
    }
    // display decoded data
    sb.append(line);
    synchronized (self) {
        linesBuffer.append(sb.toString());
    }
    // if not frozen, display it in the Swing thread
    if (!freezeButton.isSelected()) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                synchronized (self) {
                    monTextPane.append(linesBuffer.toString());
                    int LineCount = monTextPane.getLineCount();
                    if (LineCount > MAX_LINES) {
                        LineCount -= MAX_LINES;
                        try {
                            int offset = monTextPane.getLineStartOffset(LineCount);
                            monTextPane.getDocument().remove(0, offset);
                        } catch (BadLocationException ex) {
                        }
                    }
                    linesBuffer.setLength(0);
                }
            }
        };
        javax.swing.SwingUtilities.invokeLater(r);
    }
    // if requested, log to a file.
    if (logStream != null) {
        synchronized (logStream) {
            String logLine = sb.toString();
            if (!newline.equals("\n")) {
                // have to massage the line-ends
                int i = 0;
                int lim = sb.length();
                // arbitrary guess at space
                StringBuffer out = new StringBuffer(sb.length() + 10);
                for (i = 0; i < lim; i++) {
                    if (sb.charAt(i) == '\n') {
                        out.append(newline);
                    } else {
                        out.append(sb.charAt(i));
                    }
                }
                logLine = out.toString();
            }
            logStream.print(logLine);
        }
    }
}
Also used : Date(java.util.Date) BadLocationException(javax.swing.text.BadLocationException)

Example 30 with BadLocationException

use of javax.swing.text.BadLocationException in project JMRI by JMRI.

the class AbstractMonPane method initComponents.

@Override
public void initComponents() throws Exception {
    pm = jmri.InstanceManager.getNullableDefault(jmri.UserPreferencesManager.class);
    // the following code sets the frame's initial state
    // NOI18N
    clearButton.setText(Bundle.getMessage("ButtonClearScreen"));
    clearButton.setVisible(true);
    // NOI18N
    clearButton.setToolTipText(Bundle.getMessage("TooltipClearMonHistory"));
    // NOI18N
    freezeButton.setText(Bundle.getMessage("ButtonFreezeScreen"));
    freezeButton.setVisible(true);
    // NOI18N
    freezeButton.setToolTipText(Bundle.getMessage("TooltipStopScroll"));
    // NOI18N
    enterButton.setText(Bundle.getMessage("ButtonAddMessage"));
    enterButton.setVisible(true);
    // NOI18N
    enterButton.setToolTipText(Bundle.getMessage("TooltipAddMessage"));
    createDataPanes();
    // NOI18N
    entryField.setToolTipText(Bundle.getMessage("TooltipEntryPane"));
    // cap vertical size to avoid over-growth
    Dimension currentPreferredSize = entryField.getPreferredSize();
    Dimension currentMaximumSize = entryField.getMaximumSize();
    currentMaximumSize.height = currentPreferredSize.height;
    entryField.setMaximumSize(currentMaximumSize);
    //setup filterField
    // NOI18N
    filterField.setToolTipText(Bundle.getMessage("TooltipFilter"));
    filterField.setMaximumSize(currentMaximumSize);
    try {
        //restore prev values
        filterField.setText(pm.getProperty(filterFieldCheck, filterFieldCheck).toString());
    } catch (Exception e1) {
    //leave blank if previous value not retrieved
    }
    //automatically uppercase input in filterField, and only accept spaces and valid hex characters
    ((AbstractDocument) filterField.getDocument()).setDocumentFilter(new DocumentFilter() {

        // typing inserts individual characters
        static final String pattern = "[0-9a-fA-F ]*+";

        @Override
        public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attrs) throws BadLocationException {
            if (text.matches(pattern)) {
                // NOI18N
                fb.insertString(offset, text.toUpperCase(), attrs);
            } else {
                fb.insertString(offset, "", attrs);
            }
        }

        @Override
        public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            if (text.matches(pattern)) {
                // NOI18N
                fb.replace(offset, length, text.toUpperCase(), attrs);
            } else {
                fb.replace(offset, length, "", attrs);
            }
        }
    });
    // NOI18N
    startLogButton.setText(Bundle.getMessage("ButtonStartLogging"));
    startLogButton.setVisible(true);
    // NOI18N
    startLogButton.setToolTipText(Bundle.getMessage("TooltipStartLogging"));
    // NOI18N
    stopLogButton.setText(Bundle.getMessage("ButtonStopLogging"));
    stopLogButton.setVisible(true);
    // NOI18N
    stopLogButton.setToolTipText(Bundle.getMessage("TooltipStopLogging"));
    // NOI18N
    rawCheckBox.setText(Bundle.getMessage("ButtonShowRaw"));
    rawCheckBox.setVisible(true);
    // NOI18N
    rawCheckBox.setToolTipText(Bundle.getMessage("TooltipShowRaw"));
    if (pm != null)
        rawCheckBox.setSelected(pm.getSimplePreferenceState(rawDataCheck));
    // NOI18N
    timeCheckBox.setText(Bundle.getMessage("ButtonShowTimestamps"));
    timeCheckBox.setVisible(true);
    // NOI18N
    timeCheckBox.setToolTipText(Bundle.getMessage("TooltipShowTimestamps"));
    if (pm != null)
        timeCheckBox.setSelected(pm.getSimplePreferenceState(timeStampCheck));
    // NOI18N
    alwaysOnTopCheckBox.setText(Bundle.getMessage("ButtonWindowOnTop"));
    alwaysOnTopCheckBox.setVisible(true);
    // NOI18N
    alwaysOnTopCheckBox.setToolTipText(Bundle.getMessage("TooltipWindowOnTop"));
    if (pm != null)
        alwaysOnTopCheckBox.setSelected(pm.getSimplePreferenceState(alwaysOnTopCheck));
    if (getTopLevelAncestor() != null) {
        ((jmri.util.JmriJFrame) getTopLevelAncestor()).setAlwaysOnTop(alwaysOnTopCheckBox.isSelected());
    } else {
        // which can be normal, but 
        if (alwaysOnTopCheckBox.isSelected()) {
            // in this case we want to access the enclosing frame to setAlwaysOnTop.  So defer for a bit....
            log.debug("Cannot set Always On Top from preferences due to no Top Level Ancestor");
            timerCount = 0;
            timer = new javax.swing.Timer(20, (java.awt.event.ActionEvent evt) -> {
                if (getTopLevelAncestor() != null && timerCount > 3) {
                    timer.stop();
                    ((jmri.util.JmriJFrame) getTopLevelAncestor()).setAlwaysOnTop(alwaysOnTopCheckBox.isSelected());
                    log.debug("set Always On Top");
                } else {
                    log.debug("Have to repeat attempt to set Always on Top");
                    timerCount++;
                    if (timerCount > 50) {
                        log.debug("Set Always on Top failed");
                        timer.stop();
                    }
                }
            });
            timer.start();
        }
    }
    // NOI18N
    autoScrollCheckBox.setText(Bundle.getMessage("ButtonAutoScroll"));
    autoScrollCheckBox.setVisible(true);
    // NOI18N
    autoScrollCheckBox.setToolTipText(Bundle.getMessage("TooltipAutoScroll"));
    if (pm != null)
        autoScrollCheckBox.setSelected(!pm.getSimplePreferenceState(autoScrollCheck));
    // NOI18N
    openFileChooserButton.setText(Bundle.getMessage("ButtonChooseLogFile"));
    openFileChooserButton.setVisible(true);
    // NOI18N
    openFileChooserButton.setToolTipText(Bundle.getMessage("TooltipChooseLogFile"));
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    // add items to GUI
    addDataPanes();
    JPanel paneA = new JPanel();
    paneA.setLayout(new BoxLayout(paneA, BoxLayout.Y_AXIS));
    JPanel pane1 = new JPanel();
    pane1.setLayout(new BoxLayout(pane1, BoxLayout.X_AXIS));
    pane1.add(clearButton);
    pane1.add(freezeButton);
    pane1.add(rawCheckBox);
    pane1.add(timeCheckBox);
    pane1.add(alwaysOnTopCheckBox);
    pane1.add(autoScrollCheckBox);
    paneA.add(pane1);
    JPanel pane2 = new JPanel();
    pane2.setLayout(new BoxLayout(pane2, BoxLayout.X_AXIS));
    pane2.add(filterLabel);
    pane2.add(filterField);
    pane2.add(openFileChooserButton);
    pane2.add(startLogButton);
    pane2.add(stopLogButton);
    paneA.add(pane2);
    JPanel pane3 = new JPanel();
    pane3.setLayout(new BoxLayout(pane3, BoxLayout.X_AXIS));
    pane3.add(enterButton);
    pane3.add(entryField);
    paneA.add(pane3);
    add(paneA);
    // connect actions to buttons
    clearButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            clearButtonActionPerformed(e);
        }
    });
    startLogButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            startLogButtonActionPerformed(e);
        }
    });
    stopLogButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            stopLogButtonActionPerformed(e);
        }
    });
    openFileChooserButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            openFileChooserButtonActionPerformed(e);
        }
    });
    enterButton.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            enterButtonActionPerformed(e);
        }
    });
    alwaysOnTopCheckBox.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            if (getTopLevelAncestor() != null) {
                ((jmri.util.JmriJFrame) getTopLevelAncestor()).setAlwaysOnTop(alwaysOnTopCheckBox.isSelected());
            }
        }
    });
    autoScrollCheckBox.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            doAutoScroll(monTextPane, autoScrollCheckBox.isSelected());
        }
    });
    // set file chooser to a default
    logFileChooser.setSelectedFile(new File("monitorLog.txt"));
    // connect to data source
    init();
}
Also used : JPanel(javax.swing.JPanel) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) BoxLayout(javax.swing.BoxLayout) AbstractDocument(javax.swing.text.AbstractDocument) Dimension(java.awt.Dimension) DocumentFilter(javax.swing.text.DocumentFilter) ActionEvent(java.awt.event.ActionEvent) BadLocationException(javax.swing.text.BadLocationException) ActionListener(java.awt.event.ActionListener) AttributeSet(javax.swing.text.AttributeSet) File(java.io.File) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

BadLocationException (javax.swing.text.BadLocationException)88 Document (javax.swing.text.Document)24 Element (javax.swing.text.Element)13 Point (java.awt.Point)10 IOException (java.io.IOException)9 DefaultHighlighter (javax.swing.text.DefaultHighlighter)8 DefaultStyledDocument (javax.swing.text.DefaultStyledDocument)7 Highlighter (javax.swing.text.Highlighter)7 ArrayList (java.util.ArrayList)6 DocumentEvent (javax.swing.event.DocumentEvent)6 StyledDocument (javax.swing.text.StyledDocument)6 HighlightPainter (javax.swing.text.Highlighter.HighlightPainter)5 Dimension (java.awt.Dimension)4 Rectangle (java.awt.Rectangle)4 ActionEvent (java.awt.event.ActionEvent)4 ActionListener (java.awt.event.ActionListener)4 File (java.io.File)4 Date (java.util.Date)4 Style (javax.swing.text.Style)4 HTMLDocument (javax.swing.text.html.HTMLDocument)4