nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2004 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 v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
nitind | 4941845 | 2005-02-15 06:42:31 +0000 | [diff] [blame] | 11 | package org.eclipse.jst.jsp.core.internal.document; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 12 | |
david_williams | bcf9126 | 2005-11-14 03:54:42 +0000 | [diff] [blame] | 13 | import org.eclipse.jst.jsp.core.internal.Assert; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 14 | import org.eclipse.wst.sse.core.internal.provisional.AbstractAdapterFactory; |
| 15 | import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter; |
| 16 | import org.eclipse.wst.sse.core.internal.provisional.INodeAdapterFactory; |
| 17 | import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 18 | import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 19 | import org.w3c.dom.Node; |
| 20 | |
| 21 | /** |
| 22 | * This class adapts document |
| 23 | * with the an instance of PageDirectiveAdapter |
| 24 | */ |
david_williams | 285ee2d | 2005-04-10 18:21:21 +0000 | [diff] [blame] | 25 | public class PageDirectiveAdapterFactory extends AbstractAdapterFactory implements INodeAdapterFactory { |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | private PageDirectiveAdapter pageDirectiveAdapterInstance = null; |
| 29 | |
| 30 | /** |
| 31 | * Constructor for PageDirectiveAdapterFactory. |
| 32 | * Note: its important not to be a singleton, since |
| 33 | * this factory needs to track its adapter(s) and release |
| 34 | * them when they are released. |
| 35 | * |
| 36 | * @param adapterKey |
| 37 | * @param registerAdapters |
| 38 | */ |
| 39 | protected PageDirectiveAdapterFactory(Object adapterKey, boolean registerAdapters) { |
| 40 | super(adapterKey, registerAdapters); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * The no argument constructor assumes its a |
| 45 | * Factory for PageDirectiveAdapter |
| 46 | */ |
| 47 | public PageDirectiveAdapterFactory() { |
| 48 | this(PageDirectiveAdapter.class, true); |
| 49 | } |
| 50 | |
| 51 | protected INodeAdapter createAdapter(INodeNotifier target) { |
| 52 | PageDirectiveAdapter result = null; |
david_williams | c39caaf | 2005-04-05 06:07:16 +0000 | [diff] [blame] | 53 | if (target instanceof IDOMNode) { |
| 54 | IDOMNode node = (IDOMNode) target; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 55 | if (node.getNodeType() == Node.DOCUMENT_NODE) { |
| 56 | result = getAdapterInstance(target); |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | } |
| 60 | return result; |
| 61 | } |
| 62 | |
| 63 | public void release() { |
| 64 | if (pageDirectiveAdapterInstance != null) { |
pavery | 2a24f37 | 2005-06-16 03:10:09 +0000 | [diff] [blame] | 65 | pageDirectiveAdapterInstance.release(); |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * We assume this is only called for 'document' target |
| 71 | */ |
| 72 | protected PageDirectiveAdapter getAdapterInstance(INodeNotifier target) { |
| 73 | // if our instance already exists with a different |
| 74 | // target, then, somehow, the document node must |
| 75 | // have changed for a model, so we should release |
| 76 | // old adapter and create new one for new document |
| 77 | // node. This is probably a programming error. |
| 78 | if (pageDirectiveAdapterInstance != null) { |
| 79 | if (target != pageDirectiveAdapterInstance.getTarget()) { |
| 80 | release(); |
| 81 | pageDirectiveAdapterInstance = new PageDirectiveAdapterImpl(target); |
| 82 | } |
| 83 | // else return the one we have |
| 84 | } |
| 85 | else { |
| 86 | // if is equal to null, create a new one |
| 87 | pageDirectiveAdapterInstance = new PageDirectiveAdapterImpl(target); |
| 88 | } |
nitind | 73e7641 | 2005-11-10 01:50:19 +0000 | [diff] [blame] | 89 | Assert.isNotNull(pageDirectiveAdapterInstance, "pageDipageDirectiveAdapterInstance was null"); //$NON-NLS-1$ |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 90 | return pageDirectiveAdapterInstance; |
| 91 | } |
| 92 | |
david_williams | 285ee2d | 2005-04-10 18:21:21 +0000 | [diff] [blame] | 93 | public INodeAdapterFactory copy() { |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 94 | |
david_williams | 6f9dcdd | 2005-10-09 05:14:55 +0000 | [diff] [blame] | 95 | return new PageDirectiveAdapterFactory(getAdapterKey(), isShouldRegisterAdapter()); |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 96 | } |
| 97 | } |