KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > DefaultPositionBoundsResolver


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
20
21 package org.netbeans.modules.j2ee.refactoring;
22
23 import java.io.BufferedReader JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.io.UnsupportedEncodingException JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import javax.swing.JEditorPane JavaDoc;
31 import javax.swing.SwingUtilities JavaDoc;
32 import javax.swing.text.BadLocationException JavaDoc;
33 import org.netbeans.editor.BaseDocument;
34 import org.openide.ErrorManager;
35 import org.openide.loaders.DataObject;
36 import org.openide.text.CloneableEditorSupport;
37 import org.openide.text.PositionBounds;
38 import javax.swing.text.Position.Bias;
39 import org.openide.nodes.Node;
40 import org.openide.text.PositionRef;
41
42 /**
43  * Resolves position bounds of an element that is being refactored. If the
44  * <code>elementName</code> is null or no matching string can be found in the data
45  * object, returns <code>PositionBounds</code> that represents the beginning of the file.
46  *
47  * @author Erno Mononen
48  */

49 public class DefaultPositionBoundsResolver {
50     
51     /**
52      * The data object that is being refactored.
53      */

54     private final DataObject dataObject;
55     /**
56      * The editor support associated with the data object.
57      */

58     private final CloneableEditorSupport editorSupport;
59     
60     /**
61      * Name of the element that is being refactored. If the element represents class,
62      * it should be its fully qualified name.
63      */

64     private final String JavaDoc elementName;
65     
66     /**
67      * Creates a new resolver for the given <code>dataObject</code>.
68      * @param dataObject the data object that is being refactored. Must have an
69      * associated <code>CloneableEditorSupport</code> and must not be null.
70      * @param elementName the element that is being refactored. If the element represents class,
71      * it should be its fully qualified name. If a <code>null</code> is given, {@link #getPositionBounds}
72      * returns PositionBounds that represents the beginning of the file.
73      * @throws IllegalArgumentException if the given <code>dataObject</code> was null
74      * or if didn't have CloneableEditorSupport associated.
75      */

76     public DefaultPositionBoundsResolver(DataObject dataObject, String JavaDoc elementName) {
77         if (dataObject == null){
78             throw new IllegalArgumentException JavaDoc("DataObject must be given."); // NO18N
79
}
80         this.dataObject = dataObject;
81         this.editorSupport = findCloneableEditorSupport();
82         if (editorSupport == null){
83             throw new IllegalArgumentException JavaDoc("Couldn't get CloneableEditorSupport for " + dataObject); //NO18N
84
}
85         this.elementName = elementName;
86     }
87     
88     /**
89      *@return PositionBounds representing the position of the name of the entity that is being
90      * refactored or PostionBounds representing the start of the file if the position
91      * of the entity could not be resolved.
92      */

93     public PositionBounds getPositionBounds(){
94         if (elementName != null){
95             try {
96                 BaseDocument doc = getDocument();
97                 String JavaDoc text = doc.getText(0, doc.getLength());
98                 int offset = text.indexOf(elementName);
99                 if (offset > -1){
100                     PositionRef start = editorSupport.createPositionRef(offset, Bias.Forward);
101                     PositionRef end = editorSupport.createPositionRef(offset + elementName.length(), Bias.Backward);
102                     return new PositionBounds(start, end);
103                 }
104             } catch (BadLocationException JavaDoc ex) {
105                 ErrorManager.getDefault().notify(ex);
106             }
107         }
108         return getDefaultPositionBounds();
109     }
110     
111     /**
112      * @return PositionBounds representing the beginning of the file associated
113      * with the <code>dataObject</code>.
114      */

115     private PositionBounds getDefaultPositionBounds(){
116         PositionRef start = editorSupport.createPositionRef(0, Bias.Forward);
117         PositionRef end = editorSupport.createPositionRef(0, Bias.Backward);
118         return new PositionBounds(start, end);
119     }
120     
121     
122     // adapted from JSFEditorUtilities
123
private CloneableEditorSupport findCloneableEditorSupport() {
124         Node.Cookie obj = dataObject.getCookie(org.openide.cookies.OpenCookie.class);
125         if (obj instanceof CloneableEditorSupport) {
126             return (CloneableEditorSupport)obj;
127         }
128         obj = dataObject.getCookie(org.openide.cookies.EditorCookie.class);
129         if (obj instanceof CloneableEditorSupport) {
130             return (CloneableEditorSupport)obj;
131         }
132         return null;
133     }
134     
135     /**
136      * @return the BaseDocument representing contents of our <code>dataObject</code>.
137      */

138     private BaseDocument getDocument(){
139         BaseDocument result = (BaseDocument) editorSupport.getDocument();
140         // editor was not opened
141
if (result == null) {
142             final CreateXMLPane runnable = new CreateXMLPane();
143             try {
144                 if (SwingUtilities.isEventDispatchThread()){
145                     runnable.run();
146                 } else {
147                     SwingUtilities.invokeAndWait(runnable);
148                 }
149                 result = new BaseDocument(runnable.getPane().getEditorKit().getClass(), false);
150                 String JavaDoc text= readResource(dataObject.getPrimaryFile().getInputStream());
151                 result.remove(0, result.getLength());
152                 result.insertString(0, text, null);
153             } catch (InterruptedException JavaDoc ex) {
154                 ErrorManager.getDefault().notify(ex);
155             } catch (InvocationTargetException JavaDoc ex) {
156                 ErrorManager.getDefault().notify(ex);
157             } catch (FileNotFoundException JavaDoc ex) {
158                 ErrorManager.getDefault().notify(ex);
159             } catch (IOException JavaDoc ex) {
160                 ErrorManager.getDefault().notify(ex);
161             } catch (BadLocationException JavaDoc ex) {
162                 ErrorManager.getDefault().notify(ex);
163             }
164         }
165         return result;
166         
167     }
168     
169     /**
170      * Reads the given stream.
171      */

172     private String JavaDoc readResource(InputStream JavaDoc stream){
173         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
174         String JavaDoc lineSep = System.getProperty("line.separator");//NO18N
175
BufferedReader JavaDoc reader = null;
176         try {
177             reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(stream, "UTF-8"));//NO18N
178
String JavaDoc line = reader.readLine();
179             while (line != null) {
180                 result.append(line);
181                 result.append(lineSep);
182                 line = reader.readLine();
183             }
184         } catch (UnsupportedEncodingException JavaDoc ex) {
185             ErrorManager.getDefault().notify(ex);
186         } catch (IOException JavaDoc ex) {
187             ErrorManager.getDefault().notify(ex);
188         } finally {
189             try {
190                 if (reader != null){
191                     reader.close();
192                 }
193             } catch (IOException JavaDoc ex) {
194                 ErrorManager.getDefault().notify(ex);
195             }
196         }
197         return result.toString();
198     }
199     
200     // copied from JSFFrameworkProvider
201
private static class CreateXMLPane implements Runnable JavaDoc{
202         JEditorPane JavaDoc pane;
203         
204         public void run(){
205             pane = new JEditorPane JavaDoc("text/xml", "");
206         }
207         
208         public JEditorPane JavaDoc getPane(){
209             return pane;
210         }
211     }
212     
213 }
Popular Tags