Please use the Apache issue tracking system for new NetBeans issues (https://issues.apache.org/jira/projects/NETBEANS0/issues) !!
View | Details | Raw Unified | Return to bug 144579
Collapse All | Expand All

(-)editor.settings.storage/src/org/netbeans/modules/editor/settings/storage/preferences/PreferencesImpl.java (-53 / +89 lines)
 Lines 80-86    Link Here 
80
        PreferencesImpl prefs = INSTANCES.get(mimePath);
80
        PreferencesImpl prefs = INSTANCES.get(mimePath);
81
        
81
        
82
        if (prefs == null) {
82
        if (prefs == null) {
83
            prefs = new PreferencesImpl(mimePath.getPath());
83
            prefs = new PreferencesImpl(null, mimePath.getPath(), "");
84
            INSTANCES.put(mimePath, prefs);
84
            INSTANCES.put(mimePath, prefs);
85
        }
85
        }
86
        
86
        
 Lines 91-135    Link Here 
91
    // Preferences API
91
    // Preferences API
92
    // ---------------------------------------------------------------------
92
    // ---------------------------------------------------------------------
93
    
93
    
94
    public @Override String absolutePath() {
94
//    public @Override String absolutePath() {
95
        return SLASH;
95
//        return SLASH;
96
    }
96
//    }
97
97
//
98
    public @Override String[] childrenNames() throws BackingStoreException {
98
//    public @Override String[] childrenNames() throws BackingStoreException {
99
        return EMPTY_ARRAY;
99
//        return EMPTY_ARRAY;
100
    }
100
//    }
101
101
102
    public @Override boolean isUserNode() {
102
    public @Override boolean isUserNode() {
103
        return true;
103
        return true;
104
    }
104
    }
105
105
106
    public @Override String name() {
106
//    public @Override String name() {
107
        return EMPTY;
107
//        return EMPTY;
108
    }
108
//    }
109
109
110
    public @Override Preferences node(String path) {
110
//    public @Override Preferences node(String path) {
111
        if (path.length() == 0 || path.equals(SLASH)) {
111
//        if (path.length() == 0 || path.equals(SLASH)) {
112
            return this;
112
//            return this;
113
        } else {
113
//        } else {
114
            throw new IllegalStateException("Editor Preferences does not support children nodes."); //NOI18N
114
//            throw new IllegalStateException("Editor Preferences does not support children nodes."); //NOI18N
115
        }
115
//        }
116
    }
116
//    }
117
//
118
//    public @Override boolean nodeExists(String path) throws BackingStoreException {
119
//        if (path.length() == 0 || path.equals(SLASH)) {
120
//            return true;
121
//        } else {
122
//            return false;
123
//        }
124
//    }
117
125
118
    public @Override boolean nodeExists(String path) throws BackingStoreException {
126
//    public @Override Preferences parent() {
119
        if (path.length() == 0 || path.equals(SLASH)) {
127
//        return null;
120
            return true;
128
//    }
121
        } else {
122
            return false;
123
        }
124
    }
125
129
126
    public @Override Preferences parent() {
130
//    public @Override void removeNode() throws BackingStoreException {
127
        return null;
131
//        throw new IllegalStateException("Can't remove the root!"); //NOI18N
128
    }
132
//    }
129
130
    public @Override void removeNode() throws BackingStoreException {
131
        throw new IllegalStateException("Can't remove the root!"); //NOI18N
132
    }
133
133
134
    public @Override final void sync() throws BackingStoreException {
134
    public @Override final void sync() throws BackingStoreException {
135
        flushTask.waitFinished();
135
        flushTask.waitFinished();
 Lines 257-284    Link Here 
257
    // AbstractPreferences SPI
257
    // AbstractPreferences SPI
258
    // ---------------------------------------------------------------------
258
    // ---------------------------------------------------------------------
259
259
260
    @Override
260
//    @Override
261
    protected AbstractPreferences getChild(String nodeName) throws BackingStoreException {
261
//    protected AbstractPreferences getChild(String nodeName) throws BackingStoreException {
262
        throw new IllegalStateException("Should never be called."); //NOI18N
262
//        throw new IllegalStateException("Should never be called."); //NOI18N
263
//    }
264
//
265
//    @Override
266
//    protected boolean isRemoved() {
267
//        boolean superRemoved = super.isRemoved();
268
//        assert superRemoved == false : "super.isRemoved() should always == false"; //NOI18N
269
//        return superRemoved;
270
//    }
271
//    
272
    protected @Override void removeNodeSpi() throws BackingStoreException {
273
        for (String key : keys()) {
274
            remove(key);
275
        }
276
        PreferencesImpl parent = (PreferencesImpl) parent();
277
        String subnodes = parent.get(SUBNODES_KEY, null);
278
        StringBuilder nue = new StringBuilder();
279
        String name = name();
280
        if (subnodes != null) {
281
            for (String sn : subnodes.split(":")) {
282
                if (!sn.equals(name)) {
283
                    if (nue.length() > 0) nue.append(":");
284
                    nue.append(sn);
285
                }
286
            }
287
        }
288
        if (nue.length() == 0)
289
            parent.remove(SUBNODES_KEY);
290
        else
291
            parent.put(SUBNODES_KEY, nue.toString());
292
        
263
    }
293
    }
264
294
265
    @Override
295
    private static final String SUBNODES_KEY = "-private-sub-nodes-key";
266
    protected boolean isRemoved() {
267
        boolean superRemoved = super.isRemoved();
268
        assert superRemoved == false : "super.isRemoved() should always == false"; //NOI18N
269
        return superRemoved;
270
    }
271
    
272
    protected @Override void removeNodeSpi() throws BackingStoreException {
273
        throw new IllegalStateException("Should never be called."); //NOI18N
274
    }
275
276
    protected @Override String[] childrenNamesSpi() throws BackingStoreException {
296
    protected @Override String[] childrenNamesSpi() throws BackingStoreException {
277
        throw new IllegalStateException("Should never be called."); //NOI18N
297
        String subnodes = get(SUBNODES_KEY, null);
298
        if (subnodes == null) return new String[0];
299
        return subnodes.split(":");
278
    }
300
    }
279
301
280
    protected @Override AbstractPreferences childSpi(String name) {
302
    protected @Override AbstractPreferences childSpi(String name) {
281
        throw new IllegalStateException("Should never be called."); //NOI18N
303
        String subnodes = get(SUBNODES_KEY, null);
304
        boolean found = false;
305
        if (subnodes != null) {
306
            for (String sn : subnodes.split(":")) {
307
                if (sn.equals(name)) {
308
                    found = true;
309
                }
310
            }
311
            if (!found) {
312
                put(SUBNODES_KEY, subnodes + ":" + name);
313
            }
314
        } else {
315
            put(SUBNODES_KEY, name);
316
        }
317
        return new PreferencesImpl(this, mimePath, name);
282
    }
318
    }
283
319
284
    protected @Override void putSpi(String key, String value) {
320
    protected @Override void putSpi(String key, String value) {
 Lines 353-359    Link Here 
353
        // changing anything.
389
        // changing anything.
354
        if (local != null) {
390
        if (local != null) {
355
            try {
391
            try {
356
                storage.save(MimePath.parse(mimePath), null, false, local);
392
                storage.save(MimePath.parse(mimePath), absolutePath().substring(1), false, local);
357
            } catch (IOException ioe) {
393
            } catch (IOException ioe) {
358
                LOG.log(Level.WARNING, "Can't save editor preferences for '" + mimePath + "'", ioe); //NOI18N
394
                LOG.log(Level.WARNING, "Can't save editor preferences for '" + mimePath + "'", ioe); //NOI18N
359
            }
395
            }
 Lines 381-387    Link Here 
381
417
382
    private static final Logger LOG = Logger.getLogger(PreferencesImpl.class.getName());
418
    private static final Logger LOG = Logger.getLogger(PreferencesImpl.class.getName());
383
    
419
    
384
    private static final Map<MimePath, PreferencesImpl> INSTANCES =
420
    static final Map<MimePath, PreferencesImpl> INSTANCES =
385
        new WeakHashMap<MimePath, PreferencesImpl>();
421
        new WeakHashMap<MimePath, PreferencesImpl>();
386
422
387
    private static final String SLASH = "/"; //NOI18N
423
    private static final String SLASH = "/"; //NOI18N
 Lines 451-458    Link Here 
451
    private Map<String, TypedValue> local = null;
487
    private Map<String, TypedValue> local = null;
452
    private Preferences inherited = null;
488
    private Preferences inherited = null;
453
    
489
    
454
    private PreferencesImpl(String mimePath) {
490
    private PreferencesImpl(AbstractPreferences parent, String mimePath, String path) {
455
        super(null, EMPTY);
491
        super(parent, path);
456
        
492
        
457
        this.mimePath = mimePath;
493
        this.mimePath = mimePath;
458
        this.storage = EditorSettingsStorage.<String, TypedValue>get(PreferencesStorage.ID);
494
        this.storage = EditorSettingsStorage.<String, TypedValue>get(PreferencesStorage.ID);
 Lines 462-468    Link Here 
462
    private Map<String, TypedValue> getLocal() {
498
    private Map<String, TypedValue> getLocal() {
463
        if (local == null) {
499
        if (local == null) {
464
            try {
500
            try {
465
                local = new HashMap<String, TypedValue>(storage.load(MimePath.parse(mimePath), null, false));
501
                local = new HashMap<String, TypedValue>(storage.load(MimePath.parse(mimePath), absolutePath().substring(1), false));
466
            } catch (IOException ioe) {
502
            } catch (IOException ioe) {
467
                LOG.log(Level.WARNING, "Can't load editor preferences for '" + mimePath + "'", ioe); //NOI18N
503
                LOG.log(Level.WARNING, "Can't load editor preferences for '" + mimePath + "'", ioe); //NOI18N
468
                local = new HashMap<String, TypedValue>();
504
                local = new HashMap<String, TypedValue>();
(-)editor.settings.storage/src/org/netbeans/modules/editor/settings/storage/preferences/PreferencesStorage.java (-1 / +1 lines)
 Lines 89-95    Link Here 
89
    }
89
    }
90
90
91
    public boolean isUsingProfiles() {
91
    public boolean isUsingProfiles() {
92
        return false;
92
        return true;
93
    }
93
    }
94
94
95
    public String getMimeType() {
95
    public String getMimeType() {
(-)editor.settings.storage/test/unit/src/org/netbeans/modules/editor/settings/storage/preferences/PreferencesImplTest.java (+85 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.settings.storage.preferences;
43
44
import java.util.Arrays;
45
import java.util.prefs.BackingStoreException;
46
import java.util.prefs.Preferences;
47
import org.netbeans.api.editor.mimelookup.MimePath;
48
import org.netbeans.junit.NbTestCase;
49
50
/**
51
 *
52
 * @author lahvac
53
 */
54
public class PreferencesImplTest extends NbTestCase {
55
    
56
    public PreferencesImplTest(String name) {
57
        super(name);
58
    }
59
    
60
    public void testSubNodes() throws BackingStoreException, InterruptedException {
61
        PreferencesImpl javaPrefs = PreferencesImpl.get(MimePath.get("text/x-java"));
62
        Preferences subNode = javaPrefs.node("subnode");
63
        Preferences subSubNode = subNode.node("subsubnode");
64
        subSubNode.put("foo", "bar");
65
        subSubNode.flush();
66
        //XXX: should not be necessary to flush the subNode:
67
        Thread.sleep(500);
68
        PreferencesImpl.INSTANCES.clear();
69
        javaPrefs = PreferencesImpl.get(MimePath.get("text/x-java"));
70
        assertEquals(Arrays.asList("subnode"), Arrays.asList(javaPrefs.childrenNames()));
71
        subNode = javaPrefs.node("subnode");
72
        subSubNode = subNode.node("subsubnode");
73
        assertEquals("bar", subSubNode.get("foo", null));
74
        subSubNode.removeNode();
75
        assertEquals(Arrays.<String>asList(), Arrays.asList(subNode.childrenNames()));
76
        //XXX: should not be necessary to flush the subNode:
77
        Thread.sleep(500);
78
        PreferencesImpl.INSTANCES.clear();
79
        javaPrefs = PreferencesImpl.get(MimePath.get("text/x-java"));
80
        subNode = javaPrefs.node("subnode");
81
        assertEquals(Arrays.<String>asList(), Arrays.asList(subNode.childrenNames()));
82
        subSubNode = subNode.node("subsubnode");
83
        assertEquals(null, subSubNode.get("foo", null));
84
    }
85
}

Return to bug 144579
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo