blob: 7fcb51a2c33f66dceec4e9afd64dafe7b82bf9d9 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2009 Zoltan Ujhelyi 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:
* Zoltan Ujhelyi - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.visualisation.patterns;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.viatra2.visualisation.ViatraGraphViewer;
import org.eclipse.viatra2.visualisation.patterns.viewmodel.ViewPattern;
import org.eclipse.zest.core.viewers.GraphViewer;
import org.eclipse.zest.core.widgets.Graph;
import org.eclipse.zest.core.widgets.GraphContainer;
import org.eclipse.zest.core.widgets.GraphItem;
import org.eclipse.zest.core.widgets.GraphNode;
import org.eclipse.zest.layouts.LayoutAlgorithm;
/**
* A Graph Viewer component for Pattern Graph Visualisation. In order to operate
* with container nodes it is possible to set the layout of subgraphs inside
* compound elements using the setCompoundLayout() method.
* @author Zoltan Ujhelyi
*/
public class PatternGraphViewer extends ViatraGraphViewer {
LayoutAlgorithm compoundLayout;
/**
* Sets the layout algorithm to use inside of container nodes.
* @param compoundLayout
* the compoundLayout to set
*/
public void setCompoundLayout(LayoutAlgorithm compoundLayout) {
this.compoundLayout = compoundLayout;
}
/**
* Initializes the pattern graph viewer
* @param composite
* an SWT composite to store the created graph viewer
* @param style
* an SWT style mask
*/
public PatternGraphViewer(Composite composite, int style) {
super(composite, style);
// double click on nodes
final Graph g = getGraphControl();
final GraphViewer gv = this;
g.addMouseListener(new MouseAdapter() {
@Override
public void mouseDoubleClick(MouseEvent e) {
// Get the figure at the current mouse location
Object o = g.getFigureAt(e.x, e.y);
if (o != null) {
for (Object selected : ((StructuredSelection) gv
.getSelection()).toList()) {
GraphItem foundItem = gv.findGraphItem(selected);
if (foundItem instanceof GraphNode
&& ((GraphNode) foundItem).getNodeFigure()
.equals(o)) {
nodeDoubleClicked(selected);
}
}
}
}
});
}
/**
* Event handler for a node double click
* @param o
* the model object represented by the node double clicked
*/
private void nodeDoubleClicked(Object o) {
if (o instanceof ViewPattern) {
((PatternGraphDescriptor) descriptor).pushItem(((ViewPattern)o).getPattern());
}
}
@Override
public void refresh() {
super.refresh();
if (compoundLayout == null) return;
for (Object _node : getGraphControl().getNodes()) {
GraphNode node = (GraphNode) _node;
if (node instanceof GraphContainer) {
GraphContainer graphContainer = (GraphContainer) node;
graphContainer.setSize(-1, -1);
graphContainer.setLayoutAlgorithm(compoundLayout, true);
}
}
}
}