This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 129949
Collapse All | Expand All

(-)a/java.source/apichanges.xml (+12 lines)
Lines 105-110 is the proper place. Link Here
105
    <!-- ACTUAL CHANGES BEGIN HERE: -->
105
    <!-- ACTUAL CHANGES BEGIN HERE: -->
106
106
107
    <changes>
107
    <changes>
108
        <change id="CommitToGuards">
109
            <api name="general"/>
110
            <summary>Added ModificationResult.Difference.guards(boolean) and isGuarded() methods permitting to write modifications even to guarded sections</summary>
111
            <version major="0" minor="32"/>
112
            <date day="12" month="3" year="2008"/>
113
            <author login="jpokorsky"/>
114
            <compatibility addition="yes" binary="compatible" deletion="no" deprecation="no" modification="no" semantic="compatible" source="compatible"/>
115
            <description>
116
                Added methods to allow ModificationResult to write changes into guarded sections of a document in case the API client
117
                is aware of what he does.
118
            </description>
119
        </change>
108
        <change id="PartialReparseAPI">
120
        <change id="PartialReparseAPI">
109
            <api name="general"/>
121
            <api name="general"/>
110
            <summary>Added CompilationInfo.getChangedTree method returning the reparsed subtree</summary>
122
            <summary>Added CompilationInfo.getChangedTree method returning the reparsed subtree</summary>
(-)a/java.source/src/org/netbeans/api/java/source/ModificationResult.java (-20 / +71 lines)
Lines 141-166 public final class ModificationResult { Link Here
141
                    for (Difference diff : differences) {
141
                    for (Difference diff : differences) {
142
                        if (diff.isExcluded())
142
                        if (diff.isExcluded())
143
                            continue;
143
                            continue;
144
                        try {
144
                        switch (diff.getKind()) {
145
                            switch (diff.getKind()) {
145
                            case INSERT:
146
                                case INSERT:
146
                            case REMOVE:
147
                                    doc.insertString(diff.getStartPosition().getOffset(), diff.getNewText(), null);
147
                            case CHANGE:
148
                                    break;
148
                                processDocument(doc, diff);
149
                                case REMOVE:
149
                                break;
150
                                    doc.remove(diff.getStartPosition().getOffset(), diff.getEndPosition().getOffset() - diff.getStartPosition().getOffset());
150
                            case CREATE:
151
                                    break;
151
                                createUnit(diff, out);
152
                                case CHANGE:
152
                                break;
153
                                    doc.remove(diff.getStartPosition().getOffset(), diff.getEndPosition().getOffset() - diff.getStartPosition().getOffset());
154
                                    doc.insertString(diff.getStartPosition().getOffset(), diff.getNewText(), null);
155
                                    break;
156
                                case CREATE:
157
                                    createUnit(diff, out);
158
                                    break;
159
                            }
160
                        } catch (BadLocationException ex) {
161
                            IOException ioe = new IOException();
162
                            ioe.initCause(ex);
163
                            throw ioe;
164
                        }
153
                        }
165
                    }
154
                    }
166
                    success = true;
155
                    success = true;
Lines 250-255 public final class ModificationResult { Link Here
250
                out.close();
239
                out.close();
251
        }            
240
        }            
252
    }
241
    }
242
    
243
    private void processDocument(final Document doc, final Difference diff) throws IOException {
244
        final BadLocationException[] blex = new BadLocationException[1];
245
        Runnable task = new Runnable() {
246
247
            public void run() {
248
                try {
249
                    processDocumentLocked(doc, diff);
250
                } catch (BadLocationException ex) {
251
                    blex[0] = ex;
252
                }
253
            }
254
        };
255
        if (doc instanceof BaseDocument) {
256
            if (diff.isGuarded()) {
257
                ((BaseDocument) doc).runAtomic(task);
258
            } else {
259
                ((BaseDocument) doc).runAtomicAsUser(task);
260
            }
261
        } else {
262
            task.run();
263
        }
264
        if (blex[0] != null) {
265
            IOException ioe = new IOException();
266
            ioe.initCause(blex[0]);
267
            throw ioe;
268
        }
269
    }
270
    
271
    private void processDocumentLocked(Document doc, Difference diff) throws BadLocationException {
272
        switch (diff.getKind()) {
273
            case INSERT:
274
                doc.insertString(diff.getStartPosition().getOffset(), diff.getNewText(), null);
275
                break;
276
            case REMOVE:
277
                doc.remove(diff.getStartPosition().getOffset(), diff.getEndPosition().getOffset() - diff.getStartPosition().getOffset());
278
                break;
279
            case CHANGE:
280
                doc.remove(diff.getStartPosition().getOffset(), diff.getEndPosition().getOffset() - diff.getStartPosition().getOffset());
281
                doc.insertString(diff.getStartPosition().getOffset(), diff.getNewText(), null);
282
                break;
283
        }
284
    }
253
285
254
    private void createUnit(Difference diff, Writer out) {
286
    private void createUnit(Difference diff, Writer out) {
255
        CreateChange change = (CreateChange) diff;
287
        CreateChange change = (CreateChange) diff;
Lines 307-312 public final class ModificationResult { Link Here
307
        String newText;
339
        String newText;
308
        String description;
340
        String description;
309
        private boolean excluded;
341
        private boolean excluded;
342
        private boolean ignoreGuards = false;
310
343
311
        Difference(Kind kind, PositionRef startPos, PositionRef endPos, String oldText, String newText, String description) {
344
        Difference(Kind kind, PositionRef startPos, PositionRef endPos, String oldText, String newText, String description) {
312
            this.kind = kind;
345
            this.kind = kind;
Lines 350-355 public final class ModificationResult { Link Here
350
            excluded = b;
383
            excluded = b;
351
        }
384
        }
352
385
386
        /**
387
         * Gets flag if it is possible to write to guarded sections.
388
         * @return {@code true} in case the difference may be written even into
389
         *          guarded sections.
390
         * @see #guards(boolean)
391
         */
392
        public boolean isGuarded() {
393
            return ignoreGuards;
394
        }
395
        
396
        /**
397
         * Sets flag if it is possible to write to guarded sections.
398
         * @param b flag if it is possible to write to guarded sections
399
         */
400
        public void guards(boolean b) {
401
            ignoreGuards = b;
402
        }
403
353
        @Override
404
        @Override
354
        public String toString() {
405
        public String toString() {
355
            return kind + "<" + startPos.getOffset() + ", " + endPos.getOffset() + ">: " + oldText + " -> " + newText;
406
            return kind + "<" + startPos.getOffset() + ", " + endPos.getOffset() + ">: " + oldText + " -> " + newText;
(-)a/java.source/test/unit/data/goldenfiles/org/netbeans/api/java/source/ModificationResultTest/testInsertToGuardedDocument.pass (+6 lines)
Line 0 Link Here
1
test
2
new-test1
3
test
4
new-test2
5
test
6
(-)a/java.source/test/unit/src/org/netbeans/api/java/source/ModificationResultTest.java (+31 lines)
Lines 48-53 import java.util.List; Link Here
48
import java.util.List;
48
import java.util.List;
49
import javax.swing.text.Document;
49
import javax.swing.text.Document;
50
import javax.swing.text.Position.Bias;
50
import javax.swing.text.Position.Bias;
51
import javax.swing.text.StyledDocument;
51
import org.netbeans.junit.NbTestCase;
52
import org.netbeans.junit.NbTestCase;
52
import org.openide.cookies.EditorCookie;
53
import org.openide.cookies.EditorCookie;
53
import org.openide.filesystems.FileLock;
54
import org.openide.filesystems.FileLock;
Lines 56-61 import org.openide.filesystems.FileUtil; Link Here
56
import org.openide.filesystems.FileUtil;
57
import org.openide.filesystems.FileUtil;
57
import org.openide.loaders.DataObject;
58
import org.openide.loaders.DataObject;
58
import org.openide.text.CloneableEditorSupport;
59
import org.openide.text.CloneableEditorSupport;
60
import org.openide.text.NbDocument;
59
import org.openide.text.PositionRef;
61
import org.openide.text.PositionRef;
60
62
61
/**
63
/**
Lines 142-147 public class ModificationResultTest exte Link Here
142
        compareReferenceFiles();
144
        compareReferenceFiles();
143
    }
145
    }
144
    
146
    
147
    private void performTestToGuardedDocument(String creator) throws Exception {
148
        prepareTest();
149
        
150
        StyledDocument doc = ces.openDocument();
151
        
152
        NbDocument.markGuarded(doc, 4, 6);
153
        
154
        Method m = ModificationResultTest.class.getDeclaredMethod(creator, new Class[0]);
155
        
156
        ModificationResult result = (ModificationResult) m.invoke(this, new Object[0]);
157
        
158
        for (FileObject fo : result.getModifiedFileObjects()) {
159
            for (ModificationResult.Difference diff : result.getDifferences(fo)) {
160
                diff.guards(true);
161
            }
162
        }
163
164
        
165
        result.commit();
166
        
167
        ref(doc.getText(0, doc.getLength()));
168
        
169
        compareReferenceFiles();
170
    }
171
    
145
    private ModificationResult prepareRemoveResult() throws Exception {
172
    private ModificationResult prepareRemoveResult() throws Exception {
146
        PositionRef start1 = ces.createPositionRef(5, Bias.Forward);
173
        PositionRef start1 = ces.createPositionRef(5, Bias.Forward);
147
        PositionRef end1 = ces.createPositionRef(9, Bias.Forward);
174
        PositionRef end1 = ces.createPositionRef(9, Bias.Forward);
Lines 198-203 public class ModificationResultTest exte Link Here
198
        performTestToDocument("prepareInsertResult");
225
        performTestToDocument("prepareInsertResult");
199
    }
226
    }
200
    
227
    
228
    public void testInsertToGuardedDocument() throws Exception {
229
        performTestToGuardedDocument("prepareInsertResult");
230
    }
231
    
201
    public void testRemoveFromFile() throws Exception {
232
    public void testRemoveFromFile() throws Exception {
202
        performTestToFile("prepareRemoveResult");
233
        performTestToFile("prepareRemoveResult");
203
    }
234
    }

Return to bug 129949