Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 791ade1cdbecc97df67c16e6b1cdd13b3c133880 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright (c) 2008-2012, 2015 Eike Stepper (Loehne, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.util.container;

import org.eclipse.net4j.util.event.IEvent;
import org.eclipse.net4j.util.event.IListener;
import org.eclipse.net4j.util.event.INotifier;
import org.eclipse.net4j.util.event.Notifier;
import org.eclipse.net4j.util.lifecycle.ILifecycleEvent;

/**
 * A delegating {@link IListener listener} that converts {@link ILifecycleEvent lifecycle events} into
 * {@link IContainerEvent container events}.
 *
 * @author Eike Stepper
 */
public class LifecycleEventConverter<E> implements IListener
{
  private Notifier owner;

  public LifecycleEventConverter(Notifier owner)
  {
    this.owner = owner;
  }

  public INotifier getOwner()
  {
    return owner;
  }

  public void notifyEvent(IEvent event)
  {
    if (event instanceof ILifecycleEvent)
    {
      ILifecycleEvent e = (ILifecycleEvent)event;
      switch (e.getKind())
      {
      case ACTIVATED:
        added(e);
        break;

      case DEACTIVATED:
        removed(e);
        break;
      }
    }
  }

  protected void added(ILifecycleEvent e)
  {
    fireContainerEvent(e, IContainerDelta.Kind.ADDED);
  }

  protected void removed(ILifecycleEvent e)
  {
    fireContainerEvent(e, IContainerDelta.Kind.REMOVED);
  }

  @SuppressWarnings("unchecked")
  protected void fireContainerEvent(ILifecycleEvent e, IContainerDelta.Kind kind)
  {
    E element = (E)e.getSource();
    if (element != null)
    {
      IListener[] listeners = owner.getListeners();
      if (listeners != null)
      {
        owner.fireEvent(createContainerEvent((IContainer<E>)owner, element, kind), listeners);
      }
    }
  }

  protected IContainerEvent<E> createContainerEvent(IContainer<E> container, E element, IContainerDelta.Kind kind)
  {
    ContainerEvent<E> event = new ContainerEvent<E>(container);
    event.addDelta(new ContainerDelta<E>(element, kind));
    return event;
  }
}

Back to the top