| /******************************************************************************* |
| * Copyright (c) 2004-2008 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: |
| * Istvan Rath - initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.viatra2.treeeditor.providers;
|
|
|
|
|
| import org.eclipse.jface.viewers.TreeViewer; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.viatra2.core.ICoreNotificationListener; |
| import org.eclipse.viatra2.core.IModelElement; |
| import org.eclipse.viatra2.core.IRelation; |
| import org.eclipse.viatra2.core.notification.ICoreNotificationObject; |
| import org.eclipse.viatra2.core.notification.ICoreNotificationObjectCreateEntity; |
| import org.eclipse.viatra2.core.notification.ICoreNotificationObjectCreateInstanceOf; |
| import org.eclipse.viatra2.core.notification.ICoreNotificationObjectCreateRelation; |
| import org.eclipse.viatra2.core.notification.ICoreNotificationObjectCreateSupertypeOf; |
| import org.eclipse.viatra2.core.notification.ICoreNotificationObjectDeleteEntity; |
| import org.eclipse.viatra2.core.notification.ICoreNotificationObjectDeleteInstanceOf; |
| import org.eclipse.viatra2.core.notification.ICoreNotificationObjectDeleteRelation; |
| import org.eclipse.viatra2.core.notification.ICoreNotificationObjectDeleteSupertypeOf; |
| import org.eclipse.viatra2.core.notification.ICoreNotificationObjectMoveTo; |
| import org.eclipse.viatra2.core.notification.ICoreNotificationObjectSetName; |
| import org.eclipse.viatra2.core.notification.ICoreNotificationObjectSetRelationFrom; |
| import org.eclipse.viatra2.core.notification.ICoreNotificationObjectSetRelationTo; |
| import org.eclipse.viatra2.core.notification.ICoreNotificationObjectSetValue; |
| import org.eclipse.viatra2.core.notification.ICoreNotificationObjectSetViewInfo; |
| import org.eclipse.viatra2.core.simple.notification.NotificationObjectTransactionBegin; |
| import org.eclipse.viatra2.core.simple.notification.NotificationObjectUndoableTransactionBegin; |
|
|
| public class ViatraTreeviewNotificationHandler implements ICoreNotificationListener {
|
|
|
| private TreeViewer iTreeViewer;
|
| private ViatraContentProvider iContentProvider;
|
| /**
|
| * True, if refreshes should be stopped due to a (long) transaction.
|
| */
|
| private boolean inTransaction = false;
|
|
|
| private boolean signalDirty = false;
|
|
|
| public ViatraTreeviewNotificationHandler(TreeViewer tv, ViatraContentProvider cp) {
|
| iTreeViewer = tv;
|
| iContentProvider = cp;
|
| }
|
|
|
| private void refreshTreeViewer()
|
| {
|
| Display.getDefault().asyncExec(new Runnable(){
|
| public void run() {
|
| iTreeViewer.refresh();
|
| }
|
|
|
| });
|
| }
|
|
|
|
|
| public void actionPerformed(final ICoreNotificationObject notification) {
|
| //System.out.println("Core notification: "+notification.getActionType()+", inTransaction: "+inTransaction);
|
| |
| switch(notification.getActionTypeEnum()){ |
| case TA_TRANSACTION_BEGIN: |
| // case TA_SUBTRANSACTION_BEGIN: |
| Object info = ((NotificationObjectTransactionBegin) notification) |
| .getInfo(); |
| if (info instanceof Boolean && ((Boolean) info).booleanValue()) { |
| inTransaction = true; |
| } |
| return; |
| case TA_UNDOABLE_TRANSACTION_BEGIN: |
| info = ((NotificationObjectUndoableTransactionBegin) notification) |
| .getInfo(); |
| if (info instanceof Boolean && ((Boolean) info).booleanValue()) { |
| inTransaction = true; |
| } |
| return; |
| case TA_UNDO_BEGIN: |
| // Object info = |
| // ((NotificationObjectUndoableTransactionBegin)notification).getInfo(); |
| // if (info instanceof Boolean && ((Boolean)info).booleanValue()) |
| // { |
| inTransaction = true; |
| // } |
| return; |
| case TA_TRANSACTION_END: |
| // case TA_SUBTRANSACTION_END: |
| inTransaction = false; |
| // iTreeViewer.refresh(); |
| refreshTreeViewer(); |
| // iContentProvider.setDirty(); // notify editor that the model has |
| // changed |
| if (signalDirty) { |
| iContentProvider.setDirty(); // notify editor that the model has |
| // changed |
| signalDirty = false; |
| } |
| return; |
| case TA_UNDO_END: |
| inTransaction = false; |
| // iTreeViewer.refresh(); |
| refreshTreeViewer(); |
| if (signalDirty) { |
| iContentProvider.setDirty(); // notify editor that the model has |
| // changed |
| signalDirty = false; |
| } |
| return; |
| }
|
|
|
| //if (inTransaction)
|
| // return;
|
|
|
| /*
|
| * In every other case (other than transaction_begin/end), dignalDirty should be set to true.
|
| */
|
| if (inTransaction)
|
| {
|
| signalDirty = true;
|
| return;
|
| }
|
|
|
| Display.getDefault().syncExec(new Runnable(){
|
| public void run() {
|
| handleNotification(notification);
|
|
|
| }
|
| });
|
|
|
|
|
| }
|
|
|
|
|
| private void handleNotification(ICoreNotificationObject notification)
|
| { |
| switch(notification.getActionTypeEnum()){ |
| case ACTION_SET_NAME: |
| ICoreNotificationObjectSetName n = (ICoreNotificationObjectSetName)notification; |
| iTreeViewer.update(n.getElement(), null); |
| |
| // instances display refresh |
| for (IModelElement inst_elem : n.getElement().getInstances()) |
| { |
| iTreeViewer.update(inst_elem, null); |
| } |
| |
| // subtypes display refresh |
| for (IModelElement st_elem : n.getElement().getSubtypes()) |
| { |
| iTreeViewer.update(st_elem, null); |
| } |
| |
| // target relations display refresh |
| for (IRelation trg_rel : n.getElement().getRelationsTo()) |
| { |
| iTreeViewer.update(trg_rel, null); |
| } |
| |
| //iContentProvider.setDirty(); |
| break; |
| case ACTION_SET_VALUE: |
| ICoreNotificationObjectSetValue n1 = (ICoreNotificationObjectSetValue)notification; |
| iTreeViewer.update(n1.getEntity(), new String[]{"name"}); |
| break; |
| case ACTION_CREATE_INSTANCEOF: |
| ICoreNotificationObjectCreateInstanceOf n2 = (ICoreNotificationObjectCreateInstanceOf)notification; |
| iTreeViewer.update(n2.getInstance(), new String[]{"name"}); |
| break; |
| case ACTION_DELETE_INSTANCEOF: |
| ICoreNotificationObjectDeleteInstanceOf n3 = (ICoreNotificationObjectDeleteInstanceOf)notification; |
| iTreeViewer.update(n3.getInstance(), new String[]{"name"}); |
| break; |
| case ACTION_CREATE_SUPERTYPEOF: |
| ICoreNotificationObjectCreateSupertypeOf n4 = (ICoreNotificationObjectCreateSupertypeOf)notification; |
| iTreeViewer.update(n4.getSub(), new String[]{"name"}); |
| break; |
| case ACTION_DELETE_SUPERTYPEOF: |
| ICoreNotificationObjectDeleteSupertypeOf n5 = (ICoreNotificationObjectDeleteSupertypeOf)notification; |
| iTreeViewer.update(n5.getSub(), new String[]{"name"}); |
| break; |
| case ACTION_SET_VIEW_INFO: |
| ICoreNotificationObjectSetViewInfo n6 = (ICoreNotificationObjectSetViewInfo)notification; |
| iTreeViewer.update(n6.getElement(), new String[]{"name"}); |
| break; |
| case ACTION_CREATE_ENTITY: |
| ICoreNotificationObjectCreateEntity n7 = (ICoreNotificationObjectCreateEntity)notification; |
| iTreeViewer.add(n7.getContainer(),n7.getCreated()); |
| //iContentProvider.addNotificationHandler(n.getCreated()); |
| break; |
| case ACTION_CREATE_RELATION: |
| ICoreNotificationObjectCreateRelation n8 = (ICoreNotificationObjectCreateRelation)notification; |
| iTreeViewer.add(n8.getFrom(),n8.getNewRelation()); |
| //iContentProvider.addNotificationHandler(n.getNewRelation()); |
| break; |
| case ACTION_MOVE_ELEMENT_TO: |
| ICoreNotificationObjectMoveTo n9 = (ICoreNotificationObjectMoveTo)notification; |
| iTreeViewer.remove(n9.getElement()); |
| iTreeViewer.add(n9.getNewContainer(),n9.getElement()); |
| break; |
| case ACTION_DELETE_ENTITY: |
| ICoreNotificationObjectDeleteEntity n10 = (ICoreNotificationObjectDeleteEntity)notification; |
| iTreeViewer.remove(n10.getDeleted()); |
| //iContentProvider.removeNotificationHandler(n.getDeleted()); |
| break; |
| case ACTION_DELETE_RELATION: |
| ICoreNotificationObjectDeleteRelation n11 = (ICoreNotificationObjectDeleteRelation)notification; |
| iTreeViewer.remove(n11.getDeleted()); |
| //iContentProvider.removeNotificationHandler(n.getDeleted()); |
| break; |
| case ACTION_DELETE_CONTAINMENT: |
| // ICoreNotificationObjectDeleteContainment n = (ICoreNotificationObjectDeleteContainment)notification; |
| // FIXME what to do for DELETE_CONTAINMENT? |
| // save: Do nothing. |
| |
| break; |
| case ACTION_SET_RELATION_FROM: |
| ICoreNotificationObjectSetRelationFrom n12 = (ICoreNotificationObjectSetRelationFrom)notification; |
| iTreeViewer.remove(n12.getRelation()); |
| iTreeViewer.add(n12.getNewFrom(),n12.getRelation()); |
| break; |
| // ADDED BY XMI: |
| case ACTION_SET_RELATION_TO: |
| ICoreNotificationObjectSetRelationTo n13 = (ICoreNotificationObjectSetRelationTo)notification; |
| iTreeViewer.update(n13.getRelation(), new String[]{"name"}); |
| break; |
| } |
|
|
| iContentProvider.setDirty(); // notify editor that the model has changed
|
| }
|
|
|
| public int getListenerCategory() {
|
| return 0;
|
| }
|
|
|
|
|
| }
|