diff options
author | Eike Stepper | 2016-01-10 10:27:48 +0000 |
---|---|---|
committer | Eike Stepper | 2016-01-12 07:00:26 +0000 |
commit | 11cdaee2b9ed25674935f4d7b7cdbc1d7a7abd53 (patch) | |
tree | a6b9c8e0852bc1ca780b7258ceab92d0631f62a2 /plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util | |
parent | c523e8929ec6285254f381732604edf5d32bc294 (diff) | |
download | cdo-11cdaee2b9ed25674935f4d7b7cdbc1d7a7abd53.tar.gz cdo-11cdaee2b9ed25674935f4d7b7cdbc1d7a7abd53.tar.xz cdo-11cdaee2b9ed25674935f4d7b7cdbc1d7a7abd53.zip |
[485491] Provide a logging facility for CDOViewEvents
https://bugs.eclipse.org/bugs/show_bug.cgi?id=485491
Diffstat (limited to 'plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util')
3 files changed, 274 insertions, 0 deletions
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/CheckUtil.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/CheckUtil.java index b8c1d1c9c7..8acb06a4d2 100644 --- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/CheckUtil.java +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/CheckUtil.java @@ -10,6 +10,8 @@ */ package org.eclipse.net4j.util; +import org.eclipse.net4j.util.io.IOUtil; + /** * Provides static methods that check object states and invocation arguments. * @@ -18,6 +20,8 @@ package org.eclipse.net4j.util; */ public final class CheckUtil { + private static int counter; + private CheckUtil() { } @@ -61,4 +65,20 @@ public final class CheckUtil throw new IllegalStateException(handleName + " is null"); //$NON-NLS-1$ } } + + /** + * @since 3.6 + */ + public static void countUp(String message) + { + IOUtil.OUT().println(message + (++counter)); + } + + /** + * @since 3.6 + */ + public static void countDown(String message) + { + IOUtil.OUT().println(message + --counter); + } } diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/container/SelfAttachingContainerListener.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/container/SelfAttachingContainerListener.java new file mode 100644 index 0000000000..ea544fa576 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/container/SelfAttachingContainerListener.java @@ -0,0 +1,215 @@ +/* + * Copyright (c) 2004-2015 Eike Stepper (Berlin, 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.container.IContainerDelta.Kind; +import org.eclipse.net4j.util.event.EventUtil; +import org.eclipse.net4j.util.event.IEvent; +import org.eclipse.net4j.util.event.IListener; +import org.eclipse.net4j.util.lifecycle.ILifecycle; +import org.eclipse.net4j.util.lifecycle.LifecycleEventAdapter; +import org.eclipse.net4j.util.lifecycle.LifecycleUtil; + +/** + * @author Eike Stepper + * @since 3.6 + */ +public class SelfAttachingContainerListener implements IListener +{ + public SelfAttachingContainerListener() + { + } + + public void attach(Object element) + { + if (shouldAttach(element)) + { + EventUtil.addListener(element, this); + + if (shouldDescend(element)) + { + try + { + Object[] children = ContainerUtil.getElements(element); + if (children != null) + { + for (Object child : children) + { + try + { + attach(child); + } + catch (Exception ex) + { + handleException(ex); + } + } + } + } + catch (Exception ex) + { + handleException(ex); + } + } + } + } + + public void detach(Object element) + { + if (shouldAttach(element)) + { + if (shouldDescend(element)) + { + try + { + Object[] children = ContainerUtil.getElements(element); + if (children != null) + { + for (Object child : children) + { + try + { + detach(child); + } + catch (Exception ex) + { + handleException(ex); + } + } + } + } + catch (Exception ex) + { + handleException(ex); + } + } + + EventUtil.removeListener(element, this); + } + } + + public void notifyEvent(IEvent event) + { + if (event instanceof IContainerEvent<?>) + { + notifyContainerEvent((IContainerEvent<?>)event); + } + else + { + notifyOtherEvent(event); + } + } + + protected void notifyContainerEvent(IContainerEvent<?> event) + { + for (IContainerDelta<?> delta : event.getDeltas()) + { + final Object element = delta.getElement(); + + if (delta.getKind() == Kind.ADDED) + { + if (isWaitForActive() && !isActive(element)) + { + EventUtil.addListener(element, new LifecycleEventAdapter() + { + @Override + protected void onActivated(ILifecycle lifecycle) + { + lifecycle.removeListener(this); + attach(element); + } + }); + } + else + { + attach(element); + } + } + else + { + detach(element); + } + } + } + + protected void notifyOtherEvent(IEvent event) + { + } + + protected boolean shouldAttach(Object element) + { + return true; + } + + protected boolean shouldDescend(Object element) + { + return !(element instanceof DoNotDescend); + } + + protected boolean isWaitForActive() + { + return true; + } + + protected boolean isActive(Object element) + { + return LifecycleUtil.isActive(element); + } + + protected void handleException(Exception ex) + { + } + + /** + * @author Eike Stepper + */ + public interface DoNotDescend + { + } + + /** + * @author Eike Stepper + */ + public static class Delegating extends SelfAttachingContainerListener + { + private final IListener delegate; + + private final boolean delegateContainerEvents; + + public Delegating(IListener delegate, boolean delegateContainerEvents) + { + this.delegate = delegate; + this.delegateContainerEvents = delegateContainerEvents; + } + + public Delegating(IListener delegate) + { + this(delegate, false); + } + + @Override + protected void notifyContainerEvent(IContainerEvent<?> event) + { + super.notifyContainerEvent(event); + + if (delegateContainerEvents) + { + delegate.notifyEvent(event); + } + } + + @Override + protected void notifyOtherEvent(IEvent event) + { + delegate.notifyEvent(event); + } + } +} diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/event/EventPrinter.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/event/EventPrinter.java new file mode 100644 index 0000000000..9ed88a166c --- /dev/null +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/event/EventPrinter.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2016 Eike Stepper (Berlin, 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.event; + +import org.eclipse.net4j.util.io.IOUtil; + +import java.io.PrintStream; + +/** + * @author Eike Stepper + * @since 3.6 + */ +public class EventPrinter implements IListener +{ + private final PrintStream stream; + + public EventPrinter(PrintStream stream) + { + this.stream = stream; + } + + public EventPrinter() + { + this(IOUtil.OUT()); + } + + public void notifyEvent(IEvent event) + { + stream.println(event); + } +} |