Search in sources :

Example 41 with StyledDocument

use of javax.swing.text.StyledDocument in project org.alloytools.alloy by AlloyTools.

the class SwingLogPanel method logDivider.

/**
 * Write a horizontal separator into the log window.
 */
public void logDivider() {
    if (log == null)
        return;
    clearError();
    StyledDocument doc = log.getStyledDocument();
    Style dividerStyle = doc.addStyle("bar", styleRegular);
    JPanel jpanel = new JPanel();
    jpanel.setBackground(Color.LIGHT_GRAY);
    // 300 is arbitrary,
    jpanel.setPreferredSize(new Dimension(300, 1));
    // since it will
    // auto-stretch
    StyleConstants.setComponent(dividerStyle, jpanel);
    // Any character would do; "." will be
    reallyLog(".", dividerStyle);
    // replaced by the JPanel
    reallyLog("\n\n", styleRegular);
    log.setCaretPosition(doc.getLength());
    lastSize = doc.getLength();
}
Also used : JPanel(javax.swing.JPanel) StyledDocument(javax.swing.text.StyledDocument) Style(javax.swing.text.Style) Dimension(java.awt.Dimension)

Example 42 with StyledDocument

use of javax.swing.text.StyledDocument in project org.alloytools.alloy by AlloyTools.

the class SwingLogPanel method clearError.

/**
 * Removes any messages writtin in "red" style.
 */
public void clearError() {
    if (log == null)
        return;
    // Since this class always removes "red" messages prior to writing
    // anything,
    // that means if there are any red messages, they will always be at the
    // end of the JTextPane.
    StyledDocument doc = log.getStyledDocument();
    int n = doc.getLength();
    if (n > lastSize) {
        try {
            doc.remove(lastSize, n - lastSize);
        } catch (BadLocationException e) {
        }
    }
    if (batch.size() > 0) {
        for (String msg : batch) {
            reallyLog(msg, styleRegular);
        }
        batch.clear();
    }
}
Also used : StyledDocument(javax.swing.text.StyledDocument) BadLocationException(javax.swing.text.BadLocationException)

Example 43 with StyledDocument

use of javax.swing.text.StyledDocument in project org.alloytools.alloy by AlloyTools.

the class OurConsole method do_add.

/**
 * Insert the given text into the given location and with the given style if
 * where>=0; append the text if where<0.
 */
private void do_add(int where, String text, AttributeSet style) {
    try {
        StyledDocument doc = main.getStyledDocument();
        if (TableView.isTable(text)) {
            Table table = TableView.toTable(text, false);
            doc.insertString(where >= 0 ? where : doc.getLength(), table.toString(), mono);
            main.getCaret().setSelectionVisible(false);
        } else
            doc.insertString(where >= 0 ? where : doc.getLength(), text, style);
    } catch (BadLocationException ex) {
    }
}
Also used : Table(org.alloytools.util.table.Table) StyledDocument(javax.swing.text.StyledDocument) BadLocationException(javax.swing.text.BadLocationException)

Example 44 with StyledDocument

use of javax.swing.text.StyledDocument in project groovy by apache.

the class ConsoleSupport method addStylesToDocument.

protected void addStylesToDocument(JTextPane outputArea) {
    StyledDocument doc = outputArea.getStyledDocument();
    Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
    Style regular = doc.addStyle("regular", def);
    StyleConstants.setFontFamily(def, "Monospaced");
    promptStyle = doc.addStyle("prompt", regular);
    StyleConstants.setForeground(promptStyle, Color.BLUE);
    commandStyle = doc.addStyle("command", regular);
    StyleConstants.setForeground(commandStyle, Color.MAGENTA);
    outputStyle = doc.addStyle("output", regular);
    StyleConstants.setBold(outputStyle, true);
}
Also used : StyledDocument(javax.swing.text.StyledDocument) Style(javax.swing.text.Style)

Example 45 with StyledDocument

use of javax.swing.text.StyledDocument in project jmeter by apache.

the class SamplerResultTab method setupTabPane.

@Override
@SuppressWarnings({ "boxing", "JdkObsolete" })
public void setupTabPane() {
    // Clear all data before display a new
    this.clearData();
    StyledDocument statsDoc = stats.getStyledDocument();
    try {
        if (userObject instanceof SampleResult) {
            sampleResult = (SampleResult) userObject;
            // We are displaying a SampleResult
            setupTabPaneForSampleResult();
            requestPanel.setSamplerResult(sampleResult);
            final String samplerClass = sampleResult.getClass().getName();
            String typeResult = samplerClass.substring(1 + samplerClass.lastIndexOf('.'));
            StringBuilder statsBuff = new StringBuilder(200);
            statsBuff.append(JMeterUtils.getResString(// $NON-NLS-1$
            "view_results_thread_name")).append(sampleResult.getThreadName()).append(NL);
            String startTime = dateFormat.format(new Date(sampleResult.getStartTime()));
            statsBuff.append(JMeterUtils.getResString(// $NON-NLS-1$
            "view_results_sample_start")).append(startTime).append(NL);
            statsBuff.append(JMeterUtils.getResString(// $NON-NLS-1$
            "view_results_load_time")).append(sampleResult.getTime()).append(NL);
            statsBuff.append(JMeterUtils.getResString(// $NON-NLS-1$
            "view_results_connect_time")).append(sampleResult.getConnectTime()).append(NL);
            statsBuff.append(JMeterUtils.getResString(// $NON-NLS-1$
            "view_results_latency")).append(sampleResult.getLatency()).append(NL);
            statsBuff.append(JMeterUtils.getResString(// $NON-NLS-1$
            "view_results_size_in_bytes")).append(sampleResult.getBytesAsLong()).append(NL);
            statsBuff.append(JMeterUtils.getResString(// $NON-NLS-1$
            "view_results_sent_bytes")).append(sampleResult.getSentBytes()).append(NL);
            statsBuff.append(JMeterUtils.getResString(// $NON-NLS-1$
            "view_results_size_headers_in_bytes")).append(sampleResult.getHeadersSize()).append(NL);
            statsBuff.append(JMeterUtils.getResString(// $NON-NLS-1$
            "view_results_size_body_in_bytes")).append(sampleResult.getBodySizeAsLong()).append(NL);
            statsBuff.append(JMeterUtils.getResString(// $NON-NLS-1$
            "view_results_sample_count")).append(sampleResult.getSampleCount()).append(NL);
            statsBuff.append(JMeterUtils.getResString(// $NON-NLS-1$
            "view_results_error_count")).append(sampleResult.getErrorCount()).append(NL);
            statsBuff.append(JMeterUtils.getResString(// $NON-NLS-1$
            "view_results_datatype")).append(sampleResult.getDataType()).append(NL);
            statsDoc.insertString(statsDoc.getLength(), statsBuff.toString(), null);
            // reset for reuse
            statsBuff.setLength(0);
            String responseCode = sampleResult.getResponseCode();
            int responseLevel = 0;
            if (responseCode != null) {
                try {
                    responseLevel = Integer.parseInt(responseCode) / 100;
                } catch (NumberFormatException numberFormatException) {
                // no need to change the foreground color
                }
            }
            Style style = null;
            switch(responseLevel) {
                case 3:
                    style = statsDoc.getStyle(STYLE_REDIRECT);
                    break;
                case 4:
                    style = statsDoc.getStyle(STYLE_CLIENT_ERROR);
                    break;
                case 5:
                    style = statsDoc.getStyle(STYLE_SERVER_ERROR);
                    break;
                default:
                    // quieten Findbugs
                    break;
            }
            // $NON-NLS-1$
            statsBuff.append(JMeterUtils.getResString("view_results_response_code")).append(responseCode).append(NL);
            statsDoc.insertString(statsDoc.getLength(), statsBuff.toString(), style);
            // reset for reuse
            statsBuff.setLength(0);
            // response message label
            String responseMsgStr = sampleResult.getResponseMessage();
            statsBuff.append(JMeterUtils.getResString(// $NON-NLS-1$
            "view_results_response_message")).append(responseMsgStr).append(NL);
            statsBuff.append(NL);
            statsBuff.append(NL);
            statsBuff.append(// $NON-NLS-1$
            typeResult + " " + JMeterUtils.getResString("view_results_fields")).append(// $NON-NLS-2$
            NL);
            // $NON-NLS-1$
            statsBuff.append("ContentType: ").append(sampleResult.getContentType()).append(NL);
            // $NON-NLS-1$
            statsBuff.append("DataEncoding: ").append(sampleResult.getDataEncodingNoDefault()).append(NL);
            statsDoc.insertString(statsDoc.getLength(), statsBuff.toString(), null);
            // NOSONAR Help gc
            statsBuff = null;
            // Tabbed results: fill table
            resultModel.addRow(new RowResult(// $NON-NLS-1$
            JMeterUtils.getParsedLabel("view_results_thread_name"), sampleResult.getThreadName()));
            resultModel.addRow(new RowResult(// $NON-NLS-1$
            JMeterUtils.getParsedLabel("view_results_sample_start"), startTime));
            resultModel.addRow(new RowResult(// $NON-NLS-1$
            JMeterUtils.getParsedLabel("view_results_load_time"), sampleResult.getTime()));
            resultModel.addRow(new RowResult(// $NON-NLS-1$
            JMeterUtils.getParsedLabel("view_results_connect_time"), sampleResult.getConnectTime()));
            resultModel.addRow(new RowResult(// $NON-NLS-1$
            JMeterUtils.getParsedLabel("view_results_latency"), sampleResult.getLatency()));
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel(// $NON-NLS-1$
            "view_results_size_in_bytes"), sampleResult.getBytesAsLong()));
            resultModel.addRow(new RowResult(// $NON-NLS-1$
            JMeterUtils.getParsedLabel("view_results_sent_bytes"), sampleResult.getSentBytes()));
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel(// $NON-NLS-1$
            "view_results_size_headers_in_bytes"), sampleResult.getHeadersSize()));
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel(// $NON-NLS-1$
            "view_results_size_body_in_bytes"), sampleResult.getBodySizeAsLong()));
            resultModel.addRow(new RowResult(// $NON-NLS-1$
            JMeterUtils.getParsedLabel("view_results_sample_count"), sampleResult.getSampleCount()));
            resultModel.addRow(new RowResult(// $NON-NLS-1$
            JMeterUtils.getParsedLabel("view_results_error_count"), sampleResult.getErrorCount()));
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel(// $NON-NLS-1$
            "view_results_response_code"), responseCode));
            resultModel.addRow(new RowResult(JMeterUtils.getParsedLabel(// $NON-NLS-1$
            "view_results_response_message"), responseMsgStr));
            // Parsed response headers
            LinkedHashMap<String, String> lhm = JMeterUtils.parseHeaders(sampleResult.getResponseHeaders());
            Set<Map.Entry<String, String>> keySet = lhm.entrySet();
            for (Map.Entry<String, String> entry : keySet) {
                resHeadersModel.addRow(new RowResult(entry.getKey(), entry.getValue()));
            }
            headerData.setInitialText(sampleResult.getResponseHeaders());
            // Fields table
            // $NON-NLS-1$
            resFieldsModel.addRow(new RowResult("Type Result ", typeResult));
            // not sure needs I18N?
            // $NON-NLS-1$
            resFieldsModel.addRow(new RowResult("ContentType", sampleResult.getContentType()));
            // $NON-NLS-1$
            resFieldsModel.addRow(new RowResult("DataEncoding", sampleResult.getDataEncodingNoDefault()));
            // Reset search
            if (activateSearchExtension) {
                searchTextExtension.resetTextToFind();
            }
        } else if (userObject instanceof AssertionResult) {
            assertionResult = (AssertionResult) userObject;
            // We are displaying an AssertionResult
            setupTabPaneForAssertionResult();
            StringBuilder statsBuff = new StringBuilder(100);
            statsBuff.append(JMeterUtils.getResString(// $NON-NLS-1$
            "view_results_assertion_error")).append(assertionResult.isError()).append(NL);
            statsBuff.append(JMeterUtils.getResString(// $NON-NLS-1$
            "view_results_assertion_failure")).append(assertionResult.isFailure()).append(NL);
            statsBuff.append(JMeterUtils.getResString(// $NON-NLS-1$
            "view_results_assertion_failure_message")).append(assertionResult.getFailureMessage()).append(NL);
            statsDoc.insertString(statsDoc.getLength(), statsBuff.toString(), null);
        }
        stats.setCaretPosition(1);
    } catch (BadLocationException exc) {
        stats.setText(exc.getLocalizedMessage());
    }
}
Also used : AssertionResult(org.apache.jmeter.assertions.AssertionResult) DefaultStyledDocument(javax.swing.text.DefaultStyledDocument) StyledDocument(javax.swing.text.StyledDocument) Date(java.util.Date) Style(javax.swing.text.Style) SampleResult(org.apache.jmeter.samplers.SampleResult) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

StyledDocument (javax.swing.text.StyledDocument)63 BadLocationException (javax.swing.text.BadLocationException)29 Style (javax.swing.text.Style)18 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)12 Point (java.awt.Point)7 JTextPane (javax.swing.JTextPane)7 DefaultStyledDocument (javax.swing.text.DefaultStyledDocument)7 ArrayList (java.util.ArrayList)4 JLabel (javax.swing.JLabel)4 Test (org.junit.jupiter.api.Test)4 Font (java.awt.Font)3 LogRecord (java.util.logging.LogRecord)3 Matcher (java.util.regex.Matcher)3 ImageIcon (javax.swing.ImageIcon)3 JPanel (javax.swing.JPanel)3 PersistentArrayMap (clojure.lang.PersistentArrayMap)2 BorderLayout (java.awt.BorderLayout)2 Dimension (java.awt.Dimension)2 Rectangle (java.awt.Rectangle)2 IOException (java.io.IOException)2