Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Sphinx/tutorials"

Line 19: Line 19:
 
We must initially override the ''getChildren (Object object) ''method of the appropriate item provider (e.g., ''ConnectionItemProvider''. We decide creating an extended class of ''ConnectionItemProvider ''named ''ExtendedConnectionItemProvider'') like this:  
 
We must initially override the ''getChildren (Object object) ''method of the appropriate item provider (e.g., ''ConnectionItemProvider''. We decide creating an extended class of ''ConnectionItemProvider ''named ''ExtendedConnectionItemProvider'') like this:  
  
  @Override
+
  @Override
  public Collection<? extends EStructuralFeature> getChildrenFeatures(Object object) {
+
public Collection<? extends EStructuralFeature> getChildrenFeatures(Object object) {
      super.getChildrenFeatures(object);
+
    super.getChildrenFeatures(object);
      childrenFeatures.'''add'''(InstanceModel20Package.Literals.CONNECTION__SOURCE_PORT);
+
    childrenFeatures.'''add'''(InstanceModel20Package.Literals.CONNECTION__SOURCE_PORT);
      childrenFeatures.'''add'''(InstanceModel20Package.Literals.CONNECTION__TARGET_COMPONENT);
+
    childrenFeatures.'''add'''(InstanceModel20Package.Literals.CONNECTION__TARGET_COMPONENT);
      return childrenFeatures;
+
    return childrenFeatures;
  }
+
}
  
 
We must also override the notifyChanged(Notification notification) method of the ExtendedConnectionItemProvider class for refreshing the view if referenced objects (e.g., source port or target component) are set or modified toward the property sheet.  
 
We must also override the notifyChanged(Notification notification) method of the ExtendedConnectionItemProvider class for refreshing the view if referenced objects (e.g., source port or target component) are set or modified toward the property sheet.  
  
  @Override
+
  @Override
  public void notifyChanged(Notification notification) {
+
public void notifyChanged(Notification notification) {
      '''updateChildren'''(notification);
+
      '''updateChildren'''(notification);
 
+
      // For refreshing the view if source port and/or target component references are updated
+
      // For refreshing the view if source port and/or target component references are updated
      switch (notification.getFeatureID(Connection.class)) {
+
      switch (notification.getFeatureID(Connection.class)) {
              case '''InstanceModel20Package.CONNECTION__SOURCE_PORT''':
+
            case '''InstanceModel20Package.CONNECTION__SOURCE_PORT''':
                  fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
+
                  fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
              case '''InstanceModel20Package.CONNECTION__TARGET_COMPONENT''':
+
            case '''InstanceModel20Package.CONNECTION__TARGET_COMPONENT''':
                  fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
+
                  fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
                  return;
+
                  return;
      }
+
    }
      super.notifyChanged(notification);
+
    super.notifyChanged(notification);
  }
+
}
  
 
If referenced objects are displayed as children under a transient node (i.e., a non-model object, please see section 1.1.2 for more details) then we must override in this case the ''notifyChanged(Notification notification) ''method of this transient node for refreshing node content like this:  
 
If referenced objects are displayed as children under a transient node (i.e., a non-model object, please see section 1.1.2 for more details) then we must override in this case the ''notifyChanged(Notification notification) ''method of this transient node for refreshing node content like this:  
  
  @Override
+
  @Override
  public void notifyChanged(Notification notification) {
+
public void notifyChanged(Notification notification) {
      '''updateTransientItemProviderChildren'''(notification, this);
+
      '''updateTransientItemProviderChildren'''(notification, this);
 
+
      switch (notification.getFeatureID(Connection.class)) {
+
      switch (notification.getFeatureID(Connection.class)) {
            case '''InstanceModel20Package.CONNECTION__SOURCE_PORT''':
+
          case '''InstanceModel20Package.CONNECTION__SOURCE_PORT''':
                fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
+
                fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
            case '''InstanceModel20Package.CONNECTION__TARGET_COMPONENT''':
+
          case '''InstanceModel20Package.CONNECTION__TARGET_COMPONENT''':
                fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
+
                fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
                return;
+
                return;
      }
+
      }
      super.notifyChanged(notification);
+
      super.notifyChanged(notification);
  }
+
}
  
 
==== Adding Transient Nodes  ====
 
==== Adding Transient Nodes  ====
  
This section describes how to easily add transient nodes (i.e., non-model objects) between an object and its children into the tree view. We will for example introduce into the view, as shown in Figure 3, “'''Parameter Values'''” and “'''Outgoing Connections'''” transient nodes in order to separate children (of ''ParameterValue ''and ''Connection ''types) of Component model objects.
+
This section describes how to easily add transient nodes (i.e., non-model objects) between an object and its children into the tree view. We will for example introduce into the view, as shown in Figure 3, “'''Parameter Values'''” and “'''Outgoing Connections'''” transient nodes in order to separate children (of ''ParameterValue ''and ''Connection ''types) of Component model objects.  
  
 
[[Image:SphinxExampleAddingTransientNode.jpg]]  
 
[[Image:SphinxExampleAddingTransientNode.jpg]]  
Line 75: Line 75:
  
 
     public ParameterValuesItemProvider(AdapterFactory adapterFactory, '''Component '''component) {
 
     public ParameterValuesItemProvider(AdapterFactory adapterFactory, '''Component '''component) {
        super(adapterFactory, component);  
+
      super(adapterFactory, component);  
  }  
+
  }  
  
 
*''getText''(Object object) - for specifying the transient node name:<br>
 
*''getText''(Object object) - for specifying the transient node name:<br>
  
    @Override
+
  @Override
  public String getText(Object object) {
+
  public String getText(Object object) {
        return "Parameter Values"; //$NON-NLS-1$
+
      return "Parameter Values"; //$NON-NLS-1$
  }
+
  }
  
 
*''getChildrenFeatures''(Object object) – for adding children feature (e.g., parameter values from a component) to display as children of the transient node in tree view:
 
*''getChildrenFeatures''(Object object) – for adding children feature (e.g., parameter values from a component) to display as children of the transient node in tree view:
  
    @Override
+
  @Override
  public Collection&lt;? extends EStructuralFeature&gt; getChildrenFeatures(Object object) {
+
  public Collection&lt;? extends EStructuralFeature&gt; getChildrenFeatures(Object object) {
      if (childrenFeatures == null) {
+
      if (childrenFeatures == null) {
          super.getChildrenFeatures(object);
+
        super.getChildrenFeatures(object);
          childrenFeatures.'''add'''(InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES);
+
        childrenFeatures.'''add'''(InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES);
      }
+
      }
      return childrenFeatures;
+
      return childrenFeatures;
  }
+
  }
  
 
*''collectNewChildDescriptors''(Collection&lt;Object&gt; newChildDescriptors, Object object) – for adding New child actions to the transient node and allowing however creating children (e.g., parameter values) from this transient node.
 
*''collectNewChildDescriptors''(Collection&lt;Object&gt; newChildDescriptors, Object object) – for adding New child actions to the transient node and allowing however creating children (e.g., parameter values) from this transient node.
  
    @Override
+
  @Override
  protected void collectNewChildDescriptors(Collection&lt;Object&gt; newChildDescriptors, Object object) {
+
  protected void collectNewChildDescriptors(Collection&lt;Object&gt; newChildDescriptors, Object object) {
        super.collectNewChildDescriptors(newChildDescriptors, object);
+
      super.collectNewChildDescriptors(newChildDescriptors, object);
 
+
 
        newChildDescriptors.'''add'''(createChildParameter(InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES,   
+
      newChildDescriptors.'''add'''(createChildParameter(InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES,   
                                InstanceModel20Factory.eINSTANCE.createParameterValue()));
+
                              InstanceModel20Factory.eINSTANCE.createParameterValue()));
  }
+
  }
  
 
*''createDragAndDropCommand''(EditingDomain domain, Object owner, float location, int operations, int operation, Collection&lt;?&gt; collection) – for enabling dropping only possible children into the transient node.
 
*''createDragAndDropCommand''(EditingDomain domain, Object owner, float location, int operations, int operation, Collection&lt;?&gt; collection) – for enabling dropping only possible children into the transient node.
  
  @Override
+
  @Override
  protected Command createDragAndDropCommand(EditingDomain domain, Object owner, float location, int operations, int operation,  Collection&lt;?&gt; collection) {
+
protected Command createDragAndDropCommand(EditingDomain domain, Object owner, float location, int operations, int operation,  Collection&lt;?&gt; collection) {
      if (new '''AddCommand'''(domain, (EObject) owner, InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES, collection).'''canExecute'''()) {
+
    if (new '''AddCommand'''(domain, (EObject) owner, InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES, collection).'''canExecute'''()) {
          return super.createDragAndDropCommand(domain, owner, location, operations, operation, collection);
+
        return super.createDragAndDropCommand(domain, owner, location, operations, operation, collection);
      }
+
    }
      return UnexecutableCommand.INSTANCE;
+
    return UnexecutableCommand.INSTANCE;
  }
+
}
  
 
*''getResourceLocator()'' – for returning the resource locator for item provider's resources using specific plug-in Activator (e.g., org.eclipse.sphinx.examples.hummingbird20.edit.Activator).
 
*''getResourceLocator()'' – for returning the resource locator for item provider's resources using specific plug-in Activator (e.g., org.eclipse.sphinx.examples.hummingbird20.edit.Activator).
  
  @Override
+
  @Override
  public ResourceLocator getResourceLocator() {
+
public ResourceLocator getResourceLocator() {
      return Activator.INSTANCE;
+
    return Activator.INSTANCE;
  }
+
}
  
 
'''Create (or modify if exist) an extended item provider''' (e.g., ExtendedComponentItemProvider) that extends an existing item provider (e.g., ''ComponentItemProvider'') and overrides following methods:  
 
'''Create (or modify if exist) an extended item provider''' (e.g., ExtendedComponentItemProvider) that extends an existing item provider (e.g., ''ComponentItemProvider'') and overrides following methods:  
Line 127: Line 127:
 
*The first thing consists of adding required fields corresponding to transient nodes which are displaying as children of ''Component ''elements.
 
*The first thing consists of adding required fields corresponding to transient nodes which are displaying as children of ''Component ''elements.
  
    protected ParameterValuesItemProvider parameterValuesItemProvider;
+
  protected ParameterValuesItemProvider parameterValuesItemProvider;
  protected OutgoingConnectionsItemProvider outgoingConnectionsItemProvider;
+
  protected OutgoingConnectionsItemProvider outgoingConnectionsItemProvider;
  
 
*''getChildrenFeatures(Object object)'' – for no longer displaying directly ''ParameterValue ''and outgoing ''Connection ''objects as children of component objects into the view:
 
*''getChildrenFeatures(Object object)'' – for no longer displaying directly ''ParameterValue ''and outgoing ''Connection ''objects as children of component objects into the view:
  
    @Override
+
  @Override
  public Collection&lt;? extends EStructuralFeature&gt; getChildrenFeatures(Object object) {
+
  public Collection&lt;? extends EStructuralFeature&gt; getChildrenFeatures(Object object) {
      super.getChildrenFeatures(object);
+
      super.getChildrenFeatures(object);
      childrenFeatures.'''remove'''(InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES);
+
      childrenFeatures.'''remove'''(InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES);
      childrenFeatures.'''remove'''(InstanceModel20Package.Literals.COMPONENT__OUTGOING_CONNECTIONS);
+
      childrenFeatures.'''remove'''(InstanceModel20Package.Literals.COMPONENT__OUTGOING_CONNECTIONS);
      return childrenFeatures;
+
      return childrenFeatures;
  }
+
  }
  
 
*''getChildren(Object object)'' – for adding transient nodes (e.g., “'''Parameter Values'''” or “'''Outgoing Connections'''”) as children of component objects:
 
*''getChildren(Object object)'' – for adding transient nodes (e.g., “'''Parameter Values'''” or “'''Outgoing Connections'''”) as children of component objects:
  
    @Override
+
  @Override
  public Collection&lt;?&gt; getChildren(Object object) {
+
  public Collection&lt;?&gt; getChildren(Object object) {
      if (parameterValuesItemProvider == null) {
+
      if (parameterValuesItemProvider == null) {
          parameterValuesItemProvider = new ParameterValuesItemProvider(adapterFactory, (Component) object);
+
          parameterValuesItemProvider = new ParameterValuesItemProvider(adapterFactory, (Component) object);
      }
+
      }
      if (outgoingConnectionsItemProvider == null) {
+
      if (outgoingConnectionsItemProvider == null) {
            outgoingConnectionsItemProvider = new OutgoingConnectionsItemProvider(adapterFactory, (Component) object);
+
          outgoingConnectionsItemProvider = new OutgoingConnectionsItemProvider(adapterFactory, (Component) object);
      }
+
      }
 
+
 
      List&lt;Object&gt; children = new ArrayList&lt;Object&gt;(super.getChildren(object));
+
      List&lt;Object&gt; children = new ArrayList&lt;Object&gt;(super.getChildren(object));
      children.'''add'''(parameterValuesItemProvider);
+
      children.'''add'''(parameterValuesItemProvider);
      children.'''add'''(outgoingConnectionsItemProvider);
+
      children.'''add'''(outgoingConnectionsItemProvider);
 
+
 
      return children;
+
      return children;
  }
+
}
  
 
*''createAddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection&lt;?&gt; collection, int index)'' and ''createRemoveCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection&lt;?&gt; collection)'' for setting the selection (i.e., the affected object) when adding or removing elements containing in transient nodes. The selection must be in this case the appropriate transient node.
 
*''createAddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection&lt;?&gt; collection, int index)'' and ''createRemoveCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection&lt;?&gt; collection)'' for setting the selection (i.e., the affected object) when adding or removing elements containing in transient nodes. The selection must be in this case the appropriate transient node.
  
    @Override
+
  @Override
  protected Command createAddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection&lt;?&gt; collection, int index) {
+
  protected Command createAddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection&lt;?&gt; collection, int index) {
      return createWrappedCommand(super.createAddCommand(domain, owner, feature, collection, index), owner, feature);
+
      return createWrappedCommand(super.createAddCommand(domain, owner, feature, collection, index), owner, feature);
  }
+
  }
  
    @Override
+
  @Override
  protected Command createRemoveCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection&lt;?&gt; collection) {
+
  protected Command createRemoveCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection&lt;?&gt; collection) {
      return createWrappedCommand(super.createRemoveCommand(domain, owner, feature, collection), owner, feature);
+
      return createWrappedCommand(super.createRemoveCommand(domain, owner, feature, collection), owner, feature);
  }
+
  }
  
 
*Adds the ''createWrappedCommand(Command command, final EObject owner, final EStructuralFeature feature)'' method returning the appropriate transient node (i.e., the item provider) and the “real” affected object is its owner (e.g., the component object).
 
*Adds the ''createWrappedCommand(Command command, final EObject owner, final EStructuralFeature feature)'' method returning the appropriate transient node (i.e., the item provider) and the “real” affected object is its owner (e.g., the component object).
  
 
     protected Command createWrappedCommand(Command command, final EObject owner, final EStructuralFeature feature) {
 
     protected Command createWrappedCommand(Command command, final EObject owner, final EStructuralFeature feature) {
        if (feature == InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES ||
+
      if (feature == InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES ||
            feature == InstanceModel20Package.Literals.COMPONENT__OUTGOING_CONNECTIONS) {
+
          feature == InstanceModel20Package.Literals.COMPONENT__OUTGOING_CONNECTIONS) {
          return '''new CommandWrapper'''(command) {
+
          return '''new CommandWrapper'''(command) {
              @Override
+
              @Override
              public Collection&lt;?&gt; '''getAffectedObjects()''' {
+
              public Collection&lt;?&gt; '''getAffectedObjects()''' {
                  Collection&lt;?&gt; affected = super.getAffectedObjects();
+
                  Collection&lt;?&gt; affected = super.getAffectedObjects();
                  if (affected.contains(owner)) {
+
                  if (affected.contains(owner)) {
                      affected = Collections.singleton(feature == InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES&nbsp;?  
+
                      affected = Collections.singleton(feature == InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES&nbsp;?  
 
            getParameterValues()&nbsp;: getOutgoingConnections());
 
            getParameterValues()&nbsp;: getOutgoingConnections());
                  }
+
                  }
                  return affected;
+
                  return affected;
              }};
+
              }};
      }
+
      }
      return command;
+
      return command;
  }
+
  }
  
 
*''dispose()'' – for disposing any remaining children wrappers (including transient nodes) in the children store.
 
*''dispose()'' – for disposing any remaining children wrappers (including transient nodes) in the children store.
  
    @Override
+
  @Override
  public void dispose() {
+
  public void dispose() {
      super.dispose();
+
      super.dispose();
      if (parameterValuesItemProvider&nbsp;!= null &amp;&amp; outgoingConnectionsItemProvider&nbsp;!= null) {
+
      if (parameterValuesItemProvider&nbsp;!= null &amp;&amp; outgoingConnectionsItemProvider&nbsp;!= null) {
          ((IDisposable) parameterValuesItemProvider).dispose();
+
        ((IDisposable) parameterValuesItemProvider).dispose();
          ((IDisposable) outgoingConnectionsItemProvider).dispose();
+
        ((IDisposable) outgoingConnectionsItemProvider).dispose();
      }
+
      }
  }
+
  }
  
 
*Adds getters methods returning transient node fields (i.e., transient item providers)
 
*Adds getters methods returning transient node fields (i.e., transient item providers)
  
  public Object getParameterValues() {
+
  public Object getParameterValues() {
      return parameterValuesItemProvider;
+
    return parameterValuesItemProvider;
  }
+
}
 
+
  public Object getOutgoingConnections() {
+
public Object getOutgoingConnections() {
      return outgoingConnectionsItemProvider;
+
    return outgoingConnectionsItemProvider;
  }
+
}
  
 
'''Create (or modify if exist) an extended item provider for each child of the transient node'''(e.g., ''ExtendedParameterValueItemProvider ''or ''ExtendedConnectionItemProvider ''classes) that extends existing item providers (e.g., respectively ''ParameterValueItemProvider ''or ''ConnectionItemProvider''):  
 
'''Create (or modify if exist) an extended item provider for each child of the transient node'''(e.g., ''ExtendedParameterValueItemProvider ''or ''ExtendedConnectionItemProvider ''classes) that extends existing item providers (e.g., respectively ''ParameterValueItemProvider ''or ''ConnectionItemProvider''):  
Line 214: Line 214:
 
*Creates a constructor that calls its super:
 
*Creates a constructor that calls its super:
  
    public ExtendedParameterValueItemProvider(AdapterFactory adapterFactory) {
+
  public ExtendedParameterValueItemProvider(AdapterFactory adapterFactory) {
      super(adapterFactory);
+
      super(adapterFactory);
  }
+
  }
  
 
*Overrides the ''getParent(Object object)'' method to return the appropriate transient node:
 
*Overrides the ''getParent(Object object)'' method to return the appropriate transient node:
  
    @Override
+
  @Override
  public Object getParent(Object object) {
+
  public Object getParent(Object object) {
      Object component = super.getParent(object);
+
      Object component = super.getParent(object);
      ExtendedComponentItemProvider componentItemProvider = (ExtendedComponentItemProvider) adapterFactory.adapt(component, IEditingDomainItemProvider.class);
+
      ExtendedComponentItemProvider componentItemProvider = (ExtendedComponentItemProvider) adapterFactory.adapt(component, IEditingDomainItemProvider.class);
      return componentItemProvider&nbsp;!= null&nbsp;? componentItemProvider.getParameterValues()&nbsp;: null;
+
      return componentItemProvider&nbsp;!= null&nbsp;? componentItemProvider.getParameterValues()&nbsp;: null;
  }
+
  }
  
 
'''Create (or modify if exist) an extended item provider adapter factory''' (e.g., ''ExtendedInstanceModel20ItemProviderAdapterFactory'' class) that extends an existing adapter factory (e.g., ''InstanceModel20ItemProviderAdapterFactory'') and overrides methods allowing creating item providers to be use (for example objects of Component, ParameterValue or Connection types) and returning the appropriate extended item providers.  
 
'''Create (or modify if exist) an extended item provider adapter factory''' (e.g., ''ExtendedInstanceModel20ItemProviderAdapterFactory'' class) that extends an existing adapter factory (e.g., ''InstanceModel20ItemProviderAdapterFactory'') and overrides methods allowing creating item providers to be use (for example objects of Component, ParameterValue or Connection types) and returning the appropriate extended item providers.  
  
    @Override
+
  @Override
  public Adapter createComponentAdapter() {
+
  public Adapter createComponentAdapter() {
      return new ExtendedComponentItemProvider(this);
+
      return new ExtendedComponentItemProvider(this);
  }
+
  }
 
   
 
   
  @Override
+
  @Override
  public Adapter createParameterValueAdapter() {
+
  public Adapter createParameterValueAdapter() {
      return new ExtendedParameterValueItemProvider(this);
+
      return new ExtendedParameterValueItemProvider(this);
  }
+
  }
 
+
 
  @Override
+
  @Override
  public Adapter createConnectionAdapter() {
+
  public Adapter createConnectionAdapter() {
      return new ExtendedConnectionItemProvider(this);
+
      return new ExtendedConnectionItemProvider(this);
  }
+
  }
  
 
'''Add the new created extended item provider adapter factory to those to be use''' (i.e., adds ''ExtendedInstanceModel20ItemProviderAdapterFactory'' to ''Hummingbird20ItemProviderAdapterFactory'' adapter factories to be use. Hummingbird20ItemProviderAdapterFactory must be used to give custom adapter factory in content and label provider classes.  
 
'''Add the new created extended item provider adapter factory to those to be use''' (i.e., adds ''ExtendedInstanceModel20ItemProviderAdapterFactory'' to ''Hummingbird20ItemProviderAdapterFactory'' adapter factories to be use. Hummingbird20ItemProviderAdapterFactory must be used to give custom adapter factory in content and label provider classes.  
  
    public class Hummingbird20ItemProviderAdapterFactory extends ComposedAdapterFactory {
+
  public class Hummingbird20ItemProviderAdapterFactory extends ComposedAdapterFactory {
        public '''Hummingbird20ItemProviderAdapterFactory'''() {
+
      public '''Hummingbird20ItemProviderAdapterFactory'''() {
            super(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
+
          super(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
            addAdapterFactory(new '''ExtendedInstanceModel20ItemProviderAdapterFactory'''());
+
          addAdapterFactory(new '''ExtendedInstanceModel20ItemProviderAdapterFactory'''());
        }
+
      }
  }
+
  }
  
 
==== Suppressing Model Objects in views  ====
 
==== Suppressing Model Objects in views  ====
Line 263: Line 263:
 
To suppress the Description element (and its children) as child of an element of ''Connection ''type from the view, we may override the ''getChildrenFeatures(Object object) ''method of the corresponding item provider class (i.e., ExtendedConnectionItemProvider) like this:  
 
To suppress the Description element (and its children) as child of an element of ''Connection ''type from the view, we may override the ''getChildrenFeatures(Object object) ''method of the corresponding item provider class (i.e., ExtendedConnectionItemProvider) like this:  
  
  @Override
+
  @Override
  public Collection&lt;? extends EStructuralFeature&gt; getChildrenFeatures(Object object) {
+
public Collection&lt;? extends EStructuralFeature&gt; getChildrenFeatures(Object object) {
      super.getChildrenFeatures(object);
+
    super.getChildrenFeatures(object);
      childrenFeatures.'''remove'''(Common20Package.Literals.IDENTIFIABLE__DESCRIPTION);
+
    childrenFeatures.'''remove'''(Common20Package.Literals.IDENTIFIABLE__DESCRIPTION);
      return childrenFeatures;
+
    return childrenFeatures;
  }
+
}
  
 
A similar change can be done in the collectNewChildDescriptors(Collection&lt;Object&gt; newChildDescriptors, Object object) method for suppressing New child menu action allowing creating a Description element.  
 
A similar change can be done in the collectNewChildDescriptors(Collection&lt;Object&gt; newChildDescriptors, Object object) method for suppressing New child menu action allowing creating a Description element.  
Line 280: Line 280:
 
  @Override
 
  @Override
 
  public List&lt;IItemPropertyDescriptor&gt; getPropertyDescriptors(Object object) {
 
  public List&lt;IItemPropertyDescriptor&gt; getPropertyDescriptors(Object object) {
    if (itemPropertyDescriptors == null) {
+
  if (itemPropertyDescriptors == null) {
        super.getPropertyDescriptors(object);
+
      super.getPropertyDescriptors(object);
 
+
        Port sourcePort = ((Connection) object).getSourcePort();
+
      Port sourcePort = ((Connection) object).getSourcePort();
        if (sourcePort&nbsp;!= null) {
+
      if (sourcePort&nbsp;!= null) {
            addRequiredInterface(sourcePort.getRequiredInterface(), getString("_UI_Port_requiredInterface_feature"));  
+
          addRequiredInterface(sourcePort.getRequiredInterface(), getString("_UI_Port_requiredInterface_feature"));  
        }
+
      }
    }
+
  }
    return itemPropertyDescriptors;
+
  return itemPropertyDescriptors;
 
  }
 
  }
  
  private void addRequiredInterface(Interface requiredInterface, final String featureName) {
+
  private void addRequiredInterface(Interface requiredInterface, final String featureName) {
    InterfaceItemProvider interfaceItemProvider = (InterfaceItemProvider) ((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory().adapt(requiredInterface, IItemPropertySource.class);
+
  InterfaceItemProvider interfaceItemProvider = (InterfaceItemProvider) ((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory().adapt(requiredInterface, IItemPropertySource.class);
    if (interfaceItemProvider&nbsp;!= null) {
+
  if (interfaceItemProvider&nbsp;!= null) {
        List&lt;IItemPropertyDescriptor&gt; descriptors = interfaceItemProvider.getPropertyDescriptors(requiredInterface);
+
      List&lt;IItemPropertyDescriptor&gt; descriptors = interfaceItemProvider.getPropertyDescriptors(requiredInterface);
        for (IItemPropertyDescriptor descriptor&nbsp;: descriptors) {
+
      for (IItemPropertyDescriptor descriptor&nbsp;: descriptors) {
            itemPropertyDescriptors.add(new ItemPropertyDescriptorDecorator(requiredInterface, descriptor) {
+
          itemPropertyDescriptors.add(new ItemPropertyDescriptorDecorator(requiredInterface, descriptor) {
                @Override
+
              @Override
                public String getCategory(Object thisObject) {
+
              public String getCategory(Object thisObject) {
                    return featureName;
+
                    return featureName;
                }
+
              }
 
   
 
   
                @Override
+
              @Override
                public String getId(Object thisObject) {
+
              public String getId(Object thisObject) {
                    return featureName + getDisplayName(thisObject);
+
                  return featureName + getDisplayName(thisObject);
                }
+
              }
            });
+
          });
        }
+
      }
    }
+
  }
 
  }
 
  }
  
 
In the addRequiredInterface() method that adds the required interface as property of element of Connection type, we delegate to the appropriate item provider (e.g. InterfaceItemProvider) for getting the required property descriptors. We override however, in our example, the getCategory() method of the created ItemPropertyDescriptorDecorator for grouping the children properties in a specific category. For having unique identifier for each children property, we override also the getId() method of the created ItemPropertyDescriptorDecorator. In the advanced property section, we must use the “right” adapter factory (e.g. Hummingbird20ItemProviderAdapterFactory). We must however create (or use) an extended class of '''org.eclipse.sphinx.emf.ui.properties.BasicTransactionalAdvancedPropertySection '''and use it in the specified Eclipse property section contribution. The ExtendedTransactionalAdvancedPropertySection class is defined as follows:  
 
In the addRequiredInterface() method that adds the required interface as property of element of Connection type, we delegate to the appropriate item provider (e.g. InterfaceItemProvider) for getting the required property descriptors. We override however, in our example, the getCategory() method of the created ItemPropertyDescriptorDecorator for grouping the children properties in a specific category. For having unique identifier for each children property, we override also the getId() method of the created ItemPropertyDescriptorDecorator. In the advanced property section, we must use the “right” adapter factory (e.g. Hummingbird20ItemProviderAdapterFactory). We must however create (or use) an extended class of '''org.eclipse.sphinx.emf.ui.properties.BasicTransactionalAdvancedPropertySection '''and use it in the specified Eclipse property section contribution. The ExtendedTransactionalAdvancedPropertySection class is defined as follows:  
  
    public class ExtendedTransactionalAdvancedPropertySection extends BasicTransactionalAdvancedPropertySection {
+
  public class ExtendedTransactionalAdvancedPropertySection extends BasicTransactionalAdvancedPropertySection {
    protected AdapterFactory adapterFactory;
+
    protected AdapterFactory adapterFactory;
    @Override
+
    @Override
    protected AdapterFactory getCustomAdapterFactory() {
+
    protected AdapterFactory getCustomAdapterFactory() {
        if (adapterFactory == null) {
+
        if (adapterFactory == null) {
            adapterFactory = new Hummingbird20ItemProviderAdapterFactory();
+
            adapterFactory = new Hummingbird20ItemProviderAdapterFactory();
        }
+
        }
        return adapterFactory;
+
        return adapterFactory;
    }
+
    }
 
  }
 
  }
  

Revision as of 06:12, 22 July 2011

Model Editing Enhancements

Customizing Tree Views

The EMF Edit framework and its code generator allow creating easily editors and tree views from EMF models. However, the structure that we want displaying into tree view may be often different to the EMF model structure. The EMF Edit framework provides also mechanisms allowing customizing editors and tree views.

In the following sections, we’ll describe how to easily customize tree views in order to display customized tree view structure. There are several kings of tree view customization. For instance, we may want displaying object references as children, adding non-model elements or suppressing model objects in tree views. We’ll use an extract of the Hummingbird meta-model to illustrate these kinds of tree views customization, as shown in Figure 1:

An extract of the Hummingbird meta-model 2.0

    Figure 1: An extract of the Hummingbird meta-model 2.0.

Displaying References as Children

This section describes how displaying references as children into tree views. For example, we’ll display source port and target component (of a Connection object) as children of objects of Connection type. Realizing this kind of view customization is quite simple. The following figure illustrates this kind of tree view customization:

An example displaying references as children
   Figure 2: An example displaying references as children.

We must initially override the getChildren (Object object) method of the appropriate item provider (e.g., ConnectionItemProvider. We decide creating an extended class of ConnectionItemProvider named ExtendedConnectionItemProvider) like this:

 @Override
public Collection<? extends EStructuralFeature> getChildrenFeatures(Object object) {
    super.getChildrenFeatures(object);
    childrenFeatures.add(InstanceModel20Package.Literals.CONNECTION__SOURCE_PORT);
    childrenFeatures.add(InstanceModel20Package.Literals.CONNECTION__TARGET_COMPONENT);
    return childrenFeatures;
}

We must also override the notifyChanged(Notification notification) method of the ExtendedConnectionItemProvider class for refreshing the view if referenced objects (e.g., source port or target component) are set or modified toward the property sheet.

 @Override
public void notifyChanged(Notification notification) {
     updateChildren(notification);

     // For refreshing the view if source port and/or target component references are updated
     switch (notification.getFeatureID(Connection.class)) {
            case InstanceModel20Package.CONNECTION__SOURCE_PORT:
                 fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
            case InstanceModel20Package.CONNECTION__TARGET_COMPONENT:
                 fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
                 return;
    }
    super.notifyChanged(notification);
}

If referenced objects are displayed as children under a transient node (i.e., a non-model object, please see section 1.1.2 for more details) then we must override in this case the notifyChanged(Notification notification) method of this transient node for refreshing node content like this:

 @Override
public void notifyChanged(Notification notification) {
     updateTransientItemProviderChildren(notification, this);

     switch (notification.getFeatureID(Connection.class)) {
          case InstanceModel20Package.CONNECTION__SOURCE_PORT:
               fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
          case InstanceModel20Package.CONNECTION__TARGET_COMPONENT:
               fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), true, false));
               return;
     }
     super.notifyChanged(notification);
}

Adding Transient Nodes

This section describes how to easily add transient nodes (i.e., non-model objects) between an object and its children into the tree view. We will for example introduce into the view, as shown in Figure 3, “Parameter Values” and “Outgoing Connections” transient nodes in order to separate children (of ParameterValue and Connection types) of Component model objects.

SphinxExampleAddingTransientNode.jpg

Figure 3: An example adding transient nodes.

The following are the main steps for adding non-model intermediary objects into views:

Create a transient node item provider class (e.g. ParameterValuesItemProvider) that extends org.eclipse.sphinx.emf.edit.TransientItemProvider class and overrides following methods:

  • The first thing consists of defining a constructor that calls its super for giving the parent class (e.g., Component in our example) of the element like this:
   public ParameterValuesItemProvider(AdapterFactory adapterFactory, Component component) {
      super(adapterFactory, component); 
 } 
  • getText(Object object) - for specifying the transient node name:
  @Override
 public String getText(Object object) {
      return "Parameter Values"; //$NON-NLS-1$
 }
  • getChildrenFeatures(Object object) – for adding children feature (e.g., parameter values from a component) to display as children of the transient node in tree view:
  @Override
 public Collection<? extends EStructuralFeature> getChildrenFeatures(Object object) {
     if (childrenFeatures == null) {
        super.getChildrenFeatures(object);
        childrenFeatures.add(InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES);
     }
     return childrenFeatures;
 }
  • collectNewChildDescriptors(Collection<Object> newChildDescriptors, Object object) – for adding New child actions to the transient node and allowing however creating children (e.g., parameter values) from this transient node.
  @Override
 protected void collectNewChildDescriptors(Collection<Object> newChildDescriptors, Object object) {
      super.collectNewChildDescriptors(newChildDescriptors, object);
 
      newChildDescriptors.add(createChildParameter(InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES,   
                              InstanceModel20Factory.eINSTANCE.createParameterValue()));
 }
  • createDragAndDropCommand(EditingDomain domain, Object owner, float location, int operations, int operation, Collection<?> collection) – for enabling dropping only possible children into the transient node.
 @Override
protected Command createDragAndDropCommand(EditingDomain domain, Object owner, float location, int operations, int operation,  Collection<?> collection) {
    if (new AddCommand(domain, (EObject) owner, InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES, collection).canExecute()) {
        return super.createDragAndDropCommand(domain, owner, location, operations, operation, collection);
    }
    return UnexecutableCommand.INSTANCE;
}
  • getResourceLocator() – for returning the resource locator for item provider's resources using specific plug-in Activator (e.g., org.eclipse.sphinx.examples.hummingbird20.edit.Activator).
 @Override
public ResourceLocator getResourceLocator() {
    return Activator.INSTANCE;
}

Create (or modify if exist) an extended item provider (e.g., ExtendedComponentItemProvider) that extends an existing item provider (e.g., ComponentItemProvider) and overrides following methods:

  • The first thing consists of adding required fields corresponding to transient nodes which are displaying as children of Component elements.
  protected ParameterValuesItemProvider parameterValuesItemProvider;
 protected OutgoingConnectionsItemProvider outgoingConnectionsItemProvider;
  • getChildrenFeatures(Object object) – for no longer displaying directly ParameterValue and outgoing Connection objects as children of component objects into the view:
  @Override
 public Collection<? extends EStructuralFeature> getChildrenFeatures(Object object) {
     super.getChildrenFeatures(object);
     childrenFeatures.remove(InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES);
     childrenFeatures.remove(InstanceModel20Package.Literals.COMPONENT__OUTGOING_CONNECTIONS);
     return childrenFeatures;
 }
  • getChildren(Object object) – for adding transient nodes (e.g., “Parameter Values” or “Outgoing Connections”) as children of component objects:
  @Override
 public Collection<?> getChildren(Object object) {
     if (parameterValuesItemProvider == null) {
         parameterValuesItemProvider = new ParameterValuesItemProvider(adapterFactory, (Component) object);
     }
     if (outgoingConnectionsItemProvider == null) {
          outgoingConnectionsItemProvider = new OutgoingConnectionsItemProvider(adapterFactory, (Component) object);
     }
 
     List<Object> children = new ArrayList<Object>(super.getChildren(object));
     children.add(parameterValuesItemProvider);
     children.add(outgoingConnectionsItemProvider);
 
     return children;
}
  • createAddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection<?> collection, int index) and createRemoveCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection<?> collection) for setting the selection (i.e., the affected object) when adding or removing elements containing in transient nodes. The selection must be in this case the appropriate transient node.
  @Override
 protected Command createAddCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection<?> collection, int index) {
     return createWrappedCommand(super.createAddCommand(domain, owner, feature, collection, index), owner, feature);
 }
  @Override
 protected Command createRemoveCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Collection<?> collection) {
     return createWrappedCommand(super.createRemoveCommand(domain, owner, feature, collection), owner, feature);
 }
  • Adds the createWrappedCommand(Command command, final EObject owner, final EStructuralFeature feature) method returning the appropriate transient node (i.e., the item provider) and the “real” affected object is its owner (e.g., the component object).
   protected Command createWrappedCommand(Command command, final EObject owner, final EStructuralFeature feature) {
      if (feature == InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES ||
          feature == InstanceModel20Package.Literals.COMPONENT__OUTGOING_CONNECTIONS) {
         return new CommandWrapper(command) {
             @Override
             public Collection<?> getAffectedObjects() {
                 Collection<?> affected = super.getAffectedObjects();
                 if (affected.contains(owner)) {
                     affected = Collections.singleton(feature == InstanceModel20Package.Literals.COMPONENT__PARAMETER_VALUES ? 
		           getParameterValues() : getOutgoingConnections());
                 }
                 return affected;
             }};
     }
     return command;
 }
  • dispose() – for disposing any remaining children wrappers (including transient nodes) in the children store.
  @Override
 public void dispose() {
     super.dispose();
     if (parameterValuesItemProvider != null && outgoingConnectionsItemProvider != null) {
        ((IDisposable) parameterValuesItemProvider).dispose();
        ((IDisposable) outgoingConnectionsItemProvider).dispose();
     }
 }
  • Adds getters methods returning transient node fields (i.e., transient item providers)
 public Object getParameterValues() {
    return parameterValuesItemProvider;
}

public Object getOutgoingConnections() {
    return outgoingConnectionsItemProvider;
}

Create (or modify if exist) an extended item provider for each child of the transient node(e.g., ExtendedParameterValueItemProvider or ExtendedConnectionItemProvider classes) that extends existing item providers (e.g., respectively ParameterValueItemProvider or ConnectionItemProvider):

  • Creates a constructor that calls its super:
  public ExtendedParameterValueItemProvider(AdapterFactory adapterFactory) {
     super(adapterFactory);
 }
  • Overrides the getParent(Object object) method to return the appropriate transient node:
  @Override
 public Object getParent(Object object) {
     Object component = super.getParent(object);
     ExtendedComponentItemProvider componentItemProvider = (ExtendedComponentItemProvider) adapterFactory.adapt(component, IEditingDomainItemProvider.class);
     return componentItemProvider != null ? componentItemProvider.getParameterValues() : null;
 }

Create (or modify if exist) an extended item provider adapter factory (e.g., ExtendedInstanceModel20ItemProviderAdapterFactory class) that extends an existing adapter factory (e.g., InstanceModel20ItemProviderAdapterFactory) and overrides methods allowing creating item providers to be use (for example objects of Component, ParameterValue or Connection types) and returning the appropriate extended item providers.

  @Override
 public Adapter createComponentAdapter() {
     return new ExtendedComponentItemProvider(this);
 }

 @Override
 public Adapter createParameterValueAdapter() {
     return new ExtendedParameterValueItemProvider(this);
 }
 
 @Override
 public Adapter createConnectionAdapter() {
     return new ExtendedConnectionItemProvider(this);
 }

Add the new created extended item provider adapter factory to those to be use (i.e., adds ExtendedInstanceModel20ItemProviderAdapterFactory to Hummingbird20ItemProviderAdapterFactory adapter factories to be use. Hummingbird20ItemProviderAdapterFactory must be used to give custom adapter factory in content and label provider classes.

  public class Hummingbird20ItemProviderAdapterFactory extends ComposedAdapterFactory {
      public Hummingbird20ItemProviderAdapterFactory() {
          super(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
          addAdapterFactory(new ExtendedInstanceModel20ItemProviderAdapterFactory());
      }
 }

Suppressing Model Objects in views

It may often happen to want suppressing certain model objects from tree views. For example, elements of Connection or ParameterValue type may have a child of Description type (which can also have a child of Text type). We decide in our example to not display elements of Description type in the tree view. The following figure illustrates the current tree view (in the left side) and the improved tree view (in the right side).

SphinxExampleSuppressingModelObject.jpg

Figure 4: An example suppressing Description model objects into the view.

To suppress the Description element (and its children) as child of an element of Connection type from the view, we may override the getChildrenFeatures(Object object) method of the corresponding item provider class (i.e., ExtendedConnectionItemProvider) like this:

 @Override
public Collection<? extends EStructuralFeature> getChildrenFeatures(Object object) {
    super.getChildrenFeatures(object);
    childrenFeatures.remove(Common20Package.Literals.IDENTIFIABLE__DESCRIPTION);
    return childrenFeatures;
}

A similar change can be done in the collectNewChildDescriptors(Collection<Object> newChildDescriptors, Object object) method for suppressing New child menu action allowing creating a Description element.

Customizing Object Property Sheet

In Section 1.1.3, we saw how suppressing model objects in tree views. The information from these suppressed elements may be displayed as properties of their parent or grandparent object. This allows for instance the user to set or modify values of these properties. The information of children that are not suppressed can also be displayed. For example, an element of connection type has a source port that requires one interface. We can display for example the required interface as property of the connection element.

For displaying children properties to their parent object, we can override the List<IItemPropertyDescriptor> getPropertyDescriptors(Object object) method of the ExtendedConnectionItemProvider class like this:

@Override
public List<IItemPropertyDescriptor> getPropertyDescriptors(Object object) {
  if (itemPropertyDescriptors == null) {
      super.getPropertyDescriptors(object);

      Port sourcePort = ((Connection) object).getSourcePort();
      if (sourcePort != null) {
          addRequiredInterface(sourcePort.getRequiredInterface(), getString("_UI_Port_requiredInterface_feature")); 
      }
  }
  return itemPropertyDescriptors;
}
 private void addRequiredInterface(Interface requiredInterface, final String featureName) {
  InterfaceItemProvider interfaceItemProvider = (InterfaceItemProvider) ((ComposeableAdapterFactory) adapterFactory).getRootAdapterFactory().adapt(requiredInterface, IItemPropertySource.class);
  if (interfaceItemProvider != null) {
      List<IItemPropertyDescriptor> descriptors = interfaceItemProvider.getPropertyDescriptors(requiredInterface);
      for (IItemPropertyDescriptor descriptor : descriptors) {
          itemPropertyDescriptors.add(new ItemPropertyDescriptorDecorator(requiredInterface, descriptor) {
              @Override
              public String getCategory(Object thisObject) {
                   return featureName;
              }

              @Override
              public String getId(Object thisObject) {
                  return featureName + getDisplayName(thisObject);
              }
          });
      }
  }
}

In the addRequiredInterface() method that adds the required interface as property of element of Connection type, we delegate to the appropriate item provider (e.g. InterfaceItemProvider) for getting the required property descriptors. We override however, in our example, the getCategory() method of the created ItemPropertyDescriptorDecorator for grouping the children properties in a specific category. For having unique identifier for each children property, we override also the getId() method of the created ItemPropertyDescriptorDecorator. In the advanced property section, we must use the “right” adapter factory (e.g. Hummingbird20ItemProviderAdapterFactory). We must however create (or use) an extended class of org.eclipse.sphinx.emf.ui.properties.BasicTransactionalAdvancedPropertySection and use it in the specified Eclipse property section contribution. The ExtendedTransactionalAdvancedPropertySection class is defined as follows:

  public class ExtendedTransactionalAdvancedPropertySection extends BasicTransactionalAdvancedPropertySection {
   protected AdapterFactory adapterFactory;
   @Override
   protected AdapterFactory getCustomAdapterFactory() {
       if (adapterFactory == null) {
           adapterFactory = new Hummingbird20ItemProviderAdapterFactory();
       }
       return adapterFactory;
   }
}

The Figure 5 illustrates the property sheet customization result:

SphinxExampleCustomizePropertySheet.JPG

Figure 5: An example of customizing property sheet.

Back to the top