Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c46d65e4e14a8553e6cd92872aa380502414a613 (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
/*******************************************************************************
 * Copyright (c) 2000, 2005 QNX Software Systems 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:
 *     QNX Software Systems - Initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.internal.core.model;

import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IUsing;

/**
 * <p>This operation adds a using declaration/directive to an existing translation unit.
 *
 * <p>Required Attributes:<ul>
 *  <li>Translation unit
 *  <li>using name - the name of the using to add
 * </ul>
 */
public class CreateUsingOperation extends CreateElementInTUOperation {

	/**
	 * The name of the using to be created.
	 */
	protected String fUsingName;

	/**
	 * Whether it is a declaration or a directive.
	 */
	protected boolean fIsDirective;

	/**
	 * When executed, this operation will add an include to the given translation unit.
	 */
	public CreateUsingOperation(String usingName, boolean isDirective, ITranslationUnit parentElement) {
		super(parentElement);
		fIsDirective = isDirective;
		fUsingName = usingName;
	}

	/**
	 * @see CreateElementInCUOperation#generateResultHandle
	 */
	protected ICElement generateResultHandle() {
		return getTranslationUnit().getUsing(fUsingName);
	}

	/**
	 * @see CreateElementInCUOperation#getMainTaskName
	 */
	public String getMainTaskName(){
		return "operation.createUsingProgress"; //$NON-NLS-1$
	}

	/**
	 * Sets the correct position for the new using:<ul>
	 * <li> after the last using
	 * </ul>
	 *  if no using
	 */
	protected void initializeDefaultPosition() {
		try {
			ITranslationUnit cu = getTranslationUnit();
			IUsing[] usings = cu.getUsings();
			if (usings.length > 0) {
				createAfter(usings[usings.length - 1]);
				return;
			}
		} catch (CModelException npe) {
		}
	}

	/*
	 * TODO: Use the ASTRewrite once it is available.
	 */
	protected String generateElement(ITranslationUnit unit) throws CModelException {
		StringBuffer sb = new StringBuffer();
		sb.append("using "); //$NON-NLS-1$;
		if (fIsDirective) {
			sb.append("namespace "); //$NON-NLS-1$
		}
		sb.append(fUsingName);
		sb.append(';');
		sb.append(Util.LINE_SEPARATOR);
		return sb.toString();
	}
}

Back to the top