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

(-)a/db/src/org/netbeans/modules/db/explorer/node/ConnectionNodeProvider.java (-2 / +17 lines)
Lines 42-53 Link Here
42
42
43
package org.netbeans.modules.db.explorer.node;
43
package org.netbeans.modules.db.explorer.node;
44
44
45
import java.beans.PropertyChangeEvent;
46
import java.beans.PropertyChangeListener;
45
import java.util.ArrayList;
47
import java.util.ArrayList;
46
import java.util.Collection;
48
import java.util.Collection;
47
import java.util.Comparator;
49
import java.util.Comparator;
48
import java.util.List;
50
import java.util.List;
49
import javax.swing.SwingUtilities;
51
import javax.swing.SwingUtilities;
50
import org.netbeans.api.db.explorer.ConnectionListener;
52
import org.netbeans.api.db.explorer.ConnectionListener;
53
import org.netbeans.api.db.explorer.node.BaseNode;
51
import org.netbeans.api.db.explorer.node.NodeProvider;
54
import org.netbeans.api.db.explorer.node.NodeProvider;
52
import org.netbeans.api.db.explorer.node.NodeProviderFactory;
55
import org.netbeans.api.db.explorer.node.NodeProviderFactory;
53
import org.netbeans.modules.db.explorer.ConnectionList;
56
import org.netbeans.modules.db.explorer.ConnectionList;
Lines 61-66 Link Here
61
 * @author Rob Englander
64
 * @author Rob Englander
62
 */
65
 */
63
public class ConnectionNodeProvider extends NodeProvider {
66
public class ConnectionNodeProvider extends NodeProvider {
67
    private PropertyChangeListener PCL = new PropertyChangeListener() {
68
        @Override
69
        public void propertyChange(PropertyChangeEvent pce) {
70
            if(pce.getPropertyName().equals(BaseNode.PROP_DISPLAY_NAME)) {
71
                initialize();
72
            }
73
        }
74
    };
64
    
75
    
65
    // lazy initialization holder class idiom for static fields is used
76
    // lazy initialization holder class idiom for static fields is used
66
    // for retrieving the factory
77
    // for retrieving the factory
Lines 95-105 Link Here
95
        );
106
        );
96
    }
107
    }
97
108
109
    @Override
98
    protected synchronized void initialize() {
110
    protected synchronized void initialize() {
99
        List<Node> newList = new ArrayList<Node>();
111
        List<Node> newList = new ArrayList<Node>();
100
        DatabaseConnection newConnection = null;
112
        DatabaseConnection newConnection = null;
101
        DatabaseConnection[] connections = connectionList.getConnections();
113
        DatabaseConnection[] connections = connectionList.getConnections();
102
        for (DatabaseConnection connection : connections) {
114
        for (DatabaseConnection connection : connections) {
115
            // Make sure the PCL is only added once
116
            connection.removePropertyChangeListener(PCL);
117
            connection.addPropertyChangeListener(PCL);
103
            Collection<Node> matches = getNodes(connection);
118
            Collection<Node> matches = getNodes(connection);
104
            if (matches.size() > 0) {
119
            if (matches.size() > 0) {
105
                newList.addAll(matches);
120
                newList.addAll(matches);
Lines 110-116 Link Here
110
                newList.add(ConnectionNode.create(lookup, this));
125
                newList.add(ConnectionNode.create(lookup, this));
111
            }
126
            }
112
        }
127
        }
113
128
        
114
        setNodes(newList);
129
        setNodes(newList);
115
        // select added connection in explorer
130
        // select added connection in explorer
116
        final DatabaseConnection newConnectionFinal = newConnection;
131
        final DatabaseConnection newConnectionFinal = newConnection;
Lines 129-135 Link Here
129
    }
144
    }
130
145
131
    static class ConnectionComparator implements Comparator<Node> {
146
    static class ConnectionComparator implements Comparator<Node> {
132
147
        @Override
133
        public int compare(Node model1, Node model2) {
148
        public int compare(Node model1, Node model2) {
134
            return model1.getDisplayName().compareToIgnoreCase(model2.getDisplayName());
149
            return model1.getDisplayName().compareToIgnoreCase(model2.getDisplayName());
135
        }
150
        }
(-)a/db/test/unit/src/org/netbeans/modules/db/explorer/node/RootNodeTest.java (+42 lines)
Lines 42-49 Link Here
42
42
43
package org.netbeans.modules.db.explorer.node;
43
package org.netbeans.modules.db.explorer.node;
44
44
45
import java.lang.reflect.Method;
46
import java.util.ArrayList;
45
import java.util.Collection;
47
import java.util.Collection;
46
import java.util.Iterator;
48
import java.util.Iterator;
49
import java.util.List;
47
import junit.framework.TestCase;
50
import junit.framework.TestCase;
48
import org.netbeans.api.db.explorer.ConnectionManager;
51
import org.netbeans.api.db.explorer.ConnectionManager;
49
import org.netbeans.api.db.explorer.DatabaseConnection;
52
import org.netbeans.api.db.explorer.DatabaseConnection;
Lines 89-95 Link Here
89
        checkConnection(rootNode, conn);
92
        checkConnection(rootNode, conn);
90
        checkNodeChildren(rootNode);
93
        checkNodeChildren(rootNode);
91
    }
94
    }
95
    
96
    /**
97
     * Ensure, that the connection list stays sorted, if the displayName 
98
     * which is the sorting criterium, is changed
99
     */
100
    public void testSortingAfterDisplayNameChange() throws Exception {
101
        // Initialize the tree with a driver and a connection
102
        JDBCDriver driver = Util.createDummyDriver();
103
        JDBCDriverManager.getDefault().addDriver(driver);
92
104
105
        DatabaseConnection conn2 = DatabaseConnection.create(
106
                driver, "jdbc:mark//twain/conn2", "tomsawyer", null, "whitewash", true, "B2");
107
        ConnectionManager.getDefault().addConnection(conn2);
108
        
109
        DatabaseConnection conn = DatabaseConnection.create(
110
                driver, "jdbc:mark//twain/conn", "tomsawyer", null, "whitewash", true, "A1");
111
        ConnectionManager.getDefault().addConnection(conn);
112
113
        RootNode rootNode = RootNode.instance();
114
115
        List<? extends Node> children = new ArrayList(rootNode.getChildNodesSync());
116
117
        assertEquals("A1", children.get(1).getDisplayName());
118
        assertEquals("B2", 
119
                children.get(2).getDisplayName());
120
        
121
        Method m = conn.getClass().getDeclaredMethod("getDelegate", new Class<?>[]{});
122
        m.setAccessible(true);
123
        
124
        org.netbeans.modules.db.explorer.DatabaseConnection dc = 
125
                (org.netbeans.modules.db.explorer.DatabaseConnection) m.invoke(conn, new Object[]{});
126
        
127
        dc.setDisplayName("C3");
128
        
129
        children = new ArrayList(rootNode.getChildNodesSync());
130
        
131
        assertEquals("B2", children.get(1).getDisplayName());
132
        assertEquals("C3", children.get(2).getDisplayName());        
133
    }
134
    
93
    private void checkNodeChildren(RootNode root) throws Exception {
135
    private void checkNodeChildren(RootNode root) throws Exception {
94
        Collection<? extends Node> children = root.getChildNodesSync();
136
        Collection<? extends Node> children = root.getChildNodesSync();
95
        assertTrue(children.size() == 2);
137
        assertTrue(children.size() == 2);

Return to bug 215602