Skip to main content
summaryrefslogtreecommitdiffstats
blob: abab902bb88808ba0191ae03787f72f206b778d9 (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
/*******************************************************************************
 * Copyright (c) 2003, 2004 IBM Corporation and others.
 * 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:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.common.jdt.internal.integration;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.jdom.DOMFactory;
import org.eclipse.wst.common.frameworks.internal.operations.IHeadlessRunnableWithProgress;


/**
 * @author DABERG
 * 
 * This class is used by the Java snippet support to capture the insertionString that is to be
 * inserted at the users selection point. It also provides the ability to define additional fields
 * and methods to support the insertionString.
 */
public class JavaInsertionHelper {
	protected DOMFactory domFactory = new DOMFactory();
	protected List fields;
	protected List methods;
	protected List imports;
	protected String insertionString;
	protected List extendedOperations;

	/**
	 *  
	 */
	public JavaInsertionHelper() {
		super();
	}

	/**
	 * @return
	 */
	public List getFields() {
		return fields;
	}

	/**
	 * @return
	 */
	public String getInsertionString() {
		return insertionString;
	}

	/**
	 * @return
	 */
	public List getMethods() {
		return methods;
	}

	/**
	 * This is required to be set by the client. This is the String that will be inserted at the
	 * users selection point.
	 * 
	 * @param string
	 */
	public void setInsertionString(String string) {
		insertionString = string;
	}

	/**
	 * This is a utility method that will parse the methodString and create a IDOMMethod. The
	 * DOMFactory will be used to create the method. This new method will be added to the list of
	 * methods.
	 * 
	 * @param methodString
	 * @see DOMFactory#createMethod(java.lang.String)
	 * @link org.eclipse.jdt.core.jdom.IDOMMethod
	 */
	public void addMethodFromSourceString(String methodString) {
		if (methodString != null && methodString.length() > 0) {
			if (methods == null)
				methods = new ArrayList();
			methods.add(domFactory.createMethod(methodString));
		}
	}

	/**
	 * This is a utility method that will parse the fieldString and create a IDOMField. The
	 * DOMFactory will be used to create the field. This new field will be added to the list of
	 * fields.
	 * 
	 * @param fieldString
	 * @see DOMFactory#createField(java.lang.String)
	 * @link org.eclipse.jdt.core.jdom.IDOMField
	 */
	public void addFieldFromSourceString(String fieldString) {
		if (fieldString != null && fieldString.length() > 0) {
			if (fields == null)
				fields = new ArrayList();
			fields.add(domFactory.createField(fieldString));
		}
	}

	/**
	 * Add an import that is either the qualified name of a type or a package name with .* at the
	 * end.
	 * 
	 * @param importString
	 */
	public void addImport(String importString) {
		if (importString != null && importString.length() > 0) {
			if (imports == null)
				imports = new ArrayList();
			imports.add(importString);
		}
	}

	/**
	 * Return true if the insertionString is set and not a zero length.
	 * 
	 * @return
	 */
	public boolean canInsertText() {
		return insertionString != null && insertionString.length() > 0;
	}

	/**
	 * @return
	 */
	public boolean hasFields() {
		return fields != null && !fields.isEmpty();
	}

	/**
	 * @return
	 */
	public boolean hasMethods() {
		return methods != null && !methods.isEmpty();
	}

	public boolean hasImports() {
		return imports != null && !imports.isEmpty();
	}

	/**
	 * @return Returns the imports.
	 */
	public List getImportStatements() {
		return imports;
	}

	/**
	 * @return Returns the extendedOperations.
	 */
	public List getExtendedOperations() {
		return extendedOperations;
	}

	/**
	 * This method allows you to add additional operations which will be performed after this
	 * JavaInsertionHelper is processed by the JavaInsertionOperation.
	 * 
	 * @param operation
	 * @link JavaInsertionOperation
	 */
	public void addExtendedOperation(IHeadlessRunnableWithProgress operation) {
		if (operation != null) {
			if (extendedOperations == null)
				extendedOperations = new ArrayList();
			extendedOperations.add(operation);
		}
	}
}

Back to the top