001 /**
002 * <copyright>
003 *
004 * Copyright (c) 2006 IBM Corporation and others.
005 * All rights reserved. This program and the accompanying materials
006 * are made available under the terms of the Eclipse Public License v1.0
007 * which accompanies this distribution, and is available at
008 * http://www.eclipse.org/legal/epl-v10.html
009 *
010 * Contributors:
011 * IBM - Initial API and implementation
012 *
013 * </copyright>
014 *
015 * $Id: SingletonAdapterImpl.java,v 1.3 2007/06/12 20:56:17 emerks Exp $
016 */
017 package org.eclipse.emf.common.notify.impl;
018
019
020 import java.util.ArrayList;
021 import java.util.List;
022
023 import org.eclipse.emf.common.notify.Adapter;
024 import org.eclipse.emf.common.notify.Notification;
025 import org.eclipse.emf.common.notify.Notifier;
026
027
028 /**
029 * An alternate, extensible adapter implementation that is well suited to adapt for a number of objects
030 * (typically all objects of a given type).
031 * @since 2.2
032 *
033 */
034 public class SingletonAdapterImpl implements Adapter.Internal
035 {
036 /**
037 * The list of all the targets to which this adapter is set.
038 */
039 protected List<Notifier> targets;
040
041 /**
042 * Creates an instance.
043 */
044 public SingletonAdapterImpl()
045 {
046 super();
047 }
048
049 /**
050 * Returns <code>false</code>
051 * @param type the type.
052 * @return <code>false</code>
053 */
054 public boolean isAdapterForType(Object type)
055 {
056 return false;
057 }
058
059 /**
060 * Does nothing; clients may override so that it does something.
061 */
062 public void notifyChanged(Notification msg)
063 {
064 // Do nothing.
065 }
066
067 public Notifier getTarget()
068 {
069 return targets == null || targets.isEmpty() ? null : targets.get(targets.size() - 1);
070 }
071
072 public void setTarget(Notifier target)
073 {
074 if (targets == null)
075 {
076 targets = new ArrayList<Notifier>();
077 }
078 targets.add(target);
079 }
080
081 public void unsetTarget(Notifier target)
082 {
083 if (targets != null)
084 {
085 targets.remove(target);
086 }
087 }
088
089 /**
090 * Removes the adapter from all its targets.
091 */
092 public void dispose()
093 {
094 List<Notifier> oldTargets = targets;
095 targets = null;
096
097 if (oldTargets != null)
098 {
099 for (Notifier notifier : oldTargets)
100 {
101 notifier.eAdapters().remove(this);
102 }
103 }
104 }
105 }