summaryrefslogtreecommitdiff
path: root/sources/simulator/VSLogging.java
blob: fe76abc6f02daca2a114c1d7d420abac1c4a084d (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
/*
 * VS is (c) 2008 by Paul C. Buetow
 * vs@dev.buetow.org
 */
package simulator;

import java.util.*;
import java.util.regex.*;
import javax.swing.*;

import utils.*;

// TODO: Auto-generated Javadoc
/**
 * The Class VSLogging.
 */
public class VSLogging {
    
    /** The logging area. */
    private JTextArea loggingArea;
    
    /** The filter text. */
    private String filterText;
    
    /** The pause lines. */
    private ArrayList<StringBuffer> pauseLines;
    
    /** The logging lines. */
    private ArrayList<StringBuffer> loggingLines;
    
    /** The simulation canvas. */
    private VSSimulatorCanvas simulationCanvas;
    
    /** The is filtered. */
    private boolean isFiltered;
    
    /** The is paused. */
    private boolean isPaused;
    
    /** The filter pattern. */
    private Pattern filterPattern;

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

    /**
     * Sets the simulation canvas.
     * 
     * @param simulationCanvas the new simulation canvas
     */
    public void setSimulationCanvas(VSSimulatorCanvas simulationCanvas) {
        this.simulationCanvas = simulationCanvas;
    }

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

    /**
     * Logg.
     * 
     * @param message the message
     */
    public void logg(String message) {
        if (simulationCanvas == null)
            logg(message, 0);
        else
            logg(message, simulationCanvas.getTime());
    }

    /**
     * Logg.
     * 
     * @param message the message
     * @param time the time
     */
    public synchronized void logg(String message, long time) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(VSTools.getTimeString(time));
        buffer.append(": ");
        buffer.append(message);

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

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

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

            pauseLines.clear();
        }
    }

    /**
     * Logg filtered.
     * 
     * @param buffer the buffer
     */
    private void loggFiltered(StringBuffer buffer) {
        loggingLines.add(buffer);
        if (!isFiltered) {
            loggingArea.append(buffer.toString()+"\n");
            loggingArea.setCaretPosition(loggingArea.getDocument().getLength());

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

    /**
     * Checks if is filtered.
     * 
     * @param isFiltered the 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();
    }

    /**
     * Clear.
     */
    public synchronized void clear() {
        loggingLines.clear();
        pauseLines.clear();
        loggingArea.setText("");
    }

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

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

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