store messages outside model to avoid event thread problems
This commit is contained in:
parent
87ebab7113
commit
1117ce1be8
1 changed files with 31 additions and 12 deletions
|
@ -26,14 +26,14 @@
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
* $Id: MessageList.java,v 1.7 2008/11/04 17:33:42 fros4943 Exp $
|
* $Id: MessageList.java,v 1.8 2008/12/03 15:17:56 fros4943 Exp $
|
||||||
*
|
*
|
||||||
* -----------------------------------------------------------------
|
* -----------------------------------------------------------------
|
||||||
*
|
*
|
||||||
* Author : Adam Dunkels, Joakim Eriksson, Niclas Finne, Fredrik Osterlind
|
* Author : Adam Dunkels, Joakim Eriksson, Niclas Finne, Fredrik Osterlind
|
||||||
* Created : 2006-06-14
|
* Created : 2006-06-14
|
||||||
* Updated : $Date: 2008/11/04 17:33:42 $
|
* Updated : $Date: 2008/12/03 15:17:56 $
|
||||||
* $Revision: 1.7 $
|
* $Revision: 1.8 $
|
||||||
*/
|
*/
|
||||||
package se.sics.cooja.dialogs;
|
package se.sics.cooja.dialogs;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
|
@ -44,6 +44,7 @@ import java.io.InputStreamReader;
|
||||||
import java.io.PipedInputStream;
|
import java.io.PipedInputStream;
|
||||||
import java.io.PipedOutputStream;
|
import java.io.PipedOutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
import javax.swing.DefaultListCellRenderer;
|
import javax.swing.DefaultListCellRenderer;
|
||||||
import javax.swing.DefaultListModel;
|
import javax.swing.DefaultListModel;
|
||||||
import javax.swing.JList;
|
import javax.swing.JList;
|
||||||
|
@ -125,21 +126,39 @@ public class MessageList extends JList {
|
||||||
addMessage(message, NORMAL);
|
addMessage(message, NORMAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addMessage(final String message, final int type) {
|
private ArrayList<MessageContainer> messages = new ArrayList<MessageContainer>();
|
||||||
|
|
||||||
|
public MessageContainer[] getMessages() {
|
||||||
|
MessageContainer[] messagesArray = new MessageContainer[messages.size()];
|
||||||
|
messages.toArray(messagesArray);
|
||||||
|
return messagesArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateModel() {
|
||||||
boolean scroll = getLastVisibleIndex() >= getModel().getSize() - 2;
|
boolean scroll = getLastVisibleIndex() >= getModel().getSize() - 2;
|
||||||
MessageContainer msg = new MessageContainer(message, type);
|
|
||||||
((DefaultListModel) getModel()).addElement(msg);
|
while (messages.size() > getModel().getSize()) {
|
||||||
|
((DefaultListModel) getModel()).addElement(messages.get(getModel().getSize()));
|
||||||
|
}
|
||||||
|
|
||||||
if (scroll) {
|
if (scroll) {
|
||||||
java.awt.EventQueue.invokeLater(new Runnable() {
|
ensureIndexIsVisible(getModel().getSize() - 1);
|
||||||
public void run() {
|
|
||||||
ensureIndexIsVisible(getModel().getSize() - 1);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addMessage(final String message, final int type) {
|
||||||
|
MessageContainer msg = new MessageContainer(message, type);
|
||||||
|
messages.add(msg);
|
||||||
|
|
||||||
|
java.awt.EventQueue.invokeLater(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
updateModel();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public void clearMessages() {
|
public void clearMessages() {
|
||||||
|
messages.clear();
|
||||||
((DefaultListModel) getModel()).clear();
|
((DefaultListModel) getModel()).clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,7 +171,7 @@ public class MessageList extends JList {
|
||||||
// MessageContainer
|
// MessageContainer
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
|
|
||||||
private static class MessageContainer {
|
public static class MessageContainer {
|
||||||
public final int type;
|
public final int type;
|
||||||
public final String message;
|
public final String message;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue