|
18 | 18 | import java.util.List;
|
19 | 19 | import java.util.logging.Level;
|
20 | 20 | import java.util.logging.Logger;
|
| 21 | +import javax.swing.SwingUtilities; |
21 | 22 | import javax.swing.text.BadLocationException;
|
22 | 23 | import javax.swing.text.Caret;
|
23 | 24 | import javax.swing.text.EditorKit;
|
|
30 | 31 | import org.openide.loaders.DataObject;
|
31 | 32 | import org.openide.util.Lookup;
|
32 | 33 | import org.openide.filesystems.FileObject;
|
| 34 | +import org.openide.text.NbDocument; |
33 | 35 | import org.openide.util.Utilities;
|
34 | 36 |
|
35 | 37 | public class StyledDocumentWriter {
|
@@ -109,62 +111,69 @@ public static void writeOnFileWithLines(File file, Charset charset, List<String>
|
109 | 111 | }
|
110 | 112 |
|
111 | 113 | public static void writeWithEditorKit(FileInfo info)
|
112 |
| - throws FileAccessException { |
| 114 | + throws FileAccessException, IOException { |
| 115 | + |
113 | 116 | EditorCookie cookie = info.getCookie();
|
| 117 | + StyledDocument openedDocument = cookie.openDocument(); |
114 | 118 | EditorKit kit = getEditorKit(info.getDataObject());
|
115 |
| - StyledDocument document = null; |
116 |
| - int caretPosition; |
117 |
| - |
118 |
| - try { |
119 |
| - document = cookie.openDocument(); |
120 |
| - } catch (IOException ex) { |
121 |
| - throw new FileAccessException("Document could not be loaded: " + ex.getMessage()); |
122 |
| - } |
123 | 119 |
|
124 | 120 | try (InputStream is = new ByteArrayInputStream(info.getContentAsBytes())) {
|
125 | 121 | // Backup caret position
|
126 |
| - Caret caret = info.getCaret(); |
127 |
| - caretPosition = info.getCurrentCaretPosition(); |
128 |
| - |
129 |
| - // Wipe document |
130 |
| - document.remove(0, document.getLength()); |
131 |
| - |
132 |
| - LOG.log(Level.INFO, "Write to \"is\": {0}", is); |
133 |
| - LOG.log(Level.INFO, "Write to \"document\": {0}", document); |
134 |
| - |
135 |
| - // Read input stream into the document (which is a "write" operation) |
136 |
| - kit.read(is, document, 0); |
137 |
| - cookie.saveDocument(); |
138 |
| - info.getFileObject().setAttribute(ENCODING_SETTING, info.getCharset().name()); |
139 |
| - |
140 |
| - // Reset caret positon |
141 |
| - caretPosition -= info.getCaretOffset(); |
142 |
| - if (caretPosition < document.getLength()) { |
143 |
| - LOG.log(Level.INFO, "Moving caret position to: {0} / {1}", |
144 |
| - new Object[]{caretPosition, document.getLength()}); |
145 |
| - caret.setDot(caretPosition); |
| 122 | + final Caret caret = info.getCaret(); |
| 123 | + if (caret == null) { |
| 124 | + LOG.log(Level.WARNING, "Could not get Caret"); |
| 125 | + return; |
146 | 126 | }
|
147 |
| - } catch (BadLocationException | IOException ex) { |
148 |
| - throw new FileAccessException("Document could not be saved: " + ex.getMessage()); |
149 |
| - } |
150 |
| - |
151 |
| - // Reformat code (to apply ident size & styles) |
152 |
| - // TODO: Do this only if CodeStylePreferences have been changed |
153 |
| - Reformat reformat = Reformat.get(document); |
154 |
| - reformat.lock(); |
155 |
| - |
156 |
| - try { |
157 |
| - reformat.reformat(0, document.getLength()); |
158 |
| - } catch (BadLocationException ex) { |
159 |
| - LOG.log(Level.SEVERE, "AutoFormat on document not possible: {0}", ex.getMessage()); |
160 |
| - } finally { |
161 |
| - reformat.unlock(); |
162 |
| - // Save document after reformat |
163 |
| - try { |
164 |
| - cookie.saveDocument(); |
165 |
| - } catch (IOException ex) { |
166 |
| - throw new FileAccessException("Document could not be saved: " + ex.getMessage()); |
| 127 | + int caretPosition = info.getCurrentCaretPosition(); |
| 128 | + Runnable runner = () -> { |
| 129 | + NbDocument.runAtomic(openedDocument, () -> { |
| 130 | + try { |
| 131 | + // Wipe document |
| 132 | + cookie.getDocument().remove(0, cookie.getDocument().getLength()); |
| 133 | + |
| 134 | + LOG.log(Level.INFO, "Write to \"is\": {0}", is); |
| 135 | + LOG.log(Level.INFO, "Write to \"document\": {0}", cookie.getDocument()); |
| 136 | + |
| 137 | + // Read input stream into the document (which is a "write" operation) |
| 138 | + kit.read(is, cookie.getDocument(), 0); |
| 139 | + cookie.saveDocument(); |
| 140 | + |
| 141 | + info.getFileObject().setAttribute(ENCODING_SETTING, info.getCharset().name()); |
| 142 | + |
| 143 | + // Reset caret positon |
| 144 | +// caretPosition -= info.getCaretOffset(); |
| 145 | + if (caretPosition < cookie.getDocument().getLength()) { |
| 146 | + LOG.log(Level.INFO, "Moving caret position from {0} to: {1} / {2}", |
| 147 | + new Object[]{info.getCaretOffset(), caretPosition, cookie.getDocument().getLength()}); |
| 148 | + caret.setDot(caretPosition); |
| 149 | + } |
| 150 | + |
| 151 | + // Reformat code (to apply ident size & styles) |
| 152 | + // TODO: Do this only if CodeStylePreferences have been changed |
| 153 | + Reformat reformat = Reformat.get(cookie.getDocument()); |
| 154 | + reformat.lock(); |
| 155 | + try { |
| 156 | + reformat.reformat(0, cookie.getDocument().getLength()); |
| 157 | + } catch (BadLocationException ex) { |
| 158 | + LOG.log(Level.SEVERE, "AutoFormat on document not possible: {0}", ex.getMessage()); |
| 159 | + } finally { |
| 160 | + reformat.unlock(); |
| 161 | + // Save document after reformat |
| 162 | + cookie.saveDocument(); |
| 163 | + } |
| 164 | + } catch (BadLocationException | IOException ex) { |
| 165 | + LOG.log(Level.SEVERE, "Document could not be saved: {0}", ex.getMessage()); |
| 166 | + } |
| 167 | + }); |
| 168 | + }; |
| 169 | + |
| 170 | + if (SwingUtilities.isEventDispatchThread()) { |
| 171 | + runner.run(); |
| 172 | + } else { |
| 173 | + SwingUtilities.invokeLater(runner); |
167 | 174 | }
|
| 175 | + } catch (IOException ex) { |
| 176 | + LOG.log(Level.SEVERE, "Could not load content of document: {0}", ex.getMessage()); |
168 | 177 | }
|
169 | 178 | }
|
170 | 179 |
|
|
0 commit comments