KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > multiview > Utils


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 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 JavaDoc;
28 import javax.swing.text.StyledDocument JavaDoc;
29 import javax.swing.text.BadLocationException JavaDoc;
30 import java.awt.*;
31
32 /**
33  * Utils.java
34  *
35  * Created on November 16, 2004, 3:21 PM
36  * @author mkuchtiak
37  */

38 public class Utils {
39     private static final int WAIT_FINISHED_TIMEOUT = 10000;
40
41     /** This method update document in editor after change in beans hierarchy.
42      * It takes old document and new document in String.
43      * To preserve changes outside of root element only root element is replaced.
44      * To avoid regeneration of whole document in text editor following steps are done:
45      * 1) compare the begin of both documents (old one and new one)
46      * - find the first position where both documents differ
47      * 2) do the same from the ends of documents
48      * 3) remove old middle part of text (modified part) and insert new one
49      *
50      * @param doc original document
51      * @param newDoc new value of whole document
52      */

53     public static boolean replaceDocument(final StyledDocument JavaDoc doc, final String JavaDoc newDoc) {
54         if (doc == null) {
55             return true;
56         }
57         Runnable JavaDoc runnable = new Runnable JavaDoc() {
58             public void run() {
59                 try {
60                     String JavaDoc origDocument = filterEndLines(doc.getText(0, doc.getLength()));
61                     String JavaDoc newDocument = filterEndLines(newDoc);
62
63                     if (origDocument.equals(newDocument)) {
64                         // no change in document
65
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 JavaDoc s = newDocument.substring(offset, tailIndex + delta);
89                     int length = tailIndex - offset;
90                     if (doc instanceof AbstractDocument JavaDoc) {
91                         ((AbstractDocument JavaDoc) 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 JavaDoc e) {
101                     ErrorManager.getDefault().notify(e);
102                 }
103             }
104         };
105         NbDocument.runAtomic(doc, runnable);
106         return true;
107     }
108
109     /** Filter characters #13 (CR) from the specified String
110      * @param str original string
111      * @return the string without #13 characters
112      */

113     private static String JavaDoc filterEndLines(String JavaDoc 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 JavaDoc(text, 0, pos);
129     }
130
131     /**
132      * Sets focus to the next focusable component according to focus traversal policy
133      * @param component currently focused component
134      */

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     /**
151      * Scroll panel to make the component visible
152      * @param component
153      */

154     public static void scrollToVisible(final JComponent component) {
155         SwingUtilities.invokeLater(new Runnable JavaDoc() {
156             public void run() {
157                 component.scrollRectToVisible(new Rectangle(10, component.getHeight()));
158             }
159         });
160     }
161
162     /**
163      * Make sure that the code will run in AWT dispatch thread
164      * @param runnable
165      */

166     public static void runInAwtDispatchThread(Runnable JavaDoc runnable) {
167         if (SwingUtilities.isEventDispatchThread()) {
168             runnable.run();
169         } else {
170             SwingUtilities.invokeLater(runnable);
171         }
172
173     }
174
175     /**
176      * Utility that sets border and traversal keys for JTextArea in JTextField style
177      */

178     public static void makeTextAreaLikeTextField(javax.swing.JTextArea JavaDoc ta, javax.swing.JTextField JavaDoc 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 JavaDoc e) {
191                 ErrorManager.getDefault().notify(e);
192             }
193         }
194     }
195
196 }
197
Popular Tags