1 19 20 21 package org.netbeans.modules.j2ee.refactoring; 22 23 import java.io.BufferedReader ; 24 import java.io.FileNotFoundException ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 import java.io.UnsupportedEncodingException ; 29 import java.lang.reflect.InvocationTargetException ; 30 import javax.swing.JEditorPane ; 31 import javax.swing.SwingUtilities ; 32 import javax.swing.text.BadLocationException ; 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 49 public class DefaultPositionBoundsResolver { 50 51 54 private final DataObject dataObject; 55 58 private final CloneableEditorSupport editorSupport; 59 60 64 private final String elementName; 65 66 76 public DefaultPositionBoundsResolver(DataObject dataObject, String elementName) { 77 if (dataObject == null){ 78 throw new IllegalArgumentException ("DataObject must be given."); } 80 this.dataObject = dataObject; 81 this.editorSupport = findCloneableEditorSupport(); 82 if (editorSupport == null){ 83 throw new IllegalArgumentException ("Couldn't get CloneableEditorSupport for " + dataObject); } 85 this.elementName = elementName; 86 } 87 88 93 public PositionBounds getPositionBounds(){ 94 if (elementName != null){ 95 try { 96 BaseDocument doc = getDocument(); 97 String 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 ex) { 105 ErrorManager.getDefault().notify(ex); 106 } 107 } 108 return getDefaultPositionBounds(); 109 } 110 111 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 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 138 private BaseDocument getDocument(){ 139 BaseDocument result = (BaseDocument) editorSupport.getDocument(); 140 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 text= readResource(dataObject.getPrimaryFile().getInputStream()); 151 result.remove(0, result.getLength()); 152 result.insertString(0, text, null); 153 } catch (InterruptedException ex) { 154 ErrorManager.getDefault().notify(ex); 155 } catch (InvocationTargetException ex) { 156 ErrorManager.getDefault().notify(ex); 157 } catch (FileNotFoundException ex) { 158 ErrorManager.getDefault().notify(ex); 159 } catch (IOException ex) { 160 ErrorManager.getDefault().notify(ex); 161 } catch (BadLocationException ex) { 162 ErrorManager.getDefault().notify(ex); 163 } 164 } 165 return result; 166 167 } 168 169 172 private String readResource(InputStream stream){ 173 StringBuffer result = new StringBuffer (); 174 String lineSep = System.getProperty("line.separator"); BufferedReader reader = null; 176 try { 177 reader = new BufferedReader (new InputStreamReader (stream, "UTF-8")); String line = reader.readLine(); 179 while (line != null) { 180 result.append(line); 181 result.append(lineSep); 182 line = reader.readLine(); 183 } 184 } catch (UnsupportedEncodingException ex) { 185 ErrorManager.getDefault().notify(ex); 186 } catch (IOException ex) { 187 ErrorManager.getDefault().notify(ex); 188 } finally { 189 try { 190 if (reader != null){ 191 reader.close(); 192 } 193 } catch (IOException ex) { 194 ErrorManager.getDefault().notify(ex); 195 } 196 } 197 return result.toString(); 198 } 199 200 private static class CreateXMLPane implements Runnable { 202 JEditorPane pane; 203 204 public void run(){ 205 pane = new JEditorPane ("text/xml", ""); 206 } 207 208 public JEditorPane getPane(){ 209 return pane; 210 } 211 } 212 213 } | Popular Tags |