Skip to content

Commit e4da679

Browse files
committed
LoggingPanel: apply text filtering to recorded log messages
This requiers to actually store and log messages that are received by LoggingPanel, in correct order and with meta data. (LogRecorder is used for this purpose.) Whenever the user changes the text to filter for, it is neccessary to reprocess and redisplay the recorded log messages. This might require to much time to be processed in the swing thread, therefore the new class ItemTextPane is introduced. It can display an expanding list of items, and uses and worker thread when the list is replaced.
1 parent 1c4789b commit e4da679

File tree

7 files changed

+764
-74
lines changed

7 files changed

+764
-74
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.scijava.ui.swing.console;
2+
3+
import java.util.Iterator;
4+
import java.util.Map;
5+
import java.util.NoSuchElementException;
6+
import java.util.Spliterator;
7+
import java.util.Spliterators;
8+
import java.util.concurrent.ConcurrentHashMap;
9+
import java.util.concurrent.atomic.AtomicLong;
10+
import java.util.stream.Stream;
11+
import java.util.stream.StreamSupport;
12+
13+
/**
14+
* This Container manages a list of items. Items can only be added to end of
15+
* the list. It's possible to add items, while iterating over the list.
16+
* Iterators never fail, and they will always be updated. Even if an element
17+
* is added after an iterator reached the end of the list,
18+
* {@link Iterator#hasNext()} will return true again, and
19+
* {@link Iterator#next()} will return the newly added element. This Container
20+
* is fully thread safe.
21+
*
22+
* @author Matthias Arzt
23+
*/
24+
public class ConcurrentExpandableList<T> implements Iterable<T> {
25+
26+
private final AtomicLong lastKey = new AtomicLong(0);
27+
28+
private final Map<Long, T> map = new ConcurrentHashMap<>();
29+
30+
public Stream<T> stream() {
31+
Spliterator<T> spliterator = Spliterators.spliteratorUnknownSize(
32+
iterator(), Spliterator.ORDERED);
33+
return StreamSupport.stream(spliterator, /* parallel */ false);
34+
}
35+
36+
public Iterator<T> iterator() {
37+
return new MyIterator();
38+
}
39+
40+
public Iterator<T> iteratorAtEnd() {
41+
return new MyIterator(lastKey.get());
42+
}
43+
44+
public long add(T value) {
45+
long key = lastKey.getAndIncrement();
46+
map.put(key, value);
47+
return key;
48+
}
49+
50+
public void clear() {
51+
map.clear();
52+
}
53+
54+
private class MyIterator implements Iterator<T> {
55+
56+
private long nextIndex = 0;
57+
58+
public MyIterator() {
59+
this(0);
60+
}
61+
62+
public MyIterator(long nextIndex) {
63+
this.nextIndex = nextIndex;
64+
}
65+
66+
@Override
67+
public boolean hasNext() {
68+
return map.containsKey(nextIndex);
69+
}
70+
71+
@Override
72+
public T next() {
73+
T value = map.get(nextIndex);
74+
if(value == null)
75+
throw new NoSuchElementException();
76+
nextIndex++;
77+
return value;
78+
}
79+
}
80+
}
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
/*
2+
* #%L
3+
* SciJava UI components for Java Swing.
4+
* %%
5+
* Copyright (C) 2010 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison.
7+
* %%
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright notice,
12+
* this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright notice,
14+
* this list of conditions and the following disclaimer in the documentation
15+
* and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
* POSSIBILITY OF SUCH DAMAGE.
28+
* #L%
29+
*/
30+
31+
package org.scijava.ui.swing.console;
32+
33+
import java.awt.Font;
34+
import java.util.Collections;
35+
import java.util.Iterator;
36+
import java.util.Objects;
37+
38+
import javax.swing.*;
39+
import javax.swing.text.AttributeSet;
40+
import javax.swing.text.BadLocationException;
41+
import javax.swing.text.DefaultStyledDocument;
42+
import javax.swing.text.StyledDocument;
43+
44+
import org.scijava.thread.ThreadService;
45+
import org.scijava.ui.swing.StaticSwingUtils;
46+
47+
/**
48+
* {@link ItemTextPane} provides a {@link JTextPane} in a {@link JScrollPane}.
49+
* The content is provided as {@link Iterator} of {@link ItemTextPane.Item}.
50+
* ItemTextPane is used to display a large list of items, which is often
51+
* replaced and extented.
52+
* <p>
53+
* Replacing the entire list, requires an update to the {@link StyledDocument},
54+
* this is performed in a worker thread, as a result the event dispatch thread
55+
* will not be blocked.
56+
* <p>
57+
* An {@link Item} can be tagged and incomplete. If this is the case the item
58+
* will be removed, when the next item with the same tag is displayed.
59+
* <p>
60+
* {@link ItemTextPane} is used in {@link LoggingPanel}.
61+
*
62+
* @author Matthias Arzt
63+
*/
64+
class ItemTextPane {
65+
66+
private final ThreadService threadService;
67+
68+
private DocumentCalculator initialCalculator = null;
69+
70+
private JTextPane textPane = new JTextPane();
71+
72+
private JScrollPane scrollPane = new JScrollPane(textPane);
73+
74+
private boolean waitingForProcessNewItems = false;
75+
76+
private DocumentCalculator calculator =
77+
new DocumentCalculator(Collections.<Item> emptyList().iterator());
78+
79+
// -- constructor --
80+
81+
ItemTextPane(ThreadService threadService) {
82+
this.threadService = Objects.requireNonNull(threadService);
83+
textPane.setEditable(false);
84+
textPane.setFont(new Font("monospaced", Font.PLAIN, 12));
85+
}
86+
87+
// -- ItemTextPane methods --
88+
89+
JComponent getJComponent() {
90+
return scrollPane;
91+
}
92+
93+
public void setPopupMenu(JPopupMenu menu) {
94+
textPane.setComponentPopupMenu(menu);
95+
}
96+
97+
/**
98+
* Set the {@link ItemTextPane.Item}s to be displayed in the
99+
* {@link JTextPane}.
100+
*
101+
* @param data The iterator will be used by a SwingWorker, and the
102+
* SwingThread. NB: Each time {@link ItemTextPane#update()} is called
103+
* {@link Iterator#hasNext()} will be called again (even if it
104+
* returned false before) to check if maybe the iterator provides new
105+
* items.
106+
*/
107+
public void setData(Iterator<Item> data) {
108+
calculator.cancel();
109+
if (initialCalculator != null) initialCalculator.cancel();
110+
DocumentCalculator calculator = new DocumentCalculator(data);
111+
initialCalculator = calculator;
112+
threadService.run(() -> initCalculator(calculator));
113+
}
114+
115+
/**
116+
* This initiates to check {@link Iterator#hasNext()} of iterator previously
117+
* set with {@link #setData(Iterator)} again. If {@link Iterator#hasNext()}
118+
* returns true, the new items will be red from the Iterator, and displayed.
119+
*/
120+
public void update() {
121+
if (waitingForProcessNewItems) return;
122+
waitingForProcessNewItems = true;
123+
threadService.queue(this::processNewItemsInSwingThread);
124+
}
125+
126+
/** Copy selected text to the clipboard. */
127+
public void copySelectionToClipboard() {
128+
textPane.copy();
129+
}
130+
131+
// -- Helper methods --
132+
133+
private void processNewItemsInSwingThread() {
134+
if (calculator.isCanceled()) return;
135+
136+
waitingForProcessNewItems = false;
137+
boolean atBottom = StaticSwingUtils.isScrolledToBottom(scrollPane);
138+
calculator.update();
139+
if (atBottom) StaticSwingUtils.scrollToBottom(scrollPane);
140+
}
141+
142+
private void initCalculator(DocumentCalculator calculator) {
143+
calculator.update();
144+
if (calculator.isCanceled()) return;
145+
threadService.queue(() -> applyCalculator(calculator));
146+
}
147+
148+
private void applyCalculator(DocumentCalculator calculator) {
149+
if (initialCalculator != calculator) return;
150+
this.calculator = calculator;
151+
textPane.setDocument(calculator.document());
152+
processNewItemsInSwingThread();
153+
threadService.queue(() -> StaticSwingUtils.scrollToBottom(scrollPane));
154+
}
155+
156+
// -- Helper methods - testing --
157+
158+
JTextPane getTextPane() {
159+
return textPane;
160+
}
161+
162+
// -- public Helper classes --
163+
164+
public static class Item {
165+
166+
private final String text;
167+
private final AttributeSet style;
168+
169+
Item(AttributeSet style, String text) {
170+
this.style = style;
171+
this.text = text;
172+
}
173+
174+
final String text() {
175+
return text;
176+
}
177+
178+
final AttributeSet style() {
179+
return style;
180+
}
181+
}
182+
183+
// -- Helper classes --
184+
185+
/**
186+
* {@link DocumentCalculator} is used to calculate a {@link StyledDocument}
187+
* for a given {@link Iterator} of {@link Item}s. NB: Items can be incomplete
188+
* and tagged. Such incomplete tagged Item will be replaced by the following
189+
* item with the same tag.
190+
*/
191+
static class DocumentCalculator {
192+
193+
private final Iterator<Item> data;
194+
195+
private final StyledDocument document = new DefaultStyledDocument();
196+
197+
private boolean canceled = false;
198+
199+
DocumentCalculator(Iterator<Item> data) {
200+
this.data = data;
201+
}
202+
203+
public StyledDocument document() {
204+
return document;
205+
}
206+
207+
public boolean isCanceled() {
208+
return canceled;
209+
}
210+
211+
public void cancel() {
212+
canceled = true;
213+
}
214+
215+
public synchronized void update() {
216+
while (data.hasNext() && !canceled)
217+
addText(data.next());
218+
}
219+
220+
private void addText(Item item) {
221+
try {
222+
document.insertString(document.getLength(), item.text(), item.style());
223+
} catch (BadLocationException e) {
224+
// ignore
225+
}
226+
}
227+
}
228+
}

0 commit comments

Comments
 (0)