001 /**
002 * <copyright>
003 *
004 * Copyright (c) 2002-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: AdapterFactoryImpl.java,v 1.6 2006/12/05 20:19:58 emerks Exp $
016 */
017 package org.eclipse.emf.common.notify.impl;
018
019
020 import org.eclipse.emf.common.notify.Adapter;
021 import org.eclipse.emf.common.notify.AdapterFactory;
022 import org.eclipse.emf.common.notify.Notifier;
023
024
025 /**
026 * An extensible adapter factory implementation.
027 */
028 public class AdapterFactoryImpl implements AdapterFactory
029 {
030 /**
031 * Creates an instance.
032 */
033 public AdapterFactoryImpl()
034 {
035 super();
036 }
037
038 /**
039 * Returns <code>false</code>.
040 * @param type the key indicating the type of adapter in question.
041 * @return <code>false</code>.
042 */
043 public boolean isFactoryForType(Object type)
044 {
045 return false;
046 }
047
048 /**
049 * Returns either
050 * the result of calling {@link #adapt(Notifier,Object) adapt(Notifier, Object)}
051 * or the result of calling {@link #resolve resolve(Object, Object)},
052 * depending on whether the target is a notifier.
053 * @param target arbitrary object to adapt.
054 * @param type the key indicating the type of adapter required.
055 * @return either an associated adapter or the object itself.
056 * @see #adapt(Notifier,Object)
057 * @see #resolve(Object, Object)
058 */
059 public Object adapt(Object target, Object type)
060 {
061 if (target instanceof Notifier)
062 {
063 return adapt((Notifier)target, type);
064 }
065 else
066 {
067 return resolve(target, type);
068 }
069 }
070
071 /**
072 * Returns the object itself.
073 * This is called by {@link #adapt(Object,Object) adapt(Object, Object)} for objects that aren't notifiers.
074 * @param object arbitrary object to adapt.
075 * @param type the key indicating the type of adapter required.
076 * @return the object itself.
077 * @see #adapt(Object,Object)
078 */
079 protected Object resolve(Object object, Object type)
080 {
081 return object;
082 }
083
084 public Adapter adapt(Notifier target, Object type)
085 {
086 for (Adapter adapter : target.eAdapters())
087 {
088 if (adapter.isAdapterForType(type))
089 {
090 return adapter;
091 }
092 }
093 return adaptNew(target, type);
094 }
095
096 /**
097 * Creates an adapter by calling {@link #createAdapter(Notifier, Object) createAdapter(Notifier, Object)}
098 * and associates it by calling {@link #associate(Adapter, Notifier) associate}.
099 * @param target the notifier to adapt.
100 * @param type the key indicating the type of adapter required.
101 * @return a new associated adapter.
102 * @see #createAdapter(Notifier, Object)
103 * @see #associate(Adapter, Notifier)
104 */
105 public Adapter adaptNew(Notifier target, Object type)
106 {
107 Adapter adapter = createAdapter(target, type);
108 associate(adapter, target);
109 return adapter;
110 }
111
112 /**
113 * Creates an adapter by calling {@link #createAdapter(Notifier) createAdapter(Notifier)}
114 * and associates it by calling {@link #associate(Adapter, Notifier) associate}.
115 * @param target notifier to adapt.
116 * @see #createAdapter(Notifier)
117 */
118 public void adaptAllNew(Notifier target)
119 {
120 Adapter adapter = createAdapter(target);
121 associate(adapter, target);
122 }
123
124 /**
125 * Creates an adapter by calling {@link #createAdapter(Notifier) createAdapter(Notifier)}.
126 * @param target the notifier to adapt.
127 * @param type the key indicating the type of adapter required.
128 * @return a new adapter.
129 * @see #createAdapter(Notifier)
130 */
131 protected Adapter createAdapter(Notifier target, Object type)
132 {
133 return createAdapter(target);
134 }
135
136 /**
137 * Creates an {@link org.eclipse.emf.common.notify.impl.AdapterImpl}.
138 * @param target the notifier to adapt.
139 * @return a new adapter.
140 * @see #createAdapter(Notifier)
141 */
142 protected Adapter createAdapter(Notifier target)
143 {
144 return new AdapterImpl();
145 }
146
147 /**
148 * Associates an adapter with a notifier by adding it to the target's {@link org.eclipse.emf.common.notify.Notifier#eAdapters adapters}.
149 * @param adapter the new adapter to associate.
150 * @param target the notifier being adapted.
151 */
152 protected void associate(Adapter adapter, Notifier target)
153 {
154 if (adapter != null)
155 {
156 target.eAdapters().add(adapter);
157 }
158 }
159 }