diff options
Diffstat (limited to 'plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley')
16 files changed, 1320 insertions, 0 deletions
diff --git a/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractOnSelectionTableFormView.java b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractOnSelectionTableFormView.java new file mode 100644 index 000000000..fb36e364d --- /dev/null +++ b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractOnSelectionTableFormView.java @@ -0,0 +1,91 @@ +/** + * + */ +package org.eclipse.emf.parsley.views; + + +import java.util.List; + +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.parsley.factories.TableFormFactory; +import org.eclipse.emf.parsley.factories.ViewerFactory; +import org.eclipse.emf.parsley.ui.provider.FeaturesProvider; +import org.eclipse.emf.parsley.widgets.TableFormComposite; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.ScrolledComposite; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Table; +import org.eclipse.ui.IWorkbenchPart; + +import com.google.inject.Inject; + +/** + * A View that visualizes the list of elements of an EObject (it also acts as a + * selection provider), filtered by the specified type. + * + * @author Lorenzo Bettini + * + */ +public abstract class AbstractOnSelectionTableFormView extends + AbstractOnSelectionView { + + @Inject + protected ViewerFactory viewerFactory; + + @Inject + protected FeaturesProvider featuresProvider; + + @Inject + protected TableFormFactory tableFormFactory; + + protected TableFormComposite tableFormDetailComposite; + + @Override + public void createPartControl(Composite parent) { + super.createPartControl(parent); + + tableFormDetailComposite = tableFormFactory.createTableFormMasterDetailComposite(parent, + SWT.BORDER, getEClass()); + } + + @Override + protected void updateOnSelection(IWorkbenchPart sourcepart, + ISelection selection) { + + EObject eObject = getFirstSelectedEObject(selection); + tableFormDetailComposite.update(null); + if (eObject != null) { + update(eObject); + } + } + + protected void update(EObject eObject) { + EStructuralFeature feature = getEStructuralFeature(); + List<EStructuralFeature> allowedFeatures= featuresProvider.getFeatures(eObject); + if (!allowedFeatures.contains(feature)) + return; + + Object value = eObject.eGet(feature); + tableFormDetailComposite.update(value); + } + + + public void setFocus() { + tableFormDetailComposite.getViewer().getControl().setFocus(); + } + + /** + * @return the {@link EStructuralFeature} to retrieve the values of the + * selected {@link EObject} to show on the table + */ + protected abstract EStructuralFeature getEStructuralFeature(); + + /** + * @return the {@link EClass} to build the table columns + */ + protected abstract EClass getEClass(); +} diff --git a/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractOnSelectionTableView.java b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractOnSelectionTableView.java new file mode 100644 index 000000000..96a6da568 --- /dev/null +++ b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractOnSelectionTableView.java @@ -0,0 +1,106 @@ +/** + * + */ +package org.eclipse.emf.parsley.views; + + +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.parsley.factories.ViewerFactory; +import org.eclipse.emf.parsley.ui.provider.FeaturesProvider; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.ScrolledComposite; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Table; +import org.eclipse.ui.IWorkbenchPart; + +import com.google.inject.Inject; + +/** + * A View that visualizes the list of elements of an EObject (it also acts as a + * selection provider), filtered by the specified type. + * + * @author Lorenzo Bettini + * + */ +public abstract class AbstractOnSelectionTableView extends + AbstractOnSelectionView { + + @Inject + protected ViewerFactory viewerFactory; + + @Inject + protected FeaturesProvider featuresProvider; + + protected Composite parent; + + protected TableViewer tableViewer; + + public AbstractOnSelectionTableView() { + } + + @Override + public void createPartControl(Composite parent) { + super.createPartControl(parent); + + this.parent = parent; + createTableViewer(); + getSite().setSelectionProvider(tableViewer); + parent.layout(true, true); + } + + @Override + protected void updateOnSelection(IWorkbenchPart sourcepart, + ISelection selection) { + if (tableViewer == null) + return; + EObject eObject = getFirstSelectedEObject(selection); + tableViewer.setInput(null); + if (eObject != null) { + update(eObject); + } + } + + protected void update(EObject eObject) { + EStructuralFeature feature = getEStructuralFeature(); + + if (!eObject.eClass().getEAllStructuralFeatures().contains(feature)) + return; + + Object value = eObject.eGet(feature); + tableViewer.setInput(value); + } + + protected void createTableViewer() { + ScrolledComposite scrolledComposite = new ScrolledComposite(parent, + SWT.V_SCROLL | SWT.BORDER); + scrolledComposite.setExpandHorizontal(true); + scrolledComposite.setExpandVertical(true); + + tableViewer = viewerFactory.createTableViewer(scrolledComposite, + SWT.BORDER | SWT.FULL_SELECTION, null, getEClass()); + + Table table = tableViewer.getTable(); + scrolledComposite.setContent(table); + scrolledComposite.setMinSize(table + .computeSize(SWT.DEFAULT, SWT.DEFAULT)); + } + + public void setFocus() { + tableViewer.getTable().setFocus(); + } + + /** + * @return the {@link EStructuralFeature} to retrieve the values of the + * selected {@link EObject} to show on the table + */ + protected abstract EStructuralFeature getEStructuralFeature(); + + /** + * @return the {@link EClass} to build the table columns + */ + protected abstract EClass getEClass(); +} diff --git a/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractOnSelectionView.java b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractOnSelectionView.java new file mode 100644 index 000000000..ccc839cea --- /dev/null +++ b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractOnSelectionView.java @@ -0,0 +1,64 @@ +/** + * + */ +package org.eclipse.emf.parsley.views; + + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.parsley.util.EmfSelectionHelper; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.ui.ISelectionListener; +import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.ui.part.ViewPart; + +import com.google.inject.Inject; + +/** + * An abstract View that reacts on selection (it should show something related + * to an emf resource or object). + * + * @author Lorenzo Bettini + * + */ +public abstract class AbstractOnSelectionView extends ViewPart { + + @Inject + protected EmfSelectionHelper selectionHelper; + + // the listener we register with the selection service + private ISelectionListener listener = new ISelectionListener() { + public void selectionChanged(IWorkbenchPart sourcepart, + ISelection selection) { + // we ignore our own selections + if (sourcepart != AbstractOnSelectionView.this) { + updateOnSelection(sourcepart, selection); + } + } + }; + + protected abstract void updateOnSelection(IWorkbenchPart sourcepart, + ISelection selection); + + @Override + public void createPartControl(Composite parent) { + getSite().getWorkbenchWindow().getSelectionService() + .addSelectionListener(listener); + } + + protected Object getFirstSelectedElement(ISelection selection) { + return selectionHelper.getFirstSelectedElement(selection); + } + + protected EObject getFirstSelectedEObject(ISelection selection) { + return selectionHelper.getFirstSelectedEObject(selection); + } + + public void dispose() { + // important: We need do unregister our listener when the view is + // disposed + getSite().getWorkbenchWindow().getSelectionService() + .removeSelectionListener(listener); + super.dispose(); + } +} diff --git a/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractSaveableTableFormView.java b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractSaveableTableFormView.java new file mode 100644 index 000000000..6d055e7b7 --- /dev/null +++ b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractSaveableTableFormView.java @@ -0,0 +1,58 @@ +package org.eclipse.emf.parsley.views; + + +import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.parsley.factories.TableFormFactory; +import org.eclipse.emf.parsley.factories.TreeFormFactory; +import org.eclipse.emf.parsley.widgets.TableFormComposite; +import org.eclipse.emf.parsley.widgets.TreeFormComposite; +import org.eclipse.jface.viewers.StructuredViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; + +import com.google.inject.Inject; + +public abstract class AbstractSaveableTableFormView extends AbstractSaveableView + { + + @Inject + protected TableFormFactory tableFormFactory; + + protected TableFormComposite tableFormComposite; + + protected Object getContents(Resource resource) { + return resource; + } + + @Override + public void createPartControl(Composite parent) { + super.createPartControl(parent); + + tableFormComposite = tableFormFactory + .createTableFormMasterDetailComposite(parent, SWT.BORDER,getEClass()); + + tableFormComposite.update(getContents(getResource())); + + addContextMenu(tableFormComposite.getViewer()); + } + + @Override + public void setFocus() { + tableFormComposite.setFocus(); + } + + public StructuredViewer getViewer() { + return tableFormComposite.getViewer(); + } + + protected abstract EClass getEClass(); + + @Override + protected void mostRecentCommandAffectsResource(Command mostRecentCommand) { + super.mostRecentCommandAffectsResource(mostRecentCommand); + // for TableViewer the refresh does not seem to be automatic + tableFormComposite.getViewer().refresh(); + } +} diff --git a/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractSaveableTableView.java b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractSaveableTableView.java new file mode 100644 index 000000000..27c11377f --- /dev/null +++ b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractSaveableTableView.java @@ -0,0 +1,65 @@ +package org.eclipse.emf.parsley.views; + + +import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.parsley.builders.TableViewerBuilder; +import org.eclipse.emf.parsley.resource.ResourceLoader; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; + +import com.google.inject.Inject; + +public abstract class AbstractSaveableTableView extends AbstractSaveableView { + + @Inject + protected TableViewerBuilder tableViewerBuilder; + + @Inject + protected ResourceLoader resourceLoader; + + protected TableViewer tableViewer; + + @Override + public void createPartControl(Composite parent) { + super.createPartControl(parent); + + tableViewer = new TableViewer(parent, SWT.BORDER | SWT.FULL_SELECTION); + + tableViewerBuilder.buildAndFill(tableViewer, + getContents(getResource()), getEClass()); + + addContextMenu(tableViewer); + + getSite().setSelectionProvider(tableViewer); + } + + /** + * @param resource + * @return the contents from the passed resource to be shown in the table + */ + protected abstract Object getContents(Resource resource); + + /** + * @return the {@link EClass} for objects to be shown in the table + */ + protected abstract EClass getEClass(); + + @Override + public void setFocus() { + tableViewer.getTable().setFocus(); + } + + public TableViewer getViewer() { + return tableViewer; + } + + @Override + protected void mostRecentCommandAffectsResource(Command mostRecentCommand) { + super.mostRecentCommandAffectsResource(mostRecentCommand); + // for TableViewer the refresh does not seem to be automatic + tableViewer.refresh(); + } +} diff --git a/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractSaveableTreeFormView.java b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractSaveableTreeFormView.java new file mode 100644 index 000000000..500fce170 --- /dev/null +++ b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractSaveableTreeFormView.java @@ -0,0 +1,46 @@ +package org.eclipse.emf.parsley.views; + + +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.parsley.factories.TreeFormFactory; +import org.eclipse.emf.parsley.widgets.TreeFormComposite; +import org.eclipse.jface.viewers.StructuredViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; + +import com.google.inject.Inject; + +public abstract class AbstractSaveableTreeFormView extends AbstractSaveableView + { + + @Inject + protected TreeFormFactory treeFormFactory; + + protected TreeFormComposite treeFormComposite; + + protected Object getContents(Resource resource) { + return resource; + } + + @Override + public void createPartControl(Composite parent) { + super.createPartControl(parent); + + treeFormComposite = treeFormFactory + .createTreeFormMasterDetailComposite(parent, SWT.BORDER); + + treeFormComposite.update(getContents(getResource())); + + addContextMenu(treeFormComposite.getViewer()); + } + + @Override + public void setFocus() { + treeFormComposite.setFocus(); + } + + public StructuredViewer getViewer() { + return treeFormComposite.getViewer(); + } + +} diff --git a/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractSaveableTreeView.java b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractSaveableTreeView.java new file mode 100644 index 000000000..35263cc06 --- /dev/null +++ b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractSaveableTreeView.java @@ -0,0 +1,44 @@ +package org.eclipse.emf.parsley.views; + + +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.parsley.viewers.ViewerInitializer; +import org.eclipse.jface.viewers.StructuredViewer; +import org.eclipse.jface.viewers.TreeViewer; +import org.eclipse.swt.widgets.Composite; + +import com.google.inject.Inject; + +public abstract class AbstractSaveableTreeView extends AbstractSaveableView { + + @Inject + protected ViewerInitializer viewerInitializer; + + protected TreeViewer treeViewer; + + protected Object getContents(Resource resource) { + return resource; + } + + @Override + public void createPartControl(Composite parent) { + super.createPartControl(parent); + + treeViewer = new TreeViewer(parent); + viewerInitializer.initialize(treeViewer, getResource()); + + addContextMenu(treeViewer); + + getSite().setSelectionProvider(treeViewer); + } + + @Override + public void setFocus() { + treeViewer.getTree().setFocus(); + } + + public StructuredViewer getViewer() { + return treeViewer; + } + +} diff --git a/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractSaveableView.java b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractSaveableView.java new file mode 100644 index 000000000..eb415821b --- /dev/null +++ b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/AbstractSaveableView.java @@ -0,0 +1,204 @@ +package org.eclipse.emf.parsley.views; + + +import java.io.IOException; +import java.util.Collection; +import java.util.EventObject; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.common.command.CommandStack; +import org.eclipse.emf.common.command.CommandStackListener; +import org.eclipse.emf.common.ui.viewer.IViewerProvider; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.edit.command.CreateChildCommand; +import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; +import org.eclipse.emf.edit.domain.EditingDomain; +import org.eclipse.emf.edit.domain.IEditingDomainProvider; +import org.eclipse.emf.parsley.edit.ResourceSaveManager; +import org.eclipse.emf.parsley.edit.actionbar.WorkbenchActionBarContributor; +import org.eclipse.emf.parsley.resource.ResourceLoader; +import org.eclipse.emf.parsley.util.EmfCommandsUtil; +import org.eclipse.emf.parsley.viewers.ViewerInitializer; +import org.eclipse.jface.action.IMenuListener; +import org.eclipse.jface.action.IMenuManager; +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.jface.viewers.StructuredViewer; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.ui.ISaveablePart; +import org.eclipse.ui.part.ViewPart; + +import com.google.inject.Inject; +import com.google.inject.Provider; + +public abstract class AbstractSaveableView extends ViewPart implements + ISaveablePart, IEditingDomainProvider, IMenuListener, IViewerProvider { + @Inject + protected Provider<AdapterFactoryEditingDomain> editingDomainProvider; + + @Inject + protected ResourceLoader resourceLoader; + + @Inject + protected ResourceSaveManager resourceSaveManager; + + @Inject +// private TreeActionBarContributor actionBarContributor; + private WorkbenchActionBarContributor actionBarContributor; + + @Inject + private ViewerInitializer viewerInitializer; + + private Resource resource; + + private boolean dirty; + + protected AdapterFactoryEditingDomain editingDomain; + + @Override + public void createPartControl(Composite parent) { + initializeEditingDomain(); + + URI uri = createResourceURI(); + loadResource(uri); + } + + protected abstract URI createResourceURI(); + + protected void loadResource(URI uri) { + resource = resourceLoader.getResource(editingDomain, uri).getResource(); + } + + protected void initializeEditingDomain() { + editingDomain = editingDomainProvider.get(); + editingDomain.getCommandStack().addCommandStackListener( + new CommandStackListener() { + public void commandStackChanged(final EventObject event) { + getSite().getWorkbenchWindow().getShell().getDisplay() + .asyncExec(new Runnable() { + public void run() { + // Try to select the affected objects. + Command mostRecentCommand = EmfCommandsUtil + .mostRecentCommand(event); + + if (EmfCommandsUtil.affectsResource( + mostRecentCommand, + getResource())) { + mostRecentCommandAffectsResource(mostRecentCommand); + } + + postCommandStackChanged(mostRecentCommand); + } + + }); + } + }); + } + + /** + * This is called after mostRecentCommandAffectsResource, so that you can + * perform additional custom actions. + * + * The default implementation is to select in the viewer the possible new + * added child; this will also make context menu work seamlessly (if the + * selection stays in the parent element, then it will not obviously change + * and the context menu actions will not be recreated and they will be + * stale). + * + * @param mostRecentCommand + */ + protected void postCommandStackChanged(Command mostRecentCommand) { + if (mostRecentCommand != null + && mostRecentCommand instanceof CreateChildCommand) { + setSelectionToViewer(mostRecentCommand.getAffectedObjects()); + } + } + + protected void setSelectionToViewer(Collection<?> collection) { + final Collection<?> theSelection = collection; + if (theSelection != null && !theSelection.isEmpty()) { + Runnable runnable = new Runnable() { + public void run() { + // Try to select the items in the current viewer. + if (getViewer() != null) { + getViewer() + .setSelection( + new StructuredSelection( + theSelection.toArray()), true); + } + } + }; + getSite().getShell().getDisplay().asyncExec(runnable); + } + } + + /** + * It is called when the {@link CommandStack} changed and the change + * concerns something which is in this {@link Resource}. + * + * @param mostRecentCommand + */ + protected void mostRecentCommandAffectsResource(Command mostRecentCommand) { + setDirtyAndFirePropertyChange(true); + } + + protected void setDirtyAndFirePropertyChange(boolean dirtyState) { + setDirty(dirtyState); + firePropertyChange(PROP_DIRTY); + } + + protected void addContextMenu(StructuredViewer viewer) { +// viewerInitializer.addContextMenu(viewer, actionBarContributor, +// editingDomain, this); + viewerInitializer.addContextMenu(viewer, actionBarContributor, + editingDomain, this, this); + } + + protected Resource getResource() { + return resource; + } + + public EditingDomain getEditingDomain() { + return editingDomain; + } + + public void menuAboutToShow(IMenuManager menuManager) { + actionBarContributor.menuAboutToShow(menuManager); + } + + public void doSave(IProgressMonitor monitor) { + try { + saveResourceAndUpdateDirtyState(); + } catch (IOException e) { + // TODO Serious log! + e.printStackTrace(); + } + } + + protected void saveResourceAndUpdateDirtyState() throws IOException { + if (resourceSaveManager.save(resource)) { + setDirtyAndFirePropertyChange(false); + } + } + + public boolean isDirty() { + return dirty; + } + + public void setDirty(boolean dirty) { + this.dirty = dirty; + } + + public void doSaveAs() { + } + + public boolean isSaveAsAllowed() { + return false; + } + + public boolean isSaveOnCloseNeeded() { + return false; + } + +} diff --git a/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/EmfComponentsViewsActivator.java b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/EmfComponentsViewsActivator.java new file mode 100644 index 000000000..187decad3 --- /dev/null +++ b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/EmfComponentsViewsActivator.java @@ -0,0 +1,50 @@ +package org.eclipse.emf.parsley.views; + +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; + +/** + * The activator class controls the plug-in life cycle + */ +public class EmfComponentsViewsActivator extends AbstractUIPlugin { + + // The plug-in ID + public static final String PLUGIN_ID = "org.eclipse.emf.parsley.views"; //$NON-NLS-1$ + + // The shared instance + private static EmfComponentsViewsActivator plugin; + + /** + * The constructor + */ + public EmfComponentsViewsActivator() { + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) + */ + public void start(BundleContext context) throws Exception { + super.start(context); + plugin = this; + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) + */ + public void stop(BundleContext context) throws Exception { + plugin = null; + super.stop(context); + } + + /** + * Returns the shared instance + * + * @return the shared instance + */ + public static EmfComponentsViewsActivator getDefault() { + return plugin; + } + +} diff --git a/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionFormView.java b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionFormView.java new file mode 100644 index 000000000..8c82b205d --- /dev/null +++ b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionFormView.java @@ -0,0 +1,73 @@ +/** + * + */ +package org.eclipse.emf.parsley.views; + + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.parsley.factories.FormFactory; +import org.eclipse.emf.parsley.widgets.FormDetailComposite; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.ui.IWorkbenchPart; + +import com.google.inject.Inject; + +/** + * A View that visualizes the list of elements of an emf selected resource (it + * also acts as a selection provider). + * + * @author Lorenzo Bettini + * + */ +public class OnSelectionFormView extends AbstractOnSelectionView { + + @Inject + protected FormFactory formFactory; + + protected Composite parent; + + protected FormDetailComposite formComposite; + + public OnSelectionFormView() { + } + + @Override + public void createPartControl(Composite parent) { + super.createPartControl(parent); + this.parent = parent; + } + + @Override + protected void updateOnSelection(IWorkbenchPart sourcepart, + ISelection selection) { + + EObject eObject = getFirstSelectedEObject(selection); + if (eObject != null) { + resetFormComposite(); + + formComposite = formFactory.createFormDetailComposite( + parent, SWT.NONE); + formComposite.init(eObject); + + parent.layout(true, true); + } + } + + protected void resetFormComposite() { + if (formComposite != null) { + formComposite.dispose(); + parent.layout(true, true); + } + } + + @Override + public void setFocus() { + if (formComposite != null) + formComposite.setFocus(); + else + parent.setFocus(); + } + +} diff --git a/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionListView.java b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionListView.java new file mode 100644 index 000000000..045a46867 --- /dev/null +++ b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionListView.java @@ -0,0 +1,26 @@ +/** + * + */ +package org.eclipse.emf.parsley.views; + +import org.eclipse.jface.viewers.ListViewer; +import org.eclipse.jface.viewers.StructuredViewer; +import org.eclipse.swt.widgets.Composite; + +/** + * A View that visualizes the list of elements of an emf selected resource (it also acts as + * a selection provider). + * + * @author Lorenzo Bettini + * + */ +public class OnSelectionListView extends OnSelectionStructuredViewerAbstractView { + + public OnSelectionListView() { + } + + @Override + protected StructuredViewer createViewer(Composite parent) { + return new ListViewer(parent); + } +} diff --git a/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionShowAllTableView.java b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionShowAllTableView.java new file mode 100644 index 000000000..0b73566a5 --- /dev/null +++ b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionShowAllTableView.java @@ -0,0 +1,138 @@ +/** + * + */ +package org.eclipse.emf.parsley.views; + + +import java.util.List; + +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EClassifier; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.parsley.factories.ViewerFactory; +import org.eclipse.emf.parsley.ui.provider.FeaturesProvider; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.ScrolledComposite; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Table; +import org.eclipse.ui.IWorkbenchPart; + +import com.google.inject.Inject; + +/** + * A View that visualizes the list of elements of an emf selected resource (it + * also acts as a selection provider). + * + * @author Lorenzo Bettini + * + */ +public class OnSelectionShowAllTableView extends AbstractOnSelectionView { + + @Inject + protected ViewerFactory viewerFactory; + + @Inject + protected FeaturesProvider featuresProvider; + + protected Composite parent; + + protected ScrolledComposite scrolledComposite; + + public OnSelectionShowAllTableView() { + } + + @Override + public void createPartControl(Composite parent) { + super.createPartControl(parent); + + this.parent = parent; + resetView(); + } + + @Override + protected void updateOnSelection(IWorkbenchPart sourcepart, + ISelection selection) { + resetView(); + + EObject eObject = getFirstSelectedEObject(selection); + if (eObject != null) { + Composite composite = new Composite(scrolledComposite, SWT.BORDER); + scrolledComposite.setContent(composite); + composite.setLayout(new GridLayout(1, false)); + composite.setLayoutData(new GridData(GridData.FILL_BOTH)); + + buildTableForSelectedObject(eObject, composite); + + composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); + parent.layout(true, true); + } + } + + /** + * Builds many tables: the first one with the features of the passed eObject + * and, in turn, a table for each single multi feature of the eObject (each + * such table with the contents of the multi feature) + * + * @param eObject + * @param composite + */ + protected void buildTableForSelectedObject(EObject eObject, + Composite composite) { + EClass eObjectEClass = eObject.eClass(); + buildTable(eObject, eObjectEClass, composite, eObjectEClass.getName()); + List<EStructuralFeature> features = featuresProvider + .getFeatures(eObjectEClass); + for (EStructuralFeature eStructuralFeature : features) { + if (eStructuralFeature.isMany()) { + EClassifier eType = eStructuralFeature.getEType(); + if (eType instanceof EClass) { + EClass eClass = (EClass) eType; + buildTable(eObject.eGet(eStructuralFeature), eClass, + composite, eStructuralFeature.getName()); + } + } + } + } + + /** + * @param object + * the contents to show in the table + * @param eClass + * the EClass of the contents (that is, the EClass of the object + * if the contents is a single object, or the EClass of the + * objects in the list, if the contents is a list) + * @param composite + * @param label + */ + protected void buildTable(Object object, EClass eClass, + Composite composite, String label) { + Label lblNewLabel = new Label(composite, SWT.NONE); + lblNewLabel.setText(label); + + TableViewer tableViewer = viewerFactory.createTableViewer(composite, + SWT.BORDER | SWT.FULL_SELECTION, object, eClass); + + Table table = tableViewer.getTable(); + table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1)); + } + + private void resetView() { + if (scrolledComposite != null) + scrolledComposite.dispose(); + + scrolledComposite = new ScrolledComposite(parent, SWT.V_SCROLL + | SWT.BORDER); + scrolledComposite.setExpandHorizontal(true); + } + + public void setFocus() { + scrolledComposite.setFocus(); + } + +} diff --git a/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionStructuredViewerAbstractView.java b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionStructuredViewerAbstractView.java new file mode 100644 index 000000000..40784811f --- /dev/null +++ b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionStructuredViewerAbstractView.java @@ -0,0 +1,147 @@ +/** + * + */ +package org.eclipse.emf.parsley.views; + + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.parsley.viewers.ViewerInitializer; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.StructuredViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.ui.part.PageBook; + +import com.google.inject.Inject; + +/** + * An abstract View that visualizes the emf selected resource (it also acts as a + * selection provider). + * + * @author Lorenzo Bettini + * + */ +public abstract class OnSelectionStructuredViewerAbstractView extends AbstractOnSelectionView { + + private PageBook pagebook; + + private Label label; + + private StructuredViewer viewer; + + @Inject + protected ViewerInitializer viewerInitializer; + + public OnSelectionStructuredViewerAbstractView() { + } + + public StructuredViewer getViewer() { + return viewer; + } + + /** + * Sets the viewer and makes the pagebook show it + * + * @param viewer + */ + public void setViewer(StructuredViewer viewer) { + this.viewer = viewer; + pagebook.showPage(viewer.getControl()); + } + + protected void updateOnSelection(IWorkbenchPart sourcepart, + ISelection selection) { + Object element = getFirstSelectedElement(selection); +// if (element instanceof IResource) { +// IResource resource = (IResource) element; +// performUpdateOnSelection(resource); +// // viewer.expandAll(); +// showEmfViewer(); +// return; +// } else + if (element instanceof Resource) { + Resource resource = (Resource) element; + performUpdateOnSelection(resource); + showEmfViewer(); + return; + } else if (element instanceof EObject) { + EObject eObject = (EObject) element; + performUpdateOnSelection(eObject); + showEmfViewer(); + return; + } + + showText(); + } + + /** + * @param eObject + */ + protected void performUpdateOnSelection(EObject eObject) { + viewerInitializer.initialize(viewer, eObject); + } + + /** + * @param resource + */ + protected void performUpdateOnSelection(Resource resource) { + viewerInitializer.initialize(viewer, resource); + } + +// /** +// * @param resource +// */ +// protected void performUpdateOnSelection(IResource resource) { +// URI uri = URI.createPlatformResourceURI(resource.getFullPath() +// .toString(), true); +// viewerInitializer.initialize(viewer, uri); +// } + + @Override + public void createPartControl(Composite parent) { + super.createPartControl(parent); + + // the PageBook allows simple switching between two viewers + pagebook = new PageBook(parent, SWT.NONE); + + label = new Label(pagebook, SWT.H_SCROLL | SWT.V_SCROLL); + label + .setText( + "Select one XMI resource in the explorer or any EObject in a view"); + + viewer = createViewer(pagebook); + + showText(); + + getSite().setSelectionProvider(viewer); + } + + /** + * Subclasses should implement this. + * + * @param parent + * @return + */ + protected abstract StructuredViewer createViewer(Composite parent); + + protected void showText() { + pagebook.showPage(label); + } + + protected void showEmfViewer() { + pagebook.showPage(viewer.getControl()); + } + + @Override + public void setFocus() { + pagebook.setFocus(); + } + + public void init(URI resourceURI) { + viewerInitializer.initialize(viewer, resourceURI); + } +} diff --git a/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionTableFormView.java b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionTableFormView.java new file mode 100644 index 000000000..0101a6e59 --- /dev/null +++ b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionTableFormView.java @@ -0,0 +1,142 @@ +package org.eclipse.emf.parsley.views; + + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EReference; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.parsley.factories.TableFormFactory; +import org.eclipse.emf.parsley.ui.provider.FeaturesProvider; +import org.eclipse.emf.parsley.ui.provider.PropertyDescriptionProvider; +import org.eclipse.emf.parsley.widgets.TableFormComposite; +import org.eclipse.jface.viewers.ArrayContentProvider; +import org.eclipse.jface.viewers.ComboViewer; +import org.eclipse.jface.viewers.ILabelProvider; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionChangedListener; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.SelectionChangedEvent; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.ui.IWorkbenchPart; + +import com.google.inject.Inject; + +public class OnSelectionTableFormView extends AbstractOnSelectionView { + + @Inject + protected TableFormFactory tableFormFactory; + + @Inject + protected ILabelProvider labelProvider; + + @Inject + protected FeaturesProvider featuresProvider; + + protected TableFormComposite tableFormDetailComposite; + + private Label featureSelectionlabel; + + private Composite featureSelectionContainer; + + private EObject currentEObject; + + private ComboViewer featuresViewer; + + @Override + public void createPartControl(Composite parent) { + parent.setLayout(new GridLayout()); + createFeatureSelectionPart(parent); + super.createPartControl(parent); + tableFormDetailComposite = tableFormFactory.createTableFormMasterDetailComposite(parent, + SWT.BORDER); + tableFormDetailComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); + } + + private void createFeatureSelectionPart(Composite parent) { + featureSelectionContainer=new Composite(parent, SWT.NONE); + featureSelectionContainer.setLayout(new GridLayout(2,false)); + featureSelectionContainer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + featureSelectionlabel=new Label(featureSelectionContainer,SWT.NONE); + featuresViewer=new ComboViewer(featureSelectionContainer); + featuresViewer.setLabelProvider(labelProvider); + featuresViewer.setContentProvider(new ArrayContentProvider()); + featuresViewer.getControl().setLayoutData(new GridData(300,-1)); + featuresViewer.addSelectionChangedListener(new ISelectionChangedListener() { + + public void selectionChanged(SelectionChangedEvent event) { + if(!event.getSelection().isEmpty() && ((IStructuredSelection)event.getSelection()).getFirstElement()instanceof EStructuralFeature){ + EStructuralFeature feature=(EStructuralFeature) ((IStructuredSelection)event.getSelection()).getFirstElement(); + List elements= (List) currentEObject.eGet(feature); + if(elements.size()>0){ + tableFormDetailComposite.update(elements); + }else{ + tableFormDetailComposite.buildTable((EClass)feature.getEType()); + } + }else{ + tableFormDetailComposite.update(null); + } + } + }); + } + + @Override + protected void updateOnSelection(IWorkbenchPart sourcepart, + ISelection selection) { + if(getFirstSelectedElement(selection)instanceof EObject){ + currentEObject=(EObject) getFirstSelectedElement(selection); + List<EStructuralFeature> allowedFeatures = selectNotEmptyReferences(extractAllowedReferences(),currentEObject); + featuresViewer.setInput(allowedFeatures); + if(allowedFeatures.size()>1){ + featureSelectionlabel.setText("Select a feature for object "+labelProvider.getText(currentEObject)); + featureSelectionContainer.setVisible(true); + featureSelectionContainer.layout(true,true); + }else{ + featureSelectionlabel.setText(""); + featureSelectionContainer.setVisible(false); + featureSelectionContainer.layout(true,true); + } + + }else{ + currentEObject=null; + featureSelectionlabel.setText(""); + featureSelectionContainer.setVisible(false); + featureSelectionContainer.layout(true,true); + } + } + + private List<EStructuralFeature> selectNotEmptyReferences(List<EStructuralFeature> references, + EObject eobj) { + List<EStructuralFeature> notEmptyReferences=new ArrayList<EStructuralFeature>(); + for (EStructuralFeature eStructuralFeature : references) { + if(((List) eobj.eGet(eStructuralFeature)).size()>0){ + notEmptyReferences.add(eStructuralFeature); + } + } + return notEmptyReferences; + } + + private List<EStructuralFeature> extractAllowedReferences() { + List<EStructuralFeature> allowedFeatures= featuresProvider.getFeatures(currentEObject); + List<EStructuralFeature> contaimnedReferences=new ArrayList<EStructuralFeature>(); + for (EStructuralFeature eStructuralFeature : allowedFeatures) { + if(eStructuralFeature instanceof EReference && ((EReference)eStructuralFeature).isContainment()){ + contaimnedReferences.add(eStructuralFeature); + } + } + return contaimnedReferences; + } + + + @Override + public void setFocus() { + tableFormDetailComposite.setFocus(); + } + +} diff --git a/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionTreeFormView.java b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionTreeFormView.java new file mode 100644 index 000000000..fd92db3fa --- /dev/null +++ b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionTreeFormView.java @@ -0,0 +1,39 @@ +package org.eclipse.emf.parsley.views; + + +import org.eclipse.emf.parsley.factories.TreeFormFactory; +import org.eclipse.emf.parsley.widgets.TreeFormComposite; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.ui.IWorkbenchPart; + +import com.google.inject.Inject; + +public class OnSelectionTreeFormView extends AbstractOnSelectionView { + + @Inject + protected TreeFormFactory treeFormFactory; + + protected TreeFormComposite treeFormDetailComposite; + + @Override + public void createPartControl(Composite parent) { + super.createPartControl(parent); + + treeFormDetailComposite = treeFormFactory.createTreeFormMasterDetailComposite(parent, + SWT.BORDER); + } + + @Override + protected void updateOnSelection(IWorkbenchPart sourcepart, + ISelection selection) { + treeFormDetailComposite.update(getFirstSelectedElement(selection)); + } + + @Override + public void setFocus() { + treeFormDetailComposite.setFocus(); + } + +} diff --git a/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionTreeView.java b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionTreeView.java new file mode 100644 index 000000000..61de0e981 --- /dev/null +++ b/plugins/org.eclipse.emf.parsley.views.common/src/org/eclipse/emf/parsley/views/OnSelectionTreeView.java @@ -0,0 +1,27 @@ +/** + * + */ +package org.eclipse.emf.parsley.views; + +import org.eclipse.jface.viewers.StructuredViewer; +import org.eclipse.jface.viewers.TreeViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; + +/** + * A View that visualizes the tree of an emf selected resource (it also acts as + * a selection provider). + * + * @author Lorenzo Bettini + * + */ +public class OnSelectionTreeView extends OnSelectionStructuredViewerAbstractView { + + public OnSelectionTreeView() { + } + + @Override + protected StructuredViewer createViewer(Composite parent) { + return new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); + } +} |