blob: b7b57144e7eae96532aba52e0b84a8f126ab5680 [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.wizard;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Vector;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ICheckable;
import org.eclipse.jface.viewers.IElementComparer;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
import org.eclipse.ui.internal.util.BundleUtility;
import org.eclipse.viatra2.framework.FrameworkManager;
import org.eclipse.viatra2.framework.FrameworkManagerException;
import org.eclipse.viatra2.framework.IFramework;
import org.eclipse.viatra2.treeeditor.Plugin;
import org.osgi.framework.Bundle;
/**
* Summary:
* Simple wizard, which creates a new, empty VPML modelspace file.
*
* Description:
*
*
* @author David Vago, Istvan Rath
*/
public class ViatraTreeEditorNewVPMLWizard extends Wizard implements INewWizard
{
public class ViatraEditorNewVPMLWizardPage extends WizardNewFileCreationPage
{
public ViatraEditorNewVPMLWizardPage(IStructuredSelection aSelection)
{
// Call base class constructor (WizardNewFileCreationPage)
super("This wizard creates a new VPML modelspace", aSelection);
// Set dialog (wizard page) parameters
//setDescription("To set the initial contents of the created modelspace, use the vpm.properties " +
// "file of the Viatra plug-in.");
setFileName("example.vpml");
}
}
public class ViatraEditorNewVPMLWizardSelectionPage extends WizardPage
{
protected class InternalLabelProvider extends LabelProvider
{
@Override
public String getText(Object element) {
if (element instanceof IWizardCategoryContributor)
return ((IWizardCategoryContributor)element).getName();
else if (element instanceof IWizardModelContributor)
return ((IWizardModelContributor)element).getName();
else
return null;
}
}
protected class InternalContentProvider implements ITreeContentProvider
{
public Object[] getChildren(Object parentElement) {
// System.out.println("getChildren: "+parentElement);
if (parentElement instanceof IWizardCategoryContributor)
{
String catid = ((IWizardCategoryContributor)parentElement).getID();
Vector<IWizardModelContributor> v = new Vector<IWizardModelContributor>();
for (IWizardModelContributor mc : Plugin.getDefault().getModelContributions())
{
if (mc.getCategoryID().equalsIgnoreCase(catid))
v.add(mc);
}
return v.toArray();
}
return null;
}
public Object getParent(Object element) {
if (element instanceof IWizardModelContributor)
{
for (IWizardCategoryContributor cc : Plugin.getDefault().getCategoryContributions())
{
if (cc.getID().equalsIgnoreCase(((IWizardModelContributor)element).getCategoryID()))
return cc;
}
}
return null;
}
public boolean hasChildren(Object element) {
if (element instanceof IWizardCategoryContributor)
{
String catid = ((IWizardCategoryContributor)element).getID();
for (IWizardModelContributor mc : Plugin.getDefault().getModelContributions())
{
if (mc.getCategoryID().equalsIgnoreCase(catid))
return true;
}
}
return false;
}
public Object[] getElements(Object inputElement) {
// System.out.println("getElements: "+inputElement);
if (inputElement instanceof Plugin)
return Plugin.getDefault().getCategoryContributions().toArray();
else
return null;
}
public void dispose() { }
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { }
}
protected class TableDummy
{
public String key;
public Object value;
public int id;
public TableDummy(int i, String k, Object v)
{
id=i;
key=k;
value=v;
}
}
protected class InternalTableContentProvider implements IStructuredContentProvider
{
public Object[] getElements(Object inputElement) {
if (inputElement instanceof IStructuredSelection)
{
IStructuredSelection sel = (IStructuredSelection)inputElement;
if (sel.getFirstElement() instanceof IWizardModelContributor)
{
IWizardModelContributor mc = (IWizardModelContributor)sel.getFirstElement();
Vector<TableDummy> v = new Vector<TableDummy>();
v.add(new TableDummy(0,"ID",mc.getID()));
v.add(new TableDummy(1,"Name",mc.getName()));
v.add(new TableDummy(2,"Description",mc.getDescription()));
v.add(new TableDummy(3,"Contained fragment ids",mc.getContainedIDs()));
v.add(new TableDummy(4,"Required fragment ids",mc.getRequiredIDs()));
v.add(new TableDummy(5,"Forbidden fragment ids",mc.getForbiddenIDs()));
v.add(new TableDummy(6,"VTML File",mc.getFileVTML()));
v.add(new TableDummy(7,"VPML File",mc.getFileVPML()));
return v.toArray();
}
}
return new Object[] {};
}
public void dispose() { }
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { }
}
protected class InternalTableLabelProvider implements ITableLabelProvider
{
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
@SuppressWarnings("unchecked")
public String getColumnText(Object element, int columnIndex) {
if (columnIndex==0)
{
return ((TableDummy)element).key;
}
else if (columnIndex==1)
{
TableDummy td = (TableDummy)element;
switch (td.id)
{
default: return (td.value==null?"":td.value.toString());
case 3:
case 4:
case 5: Collection<String> ss = (Collection<String>)td.value;
String r = "";
for (String s: ss)
{
r+=s+"; ";
}
return r;
}
}
return null;
}
public void addListener(ILabelProviderListener listener) { }
public void dispose() { }
public boolean isLabelProperty(Object element, String property) {
return false;
}
public void removeListener(ILabelProviderListener listener) { }
}
public CheckboxTreeViewer treeviewer;
public TableViewer tableviewer;
protected ViatraEditorNewVPMLWizardSelectionPage(String name) {
super(name);
}
public void createControl(Composite parent) {
SashForm sashform = new SashForm(parent, SWT.BORDER|SWT.VERTICAL);
treeviewer = new CheckboxTreeViewer(sashform);
treeviewer.setLabelProvider(new InternalLabelProvider());
treeviewer.setContentProvider(new InternalContentProvider());
treeviewer.setInput(Plugin.getDefault());
tableviewer = new TableViewer(sashform);
TableLayout tlay = new TableLayout();
tlay.addColumnData(new ColumnWeightData(30,true));
tlay.addColumnData(new ColumnWeightData(70,true));
tableviewer.getTable().setLayout(tlay);
TableColumn keyC =new TableColumn(tableviewer.getTable(),SWT.LEFT);
keyC.setText("Key");
TableColumn valC =new TableColumn(tableviewer.getTable(),SWT.LEFT);
valC.setText("Value");
tableviewer.getTable().setHeaderVisible(true);
tableviewer.getTable().setLinesVisible(true);
tableviewer.setLabelProvider(new InternalTableLabelProvider());
tableviewer.setContentProvider(new InternalTableContentProvider());
treeviewer.addSelectionChangedListener(
new ISelectionChangedListener()
{
public void selectionChanged(SelectionChangedEvent event) {
try {
tableviewer.setInput(event.getSelection());
}
catch (Exception e) { }
// ugly hack to swallow AsserionFailedException if user clicks on
// a category contribution in the tree
}
});
treeviewer.setComparer(
new IElementComparer()
{
public boolean equals(Object a, Object b) {
boolean aIsCategory = a instanceof IWizardCategoryContributor;
boolean bIsCategory = b instanceof IWizardCategoryContributor;
boolean aIsModel = a instanceof IWizardModelContributor;
boolean bIsModel = b instanceof IWizardModelContributor;
if (aIsCategory && bIsCategory)
{
return ((IWizardCategoryContributor)a).getID()
.equalsIgnoreCase(((IWizardCategoryContributor)b).getID());
}
else if (aIsModel && bIsModel)
{
return ((IWizardModelContributor)a).getID()
.equalsIgnoreCase(((IWizardModelContributor)b).getID());
}
return false;
}
public int hashCode(Object element) {
return element.hashCode();
}
});
treeviewer.addCheckStateListener(
new ICheckStateListener()
{
protected void check(Object element, boolean state)
{
if (treeviewer.getChecked(element) == state)
return;
treeviewer.setChecked(element,state);
ICheckable _dummy = new ICheckable()
{
public void addCheckStateListener(ICheckStateListener listener) { }
public boolean getChecked(Object element) {
return false;
}
public void removeCheckStateListener(ICheckStateListener listener) { }
public boolean setChecked(Object element, boolean state) {
return false;
}
};
checkStateChanged(new CheckStateChangedEvent(_dummy,element,state));
}
private void _colorize(Object element, TreeItem item, Color c)
{
if (treeviewer.getComparer().equals(item.getData(),element))
{
// System.out.println("Colorize: color ["+c+"] set on "+item);
item.setForeground(c);
}
else
for (TreeItem child : item.getItems())
{
_colorize(element,child,c);
}
}
protected void colorize(Object element, int color)
{
Color c = null;
switch (color)
{
default:
case 0: c = new Color(Display.getDefault(),0,0,0); break; // black
case 1: c = new Color(Display.getDefault(),255,0,0); break; // red
case 2: c = new Color(Display.getDefault(),127,127,127); break; // gray
}
for (TreeItem topitem : treeviewer.getTree().getItems())
{
_colorize(element,topitem,c);
}
}
public void checkStateChanged(CheckStateChangedEvent event) {
if (event.getElement() instanceof IWizardCategoryContributor)
{
IWizardCategoryContributor cc = (IWizardCategoryContributor)event.getElement();
if (event.getChecked())
{
// enable all model contributions belonging to this category
for (IWizardModelContributor mc : Plugin.getDefault().getModelContributions())
{
if (mc.getCategoryID().equalsIgnoreCase(cc.getID()))
{
colorize(mc,0);
check(mc,true);
}
}
}
else
{
// disable all model contributions
for (IWizardModelContributor mc : Plugin.getDefault().getModelContributions())
{
if (mc.getCategoryID().equalsIgnoreCase(cc.getID()))
{
// colorize(mc,0);
check(mc,false);
}
}
}
}
else if (event.getElement() instanceof IWizardModelContributor)
{
IWizardModelContributor mc = (IWizardModelContributor)event.getElement();
if (event.getChecked())
{
// model contribution was enabled
colorize(mc,0);
// 0. if this is the only contribution in its category, check the category
for (IWizardCategoryContributor cc : Plugin.getDefault().getCategoryContributions())
{
if (cc.getID().equalsIgnoreCase(mc.getCategoryID()))
{
int i = 0;
for (IWizardModelContributor _mc : Plugin.getDefault().getModelContributions())
{
if (_mc.getCategoryID().equalsIgnoreCase(mc.getCategoryID()))
i++;
if (i>1)
break; // this category has more than one model contribution entry
}
if (i==1)
{
check(cc,true);
break;
}
}
}
// 1. enable all required ids
for (String rid : mc.getRequiredIDs())
{
for (IWizardModelContributor rmc : Plugin.getDefault().getModelContributions())
{
if (rmc.getID().equalsIgnoreCase(rid))
{
colorize(rmc,0);
check(rmc,true);
}
}
}
// 2. disable all forbidden ids
for (String fid : mc.getForbiddenIDs())
{
for (IWizardModelContributor rmc : Plugin.getDefault().getModelContributions())
{
if (rmc.getID().equalsIgnoreCase(fid))
{
colorize(rmc,1); // red
check(rmc,false);
}
}
}
}
else
{
// model contribution was unchecked
// 0. if this is the only contribution in its category, uncheck the category
for (IWizardCategoryContributor cc : Plugin.getDefault().getCategoryContributions())
{
if (cc.getID().equalsIgnoreCase(mc.getCategoryID()))
{
int i = 0;
for (IWizardModelContributor _mc : Plugin.getDefault().getModelContributions())
{
if (_mc.getCategoryID().equalsIgnoreCase(mc.getCategoryID()))
i++;
if (i>1)
break; // this category has more than one model contribution entry
}
if (i==1)
{
check(cc,false);
break;
}
}
}
// 1. ungray any forbidden ids
for (String fid : mc.getForbiddenIDs())
{
for (IWizardModelContributor rmc : Plugin.getDefault().getModelContributions())
{
if (rmc.getID().equalsIgnoreCase(fid))
{
colorize(rmc,0);
}
}
}
// 2. uncheck and gray ids that require us
for (IWizardModelContributor _mc : Plugin.getDefault().getModelContributions())
{
if (_mc.getRequiredIDs().contains(mc.getID()))
{
colorize(_mc,2);
check(_mc,false);
}
}
}
}
}
});
setDescription("Select the appropriate modelspace fragments for your new modelspace");
sashform.setWeights(new int[]{2,1});
setControl(sashform);
}
}
@SuppressWarnings("unchecked")
public boolean performFinish()
{
// Check main page completion
if (!iMainPage.isPageComplete())
return false;
// Check selection page completion
if (!iSelectionPage.isPageComplete())
return false;
// Create new file
String basefn = iMainPage.getFileName();
if (!basefn.endsWith(".vpml"))
iMainPage.setFileName(basefn + ".vpml");
IFile newFile = iMainPage.createNewFile();
if (newFile == null)
return true;
// Create absolute file name using workspace root directory
//String ws_root = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString();
//String filename = ws_root + newFile.getFullPath().toOSString();
//An IFile knows its current location, no need to circumvent this mechanism.
String filename = newFile.getLocation().toOSString();
// Create VPML file with Viatra
FrameworkManager fwm = FrameworkManager.getInstance();
IFramework iViatra = null;
try {
iViatra = fwm.createFramework();
// import stuff into the modelspace
Vector<IWizardModelContributor> v = new Vector<IWizardModelContributor>();
for (Object o : iSelectionPage.treeviewer.getCheckedElements())
{
if (o instanceof IWizardModelContributor)
{
// System.out.println("Added "+((IWizardModelContributor)o).getID());
v.add((IWizardModelContributor)o);
}
}
Collections.sort(v);
// sorts according to dependency precedence, see WizardModelContributor.compateTo()
for (IWizardModelContributor mc : v)
{
String pluginid = mc.getPluginID();
//InputStream stream = null;
File f = null;
if (mc.getFileVPML()!=null)
{
f = getFragmentFile_fixed_3(pluginid,mc.getFileVPML());
iViatra.mergeFile(f.getAbsolutePath());
}
else if (mc.getFileVTML()!=null)
{
f = getFragmentFile_fixed_3(pluginid,mc.getFileVTML());
iViatra.nativeImport(f.getAbsolutePath(), iViatra.getNativeImportersForExtension("vtml").iterator().next());
}
}
iViatra.saveFile(filename);
//fwm.disposeFramework(iViatra.getId());
} catch (Exception e) {
e.printStackTrace();
iViatra.getLogger().message(0, "Error merging modelspace: " + e.getClass().getCanonicalName(), e );
return false;
}
finally
{
try
{
fwm.disposeFramework(iViatra.getId());
//ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
// this is insane
newFile.getParent().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
}
catch (FrameworkManagerException e) {
iViatra.getLogger().fatal(e.getClass().getCanonicalName());
} catch (CoreException e) {
iViatra.getLogger().fatal(e.getClass().getCanonicalName());
}
}
return true;
}
/*
private URL _getFragmentFile(String pluginId, String imageFilePath)
{
if (pluginId == null || imageFilePath == null) {
throw new IllegalArgumentException();
}
// if the bundle is not ready then there is no image
Bundle bundle = Platform.getBundle(pluginId);
if (!BundleUtility.isReady(bundle))
return null;
// look for the image (this will check both the plugin and fragment folders
URL fullPathString = BundleUtility.find(bundle, imageFilePath);
if (fullPathString == null) {
try {
fullPathString = new URL(imageFilePath);
} catch (MalformedURLException e) {
return null;
}
}
return fullPathString;
}
*/
private File getFragmentFile_fixed_3(String bundleId, String path)
{
// ugly hack: switch /->\ on windoze
if ("\\".equals(System.getProperty("file.separator")))
{
// switch / to \ on windoze
path = path.replace('/', '\\');
}
else if ("/".equals(System.getProperty("file.separator")))
{
path = path.replace('\\', '/');
}
File f= null;
// if the bundle is not ready then there is no file, however we may assume its perfectly ready
Bundle bundle = Platform.getBundle(bundleId);
if (bundle==null || !BundleUtility.isReady(bundle))
return null;
// look for the image (this will check both the plugin and fragment folders
java.net.URL fullPathString = null;
try {
//fullPathString = BundleUtility.find(bundle, path);
fullPathString = FileLocator.toFileURL(FileLocator.find(bundle, new Path(path), null));
f = FrameworkManager.convertURLtoFile(fullPathString);
} catch (IllegalArgumentException e) {
f = new File(fullPathString.getPath());
} // impossible
catch (IOException e)
{
e.printStackTrace();
}
// FIXME nem lehet space az utvonalban windowson!
return f;
}
public void init(IWorkbench workbench, IStructuredSelection selection)
{
// Save arguments
iSelection = selection;
// Set wizard name
this.setWindowTitle("Create new VPML modelspace");
}
public void addPages()
{
// Create new (and single) wizard page, and add it to wizard
iMainPage = new ViatraEditorNewVPMLWizardPage(iSelection);
addPage(iMainPage);
iSelectionPage = new ViatraEditorNewVPMLWizardSelectionPage("Select modelspace fragments");
addPage(iSelectionPage);
}
/**
* Save IStructuredSelection argument of init().
*/
protected IStructuredSelection iSelection;
/**
* Main page of the new VPML modelspace wizard.
*/
protected ViatraEditorNewVPMLWizardPage iMainPage;
protected ViatraEditorNewVPMLWizardSelectionPage iSelectionPage;
}