blob: 35f569d7e894caab7a330cc6795ba2d261414f42 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2009 Zoltan Ujhelyi and 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:
* Zoltan Ujhelyi - initial API and implementation
* Istvan Rath - add support for labelprovider override
*******************************************************************************/
package org.eclipse.viatra2.visualisation.view;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.viatra2.visualisation.IVisualisationDescriptor;
import org.eclipse.viatra2.visualisation.ViatraColoredLabelProvider;
import org.eclipse.viatra2.visualisation.ViatraGraphViewer;
import org.eclipse.viatra2.visualisation.VisualisationManager;
import org.eclipse.viatra2.visualisation.VisualisationPlugin;
import org.eclipse.viatra2.visualisation.common.action.SetAutoLayoutAction;
import org.eclipse.viatra2.visualisation.common.extensions.FilterConfiguration;
import org.eclipse.viatra2.visualisation.common.extensions.VisualisationPreset;
import org.eclipse.viatra2.visualisation.common.extensions.internal.ExtensionAccess;
import org.eclipse.viatra2.visualisation.layouts.IViatraLayoutAlgorithm;
import org.eclipse.viatra2.visualisation.modelspace.actions.groups.CustomFilterGroup;
import org.eclipse.viatra2.visualisation.modelspace.actions.groups.LabelProviderGroup;
import org.eclipse.viatra2.visualisation.modelspace.actions.groups.LayoutGroup;
import org.eclipse.viatra2.visualisation.modelspace.actions.groups.PresetGroup;
import org.eclipse.zest.core.viewers.AbstractZoomableViewer;
import org.eclipse.zest.core.viewers.IZoomableWorkbenchPart;
import org.eclipse.zest.core.viewers.ZoomContributionViewItem;
/**
* This class acts as the common entry point of the visualisation, it handles
* the user commands from the UI and alters the graph model respectively.
* <b>WARNING</b> Only calls from the Display thread are supported except when stated otherwise.
*
* @author Zoltan Ujhelyi, Istvan Rath
*/
public class ViatraVisualisationView extends ViewPart implements
IZoomableWorkbenchPart {
private static final class OpenViewRunner implements Runnable {
IViewPart form = null;
@Override
public void run() {
IWorkbenchPage page = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage();
form = page.findView(viewId);
if (form == null) {
try {
form = page.showView(viewId);
} catch (PartInitException e) {
}
} else {
page.bringToTop(form);
}
}
public IViewPart getForm() {
return form;
}
}
/**
* The ID of the visualisation view
*/
public final static String viewId = "org.eclipse.viatra2.visualisation.view";
ViatraGraphViewer gv;
Composite parent;
Object descriptorSource;
IVisualisationDescriptor descriptor;
protected Point gvSize;
boolean autoLayout = true;
// Action definitions
private ZoomContributionViewItem zoom;
private Action clearGraph = new Action("Clear Graph") {
@Override
public void run() {
descriptor.clearGraphModel();
}
};
private Action refreshGraph = new Action("Refresh Graph") {
@Override
public void run() {
if (descriptor != null)
gv.applyLayout();
}
};
private Action saveFile = new Action("Save Graph as Image") {
@Override
public void run() {
FileDialog save = new FileDialog(PlatformUI.getWorkbench()
.getDisplay().getActiveShell(), SWT.SAVE);
save.setFilterExtensions(new String[] { "*.png" });
save.setFilterNames(new String[] { "PNG File" });
save.setFileName("modelspace.png");
String filename = save.open();
gv.saveImage(filename, SWT.IMAGE_PNG);
}
};
private SetAutoLayoutAction autoLayoutAction;
/**
* If the view is resized refresh the graph
*/
private ControlListener resizeListener = new ControlAdapter() {
/*
* (non-Javadoc)
*
* @see
* org.eclipse.swt.events.ControlAdapter#controlResized(org.eclipse.
* swt.events.ControlEvent)
*/
@Override
public void controlResized(ControlEvent e) {
if (descriptor != null && autoLayout)
redraw();
}
};
/**
* @param autoLayout
* the autoLayout to set
*/
public void setAutoLayout(boolean autoLayout) {
this.autoLayout = autoLayout;
}
/**
* Searches and returns the descriptor related to its source. If no such
* descriptor is available, the return value is null.
*
* @param descriptorSource
* the source object of the visualisation descriptor
* @return the related descriptor, or null if none is available
*/
public IVisualisationDescriptor getDescriptor(Object descriptorSource) {
return VisualisationPlugin.getVisualisationManager().getVisualisation(
descriptorSource);
}
/**
* Searches and returns the default descriptor.
*
* @return the default visualisation descriptor, or null if none is set.
*/
public IVisualisationDescriptor getDescriptor() {
return descriptor;
}
/**
* Adds a descriptorSource-descriptor pair to the view.
*
* @param descriptorSource
* the source object
* @param descriptor
* the visualisation descriptor
*/
public void setDescriptor(Object descriptorSource,
IVisualisationDescriptor descriptor) {
VisualisationManager manager = VisualisationPlugin
.getVisualisationManager();
if (this.descriptor != null) {
this.descriptor.dispose();
manager.unloadDescriptor(this.descriptorSource);
}
this.descriptorSource = descriptorSource;
this.descriptor = descriptor;
manager.loadDescriptor(descriptorSource, descriptor);
loadVisualisationDescriptor(descriptor);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.part.WorkbenchPart#dispose()
*/
@Override
public void dispose() {
if (descriptor != null) {
descriptor.dispose();
descriptor = null;
}
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
* .Composite)
*/
@Override
public void createPartControl(Composite parent) {
this.parent = parent;
if (descriptor != null)
loadVisualisationDescriptor(descriptor);
parent.addControlListener(resizeListener);
}
private void loadVisualisationDescriptor(IVisualisationDescriptor descriptor) {
// removing old controls
for (Control control : parent.getChildren()) {
control.dispose();
}
if (zoom != null)
zoom.dispose();
this.descriptor = descriptor;
initializeGraphViewer(parent);
initializeActions();
createMenu();
createToolbar();
parent.layout();
}
private void initializeActions() {
clearGraph.setImageDescriptor(VisualisationPlugin
.imageDescriptorFromPlugin(VisualisationPlugin.PLUGIN_ID,
"icons/clear.gif"));
refreshGraph.setImageDescriptor(VisualisationPlugin
.imageDescriptorFromPlugin(VisualisationPlugin.PLUGIN_ID,
"icons/refresh.gif"));
saveFile.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
.getImageDescriptor(ISharedImages.IMG_ETOOL_SAVE_EDIT));
zoom = new ZoomContributionViewItem(this);
autoLayoutAction = new SetAutoLayoutAction("Layout on resize");
autoLayoutAction.setChecked(autoLayout);
}
/**
* Initializes the graph viewer component
*
* @param parent
*/
private void initializeGraphViewer(Composite parent) {
descriptor.createFormContributions(parent);
gv = descriptor.getViewer();
// descriptor.clearGraphModel();
getSite().setSelectionProvider(gv);
gvSize = gv.getControl().getSize();
}
private void createToolbar() {
IToolBarManager mgr = getViewSite().getActionBars().getToolBarManager();
mgr.removeAll();
descriptor.addToolbarContributions(mgr);
mgr.add(refreshGraph);
mgr.add(clearGraph);
mgr.update(true);
}
/**
* Creates the menu of the view.
*/
private void createMenu() {
IMenuManager mgr = getViewSite().getActionBars().getMenuManager();
mgr.removeAll();
descriptor.addMenuContributions(mgr);
// read data from the extension point
ExtensionAccess.reInit();
// add atomic contributions
LayoutGroup layoutItems = new LayoutGroup();
layoutItems.fillContextMenu(mgr);
LabelProviderGroup lpg = new LabelProviderGroup();
lpg.fillContextMenu(mgr);
CustomFilterGroup cfg = new CustomFilterGroup();
cfg.fillContextMenu(mgr);
// add presets
mgr.add(new Separator());
PresetGroup presetItems = new PresetGroup();
presetItems.fillContextMenu(mgr);
setVisualisationPreset(presetItems.getDefaultPreset());
// Adding common elements
mgr.add(new Separator());
mgr.add(saveFile);
mgr.add(refreshGraph);
mgr.add(autoLayoutAction);
mgr.add(clearGraph);
mgr.add(new Separator());
mgr.add(zoom);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.part.WorkbenchPart#setFocus()
*/
@Override
public void setFocus() {
}
/**
* Sets the model space provider for the graph visualisation
*
* @param provider
*/
public void setProvider(IContentProvider provider) {
gv.setContentProvider(provider);
// triggering a restructuration of the graph
gv.setInput(new Object());
gv.applyLayout();
// refresh();
}
/**
* Sets the layout algorithm
*
* @param algorithm
*/
public void setLayoutAlgorithm(IViatraLayoutAlgorithm algorithm) {
descriptor.setLayoutAlgorithm(algorithm);
}
/**
* Sets the preset for graph visualisation
*
* @param vp
*/
public void setVisualisationPreset(VisualisationPreset vp) {
descriptor.setVisualisationPreset(vp);
}
/**
* Sets the label provider for the visualisation
*
* @param lp
*/
public void setLabelProvider(ViatraColoredLabelProvider lp) {
descriptor.setLabelProvider(lp, true);
}
/**
* Sets a filter configuration for the visualisation
*
* @param fc
*/
public void setFilterConfiguration(FilterConfiguration fc) {
descriptor.setFilterConfiguration(fc, true);
}
/**
* Triggers a refresh of the visualisation with the currently set parameters
* - no relayouting.
*
* @see #redraw() for relayouting
*/
public void refresh() {
if (gv != null) {
// gv.applyLayout();
gv.refresh();
}
}
/**
* Triggers a relayouting of the visualisation with the currently set
* parameters.
*
* @see #refresh() can be used if only the labels have to be updated
*/
public void redraw() {
if (gv != null) {
gv.applyLayout();
}
}
public AbstractZoomableViewer getZoomableViewer() {
return gv;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.part.WorkbenchPart#getAdapter(java.lang.Class)
*/
@Override
public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) {
if (descriptor != null) {
Object descriptorAdapter = descriptor.getAdapter(adapter);
if (descriptorAdapter != null)
return descriptorAdapter;
}
return super.getAdapter(adapter);
}
/**
* Opens or shows the instance of the Viatra Visualisation view. This method is safe to call from background threads.
*
* @return a reference to the opened view allowing the caller to reach it.
*/
public static ViatraVisualisationView openView() {
OpenViewRunner runner = new OpenViewRunner();
Display.getDefault().syncExec(runner);
return (ViatraVisualisationView) runner.getForm();
}
}