Search in sources :

Example 76 with BadLocationException

use of javax.swing.text.BadLocationException in project android by JetBrains.

the class TraceViewPanel method createSearchField.

private SearchTextField createSearchField() {
    SearchTextField stf = new SearchTextField(true);
    stf.setOpaque(false);
    stf.setEnabled(true);
    Utils.setSmallerFont(stf);
    stf.addDocumentListener(new DocumentAdapter() {

        @Override
        protected void textChanged(DocumentEvent e) {
            searchTextChanged(getText(e));
        }

        private String getText(DocumentEvent e) {
            try {
                return e.getDocument().getText(0, e.getDocument().getLength());
            } catch (BadLocationException e1) {
                return "";
            }
        }
    });
    JTextField editorTextField = stf.getTextEditor();
    editorTextField.setMinimumSize(new Dimension(JBUI.scale(200), -1));
    editorTextField.registerKeyboardAction(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            closeSearchComponent();
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
    return stf;
}
Also used : SearchTextField(com.intellij.ui.SearchTextField) DocumentAdapter(com.intellij.ui.DocumentAdapter) DocumentEvent(javax.swing.event.DocumentEvent) BadLocationException(javax.swing.text.BadLocationException)

Example 77 with BadLocationException

use of javax.swing.text.BadLocationException in project android by JetBrains.

the class MergedManifestFixture method requireText.

public void requireText(@NotNull String expected, boolean wrap) {
    try {
        Document document = myInfoPane.target().getDocument();
        String text = document.getText(0, document.getLength()).trim();
        if (wrap) {
            text = SdkUtils.wrap(text, 80, null);
        }
        assertEquals(expected, text);
    } catch (BadLocationException ignore) {
    }
}
Also used : Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException)

Example 78 with BadLocationException

use of javax.swing.text.BadLocationException in project android by JetBrains.

the class SelectionEditors method createDocumentListener.

/**
   * Create a document listener that will update the {@link MockupViewPanel} selection
   * when the attached document is updated.
   *
   * @return the new document listener
   */
private DocumentListener createDocumentListener() {
    return new DocumentListener() {

        private void processChange(@NotNull DocumentEvent e) {
            if (myBoundsUpdating) {
                return;
            }
            try {
                Document document = e.getDocument();
                int value;
                if (document.getLength() <= 0) {
                    value = 0;
                } else {
                    value = Integer.parseInt(document.getText(0, document.getLength()));
                }
                SelectionLayer selectionLayer = myMockupViewPanel.getSelectionLayer();
                Rectangle selection = selectionLayer.getSelection();
                if (document == myBoundsDocuments[W]) {
                    selectionLayer.setSelection(selection.x, selection.y, value, selection.height);
                } else if (document == myBoundsDocuments[H]) {
                    selectionLayer.setSelection(selection.x, selection.y, selection.width, value);
                } else if (document == myBoundsDocuments[X]) {
                    selectionLayer.setSelection(value, selection.y, selection.width, selection.height);
                } else if (document == myBoundsDocuments[Y]) {
                    selectionLayer.setSelection(selection.x, value, selection.width, selection.height);
                }
            } catch (BadLocationException | NumberFormatException ex) {
            // Do nothing
            }
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            processChange(e);
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            processChange(e);
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
        }
    };
}
Also used : DocumentListener(javax.swing.event.DocumentListener) SelectionLayer(com.android.tools.idea.uibuilder.mockup.editor.SelectionLayer) DocumentEvent(javax.swing.event.DocumentEvent) Document(javax.swing.text.Document) NotNull(org.jetbrains.annotations.NotNull) BadLocationException(javax.swing.text.BadLocationException)

Example 79 with BadLocationException

use of javax.swing.text.BadLocationException in project jdk8u_jdk by JetBrains.

the class TextComponentPrintable method calculateRowsMetrics.

/**
     * Calculates rowsMetrics for the document. The result is stored
     * in the {@code rowsMetrics}.
     *
     * Two steps process.
     * First step is to find yStart and yEnd for the every document position.
     * Second step is to merge all intersected segments ( [yStart, yEnd] ).
     */
private void calculateRowsMetrics() {
    final int documentLength = printShell.getDocument().getLength();
    List<IntegerSegment> documentMetrics = new ArrayList<IntegerSegment>(LIST_SIZE);
    Rectangle rect;
    for (int i = 0, previousY = -1, previousHeight = -1; i < documentLength; i++) {
        try {
            rect = printShell.modelToView(i);
            if (rect != null) {
                int y = (int) rect.getY();
                int height = (int) rect.getHeight();
                if (height != 0 && (y != previousY || height != previousHeight)) {
                    /*
                         * we do not store the same value as previous. in our
                         * documents it is often for consequent positons to have
                         * the same modelToView y and height.
                         */
                    previousY = y;
                    previousHeight = height;
                    documentMetrics.add(new IntegerSegment(y, y + height - 1));
                }
            }
        } catch (BadLocationException e) {
            assert false;
        }
    }
    /*
         * Merge all intersected segments.
         */
    Collections.sort(documentMetrics);
    int yStart = Integer.MIN_VALUE;
    int yEnd = Integer.MIN_VALUE;
    for (IntegerSegment segment : documentMetrics) {
        if (yEnd < segment.start) {
            if (yEnd != Integer.MIN_VALUE) {
                rowsMetrics.add(new IntegerSegment(yStart, yEnd));
            }
            yStart = segment.start;
            yEnd = segment.end;
        } else {
            yEnd = segment.end;
        }
    }
    if (yEnd != Integer.MIN_VALUE) {
        rowsMetrics.add(new IntegerSegment(yStart, yEnd));
    }
}
Also used : ArrayList(java.util.ArrayList) Rectangle(java.awt.Rectangle) BadLocationException(javax.swing.text.BadLocationException)

Example 80 with BadLocationException

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

the class CbusConsolePane method nextLine.

public void nextLine(String line, String decoded, String priorities, int filter) {
    // handle display of traffic
    // line is the traffic in 'normal form', decoded is the decoded,
    // protocol specific, form
    // Both should contain the same number of well-formed lines, e.g. end
    // with \n
    StringBuffer sbCan = new StringBuffer(80);
    StringBuffer sbCbus = new StringBuffer(80);
    final int filterIndex = filter;
    log.debug("_filterFrame: " + _filterFrame + " filter: " + filter);
    if (filterIndex >= 0) {
        final Color filterColor = _filterFrame.getColor(filter);
        cbusHighlightPainter = new cbusHighlightPainter(filterColor);
    }
    // display the timestamp if requested
    if (timeCheckBox.isSelected()) {
        sbCan.append(df.format(new Date())).append(": ");
        sbCbus.append(df.format(new Date())).append(": ");
    }
    // display CBUS the priorities if requested
    if (priCheckBox.isSelected()) {
        sbCbus.append((priorities + " "));
    }
    if (filterIndex >= 0) {
        sbCan.append(("Filter " + (filterIndex + 1) + ": "));
        sbCbus.append(("Filter " + (filterIndex + 1) + ": "));
    }
    // display decoded data
    sbCan.append(line);
    sbCbus.append(decoded);
    //        synchronized( linesBufferCbus ) {
    synchronized (linesBuffer) {
        linesBuffer[CAN].append(sbCan.toString());
        linesBuffer[CBUS].append(sbCbus.toString());
    }
    // if not frozen, display it in the Swing thread
    if (!freezeButton.isSelected()) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                synchronized (linesBuffer) {
                    final int start = monTextPaneCbus.getText().length();
                    monTextPaneCan.append(linesBuffer[CAN].toString());
                    monTextPaneCbus.append(linesBuffer[CBUS].toString());
                    final int end = monTextPaneCbus.getText().length();
                    int LineCount = monTextPaneCan.getLineCount();
                    if (LineCount > MAX_LINES) {
                        LineCount -= MAX_LINES;
                        try {
                            int offset = monTextPaneCan.getLineStartOffset(LineCount);
                            monTextPaneCan.getDocument().remove(0, offset);
                            monTextPaneCbus.getDocument().remove(0, offset);
                        } catch (BadLocationException ex) {
                        }
                    }
                    try {
                        if (filterIndex >= 0) {
                            log.debug("Add highlight start: " + start + " end: " + end);
                            cbusHighlighter.addHighlight(start, end - 1, cbusHighlightPainter);
                        }
                    } catch (BadLocationException e) {
                    // do nothing
                    }
                    linesBuffer[CAN].setLength(0);
                    linesBuffer[CBUS].setLength(0);
                }
            }
        };
        javax.swing.SwingUtilities.invokeLater(r);
    }
    // if requested, log to a file.
    if (logStream != null) {
        String logLine = sbCbus.toString();
        if (!newline.equals("\n")) {
            // have to massage the line-ends
            int j;
            int lim = sbCbus.length();
            // arbitrary guess at space
            StringBuffer out = new StringBuffer(sbCbus.length() + 10);
            for (j = 0; j < lim; j++) {
                if (sbCbus.charAt(j) == '\n') {
                    out.append(newline);
                } else {
                    out.append(sbCbus.charAt(j));
                }
            }
            logLine = new String(out);
        }
        logStream.print(logLine);
    }
}
Also used : Color(java.awt.Color) Date(java.util.Date) 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