1 19 20 package org.netbeans.modules.xml.multiview; 21 22 import org.openide.ErrorManager; 23 import org.openide.text.NbDocument; 24 import org.openide.util.RequestProcessor; 25 26 import javax.swing.*; 27 import javax.swing.text.AbstractDocument ; 28 import javax.swing.text.StyledDocument ; 29 import javax.swing.text.BadLocationException ; 30 import java.awt.*; 31 32 38 public class Utils { 39 private static final int WAIT_FINISHED_TIMEOUT = 10000; 40 41 53 public static boolean replaceDocument(final StyledDocument doc, final String newDoc) { 54 if (doc == null) { 55 return true; 56 } 57 Runnable runnable = new Runnable () { 58 public void run() { 59 try { 60 String origDocument = filterEndLines(doc.getText(0, doc.getLength())); 61 String newDocument = filterEndLines(newDoc); 62 63 if (origDocument.equals(newDocument)) { 64 return; 66 } 67 68 char[] origChars = origDocument.toCharArray(); 69 char[] newcChars = newDocument.toCharArray(); 70 int tailIndex = origChars.length; 71 int delta = newcChars.length - tailIndex; 72 int n = delta < 0 ? tailIndex + delta : tailIndex; 73 int offset; 74 for (offset = 0; offset < n; offset++) { 75 if (origChars[offset] != newcChars[offset]) { 76 break; 77 } 78 } 79 n = delta < 0 ? offset - delta : offset; 80 for (int i = tailIndex - 1; i >= n; i--) { 81 if (origChars[i] == newcChars[i + delta]) { 82 tailIndex = i; 83 } else { 84 break; 85 } 86 } 87 88 String s = newDocument.substring(offset, tailIndex + delta); 89 int length = tailIndex - offset; 90 if (doc instanceof AbstractDocument ) { 91 ((AbstractDocument ) doc).replace(offset, length, s, null); 92 } else { 93 if (length > 0) { 94 doc.remove(offset, length); 95 } 96 if (s.length() > 0) { 97 doc.insertString(offset, s, null); 98 } 99 } 100 } catch (BadLocationException e) { 101 ErrorManager.getDefault().notify(e); 102 } 103 } 104 }; 105 NbDocument.runAtomic(doc, runnable); 106 return true; 107 } 108 109 113 private static String filterEndLines(String str) { 114 char[] text = str.toCharArray(); 115 if (text.length == 0) { 116 return ""; 117 } 118 int pos = 0; 119 for (int i = 0; i < text.length; i++) { 120 char c = text[i]; 121 if (c != 13) { 122 if (pos != i) { 123 text[pos] = c; 124 } 125 pos++; 126 } 127 } 128 return new String (text, 0, pos); 129 } 130 131 135 public static void focusNextComponent(Component component) { 136 Container focusCycleRoot = component.getFocusCycleRootAncestor(); 137 if (focusCycleRoot == null) { 138 return; 139 } 140 final FocusTraversalPolicy focusTraversalPolicy = focusCycleRoot.getFocusTraversalPolicy(); 141 if (focusTraversalPolicy == null) { 142 return; 143 } 144 final Component componentAfter = focusTraversalPolicy.getComponentAfter(focusCycleRoot, component); 145 if (componentAfter != null) { 146 componentAfter.requestFocus(); 147 } 148 } 149 150 154 public static void scrollToVisible(final JComponent component) { 155 SwingUtilities.invokeLater(new Runnable () { 156 public void run() { 157 component.scrollRectToVisible(new Rectangle(10, component.getHeight())); 158 } 159 }); 160 } 161 162 166 public static void runInAwtDispatchThread(Runnable runnable) { 167 if (SwingUtilities.isEventDispatchThread()) { 168 runnable.run(); 169 } else { 170 SwingUtilities.invokeLater(runnable); 171 } 172 173 } 174 175 178 public static void makeTextAreaLikeTextField(javax.swing.JTextArea ta, javax.swing.JTextField tf) { 179 ta.setBorder(tf.getBorder()); 180 ta.setFocusTraversalKeys(java.awt.KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, 181 tf.getFocusTraversalKeys(java.awt.KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS)); 182 ta.setFocusTraversalKeys(java.awt.KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, 183 tf.getFocusTraversalKeys(java.awt.KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS)); 184 } 185 186 public static void waitFinished(RequestProcessor.Task task) { 187 if (task.getDelay() > 0 && !task.isFinished()) { 188 try { 189 task.waitFinished(WAIT_FINISHED_TIMEOUT); 190 } catch (InterruptedException e) { 191 ErrorManager.getDefault().notify(e); 192 } 193 } 194 } 195 196 } 197 | Popular Tags |