Skip to main content
summaryrefslogtreecommitdiffstats
blob: 79054b80ca799205ab5487e30a748c3764a6f5be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*******************************************************************************
 * Copyright (c) 2006 Oracle Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Cameron Bateman/Oracle - initial API and implementation
 *    
 ********************************************************************************/

package org.eclipse.jst.jsf.context.structureddocument.internal.impl;

import java.util.Iterator;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jst.jsf.context.internal.provisional.AbstractDelegatingFactory;
import org.eclipse.jst.jsf.context.structureddocument.internal.provisional.IStructuredDocumentContext;
import org.eclipse.jst.jsf.context.structureddocument.internal.provisional.IStructuredDocumentContextFactory;
import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.sse.ui.internal.StructuredTextViewer;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr;
import org.w3c.dom.Node;

/**
 * @author cbateman
 *
 */
public class StructuredDocumentContextFactory extends AbstractDelegatingFactory
			 implements IStructuredDocumentContextFactory
{
	/* static attributes */
	private static StructuredDocumentContextFactory  INSTANCE;

	/**
	 * @return an instance (possibly shared) of the this factory
	 */
	public synchronized static StructuredDocumentContextFactory getInstance()
	{
		if (INSTANCE == null)
		{
			INSTANCE = new StructuredDocumentContextFactory();
		}
		
		return INSTANCE;
	}
	
	
	/**
	 * Construct the factory
	 */
	protected StructuredDocumentContextFactory()
	{
		super(new Class[] {IStructuredDocumentContextFactory.class});
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jst.jsf.context.structureddocument.IStructuredDocumentContextFactory#getContext(org.eclipse.jface.text.ITextViewer, int)
	 */
	public final IStructuredDocumentContext getContext(ITextViewer textViewer, int documentPosition) 
	{
		// first see if I know how to make one
		IStructuredDocumentContext  context = internalGetContext(textViewer, documentPosition);
		
		// if I don't know, ask my delegates
		if (context == null)
		{
			context = delegateGetContext(textViewer, documentPosition);
		}
		
		return context;
	}

	private IStructuredDocumentContext internalGetContext(ITextViewer textViewer, int documentPosition)
	{
		if (textViewer instanceof StructuredTextViewer)
		{
			IDocument document = ((StructuredTextViewer)textViewer).getDocument();
			return internalGetContext(document, documentPosition);
		}
		
		return null;
	}
	
	private IStructuredDocumentContext internalGetContext(IDocument document, int documentPosition)
	{
		if (document instanceof IStructuredDocument)
		{
			return new DefaultStructuredDocumentContext((IStructuredDocument)document, documentPosition);
		}
		
		return null;
	}
	
	private IStructuredDocumentContext delegateGetContext(ITextViewer textViewer, int documentPosition)
	{
		synchronized(_delegates)
		{
			for (final Iterator it = _delegates.iterator(); it.hasNext();)
			{
				IStructuredDocumentContextFactory delegateFactory = (IStructuredDocumentContextFactory) ((IAdaptable) it.next()).getAdapter(IStructuredDocumentContextFactory.class);
				IStructuredDocumentContext context = delegateFactory.getContext(textViewer, documentPosition);
				
				if (context != null)
				{
					return context;
				}
			}
			
			return null;
		}
	}

	/**
	 * @see org.eclipse.jst.jsf.context.structureddocument.internal.provisional.IStructuredDocumentContextFactory#getContext(org.eclipse.jface.text.IDocument, int)
	 */
	public IStructuredDocumentContext getContext(IDocument document, int documentPosition) {
		// first see if I know how to make one
		IStructuredDocumentContext  context = internalGetContext(document, documentPosition);
		
		// if I don't know, ask my delegates
		if (context == null)
		{
			context = delegateGetContext(document, documentPosition);
		}
		
		return context;
	}
	
	private IStructuredDocumentContext delegateGetContext(IDocument document, int documentPosition)
	{
		synchronized(_delegates)
		{
			for (final Iterator it = _delegates.iterator(); it.hasNext();)
			{
				IStructuredDocumentContextFactory delegateFactory = (IStructuredDocumentContextFactory) ((IAdaptable) it.next()).getAdapter(IStructuredDocumentContextFactory.class);
				IStructuredDocumentContext context = delegateFactory.getContext(document, documentPosition);
				
				if (context != null)
				{
					return context;
				}
			}
			
			return null;
		}
	}

    public IStructuredDocumentContext getContext(IDocument document, Node node) 
    {
        // first see if I know how to make one
        IStructuredDocumentContext  context = internalGetContext(document, node);
        
        // if I don't know, ask my delegates
        if (context == null)
        {
            context = delegateGetContext(document, node);
        }
        
        return context;

    }
    
    private IStructuredDocumentContext internalGetContext(IDocument document, Node node)
    {
        if (document instanceof IStructuredDocument)
        {
            final IStructuredDocument sDoc = (IStructuredDocument) document;
            if (node instanceof IndexedRegion)
            {
                final int position = ((IndexedRegion)node).getStartOffset();
                return new DefaultStructuredDocumentContext(sDoc, position);
            }
            else if (node instanceof IDOMAttr)
            {
                IDOMAttr  attr = (IDOMAttr) node;
                final int position = attr.getValueRegionStartOffset();
                return new DefaultStructuredDocumentContext(sDoc, position);
            }
        }
        
        return null;
    }
    
    private IStructuredDocumentContext delegateGetContext(IDocument document, Node node)
    {
        synchronized(_delegates)
        {
            for (final Iterator it = _delegates.iterator(); it.hasNext();)
            {
                IStructuredDocumentContextFactory delegateFactory = (IStructuredDocumentContextFactory) ((IAdaptable) it.next()).getAdapter(IStructuredDocumentContextFactory.class);
                IStructuredDocumentContext context = delegateFactory.getContext(document, node);
                
                if (context != null)
                {
                    return context;
                }
            }
            
            return null;
        }
    }
}

Back to the top