blob: 1b38f8ccaf558c1e28de9319b854c608d901e95c [file] [log] [blame]
/*******************************************************************************
* 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 java.util.Collection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.viatra2.core.IEntity;
import org.eclipse.viatra2.core.IModelElement;
import org.eclipse.viatra2.core.IRelation;
import org.eclipse.viatra2.treeeditor.ViatraTreeEditor;
public class ViatraContentProvider implements ITreeContentProvider {
private ViatraTreeviewNotificationHandler iNotificationHandler;
private ViatraTreeEditor iVTE;
private ViatraRootProvider iVRP;
public ViatraContentProvider(TreeViewer tv, ViatraTreeEditor vte) {
iVTE = vte;
iNotificationHandler = new ViatraTreeviewNotificationHandler(tv, this);
iVTE.getFramework().getTopmodel().getNotificationManager().addAllListener(iNotificationHandler);
}
public void setDirty() {
iVTE.setDirty();
}
/*
* This function should always receive root entities.
*/
public Object[] getElements(Object inputElement) {
//System.out.println("CP: getElements called for "+inputElement.toString());
if (inputElement instanceof IModelElement || inputElement instanceof ViatraRootProvider)
return getChildren(inputElement);
else
return null;
}
public void dispose()
{
iVTE.getFramework().getTopmodel().getNotificationManager().removeAllListener(iNotificationHandler);
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
if (newInput instanceof ViatraRootProvider)
iVRP = (ViatraRootProvider) newInput;
// empty for the moment
}
public class TargetRelationDummy
{
public IRelation rel;
public TargetRelationDummy(IRelation r) {
this.rel = r;
}
}
public Object[] getChildren(Object parentElement) {
// Special case: dummy root element for the content provider to show root
if (parentElement instanceof ViatraRootProvider)
{
IEntity ent = ((ViatraRootProvider) parentElement).getRootEntity();
// addNotificationHandler(ent);
return new Object[] { ent };
}
// register NH if not registered yet
// addNotificationHandler((IModelElement)parentElement);
Collection children = ((IModelElement)parentElement).getElementsInNamespace();
String showTargets = iVTE.getFramework().getProperties().getRuntimeProperty(ViatraEditorPropertyProvider.VIATRA_EDITOR_PROVIDER_ID, ViatraEditorPropertyProvider.PROP_FORMATSTRING_RELATION_TARGET_SHOW);
if ("true".equalsIgnoreCase(showTargets))
{
for (IRelation r : ((IModelElement)parentElement).getRelationsTo() )
{
children.add(new TargetRelationDummy(r));
}
}
return children.toArray();
}
public Object getParent(Object element) {
if (element instanceof IEntity)
if (((IEntity) element).getParent() == null)
return iVRP;
else
return ((IEntity)element).getParent();
else if (element instanceof IRelation)
return ((IRelation)element).getFrom();
else if (element instanceof TargetRelationDummy)
return ((TargetRelationDummy)element).rel.getTo();
return null;
}
public boolean hasChildren(Object element) {
//System.out.println("CP: hasChildren called for "+element.toString());
if (element instanceof ViatraRootProvider)
return true;
if (element instanceof IModelElement && !((IModelElement)element).getElementsInNamespace().isEmpty())
return true;
String showTargets = iVTE.getFramework().getProperties().getRuntimeProperty(ViatraEditorPropertyProvider.VIATRA_EDITOR_PROVIDER_ID, ViatraEditorPropertyProvider.PROP_FORMATSTRING_RELATION_TARGET_SHOW);
if ("true".equalsIgnoreCase(showTargets))
{
if (element instanceof IModelElement && !((IModelElement)element).getRelationsTo().isEmpty())
return true;
}
return false;
}
protected void addNotificationHandler(IModelElement me) {
if (!(me.getModelSpace()
.getNotificationManager()
.getNotificationListeners(me).contains(iNotificationHandler))) {
// System.out.println("NH: registered for "+me.getFullyQualifiedName());
me.getModelSpace()
.getNotificationManager()
.addNotificationListener(me,iNotificationHandler);
}
}
protected void removeNotificationHandler(IModelElement me) {
if ((me.getModelSpace()
.getNotificationManager()
.getNotificationListeners(me).contains(iNotificationHandler))) {
// System.out.println("NH: deregistered for "+me.getFullyQualifiedName());
me.getModelSpace()
.getNotificationManager()
.removeNotificationListener(me,iNotificationHandler);
}
}
}