/**
     * {@inheritedDoc}.
     *
     * @param e the e
     */
    @Override
    public void widgetSelected(SelectionEvent e) {
      try {
        if (treeViewer.getSelection().isEmpty()) {
          return;
        }

        IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();

        EObject elt = EMFHelper.getEObject(selection.getFirstElement());
        if (elt == null) {
          return;
        }

        setContainer(elt);
        ModelSet modelSet = ServiceUtilsForEObject.getInstance().getModelSet(elt);

        ICreationCommand creationCommand =
            iCreationCommandRegistry.getCommand(commandDescriptor.getCommandId());
        creationCommand.createDiagram(modelSet, container, null);

        // refresh several filtered tree
        getDiagramfilteredTree().getViewer().refresh();
        getModeFilteredTree().getViewer().refresh();
      } catch (NotFoundException ex) {
        Activator.log.error(ex);
      } catch (ServiceException ex) {
        Activator.log.error(ex);
      }
    }
  /**
   * Instantiates a new editor look for diagram.
   *
   * @param editorFactoryRegistry the editor factory registry
   * @param amodel the amodel
   */
  public EditorLookForEditorShell(IPageIconsRegistry editorFactoryRegistry, EObject amodel) {
    super();
    this.model = amodel;
    // create the shell
    createLookforShell();

    // intall tree with uml element
    treeViewer = getModeFilteredTree().getViewer();
    treeViewer.setUseHashlookup(true);
    //		treeViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory) {
    //
    //			@Override
    //			public Image getImage(Object object) {
    //				//TODO
    ////				if(object instanceof PapyrusTableInstance) {
    ////					return editorRegistry.getEditorIcon((object));
    ////				} else {
    ////					return super.getImage(object);
    ////				}
    //				Image im = super.getImage(object);
    //				return im;
    ////				return null;
    //			}
    //		});

    ILabelProvider labelProvider;
    ServicesRegistry registry = null;

    try {
      registry = ServiceUtilsForEObject.getInstance().getServiceRegistry(model);
      labelProvider = registry.getService(LabelProviderService.class).getLabelProvider();
    } catch (ServiceException ex) {
      Activator.log.error(ex);
      labelProvider = new LabelProvider();
    }

    treeViewer.setLabelProvider(labelProvider);
    // treeViewer.setContentProvider(new
    // CustomAdapterFactoryContentProvider(adapterFactory));
    // treeViewer.setContentProvider(new
    // SemanticEMFContentProvider(amodel)); //This content provider will
    // only display the selected element, instead of the root element
    // FIXME:  Use a standard, non-deprecated content
    treeViewer.setContentProvider(
        new SemanticEMFContentProvider(
            null, null, new EObject[] {EcoreUtil.getRootContainer(amodel)}) {

          @Override
          public boolean hasChildren(Object element) {
            return super.getChildren(element).length > 0;
          }

          /**
           * @see
           *     org.eclipse.papyrus.infra.emf.providers.MoDiscoContentProvider#getChildren(java.lang.Object)
           * @param parentElement
           * @return
           */
          // in some case we return diagram twice!
          // TODO the best correction we be able to manage applied facet, because if we get diagram
          // twice it is probably because there are 2 facets with the same behavior applied
          @Override
          public Object[] getChildren(Object parentElement) {
            Set<Object> alreadyVisited = new HashSet<Object>();
            List<Object> returnedChildren = new ArrayList<Object>();
            Object[] children = super.getChildren(parentElement);
            for (Object current : children) {
              EObject el = EMFHelper.getEObject(current);
              if (el != null) {
                if (!alreadyVisited.contains(el)) {
                  returnedChildren.add(current);
                  alreadyVisited.add(el);
                }
              }
            }
            return returnedChildren.toArray();
          }
        });
    // treeViewer.setInput(model.eResource());
    treeViewer.setInput(registry);

    // install diagramlist
    diagramListTreeViewer = getDiagramfilteredTree().getViewer();
    diagramListTreeViewer.setUseHashlookup(true);

    // fill list of diagram
    // TODO
    // diagramListTreeViewer.setLabelProvider(new ObjectLabelProvider(null));

    // we can't reuse the same instance of the label provider see bug 385599: [Hyperlink] We can't
    // select the diagram/table for referencing them
    diagramListTreeViewer.setLabelProvider(labelProvider);

    diagramListTreeViewer.setContentProvider(new EditorListContentProvider(model));
    diagramListTreeViewer.setInput(""); // $NON-NLS-1$

    // add listner on the new button to display menu for each diagram
    diagramMenuButton = new Menu(getNewDiagrambutton());
    getNewDiagrambutton().setMenu(diagramMenuButton);
    CreationCommandRegistry commandRegistry =
        CreationCommandRegistry.getInstance(org.eclipse.papyrus.infra.core.Activator.PLUGIN_ID);
    for (CreationCommandDescriptor desc : commandRegistry.getCommandDescriptors()) {
      MenuItem menuItem = new MenuItem(diagramMenuButton, SWT.PUSH);
      menuItem.addSelectionListener(new DiagramCreateListener(desc, null, commandRegistry));
      menuItem.setText(desc.getLabel());
    }
    getNewDiagrambutton()
        .addSelectionListener(
            new org.eclipse.swt.events.SelectionAdapter() {

              @Override
              public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
                diagramMenuButton.setVisible(true);
              }
            });

    // add listener to remove diagram
    getRemoveDiagrambutton()
        .addSelectionListener(
            new org.eclipse.swt.events.SelectionAdapter() {

              @Override
              public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
                IStructuredSelection iSelection =
                    (IStructuredSelection) getModeFilteredTree().getViewer().getSelection();
                Iterator<?> iterator = iSelection.iterator();

                final IPageManager pageManager;
                try {
                  pageManager = ServiceUtilsForEObject.getInstance().getIPageManager(model);
                } catch (ServiceException ex) {
                  Activator.log.error(ex);
                  return;
                }

                final List<Object> pagesToDelete = new LinkedList<Object>();
                while (iterator.hasNext()) {
                  EObject selectedElement = EMFHelper.getEObject(iterator.next());
                  if (pageManager.allPages().contains(selectedElement)) {
                    pagesToDelete.add(selectedElement);
                  }
                }

                if (pagesToDelete.isEmpty()) {
                  return;
                }

                for (Object page : pagesToDelete) {
                  pageManager.closeAllOpenedPages(page);
                }

                // getDiagramfilteredTree().getViewer().setInput(""); //$NON-NLS-1$
                getModeFilteredTree().getViewer().refresh();
              }
            });

    // add listener to keep the selected diagram in the list for the model
    // view
    getModeFilteredTree()
        .getViewer()
        .addSelectionChangedListener(
            new ISelectionChangedListener() {

              public void selectionChanged(SelectionChangedEvent event) {
                Object selection =
                    ((IStructuredSelection) getModeFilteredTree().getViewer().getSelection())
                        .getFirstElement();
                refresh(selection);
              }
            });

    // add listener to keep in mind the selected diagram in the list for the
    // view of digram list
    getDiagramfilteredTree()
        .getViewer()
        .addSelectionChangedListener(
            new ISelectionChangedListener() {

              public void selectionChanged(SelectionChangedEvent event) {
                Object selection =
                    ((IStructuredSelection) getDiagramfilteredTree().getViewer().getSelection())
                        .getFirstElement();
                refresh(selection);
              }
            });

    // add listener for the button ok, keep the selected diagram
    getOKbutton()
        .addSelectionListener(
            new SelectionListener() {

              public void widgetSelected(SelectionEvent e) {
                getLookforShell().close();
              }

              public void widgetDefaultSelected(SelectionEvent e) {}
            });

    // add listener for the button cancel, remove the value of the selected
    // diagram
    getCancelbutton()
        .addSelectionListener(
            new SelectionListener() {

              public void widgetSelected(SelectionEvent e) {
                setSelectedEditor(null);
                getLookforShell().close();
              }

              public void widgetDefaultSelected(SelectionEvent e) {}
            });

    // dispose the adapter factory when the shell is closed
    getLookforShell()
        .addDisposeListener(
            new DisposeListener() {

              public void widgetDisposed(DisposeEvent e) {
                // we created the adapter factory, so we should dispose it
                if (adapterFactory instanceof IDisposable) {
                  ((IDisposable) adapterFactory).dispose();
                }
              }
            });
  }