added option to limit the number of messages in list

This commit is contained in:
fros4943 2009-10-19 17:33:25 +00:00
parent 2bc52699c6
commit 9d7275db7e

View file

@ -26,14 +26,14 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: MessageList.java,v 1.13 2009/06/25 15:46:57 fros4943 Exp $
* $Id: MessageList.java,v 1.14 2009/10/19 17:33:25 fros4943 Exp $
*
* -----------------------------------------------------------------
*
* Author : Adam Dunkels, Joakim Eriksson, Niclas Finne, Fredrik Osterlind
* Created : 2006-06-14
* Updated : $Date: 2009/06/25 15:46:57 $
* $Revision: 1.13 $
* Updated : $Date: 2009/10/19 17:33:25 $
* $Revision: 1.14 $
*/
package se.sics.cooja.dialogs;
@ -79,6 +79,8 @@ public class MessageList extends JList {
private JPopupMenu popup = null;
private boolean hideNormal = false;
private int max = -1;
public MessageList() {
super.setModel(new MessageModel());
@ -86,6 +88,14 @@ public class MessageList extends JList {
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
/**
* @param max Maximum number of messages
*/
public MessageList(int max) {
this();
this.max = max;
}
public Color getForeground(int type) {
Color c = type > 0 && type <= foregrounds.length
? foregrounds[type - 1] : null;
@ -158,6 +168,10 @@ public class MessageList extends JList {
while (messages.size() > getModel().getSize()) {
((DefaultListModel) getModel()).addElement(messages.get(getModel().getSize()));
}
while (max > 0 && getModel().getSize() > max) {
((DefaultListModel) getModel()).removeElementAt(0);
messages.remove(0);
}
if (scroll) {
ensureIndexIsVisible(getModel().getSize() - 1);