david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2001, 2005 IBM Corporation and others. |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 7 | * |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | * Jens Lukowski/Innoopract - initial renaming/restructuring |
| 11 | * |
| 12 | *******************************************************************************/ |
| 13 | package org.eclipse.wst.xml.core.internal.propagate; |
| 14 | |
| 15 | |
| 16 | import java.util.ArrayList; |
| 17 | import java.util.Iterator; |
| 18 | import java.util.List; |
| 19 | |
david_williams | 68bf9b3 | 2005-03-26 04:29:10 +0000 | [diff] [blame] | 20 | import org.eclipse.wst.sse.core.internal.PropagatingAdapter; |
| 21 | import org.eclipse.wst.sse.core.internal.PropagatingAdapterFactory; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 22 | import org.eclipse.wst.sse.core.internal.provisional.AbstractAdapterFactory; |
| 23 | import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter; |
| 24 | import org.eclipse.wst.sse.core.internal.provisional.INodeAdapterFactory; |
| 25 | import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier; |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | /** |
| 29 | * The PropagatingAdapterFactory is part of the "adapt on create" mechanism. A |
| 30 | * PropagatingAdapter, once added to a node, will cause proagating adapters to |
| 31 | * be created for all child nodes. A side effect of creating a |
| 32 | * PropagatingAdapter for a node is that is is also creates adapters for and |
| 33 | * adapts the Node for all other registered 'create on adapt' Adapters. This |
| 34 | * other adapters are registered by registering their factories via plugin |
| 35 | * extension point. |
| 36 | */ |
| 37 | public class PropagatingAdapterFactoryImpl extends AbstractAdapterFactory implements PropagatingAdapterFactory { |
| 38 | |
david_williams | 6f9dcdd | 2005-10-09 05:14:55 +0000 | [diff] [blame] | 39 | private PropagatingAdapter fAdapterInstance; |
| 40 | private List fContributedFactories = null; |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * PropagatingAdapterFactory constructor comment. |
| 44 | */ |
| 45 | public PropagatingAdapterFactoryImpl() { |
| 46 | this(PropagatingAdapter.class, true); |
| 47 | } |
| 48 | |
david_williams | 6f9dcdd | 2005-10-09 05:14:55 +0000 | [diff] [blame] | 49 | protected PropagatingAdapterFactoryImpl(Object adapterKey, boolean registerAdapters) { // , |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 50 | super(adapterKey, registerAdapters); |
| 51 | } |
| 52 | |
david_williams | 285ee2d | 2005-04-10 18:21:21 +0000 | [diff] [blame] | 53 | public void addContributedFactories(INodeAdapterFactory factory) { |
david_williams | 6f9dcdd | 2005-10-09 05:14:55 +0000 | [diff] [blame] | 54 | if (fContributedFactories != null) { |
| 55 | fContributedFactories.add(factory); |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | } |
| 59 | |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 60 | /** |
| 61 | * createAdapter method comment. |
| 62 | */ |
| 63 | protected INodeAdapter createAdapter(INodeNotifier target) { |
| 64 | // every notifier get's one of these |
| 65 | // (and the same instance of it) |
| 66 | return getAdapterInstance(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Gets the adapterInstance. |
| 71 | * |
| 72 | * @return Returns a PropagatingAdapter |
| 73 | */ |
david_williams | 6f9dcdd | 2005-10-09 05:14:55 +0000 | [diff] [blame] | 74 | private PropagatingAdapter getAdapterInstance() { |
| 75 | if (fAdapterInstance == null) { |
| 76 | fAdapterInstance = new PropagatingAdapterImpl(); |
| 77 | if (fContributedFactories != null) { |
| 78 | for (int i = 0; i < fContributedFactories.size(); i++) |
| 79 | fAdapterInstance.addAdaptOnCreateFactory((PropagatingAdapterFactory) fContributedFactories.get(i)); |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
david_williams | 6f9dcdd | 2005-10-09 05:14:55 +0000 | [diff] [blame] | 82 | return fAdapterInstance; |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | public void release() { |
| 86 | // give the adapter instance a chance to release its factories |
| 87 | getAdapterInstance().release(); |
| 88 | |
| 89 | } |
| 90 | |
| 91 | public void setContributedFactories(ArrayList list) { |
david_williams | 6f9dcdd | 2005-10-09 05:14:55 +0000 | [diff] [blame] | 92 | fContributedFactories = list; |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 93 | |
| 94 | } |
| 95 | |
david_williams | 6f9dcdd | 2005-10-09 05:14:55 +0000 | [diff] [blame] | 96 | public INodeAdapterFactory copy() { |
| 97 | PropagatingAdapterFactory clonedInstance = new PropagatingAdapterFactoryImpl(getAdapterKey(), isShouldRegisterAdapter()); |
| 98 | // clone this adapters specific list of adapter factories too |
| 99 | if (fContributedFactories != null) { |
| 100 | |
| 101 | Iterator iterator = fContributedFactories.iterator(); |
| 102 | clonedInstance.setContributedFactories(new ArrayList()); |
| 103 | while (iterator.hasNext()) { |
| 104 | INodeAdapterFactory existingFactory = (INodeAdapterFactory) iterator.next(); |
| 105 | clonedInstance.addContributedFactories(existingFactory.copy()); |
| 106 | } |
| 107 | } |
| 108 | return clonedInstance; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 113 | } |