blob: 79a6d8e20f571a16f672fc5368d1c377fcdb990e [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Andras Schmidt, Andras Balogh, Istvan Rath and Daniel Varro
* 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:
* Andras Schmidt, Andras Balogh, Istvan Rath - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.core.simple;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.viatra2.core.ICoreNotificationListener;
import org.eclipse.viatra2.core.IModelElement;
import org.eclipse.viatra2.core.IModelSpace;
import org.eclipse.viatra2.core.INotificationManager;
import org.eclipse.viatra2.core.notification.ICoreNotificationObject;
import org.eclipse.viatra2.errors.VPMRuntimeException;
import org.eclipse.viatra2.logger.Logger;
/**
* This class specifies the required services of a notification manager.
*
* @author Andras Schmidt
*/
public class SimpleNotificationManager implements INotificationManager {
/**
* Notification category for undo managers.
*/
public final static int UNDO_SUPPORT = 0;
/**
* Notification category for transaction managers.
*/
public final static int TRANSACTION_SUPPORT = 1;
/**
* Notification category for other core event listeners.
*/
public final static int CORE_SUPPORT = 2;
/**
* Notification category for interpreter-level notification support.
*/
public final static int INTERPRETER_SUPPORT = 3;
/**
* Notification category for gui managers.
*/
public final static int GUI_SUPPORT = 4;
boolean notificationEnabled = true;
// List defaultListeners=new ArrayList();
/**
* They listen all notifications on the whole modelspace.
*/
List<ICoreNotificationListener> allListeners = new ArrayList<ICoreNotificationListener>();
int notificationLevel = 0;
Logger logger;
IModelSpace modelSpace;
/**
* Initializes the notification manager.
*
* @param l
* - the framework logger
* @param m
* - the owning modelspace
* @throws VPMRuntimeException
*/
public void init(Logger l, IModelSpace m) throws VPMRuntimeException {
modelSpace = m;
}
/**
* Adds a new default listner that has to be associated with all new model
* elements.
*
* @param l
* the new listner object
*/
/*
* public void addDefaultListener(ICoreNotificationListener l) {
* defaultListeners.add(l); }
*/
/**
* Removes a default listener
*
* @param l
* the listener to be removed
*/
/*
* public void removeDefaultListener(ICoreNotificationListener l) {
* defaultListeners.remove(l); }
*/
/**
* Returns the current collection of default listeners.
*
* @return collection of listeners
*/
/*
* public Collection getDefaultListeners() { return defaultListeners; }
*/
/**
* Enables notification dispatching.
*/
public void enableNotifications() {
notificationEnabled = true;
}
/**
* Disables notification dispatching.
*/
public void disableNotifications() {
notificationEnabled = false;
}
/**
* Checks whether notifications are enabled or not.
*
* @return true, if notification dispatching is enabed.
*/
public boolean isNotificationEnabled() {
return notificationEnabled;
}
/**
* Sets the notification level. All notifications having a category that is
* less or equal to the level will be dispatched.
*
* @param l
* the new level
*
*/
public void setNotificationLevel(int l) {
notificationLevel = l;
}
/**
* Retrives the current notification level.
*
* @return current notification level
*/
public int getNotificationLevel() {
return notificationLevel;
}
public void addAllListener(ICoreNotificationListener l) {
allListeners.add(l);
}
public void removeAllListener(ICoreNotificationListener l) {
allListeners.remove(l);
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.viatra2.core.INotificationManager#getListenAllListeners()
*/
public Collection<ICoreNotificationListener> getListenAllListeners() {
return allListeners;
}
protected int no_atom_depth = 0;
boolean skip_note_block = false;
/**
* Send the given notification to all listeners, which have a category
* higher than the set notification level.
*
* @param notification
*/
protected void sendNotification(ICoreNotificationObject notification) {
switch (notification.getActionTypeEnum()) {
case ACTION_SKIP_NOTIFICATIONS_START:
skip_note_block = true;
return;
case ACTION_SKIP_NOTIFICATIONS_END:
skip_note_block = false;
return;
}
/*if (notification.getActionTypeEnum().equals(
NotificationType.ACTION_SKIP_NOTIFICATIONS_START)) {
skip_note_block = true;
return;
}
if (notification.getActionTypeEnum().equals(
NotificationType.ACTION_SKIP_NOTIFICATIONS_END)) {
skip_note_block = false;
return;
}*/
if (skip_note_block)
return;
switch(notification.getActionTypeEnum()){
case ACTION_ATOMIC_STEP_READY:
if (no_atom_depth > 0)
return;
else
break;
case ACTION_MORE_ATOMICS_TO_ONE_ATOM_START:
no_atom_depth++;
return;
case ACTION_MORE_ATOMICS_TO_ONE_ATOM_END:
no_atom_depth--;
// TODO
if (no_atom_depth < 0)
throw new RuntimeException("error internal");
return;
}
if (notificationEnabled) {
notifyListeners(notification);
}
}
/**
* Notification is sent to all listeners who registered
*
* @param notification
*/
protected void notifyListeners(ICoreNotificationObject notification) {
// We need all listeners, who are linked to the changed elements
int level = getNotificationLevel();
// Send to all listeners, who have high enough level
for (ICoreNotificationListener listener: notification.getListeners()) {
if (level >= listener.getListenerCategory()) {
listener.actionPerformed(notification);
}
}
// All listeners also get a notification
// it = getListenAllListeners().iterator();
// while (it.hasNext()) {
// ICoreNotificationListener listener = (ICoreNotificationListener) it
// .next();
// if (level >= listener.getListenerCategory()) {
// listener.actionPerformed(notification);
// }
// }
for (ICoreNotificationListener l : new ArrayList<ICoreNotificationListener>(getListenAllListeners()))
{
if (level >= l.getListenerCategory()) {
l.actionPerformed(notification);
}
}
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.viatra2.core.INotificationManager#addNotificationListener
* (org.eclipse.viatra2.core.IModelElement,
* org.eclipse.viatra2.core.ICoreNotificationListener)
*/
public void addNotificationListener(IModelElement me,
ICoreNotificationListener l) {
((SimpleModelElement) me).addNotificationListener(l);
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.viatra2.core.INotificationManager#removeNotificationListener
* (org.eclipse.viatra2.core.IModelElement,
* org.eclipse.viatra2.core.ICoreNotificationListener)
*/
public void removeNotificationListener(IModelElement me,
ICoreNotificationListener l) {
((SimpleModelElement) me).removeNotificationListener(l);
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.viatra2.core.INotificationManager#getNotificationListeners
* (org.eclipse.viatra2.core.IModelElement)
*/
public Collection<ICoreNotificationListener> getNotificationListeners(IModelElement me) {
return ((SimpleModelElement) me).getNotificationListeners();
}
}