KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > ui > refactoring > TextEditChangeNode


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ltk.ui.refactoring;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.resources.IFile;
19
20 import org.eclipse.ltk.core.refactoring.Change;
21 import org.eclipse.ltk.core.refactoring.TextEditBasedChange;
22 import org.eclipse.ltk.core.refactoring.TextEditBasedChangeGroup;
23 import org.eclipse.ltk.core.refactoring.TextFileChange;
24
25 import org.eclipse.ltk.internal.ui.refactoring.InternalTextEditChangeNode;
26 import org.eclipse.ltk.internal.ui.refactoring.Messages;
27 import org.eclipse.ltk.internal.ui.refactoring.PreviewNode;
28 import org.eclipse.ltk.internal.ui.refactoring.RefactoringPluginImages;
29 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages;
30 import org.eclipse.ltk.internal.ui.refactoring.TextEditGroupNode;
31
32 import org.eclipse.jface.resource.ImageDescriptor;
33
34 /**
35  * A special preview node to represent <code>TextEditBasedChange</code>s in the
36  * preview tree.
37  * <p>
38  * This class should be subclassed by clients wishing to provide language
39  * aware substructures for special <code>TextEditBasedChange<code>s. The preview
40  * infrastructure accesses to preview node for a <code>TextEditBasedChange<code>
41  * by asking the change for an adapter of type <code>TextEditChangeNode</code>.
42  * If no adapter is returned, this default implementation will be used to present
43  * <code>TextEditBasedChange<code> in the preview tree.
44  * </p>
45  * @since 3.2
46  */

47 public class TextEditChangeNode extends InternalTextEditChangeNode {
48
49     public static abstract class ChildNode extends PreviewNode {
50         protected ChildNode(PreviewNode parent) {
51             super(parent);
52         }
53         protected TextEditChangeNode getTextEditChangeNode() {
54             return internalGetTextEditChangeNode(this);
55         }
56     }
57     
58     /**
59      * Creates a new child node for the given parent and change group.
60      *
61      * @param parent the parent of the new child node
62      * @param changeGroup the <code>TextEditBasedChangeGroup</code> this child node
63      * represents in the preview tree
64      *
65      * @return the new child node
66      */

67     public static ChildNode createTextEditGroupNode(ChildNode parent, TextEditBasedChangeGroup changeGroup) {
68         return new TextEditGroupNode(parent, changeGroup);
69     }
70     
71     /**
72      * Creates a new child node for the given parent and change group.
73      *
74      * @param parent the parent of the new child node
75      * @param changeGroup the <code>TextEditBasedChangeGroup</code> this child node
76      * represents in the preview tree
77      *
78      * @return the new child node
79      */

80     public static ChildNode createTextEditGroupNode(TextEditChangeNode parent, TextEditBasedChangeGroup changeGroup) {
81         return new TextEditGroupNode(parent, changeGroup);
82     }
83     
84     /**
85      * Creates a new text edit change node for the given change.
86      *
87      * @param change the <code>TextEditBasedChange</code> this node
88      * represents in the preview tree
89      */

90     public TextEditChangeNode(TextEditBasedChange change) {
91         // the parent will be set lazily via the initialize method
92
super(null, change);
93     }
94
95     /**
96      * Returns the <code>TextEditBasedChange</code> this node is
97      * associated with.
98      *
99      * @return the <code>TextEditBasedChange<code>
100      */

101     public final TextEditBasedChange getTextEditBasedChange() {
102         return super.getTextEditBasedChange();
103     }
104     
105     /**
106      * Returns the text used to render this node in the
107      * UI.
108      *
109      * @return a human readable representation of this node
110      */

111     public String JavaDoc getText() {
112         Change change= getTextEditBasedChange();
113         if (change instanceof TextFileChange) {
114             IFile file= ((TextFileChange)change).getFile();
115             return Messages.format(
116                 RefactoringUIMessages.PreviewWizardPage_changeElementLabelProvider_textFormat,
117                 new String JavaDoc[] {file.getName(), getPath(file)});
118         }
119         return super.getText();
120     }
121     
122     /**
123      * Returns the image descriptor used to render this node
124      * in the UI.
125      *
126      * @return the image descriptor representing this node
127      */

128     public ImageDescriptor getImageDescriptor() {
129         return RefactoringPluginImages.DESC_OBJS_FILE_CHANGE;
130     }
131     
132     protected ChildNode[] createChildNodes() {
133         TextEditBasedChangeGroup[] groups= getSortedChangeGroups(getTextEditBasedChange());
134         ChildNode[] result= new ChildNode[groups.length];
135         for (int i= 0; i < groups.length; i++) {
136             result[i]= new TextEditGroupNode(this, groups[i]);
137         }
138         return result;
139     }
140     
141     private static class OffsetComparator implements Comparator JavaDoc {
142         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
143             TextEditBasedChangeGroup c1= (TextEditBasedChangeGroup)o1;
144             TextEditBasedChangeGroup c2= (TextEditBasedChangeGroup)o2;
145             int p1= getOffset(c1);
146             int p2= getOffset(c2);
147             if (p1 < p2)
148                 return -1;
149             if (p1 > p2)
150                 return 1;
151             // same offset
152
return 0;
153         }
154         private int getOffset(TextEditBasedChangeGroup edit) {
155             return edit.getRegion().getOffset();
156         }
157     }
158     
159     private TextEditBasedChangeGroup[] getSortedChangeGroups(TextEditBasedChange change) {
160         TextEditBasedChangeGroup[] groups= change.getChangeGroups();
161         List JavaDoc result= new ArrayList JavaDoc(groups.length);
162         for (int i= 0; i < groups.length; i++) {
163             if (!groups[i].getTextEditGroup().isEmpty())
164                 result.add(groups[i]);
165         }
166         Comparator JavaDoc comparator= new OffsetComparator();
167         Collections.sort(result, comparator);
168         return (TextEditBasedChangeGroup[])result.toArray(new TextEditBasedChangeGroup[result.size()]);
169     }
170     
171     private String JavaDoc getPath(IFile file) {
172         StringBuffer JavaDoc result= new StringBuffer JavaDoc(file.getProject().getName());
173         String JavaDoc projectRelativePath= file.getParent().getProjectRelativePath().toString();
174         if (projectRelativePath.length() > 0) {
175             result.append('/');
176             result.append(projectRelativePath);
177         }
178         return result.toString();
179     }
180 }
Popular Tags