Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0b98a90c2d19dc7eff7e6d745acc7621d53ac76b (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
/*******************************************************************************
  * Copyright (c) 2010, 2012 Red Hat, Inc.
  * Distributed under license by Red Hat, Inc. All rights reserved.
  * This program is 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:
  *     Red Hat, Inc. - initial API and implementation
  *     Oracle
  ******************************************************************************/
package org.eclipse.jpt.common.core.internal.resource.java.source;

import org.eclipse.jdt.core.dom.IPackageBinding;
import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jpt.common.core.internal.utility.jdt.JDTPackage;
import org.eclipse.jpt.common.core.resource.java.JavaResourceCompilationUnit;
import org.eclipse.jpt.common.core.resource.java.JavaResourcePackage;
import org.eclipse.jpt.common.core.utility.jdt.AnnotatedPackage;

/**
 * @author Dmitry Geraskov
 * Source package-info.java
 *
 */
public final class SourcePackage
	extends SourceAnnotatedElement<AnnotatedPackage>
	implements JavaResourcePackage
{

	private String name;

	/**
	 * construct package info
	 */
	public static JavaResourcePackage newInstance(
			JavaResourceCompilationUnit parent,
			PackageDeclaration declaringPackage) {
		AnnotatedPackage pack = new JDTPackage(
				declaringPackage,
				parent.getCompilationUnit(), 
				parent.getModifySharedDocumentCommandExecutor(),
				parent.getAnnotationEditFormatter());
		SourcePackage jrpp = new SourcePackage(parent, pack);
		jrpp.initialize(declaringPackage);
		return jrpp;
	}	

	private SourcePackage(
			JavaResourceCompilationUnit parent,
			AnnotatedPackage pack){
		super(parent, pack);
	}


	protected void initialize(PackageDeclaration packageDeclaration) {
		super.initialize(packageDeclaration, packageDeclaration.getName());
		this.initialize(packageDeclaration.resolveBinding());
	}

	protected void initialize(IPackageBinding packageBinding) {
		this.name = this.buildName(packageBinding);
	}

	// ******** JavaResourceAnnotatedElement implementation ********
	
	public Kind getKind() {
		return Kind.PACKAGE;
	}


	// ********** JavaResourcePackageInfo implementation **********
	
	// ***** name
	public String getName() {
		return this.name;
	}

	private void syncName(String astName) {
		if (valuesAreDifferent(astName, this.name)){
			String old = this.name;
			this.name = astName;
			this.firePropertyChanged(NAME_PROPERTY, old, astName);
		}		
	}

	private String buildName(IPackageBinding binding) {
		return (binding == null) ? null : binding.getName();
	}


	// ********** Java changes **********

	public void synchronizeWith(PackageDeclaration packageDeclaration) {
		super.synchronizeWith(packageDeclaration, packageDeclaration.getName());
		this.synchronizeWith(packageDeclaration.resolveBinding());
	}

	protected void synchronizeWith(IPackageBinding packageBinding) {
		this.syncName(this.buildName(packageBinding));
	}

	@Override
	public void toString(StringBuilder sb) {
		sb.append(this.name);
	}
}

Back to the top