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 221844
Collapse All | Expand All

(-)a/editor.guards/apichanges.xml (+17 lines)
Lines 108-113 Link Here
108
    <!-- ACTUAL CHANGES BEGIN HERE: -->
108
    <!-- ACTUAL CHANGES BEGIN HERE: -->
109
109
110
    <changes>
110
    <changes>
111
        <change id="AbstractGuardedSectionsProvider-useReadersWritersOnSet">
112
            <api name="general"/>
113
            <summary>Ability run guarded readers/writers when the content of the guarded section's content is set</summary>
114
            <version major="1" minor="20"/>
115
            <date day="23" month="11" year="2012"/>
116
            <author login="jlahoda"/>
117
            <compatibility addition="yes" binary="compatible" deletion="no" deprecation="no" modification="no"/>
118
            <description>
119
                <p>When this option is set, setting the content of a GuardedSection will pass the data through the given
120
                guarded writer and back through the given guarded reader, to ensure the result is the same
121
                as if it would be read from the disk.</p>
122
                <p>Note that this new mode is not fully compatible with the original mode, e.g. all the set methods
123
                of all the GuardedSection classes will throw IllegalStateException if invoked inside the write&amp;read part.</p>
124
            </description>
125
            <issue number="221844"/>
126
        </change>
127
        
111
        <change id="GuardedSectionsProvider-Charset">
128
        <change id="GuardedSectionsProvider-Charset">
112
            <api name="general"/>
129
            <api name="general"/>
113
            <summary>GuardedSectionsProvider supports Charset</summary>
130
            <summary>GuardedSectionsProvider supports Charset</summary>
(-)a/editor.guards/manifest.mf (-1 / +1 lines)
Lines 1-5 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.netbeans.modules.editor.guards/1
2
OpenIDE-Module: org.netbeans.modules.editor.guards/1
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/editor/guards/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/editor/guards/Bundle.properties
4
OpenIDE-Module-Specification-Version: 1.19
4
OpenIDE-Module-Specification-Version: 1.20
5
5
(-)a/editor.guards/src/org/netbeans/api/editor/guards/GuardedSection.java (-8 / +27 lines)
Lines 43-48 Link Here
43
 */
43
 */
44
package org.netbeans.api.editor.guards;
44
package org.netbeans.api.editor.guards;
45
45
46
import org.netbeans.modules.editor.guards.OffsetPosition;
46
import java.beans.PropertyVetoException;
47
import java.beans.PropertyVetoException;
47
import javax.swing.text.Position;
48
import javax.swing.text.Position;
48
import org.netbeans.modules.editor.guards.GuardedSectionImpl;
49
import org.netbeans.modules.editor.guards.GuardedSectionImpl;
Lines 50-58 Link Here
50
/**
51
/**
51
 * Represents one guarded section.
52
 * Represents one guarded section.
52
 */
53
 */
53
public class GuardedSection {
54
public abstract class GuardedSection {
54
    
55
    
55
    private final GuardedSectionImpl impl;
56
    private final GuardedSectionImpl impl;
57
    private final GuardedSection delegate;
58
            final int offset;
56
59
57
    /**
60
    /**
58
     * Creates new section.
61
     * Creates new section.
Lines 62-67 Link Here
62
        assert impl != null;
65
        assert impl != null;
63
        this.impl = impl;
66
        this.impl = impl;
64
        impl.attach(this);
67
        impl.attach(this);
68
        this.delegate = null;
69
        this.offset = 0;
70
    }
71
    
72
    GuardedSection(GuardedSection delegate, int offset) {
73
        this.impl = null;
74
        this.delegate = delegate;
75
        this.offset = offset;
65
    }
76
    }
66
    
77
    
67
    /**
78
    /**
Lines 69-75 Link Here
69
     * @return the name
80
     * @return the name
70
     */
81
     */
71
    public String getName() {
82
    public String getName() {
72
        return impl.getName();
83
        return impl != null ? impl.getName() : delegate.getName();
73
    }
84
    }
74
85
75
    /**
86
    /**
Lines 78-83 Link Here
78
     * @exception PropertyVetoException if the new name is already in use
89
     * @exception PropertyVetoException if the new name is already in use
79
     */
90
     */
80
    public void setName(String name) throws PropertyVetoException {
91
    public void setName(String name) throws PropertyVetoException {
92
        if (impl == null) throw new IllegalStateException();
81
        impl.setName(name);
93
        impl.setName(name);
82
    }
94
    }
83
95
Lines 87-92 Link Here
87
     * and it will be impossible to use its methods.
99
     * and it will be impossible to use its methods.
88
     */
100
     */
89
    public void deleteSection() {
101
    public void deleteSection() {
102
        if (impl == null) throw new IllegalStateException();
90
        impl.deleteSection();
103
        impl.deleteSection();
91
    }
104
    }
92
105
Lines 95-101 Link Here
95
     * source.
108
     * source.
96
     */
109
     */
97
    public boolean isValid() {
110
    public boolean isValid() {
98
        return impl.isValid();
111
        return impl != null ? impl.isValid() : delegate.isValid();
99
    }
112
    }
100
113
101
    /**
114
    /**
Lines 104-109 Link Here
104
     * instead of calling NbDocument.
117
     * instead of calling NbDocument.
105
     */
118
     */
106
    public void removeSection() {
119
    public void removeSection() {
120
        if (impl == null) throw new IllegalStateException();
107
        impl.removeSection();
121
        impl.removeSection();
108
    }
122
    }
109
    
123
    
Lines 113-119 Link Here
113
     * @return the position to place the caret.
127
     * @return the position to place the caret.
114
     */
128
     */
115
    public Position getCaretPosition() {
129
    public Position getCaretPosition() {
116
        return impl.getCaretPosition();
130
        return impl != null ? impl.getCaretPosition() : new OffsetPosition(delegate.getCaretPosition(), offset);
117
    }
131
    }
118
    
132
    
119
    /**
133
    /**
Lines 121-127 Link Here
121
     * @return The text contained in the section.
135
     * @return The text contained in the section.
122
     */
136
     */
123
    public String getText() {
137
    public String getText() {
124
        return impl.getText();
138
        return impl != null ? impl.getText() : delegate.getText();
125
    }
139
    }
126
140
127
    /**
141
    /**
Lines 134-140 Link Here
134
     * @return <code>true</code> if the position is inside section.
148
     * @return <code>true</code> if the position is inside section.
135
     */
149
     */
136
    public boolean contains(Position pos, boolean permitHoles) {
150
    public boolean contains(Position pos, boolean permitHoles) {
137
        return impl.contains(pos, permitHoles);
151
        return impl != null ? impl.contains(pos, permitHoles) : delegate.contains(new OffsetPosition(pos, -offset), permitHoles);
138
    }
152
    }
139
    
153
    
140
    /**
154
    /**
Lines 142-148 Link Here
142
     * @return the end position of the guarded section.
156
     * @return the end position of the guarded section.
143
     */
157
     */
144
    public Position getEndPosition() {
158
    public Position getEndPosition() {
145
        return impl.getEndPosition();
159
        return impl != null ? impl.getEndPosition() : new OffsetPosition(delegate.getEndPosition(), offset);
146
    }
160
    }
147
    
161
    
148
    /** 
162
    /** 
Lines 150-160 Link Here
150
     * @return the start position of the guarded section.
164
     * @return the start position of the guarded section.
151
     */
165
     */
152
    public Position getStartPosition() {
166
    public Position getStartPosition() {
153
        return impl.getStartPosition();
167
        return impl != null ? impl.getStartPosition() : new OffsetPosition(delegate.getStartPosition(), offset);
154
    }
168
    }
155
    
169
    
156
    GuardedSectionImpl getImpl() {
170
    GuardedSectionImpl getImpl() {
157
        return impl;
171
        return impl;
158
    }
172
    }
159
173
174
    GuardedSection getDelegate() {
175
        return delegate;
176
    }
177
178
    abstract GuardedSection clone(int offset);
160
}
179
}
(-)a/editor.guards/src/org/netbeans/api/editor/guards/GuardedSectionManager.java (+5 lines)
Lines 156-161 Link Here
156
            public GuardedSectionImpl getImpl(GuardedSection gs) {
156
            public GuardedSectionImpl getImpl(GuardedSection gs) {
157
                return gs.getImpl();
157
                return gs.getImpl();
158
            }
158
            }
159
160
            @Override
161
            public GuardedSection clone(GuardedSection gs, int offset) {
162
                return gs.clone(offset);
163
            }
159
            
164
            
160
        };
165
        };
161
    }
166
    }
(-)a/editor.guards/src/org/netbeans/api/editor/guards/InteriorSection.java (+23 lines)
Lines 43-48 Link Here
43
 */
43
 */
44
package org.netbeans.api.editor.guards;
44
package org.netbeans.api.editor.guards;
45
45
46
import org.netbeans.modules.editor.guards.OffsetPosition;
46
import javax.swing.text.Position;
47
import javax.swing.text.Position;
47
import org.netbeans.modules.editor.guards.InteriorSectionImpl;
48
import org.netbeans.modules.editor.guards.InteriorSectionImpl;
48
49
Lines 60-70 Link Here
60
        super(impl);
61
        super(impl);
61
    }
62
    }
62
63
64
    InteriorSection(GuardedSection delegate, int offset) {
65
        super(delegate, offset);
66
    }
67
    
63
    /**
68
    /**
64
     * Sets the text of the body.
69
     * Sets the text of the body.
65
     * @param text the new text
70
     * @param text the new text
66
     */
71
     */
67
    public void setBody(String text) {
72
    public void setBody(String text) {
73
        if (getImpl() == null) throw new IllegalStateException();
68
        getImpl().setBody(text);
74
        getImpl().setBody(text);
69
    }
75
    }
70
    
76
    
Lines 74-79 Link Here
74
     * @return contents of the body or null, if the section is not valid.
80
     * @return contents of the body or null, if the section is not valid.
75
     */
81
     */
76
    public String getBody() {
82
    public String getBody() {
83
        if (getImpl() == null) return getDelegate().getBody();
77
        return getImpl().getBody();
84
        return getImpl().getBody();
78
    }
85
    }
79
86
Lines 82-87 Link Here
82
     * @param text the new text
89
     * @param text the new text
83
     */
90
     */
84
    public void setHeader(String text) {
91
    public void setHeader(String text) {
92
        if (getImpl() == null) throw new IllegalStateException();
85
        getImpl().setHeader(text);
93
        getImpl().setHeader(text);
86
    }
94
    }
87
95
Lines 91-96 Link Here
91
     * @return contents of the header or null, if the section is not valid.
99
     * @return contents of the header or null, if the section is not valid.
92
     */
100
     */
93
    public String getHeader() {
101
    public String getHeader() {
102
        if (getImpl() == null) return getDelegate().getHeader();
94
        return getImpl().getHeader();
103
        return getImpl().getHeader();
95
    }
104
    }
96
105
Lines 102-107 Link Here
102
     * @param text the new text
111
     * @param text the new text
103
     */
112
     */
104
    public void setFooter(String text) {
113
    public void setFooter(String text) {
114
        if (getImpl() == null) throw new IllegalStateException();
105
        getImpl().setFooter(text);
115
        getImpl().setFooter(text);
106
    }
116
    }
107
117
Lines 111-116 Link Here
111
     * @return contents of the footer part, or null if the section is not valid.
121
     * @return contents of the footer part, or null if the section is not valid.
112
     */
122
     */
113
    public String getFooter() {
123
    public String getFooter() {
124
        if (getImpl() == null) return getDelegate().getFooter();
114
        return getImpl().getFooter();
125
        return getImpl().getFooter();
115
    }
126
    }
116
    
127
    
Lines 119-124 Link Here
119
     * @return the start position of the body part
130
     * @return the start position of the body part
120
     */
131
     */
121
    public Position getBodyStartPosition() {
132
    public Position getBodyStartPosition() {
133
        if (getImpl() == null) return new OffsetPosition(getDelegate().getBodyStartPosition(), offset);
122
        return getImpl().getBodyStartPosition();
134
        return getImpl().getBodyStartPosition();
123
    }
135
    }
124
    
136
    
Lines 127-132 Link Here
127
     * @return the end position of the body part
139
     * @return the end position of the body part
128
     */
140
     */
129
    public Position getBodyEndPosition() {
141
    public Position getBodyEndPosition() {
142
        if (getImpl() == null) return new OffsetPosition(getDelegate().getBodyEndPosition(), offset);
130
        return getImpl().getBodyEndPosition();
143
        return getImpl().getBodyEndPosition();
131
    }
144
    }
132
    
145
    
Lines 154-157 Link Here
154
      }
167
      }
155
      return buf.toString();
168
      return buf.toString();
156
    }*/
169
    }*/
170
171
    @Override
172
    InteriorSection getDelegate() {
173
        return (InteriorSection) super.getDelegate();
174
    }
175
176
    @Override
177
    GuardedSection clone(int offset) {
178
        return new InteriorSection(this, offset);
179
    }
157
}
180
}
(-)a/editor.guards/src/org/netbeans/api/editor/guards/SimpleSection.java (+10 lines)
Lines 61-71 Link Here
61
        super(impl);
61
        super(impl);
62
    }
62
    }
63
63
64
    SimpleSection(GuardedSection delegate, int offset) {
65
        super(delegate, offset);
66
    }
67
64
    /**
68
    /**
65
     * Sets the text of the section.
69
     * Sets the text of the section.
66
     * @param text the new text
70
     * @param text the new text
67
     */
71
     */
68
    public void setText(String text) {
72
    public void setText(String text) {
73
        if (getImpl() == null) throw new IllegalStateException();
69
        getImpl().setText(text);
74
        getImpl().setText(text);
70
    }
75
    }
71
76
Lines 87-90 Link Here
87
    SimpleSectionImpl getImpl() {
92
    SimpleSectionImpl getImpl() {
88
        return (SimpleSectionImpl) super.getImpl();
93
        return (SimpleSectionImpl) super.getImpl();
89
    }
94
    }
95
96
    @Override
97
    GuardedSection clone(int offset) {
98
        return new SimpleSection(this, offset);
99
    }
90
}
100
}
(-)a/editor.guards/src/org/netbeans/modules/editor/guards/GuardedReader.java (-1 / +1 lines)
Lines 189-195 Link Here
189
            isClosed = true;
189
            isClosed = true;
190
            reader.close();
190
            reader.close();
191
            if (this.result != null) {
191
            if (this.result != null) {
192
                callback.fillSections(this.result.getGuardedSections(), newLineStream.getNewLineType());
192
                callback.fillSections(gr, this.result.getGuardedSections(), newLineStream.getNewLineType());
193
            }
193
            }
194
        }
194
        }
195
    }
195
    }
(-)a/editor.guards/src/org/netbeans/modules/editor/guards/GuardedSectionImpl.java (-1 / +20 lines)
Lines 46-55 Link Here
46
46
47
import java.beans.PropertyChangeEvent;
47
import java.beans.PropertyChangeEvent;
48
import java.beans.PropertyVetoException;
48
import java.beans.PropertyVetoException;
49
import java.io.ByteArrayOutputStream;
50
import java.nio.charset.Charset;
51
import java.util.Collections;
52
import java.util.List;
49
import javax.swing.text.BadLocationException;
53
import javax.swing.text.BadLocationException;
50
import javax.swing.text.Position;
54
import javax.swing.text.Position;
51
import javax.swing.text.StyledDocument;
55
import javax.swing.text.StyledDocument;
52
import org.netbeans.api.editor.guards.GuardedSection;
56
import org.netbeans.api.editor.guards.GuardedSection;
57
import org.netbeans.spi.editor.guards.support.AbstractGuardedSectionsProvider.Result;
53
import org.openide.text.NbDocument;
58
import org.openide.text.NbDocument;
54
59
55
/** Represents one guarded section.
60
/** Represents one guarded section.
Lines 163-169 Link Here
163
     * @param minLen If true the text has to have length more than 2 chars.
168
     * @param minLen If true the text has to have length more than 2 chars.
164
     * @return <code>true</code> if the operation was successful, otherwise <code>false</code>
169
     * @return <code>true</code> if the operation was successful, otherwise <code>false</code>
165
     */
170
     */
166
    protected boolean setText(PositionBounds bounds, String text, boolean minLen) {
171
    protected boolean setText(PositionBounds bounds, String text, boolean minLen, ContentGetter contentGetter) {
167
        if (!valid)
172
        if (!valid)
168
            return false;
173
            return false;
169
        
174
        
Lines 179-190 Link Here
179
        
184
        
180
        try {
185
        try {
181
            bounds.setText(text);
186
            bounds.setText(text);
187
            if (guards.gr != null) {
188
                int offset = getStartPosition().getOffset();
189
                char[] data = guards.gr.writeSections(Collections.singletonList(GuardsAccessor.DEFAULT.clone(guard, offset)), (new PositionBounds(getStartPosition(), getEndPosition(), guards).getText() + "\n").toCharArray());
190
                Result result = guards.gr.readSections(data);
191
                List<GuardedSection> guardedSections = guards.gr.readSections(data).getGuardedSections();
192
                if (guardedSections.size() == 1) {
193
                    PositionBounds contentBounds = contentGetter.getContent(GuardsAccessor.DEFAULT.getImpl(guardedSections.get(0)));
194
                    bounds.setText(new String(result.getContent(), contentBounds.getBegin().getOffset(), contentBounds.getEnd().getOffset() - contentBounds.getBegin().getOffset()));
195
                }
196
            }
182
            return true;
197
            return true;
183
        } catch (BadLocationException e) {
198
        } catch (BadLocationException e) {
184
        }
199
        }
185
        return false;
200
        return false;
186
    }
201
    }
187
    
202
    
203
    interface ContentGetter<T extends GuardedSectionImpl> {
204
        public PositionBounds getContent(GuardedSectionImpl t);
205
    }
206
    
188
    /** Marks or unmarks the section as guarded.
207
    /** Marks or unmarks the section as guarded.
189
     * @param doc The styled document where this section placed in.
208
     * @param doc The styled document where this section placed in.
190
     * @param bounds The rangeof text which should be marked or unmarked.
209
     * @param bounds The rangeof text which should be marked or unmarked.
(-)a/editor.guards/src/org/netbeans/modules/editor/guards/GuardedSectionsImpl.java (-1 / +4 lines)
Lines 254-266 Link Here
254
    
254
    
255
    // package
255
    // package
256
    
256
    
257
    AbstractGuardedSectionsProvider gr;
258
    
257
    /** Takes the section descriptors from the GuardedReader and
259
    /** Takes the section descriptors from the GuardedReader and
258
    * fills the table 'sections', also marks as guarded all sections
260
    * fills the table 'sections', also marks as guarded all sections
259
    * in the given document.
261
    * in the given document.
260
    * @param is Where to take the guarded section descriptions.
262
    * @param is Where to take the guarded section descriptions.
261
    * @param doc Where to mark guarded.
263
    * @param doc Where to mark guarded.
262
    */
264
    */
263
    void fillSections(List<GuardedSection> l, NewLine newLineType) {
265
    void fillSections(AbstractGuardedSectionsProvider gr, List<GuardedSection> l, NewLine newLineType) {
266
        this.gr = GuardsSupportAccessor.DEFAULT.isUseReadersWritersOnSet(gr) ? gr : null;
264
        this.newLineType = newLineType;
267
        this.newLineType = newLineType;
265
        // XXX this should invalidate removed GS instances
268
        // XXX this should invalidate removed GS instances
266
        // XXX maybe would be useful to map new list to old list to keep track of valid instances as much as possible
269
        // XXX maybe would be useful to map new list to old list to keep track of valid instances as much as possible
(-)a/editor.guards/src/org/netbeans/modules/editor/guards/GuardsAccessor.java (+2 lines)
Lines 74-77 Link Here
74
    
74
    
75
    public abstract GuardedSectionImpl getImpl(GuardedSection gs);
75
    public abstract GuardedSectionImpl getImpl(GuardedSection gs);
76
    
76
    
77
    public abstract GuardedSection clone(GuardedSection gs, int offset);
78
    
77
}
79
}
(-)0f6733730c8f (+68 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * Contributor(s):
28
 *
29
 * The Original Software is NetBeans. The Initial Developer of the Original
30
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
31
 * Microsystems, Inc. All Rights Reserved.
32
 *
33
 * If you wish your version of this file to be governed by only the CDDL
34
 * or only the GPL Version 2, indicate your decision by adding
35
 * "[Contributor] elects to include this software in this distribution
36
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
37
 * single choice of license, a recipient has the option to distribute
38
 * your version of this file under either the CDDL, the GPL Version 2 or
39
 * to extend the choice of license to its licensees as provided above.
40
 * However, if you add GPL Version 2 code and therefore, elected the GPL
41
 * Version 2 license, then the option applies only if the new code is
42
 * made subject to such option by the copyright holder.
43
 */
44
45
package org.netbeans.modules.editor.guards;
46
47
import org.netbeans.spi.editor.guards.support.AbstractGuardedSectionsProvider;
48
49
/**
50
 *
51
 * @author Jan Pokorsky
52
 */
53
public abstract class GuardsSupportAccessor {
54
    
55
    public static GuardsSupportAccessor DEFAULT;
56
    
57
    static {
58
        Class clazz = AbstractGuardedSectionsProvider.class;
59
        try {
60
            Class.forName(clazz.getName(), true, clazz.getClassLoader());
61
        } catch (ClassNotFoundException cnfe) {
62
            cnfe.printStackTrace();
63
        }
64
    }
65
    
66
    public abstract boolean isUseReadersWritersOnSet(AbstractGuardedSectionsProvider impl);
67
    
68
}
(-)a/editor.guards/src/org/netbeans/modules/editor/guards/InteriorSectionImpl.java (-3 / +15 lines)
Lines 79-85 Link Here
79
     * @param text the new text
79
     * @param text the new text
80
     */
80
     */
81
    public void setBody(String text) {
81
    public void setBody(String text) {
82
        setText(body, text, false);
82
        setText(body, text, false, new ContentGetter() {
83
            @Override public PositionBounds getContent(GuardedSectionImpl t) {
84
                return ((InteriorSectionImpl) t).body;
85
            }
86
        });
83
    }
87
    }
84
88
85
    public String getBody() {
89
    public String getBody() {
Lines 99-105 Link Here
99
     * @param text the new text
103
     * @param text the new text
100
     */
104
     */
101
    public void setHeader(String text) {
105
    public void setHeader(String text) {
102
        setText(header, text, true);
106
        setText(header, text, true, new ContentGetter() {
107
            @Override public PositionBounds getContent(GuardedSectionImpl t) {
108
                return ((InteriorSectionImpl) t).header;
109
            }
110
        });
103
    }
111
    }
104
112
105
    /**
113
    /**
Lines 137-143 Link Here
137
            }
145
            }
138
            text = text.replace('\n', ' ');
146
            text = text.replace('\n', ' ');
139
        }
147
        }
140
        setText(footer, text, true);
148
        setText(footer, text, true, new ContentGetter() {
149
            @Override public PositionBounds getContent(GuardedSectionImpl t) {
150
                return ((InteriorSectionImpl) t).footer;
151
            }
152
        });
141
    }
153
    }
142
154
143
    /**
155
    /**
(-)0f6733730c8f (+64 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2012 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.editor.guards;
43
44
import javax.swing.text.Position;
45
46
/**
47
 *
48
 * @author lahvac
49
 */
50
public class OffsetPosition implements Position {
51
    private final Position delegate;
52
    private final int offset;
53
54
    public OffsetPosition(Position delegate, int offset) {
55
        this.delegate = delegate;
56
        this.offset = offset;
57
    }
58
59
    @Override
60
    public int getOffset() {
61
        return delegate.getOffset() - offset;
62
    }
63
    
64
}
(-)a/editor.guards/src/org/netbeans/modules/editor/guards/SimpleSectionImpl.java (-1 / +5 lines)
Lines 73-79 Link Here
73
     * @param text the new text
73
     * @param text the new text
74
     */
74
     */
75
    public void setText(String text) {
75
    public void setText(String text) {
76
        setText(bounds, text, true);
76
        setText(bounds, text, true, new ContentGetter() {
77
            @Override public PositionBounds getContent(GuardedSectionImpl t) {
78
                return ((SimpleSectionImpl) t).bounds;
79
            }
80
        });
77
    }
81
    }
78
82
79
    void markGuarded(StyledDocument doc) {
83
    void markGuarded(StyledDocument doc) {
(-)a/editor.guards/src/org/netbeans/spi/editor/guards/support/AbstractGuardedSectionsProvider.java (+21 lines)
Lines 55-60 Link Here
55
import org.netbeans.api.editor.guards.InteriorSection;
55
import org.netbeans.api.editor.guards.InteriorSection;
56
import org.netbeans.api.editor.guards.SimpleSection;
56
import org.netbeans.api.editor.guards.SimpleSection;
57
import org.netbeans.modules.editor.guards.GuardedSectionsImpl;
57
import org.netbeans.modules.editor.guards.GuardedSectionsImpl;
58
import org.netbeans.modules.editor.guards.GuardsSupportAccessor;
58
import org.netbeans.modules.editor.guards.PositionBounds;
59
import org.netbeans.modules.editor.guards.PositionBounds;
59
import org.netbeans.spi.editor.guards.GuardedEditorSupport;
60
import org.netbeans.spi.editor.guards.GuardedEditorSupport;
60
import org.netbeans.spi.editor.guards.GuardedSectionsProvider;
61
import org.netbeans.spi.editor.guards.GuardedSectionsProvider;
Lines 69-81 Link Here
69
public abstract class AbstractGuardedSectionsProvider implements GuardedSectionsProvider {
70
public abstract class AbstractGuardedSectionsProvider implements GuardedSectionsProvider {
70
71
71
    private final GuardedSectionsImpl impl;
72
    private final GuardedSectionsImpl impl;
73
    private final boolean useReadersWritersOnSet;
72
    
74
    
73
    /**
75
    /**
74
     * Creates an AbstractGuardedSectionsProvider.
76
     * Creates an AbstractGuardedSectionsProvider.
75
     * @param editor an editor abstraction
77
     * @param editor an editor abstraction
76
     */
78
     */
77
    protected AbstractGuardedSectionsProvider(GuardedEditorSupport editor) {
79
    protected AbstractGuardedSectionsProvider(GuardedEditorSupport editor) {
80
        this(editor, false);
81
    }
82
    
83
    /**
84
     * Creates an AbstractGuardedSectionsProvider.
85
     * @param editor an editor abstraction
86
     * @param useReadersWritersOnSet if readers and writers should be used when the content of the guarded section's text is set
87
     * @since 1.20
88
     */
89
    protected AbstractGuardedSectionsProvider(GuardedEditorSupport editor, boolean useReadersWritersOnSet) {
78
        this.impl = new GuardedSectionsImpl(editor);
90
        this.impl = new GuardedSectionsImpl(editor);
91
        this.useReadersWritersOnSet = useReadersWritersOnSet;
79
    }
92
    }
80
93
81
    public final Reader createGuardedReader(InputStream stream, Charset charset) {
94
    public final Reader createGuardedReader(InputStream stream, Charset charset) {
Lines 157-160 Link Here
157
            return this.sections;
170
            return this.sections;
158
        }
171
        }
159
    }
172
    }
173
    
174
    static {
175
        GuardsSupportAccessor.DEFAULT = new GuardsSupportAccessor() {
176
            @Override public boolean isUseReadersWritersOnSet(AbstractGuardedSectionsProvider impl) {
177
                return impl.useReadersWritersOnSet;
178
            }
179
        };
180
    }
160
}
181
}
(-)a/java.guards/nbproject/project.xml (-1 / +1 lines)
Lines 55-61 Link Here
55
                    <compile-dependency/>
55
                    <compile-dependency/>
56
                    <run-dependency>
56
                    <run-dependency>
57
                        <release-version>1</release-version>
57
                        <release-version>1</release-version>
58
                        <specification-version>1.0</specification-version>
58
                        <specification-version>1.20</specification-version>
59
                    </run-dependency>
59
                    </run-dependency>
60
                </dependency>
60
                </dependency>
61
                <dependency>
61
                <dependency>
(-)a/java.guards/src/org/netbeans/modules/java/guards/JavaGuardedReader.java (-22 / +9 lines)
Lines 78-94 Link Here
78
     * 2. Replace the comments with spaces to hide them from user but to preserve
78
     * 2. Replace the comments with spaces to hide them from user but to preserve
79
     *    overall length and positions.
79
     *    overall length and positions.
80
     * 3. Remove the comments completely.
80
     * 3. Remove the comments completely.
81
     * Default is #3, can be changed via branding. For a long time #2 used to be
81
     * 
82
     * the default, but neither 1 nor 2 is reliable since artificial chars are
82
     * Option #3 causes that offsets inside the editor differ from the offsets on disk,
83
     * made part of the sections' content which the clients do not know about.
83
     * which then breaks many clients that create PositionRefs for a closed file and then
84
     * Once a client changes the section content, the artificial comments or spaces
84
     * use it for an opened file, and vice versa.
85
     * are gone from the document, but they get written to the file on saving and
85
     * 
86
     * so the file is then inconsistent with the document just like in case of #3.
86
     * Default is #2.
87
     */
87
     */
88
    private static boolean KEEP_GUARD_COMMENTS    // not final only for tests
88
    private static boolean KEEP_GUARD_COMMENTS    // not final only for tests
89
            = getPresetValue("KEEP_GUARD_COMMENTS", false); // NOI18N
89
            = getPresetValue("KEEP_GUARD_COMMENTS", false); // NOI18N
90
    private static boolean REPLACE_GUARD_COMMENTS_WITH_SPACES    // not final only for tests
91
            = getPresetValue("REPLACE_GUARD_COMMENTS_WITH_SPACES", false); // NOI18N
92
90
93
    /** The list of the SectionsDesc. */
91
    /** The list of the SectionsDesc. */
94
    private final LinkedList<SectionDescriptor> list;
92
    private final LinkedList<SectionDescriptor> list;
Lines 219-231 Link Here
219
                    if (KEEP_GUARD_COMMENTS) { // keep guard comment (content unchanged)
217
                    if (KEEP_GUARD_COMMENTS) { // keep guard comment (content unchanged)
220
                        i -= match.length();
218
                        i -= match.length();
221
                        charBuffPtr += MAGICLEN;
219
                        charBuffPtr += MAGICLEN;
222
                    } else if (REPLACE_GUARD_COMMENTS_WITH_SPACES) {
220
                    } else {
223
                        i += toNl;
221
                        i += toNl;
224
                        Arrays.fill(charBuff,charBuffPtr,charBuffPtr+sectionSize,' ');
222
                        Arrays.fill(charBuff,charBuffPtr,charBuffPtr+sectionSize,' ');
225
                        charBuffPtr+=sectionSize;
223
                        charBuffPtr+=sectionSize;
226
                    } else { // remove the guard comment
227
                        i += toNl;
228
                        desc.setEnd(charBuffPtr);
229
                    }
224
                    }
230
                }
225
                }
231
            }
226
            }
Lines 400-414 Link Here
400
        return defaultValue;
395
        return defaultValue;
401
    }
396
    }
402
397
403
    static void setGuardCommentProcessingForTest(int type) {
398
    static void setKeepGuardCommentsForTest(boolean keep) {
404
        if (type == 1) {
399
        KEEP_GUARD_COMMENTS = keep;
405
            KEEP_GUARD_COMMENTS = true;
406
        } else if (type == 2) {
407
            KEEP_GUARD_COMMENTS = false;
408
            REPLACE_GUARD_COMMENTS_WITH_SPACES = true;
409
        } else if (type == 3) {
410
            KEEP_GUARD_COMMENTS = false;
411
            REPLACE_GUARD_COMMENTS_WITH_SPACES = false;
412
        }
413
    }
400
    }
414
}
401
}
(-)a/java.guards/src/org/netbeans/modules/java/guards/JavaGuardedSectionsProvider.java (-1 / +1 lines)
Lines 56-62 Link Here
56
public final class JavaGuardedSectionsProvider extends AbstractGuardedSectionsProvider {
56
public final class JavaGuardedSectionsProvider extends AbstractGuardedSectionsProvider {
57
    
57
    
58
    public JavaGuardedSectionsProvider(GuardedEditorSupport editor) {
58
    public JavaGuardedSectionsProvider(GuardedEditorSupport editor) {
59
        super(editor);
59
        super(editor, true);
60
    }
60
    }
61
61
62
    public char[] writeSections(List<GuardedSection> sections, char[] buff) {
62
    public char[] writeSections(List<GuardedSection> sections, char[] buff) {
(-)a/java.guards/test/unit/src/org/netbeans/modules/java/guards/JavaGuardedReaderTest.java (-26 / +2 lines)
Lines 71-77 Link Here
71
        provider = new JavaGuardedSectionsProvider(editor);
71
        provider = new JavaGuardedSectionsProvider(editor);
72
        
72
        
73
        instance = new JavaGuardedReader(provider);
73
        instance = new JavaGuardedReader(provider);
74
        JavaGuardedReader.setGuardCommentProcessingForTest(2);
75
    }
74
    }
76
75
77
    protected void tearDown() throws Exception {
76
    protected void tearDown() throws Exception {
Lines 124-130 Link Here
124
        String expStr =  "\nclass A {//" + "GEN-BEGIN:hu\n\n}//" + "GEN-END:hu\n";
123
        String expStr =  "\nclass A {//" + "GEN-BEGIN:hu\n\n}//" + "GEN-END:hu\n";
125
        char[] readBuff = readStr.toCharArray();
124
        char[] readBuff = readStr.toCharArray();
126
125
127
        JavaGuardedReader.setGuardCommentProcessingForTest(1);
126
        JavaGuardedReader.setKeepGuardCommentsForTest(true);
128
        char[] result = instance.translateToCharBuff(readBuff);
127
        char[] result = instance.translateToCharBuff(readBuff);
129
        List<GuardedSection> sections = instance.getGuardedSections();
128
        List<GuardedSection> sections = instance.getGuardedSections();
130
        
129
        
Lines 147-176 Link Here
147
        String expStr =  "\nclass A {  " + "            \n\n}  " + "          \n";
146
        String expStr =  "\nclass A {  " + "            \n\n}  " + "          \n";
148
        char[] readBuff = readStr.toCharArray();
147
        char[] readBuff = readStr.toCharArray();
149
        
148
        
150
        JavaGuardedReader.setGuardCommentProcessingForTest(2);
149
        JavaGuardedReader.setKeepGuardCommentsForTest(false);
151
        char[] result = instance.translateToCharBuff(readBuff);
152
        List<GuardedSection> sections = instance.getGuardedSections();
153
        
154
        assertEquals(expStr, String.valueOf(result));
155
        assertEquals("sections", 1, sections.size());
156
        
157
        GuardedSection expSection = sections.get(0);
158
        assertEquals(SimpleSection.class, expSection.getClass());
159
        assertEquals("section valid", true, expSection.isValid());
160
        assertEquals("section name", "hu", expSection.getName());
161
        assertEquals("begin", 1, expSection.getStartPosition().getOffset());
162
        assertEquals("end", expStr.length() - 1, expSection.getEndPosition().getOffset());
163
    }
164
165
    public void testTranslateBEGIN_END3() {
166
        System.out.println("read //" + "GEN-BEGIN_END3:");
167
        
168
        String readStr = "\nclass A {//" + "GEN-BEGIN:hu\n\n}//" + "GEN-END:hu\n";
169
        editor.setStringContent(readStr);
170
        String expStr =  "\nclass A {" +               "\n\n}" +              "\n";
171
        char[] readBuff = readStr.toCharArray();
172
        
173
        JavaGuardedReader.setGuardCommentProcessingForTest(3);
174
        char[] result = instance.translateToCharBuff(readBuff);
150
        char[] result = instance.translateToCharBuff(readBuff);
175
        List<GuardedSection> sections = instance.getGuardedSections();
151
        List<GuardedSection> sections = instance.getGuardedSections();
176
        
152
        

Return to bug 221844