| 1 |
/** |
| 2 |
* Copyright (c) 2006-2010 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v20.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM - Initial API and implementation |
| 10 |
*/ |
| 11 |
package org.eclipse.emf.common.notify.impl; |
| 12 |
|
| 13 |
|
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.List; |
| 16 |
|
| 17 |
import org.eclipse.emf.common.notify.Adapter; |
| 18 |
import org.eclipse.emf.common.notify.Notification; |
| 19 |
import org.eclipse.emf.common.notify.Notifier; |
| 20 |
|
| 21 |
|
| 22 |
/** |
| 23 |
* An alternate, extensible adapter implementation that is well suited to adapt for a number of objects |
| 24 |
* (typically all objects of a given type). |
| 25 |
* @since 2.2 |
| 26 |
* |
| 27 |
*/ |
| 28 |
public class SingletonAdapterImpl implements Adapter.Internal |
| 29 |
{ |
| 30 |
/** |
| 31 |
* The list of all the targets to which this adapter is set. |
| 32 |
*/ |
| 33 |
protected List<Notifier> targets; |
| 34 |
|
| 35 |
/** |
| 36 |
* Creates an instance. |
| 37 |
*/ |
| 38 |
public SingletonAdapterImpl() |
| 39 |
{ |
| 40 |
super(); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Returns <code>false</code> |
| 45 |
* @param type the type. |
| 46 |
* @return <code>false</code> |
| 47 |
*/ |
| 48 |
public boolean isAdapterForType(Object type) |
| 49 |
{ |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Does nothing; clients may override so that it does something. |
| 55 |
*/ |
| 56 |
public void notifyChanged(Notification msg) |
| 57 |
{ |
| 58 |
// Do nothing. |
| 59 |
} |
| 60 |
|
| 61 |
public Notifier getTarget() |
| 62 |
{ |
| 63 |
return targets == null || targets.isEmpty() ? null : targets.get(targets.size() - 1); |
| 64 |
} |
| 65 |
|
| 66 |
public void setTarget(Notifier target) |
| 67 |
{ |
| 68 |
if (targets == null) |
| 69 |
{ |
| 70 |
targets = new ArrayList<Notifier>(); |
| 71 |
} |
| 72 |
targets.add(target); |
| 73 |
} |
| 74 |
|
| 75 |
public void unsetTarget(Notifier target) |
| 76 |
{ |
| 77 |
if (targets != null) |
| 78 |
{ |
| 79 |
targets.remove(target); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Removes the adapter from all its targets. |
| 85 |
*/ |
| 86 |
public void dispose() |
| 87 |
{ |
| 88 |
List<Notifier> oldTargets = targets; |
| 89 |
targets = null; |
| 90 |
|
| 91 |
if (oldTargets != null) |
| 92 |
{ |
| 93 |
for (Notifier notifier : oldTargets) |
| 94 |
{ |
| 95 |
notifier.eAdapters().remove(this); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |