Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e3ed8e091562de465c77290ec0aea97a36d63420 (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
208
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 *
 * 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:
 *  CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.views.search.results;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.services.openelement.service.OpenElementService;
import org.eclipse.papyrus.views.search.scope.ScopeEntry;
import org.eclipse.ui.PartInitException;
import org.eclipse.uml2.uml.Stereotype;

/**
 *
 * A match raised by the value of an attribute
 *
 */
public class AttributeMatch extends ModelMatch {

	/**
	 * The element containing the value of the attribute that matches
	 */



	protected Object attribute;

	private Stereotype stereotype;



	/**
	 * Similar to {@link AbstractResultEntry} but adds an information about which attribute raised the match and which element contains the value of
	 * the attribute that matches
	 *
	 * @param offset
	 * @param lenght
	 * @param target
	 *            the element containing the value of the attribute that matches
	 * @param scopeEntry
	 * @param attribute
	 *            the attribute that raised the match
	 */
	public AttributeMatch(int offset, int lenght, Object target, ScopeEntry scopeEntry, Object attribute, Stereotype stereotype) {
		super(offset, lenght, target, scopeEntry);
		this.attribute = attribute;
		this.stereotype = stereotype;

		// if(target instanceof EObject) {
		// this.uriSource = EcoreUtil.getURI((EObject)target);
		// }
		this.parent = new ResultEntry(target, scopeEntry);
		((ResultEntry) this.parent).setParent(new ResultEntry(scopeEntry.getResourceURI(), scopeEntry));
		
		// recursiveHierarchy((AbstractResultEntry) parent);
	}

	/**
	 * Create an attribute-match in a parent result entry that already exists.
	 *
	 * @param parent
	 *            the parent result entry
	 * @param offset
	 * @param lengtt
	 * @param target
	 *            the element containing the value of the attribute that matches
	 * @param attribute
	 *            the attribute that raised the match
	 */
	public AttributeMatch(AbstractResultEntry parent, int offset, int length, Object target, Object attribute, Stereotype stereotype) {
		super(offset, length, target, (ScopeEntry) parent.getElement());

		this.attribute = attribute;
		this.stereotype = stereotype;
		this.parent = parent;
	}

	/**
	 *
	 * @see org.eclipse.papyrus.views.search.results.AbstractResultEntry#equals(java.lang.Object)
	 *      In addition, it checks if target is the same as well.
	 *
	 * @param obj
	 * @return
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof AttributeMatch) {
			if (super.equals(obj)) {
				if (((AttributeMatch) obj).getSource() instanceof EObject && this.getSource() instanceof EObject) {
					if (EcoreUtil.getURI((EObject) ((AttributeMatch) obj).getSource()).equals(EcoreUtil.getURI((EObject) this.getSource()))) {
						if (obj.hashCode() == this.hashCode()) {
							return true;
						}
					}
				} else {
					if (((AttributeMatch) obj).getSource().equals(this.getSource())) {
						if (obj.hashCode() == this.hashCode()) {
							return true;
						}
					}
				}
			}
			return false;

		} else {
			return false;
		}
	}
	
	/**
	 * @see org.eclipse.papyrus.views.search.results.AbstractResultEntry#hashCode()
	 * 
	 * Adds the identity hash since two instances of match with same offset and length will have
	 * the same hash code, although they should not if they are two different attributes.
	 * 
	 */
	@Override
	public int hashCode() {
		return super.hashCode() + System.identityHashCode(this);
	}

	// public Object getTarget() {
	// // ResourceSet resSet = new ResourceSetImpl();
	// if(this.uriSource != null) {
	// ResourceSet resSet = ((ScopeEntry)this.getElement()).getModelSet();
	// return resSet.getEObject(this.uriSource, true);
	// }
	// return null;
	//
	// }
	//
	// public void setTarget(Object target) {
	// if(target instanceof EObject) {
	// this.uriSource = EcoreUtil.getURI((EObject)target);
	// }
	//
	// }

	/**
	 *
	 * @see org.eclipse.papyrus.views.search.results.AbstractResultEntry#elementToDisplay()
	 *
	 * @return
	 */
	@Override
	public Object elementToDisplay() {
		return this;

	}

	/**
	 *
	 * @see org.eclipse.papyrus.views.search.results.AbstractResultEntry#elementToCheckFilterFor()
	 *
	 * @return
	 */
	@Override
	public Object elementToCheckFilterFor() {
		return this.getSource();
	}

	/**
	 *
	 * @see org.eclipse.papyrus.views.search.results.AbstractResultEntry#openElement()
	 *
	 * @return
	 */
	@Override
	public Object openElement(OpenElementService service) throws ServiceException, PartInitException {
		if (this.getSource() instanceof EObject) {
			return service.openSemanticElement((EObject) this.getSource());
		}
		return null;
	}

	public Object getMetaAttribute() {
		// TODO Auto-generated method stub
		return this.attribute;
	}

	public Stereotype getStereotype() {
		return stereotype;
	}


	public void setStereotype(Stereotype stereotype) {
		this.stereotype = stereotype;
	}

	public URI getURISource() {

		return uriSource;
	}

}

Back to the top