Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 289034ff9acde5b10741001846ae5bae55101597 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*******************************************************************************
 * Copyright (c) 2011, 2012 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.internal.debug.ui.model;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.RGB;

class StyledStringBuffer {

    /**
     * The font style constants.
     */
    public static final int
        NORMAL          = SWT.NORMAL,
        BOLD            = SWT.BOLD,
        ITALIC          = SWT.ITALIC,
        MONOSPACED      = 1 << 2;

    private final StringBuffer bf = new StringBuffer();
    private final ArrayList<Style> styles = new ArrayList<Style>();

    static class Style {
        int pos;
        int len;
        int font;
        RGB bg;
        RGB fg;
    }

    private boolean full_error_reports;

    void enableFullErrorReports(boolean full_error_reports) {
        this.full_error_reports = full_error_reports;
    }

    StyledStringBuffer append(int pos, int font, RGB bg, RGB fg) {
        Style x = new Style();
        x.pos = pos;
        x.len = bf.length() - pos;
        x.font = font;
        x.bg = bg;
        x.fg = fg;
        styles.add(x);
        return this;
    }

    StyledStringBuffer append(String s) {
        bf.append(s);
        return this;
    }

    StyledStringBuffer append(char ch) {
        bf.append(ch);
        return this;
    }

    StyledStringBuffer append(int i) {
        bf.append(i);
        return this;
    }

    StyledStringBuffer append(String s, int font) {
        Style x = new Style();
        x.pos = bf.length();
        x.len = s.length();
        x.font = font;
        styles.add(x);
        bf.append(s);
        return this;
    }

    StyledStringBuffer append(String s, int font, RGB bg, RGB fg) {
        Style x = new Style();
        x.pos = bf.length();
        x.len = s.length();
        x.font = font;
        x.bg = bg;
        x.fg = fg;
        styles.add(x);
        bf.append(s);
        return this;
    }

    StyledStringBuffer append(StyledStringBuffer s) {
        int offs = bf.length();
        for (Style y : s.styles) {
            Style x = new Style();
            x.pos = y.pos + offs;
            x.len = y.len;
            x.font = y.font;
            x.bg = y.bg;
            x.fg = y.fg;
            styles.add(x);
        }
        bf.append(s.bf);
        return this;
    }

    StyledStringBuffer append(Throwable x, RGB color) {
        if (x == null) return this;
        if (full_error_reports) {
            String[] a = ("Exception: " + TCFModel.getErrorMessage(x, true)).split("\n");
            for (String s : a) {
                int i = s.indexOf(':');
                if (i >= 0) {
                    append(s.substring(0, i + 1), SWT.BOLD, null, color);
                    append(s.substring(i + 1), SWT.ITALIC, null, color);
                }
                else {
                    append(s, SWT.ITALIC, null, color);
                }
                bf.append('\n');
            }
        }
        else {
            append("Exception: ", SWT.BOLD, null, color);
            append(TCFModel.getErrorMessage(x, false), SWT.ITALIC, null, color);
            bf.append('\n');
        }
        return this;
    }

    StringBuffer getStringBuffer() {
        return bf;
    }

    Collection<Style> getStyle() {
        return styles;
    }

    int length() {
        return bf.length();
    }

    @Override
    public String toString() {
        return bf.toString();
    }
}

Back to the top