at.spardat.enterprise.exc.NotificationList Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
// @(#) $Id: NotificationList.java 2093 2007-11-28 14:23:36Z s3460 $
package at.spardat.enterprise.exc;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
/**
* A NotificationList is a {@link Notification} which acts as container
* for other notifications. Therefore, this class enriches Notification
* with the capability to add other notifications to this, see {@link #add(INotification)} and
* to iterate over the contained Notifications, see {@link #iterator}.
*
* Since the contained INotifications may also be NotificationLists,
* a NotificationList may hold a tree of INotifications.
*
* The type of the notification may be explicitely set via {@link #setType}. If no
* type is set, the type is derived from the most severe type of the childs.
* The order of type severity is (from most severe to least severe): T_ERROR,
* T_WARNING, T_INFORMATION, T_QUESTION.
*
* @author YSD, 22.05.2003 09:27:54
*/
public class NotificationList extends Notification {
/**
* The notifications aggregated by this.
*/
private ArrayList notifications_ = new ArrayList();
/**
* Defines that the type should be derived from the contained Notifcations
* in this.
*/
private boolean autoType_ = true;
/**
* Constructs and sets the message of this from a two
* parameter java.text.MessageFormat compatible string.
*
* @param messageFmt format string as defined in java.text.MessageFormat.
* @param param1 first message parameter
* @param param2 second message parameter
*/
public NotificationList (String messageFmt, Object param1, Object param2) {
this (messageFmt, null, new Object[]{param1,param2});
}
/**
* Constructs and sets the message of this from a one
* parameter java.text.MessageFormat compatible string.
*
* @param messageFmt format string as defined in java.text.MessageFormat.
* @param param1 message parameter
*/
public NotificationList (String messageFmt, Object param1) {
this (messageFmt, null, new Object[]{param1});
}
/**
* Constructs and sets the message to the provided value.
*
* @param message the message to set.
*/
public NotificationList (String message) {
this (message, null, null);
}
/**
* Constructs this with default values. Use one of the setMessage-methods
* to set the message for this compound notification.
*/
public NotificationList () {
this ("");
}
/**
* Constructs and sets the message from a format string as
* defined in java.text.MessageFormat. The required parameters
* must be contained in the params array.
*
* @param message format string as defined in java.text.MessageFormat or
* plain text, depending on params.
* @param l a java.util.Locale to format locale
* dependent data types or null if the
* params do not contain local specifics.
* @param params the parameters of the message. If params is null,
* message is not to considered to be a MessageFormat-string.
*/
public NotificationList (String message, Locale l, Object[] params) {
super (message, l, params);
}
/**
* Sets the type which may be one of the T_-constants. If this method
* is not called, the type is defined by the contained Notifications with
* the highest type-severity.
*
* @see INotification#getType()
* @return this
*/
public Notification setType (int type) {
super.setType (type);
autoType_ = false;
return this;
}
/**
* Returns the type. Either it has been explicitely set, then the one that
* has been set is returned. Otherwise, the most severe type of the childs
* is returned.
*
* @return one of the constants with praefix T_ in {@link INotification}.
* @see at.spardat.enterprise.exc.INotification#getType()
*/
public int getType () {
if (autoType_) {
/**
* the type is calculated from the types of the childs.
*/
int maxSevere = 0;
Iterator iter = iterator();
while (iter.hasNext()) {
INotification noti = (INotification) iter.next();
maxSevere = Math.max(maxSevere, noti.getType());
}
if (maxSevere == 0) {
// the NotificationList does not contain a single Notification in the
// complete subtree. In this case, the type defaults to T_ERROR (which
// is the default type for INotifications).
maxSevere = T_ERROR;
}
return maxSevere;
} else {
return super.getType();
}
}
/**
* Returns the number of Notifications contained in this that are
* direct childs of this.
*/
public int getChildCount () {
return notifications_.size();
}
/**
* Returns the number of Notifications contained in the subtree rooted at
* this that are not NotificationLists.
*/
public int getNotificationCount () {
int result = 0;
Iterator iterator = iterator();
while (iterator.hasNext()) {
INotification noti = (INotification) iterator.next();
if (noti instanceof NotificationList) result += ((NotificationList)noti).getNotificationCount();
else result++;
}
return result;
}
/**
* Returns an Iterator to iterate over the contained Notifications.
*/
public Iterator iterator() {
return notifications_.iterator();
}
/**
* Adds a provided notification to the end of the list of notifications. Warning:
* If the provided notification is a NotificationList itself,
* the list gets tree structured.
*
* @param INotification the notification to add
*/
public void add (INotification notification) {
notifications_.add(notification);
}
}