KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > java > DiffElement


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.refactoring.java;
20
21 import java.io.Reader JavaDoc;
22 import java.io.StringReader JavaDoc;
23 import javax.swing.text.StyledDocument JavaDoc;
24 import org.netbeans.api.java.source.ModificationResult;
25 import org.netbeans.api.java.source.ModificationResult.Difference;
26 import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImpl;
27 import org.netbeans.modules.refactoring.java.ui.tree.ElementGripFactory;
28 import org.openide.filesystems.FileObject;
29 import org.openide.text.PositionBounds;
30 import static org.netbeans.modules.refactoring.java.RetoucheUtils.*;
31 import org.openide.text.PositionRef;
32
33 /**
34  *
35  * @author Jan Becicka
36  */

37  public class DiffElement extends SimpleRefactoringElementImpl {
38     private PositionBounds bounds;
39     private String JavaDoc displayText;
40     private FileObject parentFile;
41     private Difference diff;
42     private ModificationResult modification;
43     
44     public DiffElement(Difference diff, PositionBounds bounds, FileObject parentFile, ModificationResult modification) {
45         this.bounds = bounds;
46         this.displayText = diff.getDescription();
47         this.parentFile = parentFile;
48         this.diff = diff;
49         this.modification = modification;
50     }
51
52     public String JavaDoc getDisplayText() {
53         return displayText;
54     }
55
56     public Object JavaDoc getComposite() {
57         Object JavaDoc composite = ElementGripFactory.getDefault().get(parentFile, bounds.getBegin().getOffset());
58         if (composite==null)
59             composite = parentFile;
60         return composite;
61     }
62     
63     public void setEnabled(boolean enabled) {
64         diff.exclude(!enabled);
65         super.setEnabled(enabled);
66     }
67
68     public PositionBounds getPosition() {
69         return bounds;
70     }
71
72     public String JavaDoc getText() {
73         return displayText;
74     }
75
76     public void performChange() {
77     }
78
79     public FileObject getParentFile() {
80         return parentFile;
81     }
82     
83     public Reader JavaDoc getReader() {
84         try {
85             return new StringReader JavaDoc(modification.getResultingSource(parentFile));
86         } catch (Exception JavaDoc e) {
87             return null;
88         }
89     }
90     
91 // public static DiffElement create(Difference diff, FileObject fileObject) {
92
// PositionRef start = diff.getStartPosition();
93
// PositionRef end = diff.getEndPosition();
94
// //diff.exclude(true);
95
// int a = 0;
96
// int b = 0;
97
// StyledDocument doc = null;
98
// try {
99
// doc =start.getCloneableEditorSupport().openDocument();
100
// a = NbDocument.findLineOffset(doc, start.getLine());
101
// b = NbDocument.findLineOffset(doc, start.getLine()+1)-1;
102
// } catch (IOException ex) {
103
// ex.printStackTrace();
104
// }
105
// PositionBounds bounds = new PositionBounds(start, end);
106
// StringBuffer sb = new StringBuffer();
107
// try {
108
// sb.append(RetoucheUtils.getHtml(doc.getText(a,(int)start.getOffset()-a).trim()));
109
// sb.append(UI.getHTMLDiff(diff.getOldText(), diff.getNewText()));
110
// sb.append(RetoucheUtils.getHtml(doc.getText(end.getOffset(),b-end.getOffset()).trim()));
111
// } catch (BadLocationException ex) {
112
// ex.printStackTrace();
113
// }
114
// return new DiffElement(diff, bounds, sb.toString().trim(), fileObject, null);
115
// }
116

117     public static DiffElement create(Difference diff, FileObject fileObject, ModificationResult modification) {
118         PositionRef start = diff.getStartPosition();
119         PositionRef end = diff.getEndPosition();
120         StyledDocument JavaDoc doc = null;
121         PositionBounds bounds = new PositionBounds(start, end);
122         return new DiffElement(diff, bounds, fileObject, modification);
123     }
124 }
125
Popular Tags