1 19 package org.netbeans.modules.xml.core.text; 20 21 import java.awt.Point ; 22 import java.awt.Component ; 23 24 import javax.swing.JEditorPane ; 25 import javax.swing.JViewport ; 26 import javax.swing.text.StyledDocument ; 27 28 import org.openide.*; 29 import org.openide.text.*; 30 import org.openide.nodes.*; 31 import org.openide.cookies.*; 32 33 import org.netbeans.modules.xml.core.*; 34 import org.netbeans.modules.xml.core.sync.*; 35 36 43 public class XMLTextRepresentation extends TextRepresentation { 44 45 46 public XMLTextRepresentation(TextEditorSupport editor, Synchronizator sync) { 47 super(editor, sync); 48 } 49 50 51 56 public void updateText (Object input) { 57 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("XMLTextRepresentation::updateText"); 59 final String in = (String ) input; 60 61 try { 62 63 EditorCookie es = editor; 64 if (es != null) { 65 66 StyledDocument tmpdoc = es.getDocument(); 67 if (tmpdoc == null) 68 tmpdoc = es.openDocument(); 69 70 72 JEditorPane [] eps = es.getOpenedPanes(); 73 JEditorPane pane = null; 74 JViewport port = null; 75 int caretPosition = 0; 76 Point viewPosition = null; 77 if (eps != null) { 78 pane = eps[0]; 79 caretPosition = pane.getCaretPosition(); 80 port = getParentViewport (pane); 81 if (port != null) 82 viewPosition = port.getViewPosition(); 83 } 84 85 87 final Exception [] taskEx = new Exception [] {null}; 88 final StyledDocument sdoc = tmpdoc; 89 90 Runnable task = new Runnable () { 91 public void run() { 92 try { 93 sdoc.remove (0, sdoc.getLength()); 95 sdoc.insertString (0, in, null); 97 } catch (Exception iex) { 98 taskEx[0] = iex; 99 } 100 } 101 }; 102 103 105 org.openide.text.NbDocument.runAtomicAsUser(sdoc, task); 106 107 109 111 if (eps != null) { 112 try { 113 pane.setCaretPosition (caretPosition); 114 } catch (IllegalArgumentException e) { 115 } 116 port.setViewPosition (viewPosition); 117 } 118 119 if (taskEx[0]!=null) { 120 throw taskEx[0]; 121 } 122 123 } 125 } catch (Exception e) { 126 ErrorManager.getDefault().notify(e); 127 } 128 } 129 130 131 134 public void update(Object change) { 135 if (change instanceof String ) { 136 String update = (String ) change; 137 updateText(update); 138 } 139 } 140 141 144 public boolean isModified() { 145 return false; } 147 148 149 private JViewport getParentViewport (JEditorPane component) { 150 Component pc = component.getParent(); 151 return (pc instanceof JViewport ) ? (JViewport )pc : null; 152 } 153 154 } 155 | Popular Tags |