KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > dom > fragments > SimpleFragment


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.dom.fragments;
12
13 import org.eclipse.text.edits.TextEditGroup;
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.jdt.core.dom.ASTNode;
18 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
19
20 import org.eclipse.jdt.internal.corext.dom.JdtASTMatcher;
21
22 class SimpleFragment extends ASTFragment {
23     private final ASTNode fNode;
24
25     SimpleFragment(ASTNode node) {
26         Assert.isNotNull(node);
27         fNode= node;
28     }
29
30     public IASTFragment[] getMatchingFragmentsWithNode(ASTNode node) {
31         if (! JdtASTMatcher.doNodesMatch(getAssociatedNode(), node))
32             return new IASTFragment[0];
33
34         IASTFragment match= ASTFragmentFactory.createFragmentForFullSubtree(node);
35         Assert.isTrue(match.matches(this) || this.matches(match));
36         return new IASTFragment[] { match };
37     }
38
39     public boolean matches(IASTFragment other) {
40         return other.getClass().equals(getClass()) && JdtASTMatcher.doNodesMatch(other.getAssociatedNode(), getAssociatedNode());
41     }
42
43     public IASTFragment[] getSubFragmentsMatching(IASTFragment toMatch) {
44         return ASTMatchingFragmentFinder.findMatchingFragments(getAssociatedNode(), (ASTFragment) toMatch);
45     }
46
47     public int getStartPosition() {
48         return fNode.getStartPosition();
49     }
50
51     public int getLength() {
52         return fNode.getLength();
53     }
54
55     public ASTNode getAssociatedNode() {
56         return fNode;
57     }
58     
59     public void replace(ASTRewrite rewrite, ASTNode replacement, TextEditGroup textEditGroup) {
60         rewrite.replace(fNode, replacement, textEditGroup);
61     }
62
63     public int hashCode() {
64         return fNode.hashCode();
65     }
66
67     public boolean equals(Object JavaDoc obj) {
68         if (this == obj)
69             return true;
70         if (obj == null)
71             return false;
72         if (getClass() != obj.getClass())
73             return false;
74         SimpleFragment other= (SimpleFragment) obj;
75         return fNode.equals(other.fNode);
76     }
77     
78 }
79
Popular Tags