summaryrefslogtreecommitdiff
path: root/sources/simulator/VSLogging.java
blob: 66cb563ea8d15e22dd200936afa165f352fb7f76 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package simulator;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JTextArea;

import utils.VSTools;

/**
 * The class VSLogging, an object of this class is responsible for the loging
 * of text messages into the simulator's loging window.
 *
 * @author Paul C. Buetow
 */
public class VSLogging {
    /** The loging area. */
    private JTextArea logingArea;

    /** The filter text. */
    private String filterText;

    /** The pause lines. Used for cacheing the loging if the loging is
     * deactivated for a while
     */
    private ArrayList<StringBuffer> pauseLines;

    /** The loging lines. */
    private ArrayList<StringBuffer> logingLines;

    /** The simulator canvas. */
    private VSSimulatorVisualization simulatorVisualization;

    /** The loging messages are filtered. */
    private boolean isFiltered;

    /** The loging is paused. */
    private boolean isPaused;

    /** The filter pattern. */
    private Pattern filterPattern;

    /**
     * Instantiates a new VSLogging object.
     */
    public VSLogging() {
        logingArea = new JTextArea(0, 0);
        logingArea.setEditable(false);
        logingArea.setLineWrap(true);
        logingArea.setWrapStyleWord(true);
        logingLines = new ArrayList<StringBuffer>();
        pauseLines = new ArrayList<StringBuffer>();
        filterText = "";
    }

    /**
     * Sets the simulator canvas.
     *
     * @param sv the simulator canvas
     */
    public void setSimulatorCanvas(VSSimulatorVisualization sv) {
        this.simulatorVisualization = sv;
    }

    /**
     * Gets the loging area.
     *
     * @return the loging area
     */
    public JTextArea getLoggingArea() {
        return logingArea;
    }

    /**
     * Loggs a message using the global time.
     *
     * @param message the message
     */
    public void log(String message) {
        if (simulatorVisualization == null)
            log(message, 0);
        else
            log(message, simulatorVisualization.getTime());
    }

    /**
     * Loggs a message using the specified time.
     *
     * @param message the message
     * @param time the time
     */
    public synchronized void log(String message, long time) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(VSTools.getTimeString(time));
        buffer.append(": ");
        buffer.append(message);

        if (isPaused)
            pauseLines.add(buffer);
        else
            logFiltered(buffer);
    }

    /**
     * Sets if the loging is paused.
     *
     * @param isPaused true, if the loging is paused
     */
    public synchronized void isPaused(boolean isPaused) {
        this.isPaused = isPaused;

        if (!isPaused) {
            for (StringBuffer buffer : pauseLines)
                logFiltered(buffer);

            pauseLines.clear();
        }
    }

    /**
     * If the loging is filtered, it's using the pattern matching.
     *
     * @param buffer the loging buffer to filter
     */
    private void logFiltered(StringBuffer buffer) {
        logingLines.add(buffer);
        if (!isFiltered) {
            logingArea.append(buffer.toString()+"\n");
            logingArea.setCaretPosition(
                logingArea.getDocument().getLength());

        } else if (filterPattern != null &&
                   filterPattern.matcher(buffer).find()) {
            logingArea.append(buffer.toString()+"\n");
            logingArea.setCaretPosition(
                logingArea.getDocument().getLength());
        }
    }

    /**
     * Checks if the loging is filtered.
     *
     * @param isFiltered true, if the loging is filtered
     */
    public synchronized void isFiltered(boolean isFiltered) {
        this.isFiltered = isFiltered;

        if (!isFiltered)
            setFilterText("");
        else
            filter();
    }

    /**
     * Sets the filter text.
     *
     * @param filterText the new filter text
     */
    public synchronized void setFilterText(String filterText) {
        this.filterText = filterText;
        filter();
    }

    /**
     * Clears the loging.
     */
    public synchronized void clear() {
        logingLines.clear();
        pauseLines.clear();
        logingArea.setText("");
    }

    /**
     * Filters the loging.
     */
    private void filter() {
        try {
            filterPattern = Pattern.compile(filterText);
            StringBuffer buffer = new StringBuffer();

            for (StringBuffer line : logingLines) {
                if (isFiltered) {
                    Matcher matcher = filterPattern.matcher(line);
                    if (matcher.find()) {
                        buffer.append(line);
                        buffer.append("\n");
                    }
                } else {
                    buffer.append(line);
                    buffer.append("\n");
                }
            }
            logingArea.setText(buffer.toString());

        } catch (Exception e) {
            filterPattern = null;
            logingArea.setText("");
        }
    }
}