[hg] main-silver: #202833: Add ability to filter the listed data...

  • From: Jaroslav Havlin < >
  • To:
  • Subject: [hg] main-silver: #202833: Add ability to filter the listed data...
  • Date: Mon, 18 Feb 2013 09:39:08 -0800

changeset 268ae2935a0a in main-silver ((none))
details: http://hg.netbeans.org/main-silver/rev/268ae2935a0a
description:
        #202833: Add ability to filter the listed database/schemas in the 
Databases node

diffstat:

 db/src/org/netbeans/api/db/explorer/node/NodeProvider.java                |  
  2 +-
 db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java           |  
 61 ++++
 db/src/org/netbeans/modules/db/explorer/DatabaseConnectionConvertor.java  |  
 22 +
 db/src/org/netbeans/modules/db/explorer/action/Bundle.properties          |  
  3 +
 db/src/org/netbeans/modules/db/explorer/action/ToggleImportantAction.java |  
147 ++++++++++
 db/src/org/netbeans/modules/db/explorer/node/CatalogNode.java             |  
 25 +-
 db/src/org/netbeans/modules/db/explorer/node/CatalogNodeProvider.java     |  
131 +++++++-
 db/src/org/netbeans/modules/db/explorer/node/SchemaNode.java              |  
 18 +-
 db/src/org/netbeans/modules/db/explorer/node/SchemaNodeProvider.java      |  
 93 ++++++-
 db/src/org/netbeans/modules/db/explorer/node/ToggleImportantInfo.java     |  
 84 +++++
 db/src/org/netbeans/modules/db/resources/mf-layer.xml                     |  
  6 +
 11 files changed, 557 insertions(+), 35 deletions(-)

diffs (944 lines):

diff --git a/db/src/org/netbeans/api/db/explorer/node/NodeProvider.java 
b/db/src/org/netbeans/api/db/explorer/node/NodeProvider.java
--- a/db/src/org/netbeans/api/db/explorer/node/NodeProvider.java
+++ b/db/src/org/netbeans/api/db/explorer/node/NodeProvider.java
@@ -157,7 +157,7 @@
         synchronized (nodeSet) {
             for (Node child : nodeSet) {
                 Object obj = child.getLookup().lookup(dataObject.getClass());
-                if (obj.hashCode() == dataObject.hashCode() && 
obj.equals(dataObject)) {
+                if (obj != null && obj.hashCode() == dataObject.hashCode() 
&& obj.equals(dataObject)) {
                     results.add(child);
                 }
             }
diff --git a/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java 
b/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java
--- a/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java
+++ b/db/src/org/netbeans/modules/db/explorer/DatabaseConnection.java
@@ -125,6 +125,10 @@
     /** The default schema */
     private String defaultSchema = null;
 
+    private Set<String> importantSchemas = null;
+
+    private Set<String> importantCatalogs = null;
+
     /** Schema name */
     private String schema;
 
@@ -1214,4 +1218,61 @@
         return this;
     }
 
+    public Set<String> getImportantSchemas() {
+        if (importantSchemas == null) {
+            return Collections.emptySet();
+        } else {
+            return Collections.unmodifiableSet(importantSchemas);
 }
+    }
+
+    public void addImportantSchema(String schema) {
+        if (importantSchemas == null) {
+            importantSchemas = new HashSet<String>();
+        }
+        List<String> oldList = new ArrayList<String>(importantSchemas);
+        importantSchemas.add(schema);
+        propertySupport.firePropertyChange("importantSchemas", oldList, 
importantSchemas); //NOI18N
+    }
+
+    public void removeImportantSchema(String schema) {
+        if (importantSchemas != null) {
+            List<String> oldList = new ArrayList<String>(importantSchemas);
+            importantSchemas.remove(schema);
+            propertySupport.firePropertyChange("importantSchemas", oldList, 
importantSchemas); //NOI18N
+        }
+    }
+
+    public boolean isImportantSchema(String schema) {
+        return importantSchemas != null && importantSchemas.contains(schema);
+    }
+
+    public Set<String> getImportantCatalogs() {
+        if (importantCatalogs == null) {
+            return Collections.emptySet();
+        } else {
+            return Collections.unmodifiableSet(importantCatalogs);
+        }
+    }
+
+    public void addImportantCatalog(String database) {
+        if (importantCatalogs == null) {
+            importantCatalogs = new HashSet<String>();
+        }
+        List<String> oldList = new ArrayList<String>(importantCatalogs);
+        importantCatalogs.add(database);
+        propertySupport.firePropertyChange("importantCatalogs", oldList, 
importantCatalogs); //NOI18N
+    }
+
+    public void removeImportantCatalog(String database) {
+        if (importantCatalogs != null) {
+            List<String> oldList = new ArrayList<String>(importantCatalogs);
+            importantCatalogs.remove(database);
+            propertySupport.firePropertyChange("importantCatalogs", oldList, 
importantCatalogs); //NOI18N
+        }
+    }
+
+    public boolean isImportantCatalog(String database) {
+        return importantCatalogs != null && 
importantCatalogs.contains(database);
+    }
+}
diff --git 
a/db/src/org/netbeans/modules/db/explorer/DatabaseConnectionConvertor.java 
b/db/src/org/netbeans/modules/db/explorer/DatabaseConnectionConvertor.java
--- a/db/src/org/netbeans/modules/db/explorer/DatabaseConnectionConvertor.java
+++ b/db/src/org/netbeans/modules/db/explorer/DatabaseConnectionConvertor.java
@@ -58,7 +58,9 @@
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetDecoder;
 import java.nio.charset.CoderResult;
+import java.util.ArrayList;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
 import java.util.WeakHashMap;
 import java.util.concurrent.ConcurrentHashMap;
@@ -242,6 +244,12 @@
         if (handler.displayName != null) {
             dbconn.setDisplayName(handler.displayName);
         }
+        for (String importantSchema : handler.importantSchemas) {
+            dbconn.addImportantSchema(importantSchema);
+        }
+        for (String importantDatabase : handler.importantCatalogs) {
+            dbconn.addImportantCatalog(importantDatabase);
+        }
         LOGGER.fine("Created DatabaseConnection[" + dbconn.toString() + "] 
from file: " + handler.connectionFileName);
 
         return dbconn;
@@ -395,6 +403,12 @@
             if (!instance.getName().equals(instance.getDisplayName())) {
                 pw.println("  <display-name value='" + 
XMLUtil.toAttributeValue(instance.getDisplayName()) + "'/>"); //NOI18N
             }
+            for (String importantSchema : instance.getImportantSchemas()) {
+                pw.println("  <important-schema value='" + 
XMLUtil.toAttributeValue(importantSchema) + "'/>"); //NOI18N
+            }
+            for (String importantDatabase : instance.getImportantCatalogs()) 
{
+                pw.println("  <important-catalog value='" + 
XMLUtil.toAttributeValue(importantDatabase) + "'/>"); //NOI18N
+            }
             if (instance.rememberPassword() ) {
                 char[] password = instance.getPassword() == null ? new 
char[0] : instance.getPassword().toCharArray();
                 
@@ -418,6 +432,8 @@
         private static final String ELEMENT_USER = "user"; // NOI18N
         private static final String ELEMENT_PASSWORD = "password"; // NOI18N
         private static final String ELEMENT_DISPLAY_NAME = "display-name"; 
// NOI18N
+        private static final String ELEMENT_IMPORTANT_SCHEMA = 
"important-schema"; //NOI18N
+        private static final String ELEMENT_IMPORTANT_CATALOG = 
"important-catalog"; //NOI18N
         private static final String ATTR_PROPERTY_VALUE = "value"; // NOI18N
         
         final String connectionFileName;
@@ -428,6 +444,8 @@
         String schema;
         String user;
         String displayName;
+        List<String> importantSchemas = new ArrayList<String>();
+        List<String> importantCatalogs = new ArrayList<String>();
         
         public Handler(String connectionFileName) {
             this.connectionFileName = connectionFileName;
@@ -480,6 +498,10 @@
                         // no password stored => this will require the user 
to re-enter the password
                     }
                 }
+            } else if (ELEMENT_IMPORTANT_SCHEMA.equals(qName)) {
+                importantSchemas.add(value);
+            } else if (ELEMENT_IMPORTANT_CATALOG.equals(qName)) {
+                importantCatalogs.add(value);
             }
         }
     }
diff --git a/db/src/org/netbeans/modules/db/explorer/action/Bundle.properties 
b/db/src/org/netbeans/modules/db/explorer/action/Bundle.properties
--- a/db/src/org/netbeans/modules/db/explorer/action/Bundle.properties
+++ b/db/src/org/netbeans/modules/db/explorer/action/Bundle.properties
@@ -60,6 +60,9 @@
 ExecuteCommand=Execute Command...
 MakeDefaultCatalog=Set As Default Catalog
 MakeDefaultSchema=Set as Default Schema
+ToggleImportantAdd=Mark as important
+ToggleImportantRemove=Mark as unimportant
+ToggleImportant=Toggle important
 
 MSG_ViewsAreNotSupported={0} does not support views.
 
diff --git 
a/db/src/org/netbeans/modules/db/explorer/action/ToggleImportantAction.java 
b/db/src/org/netbeans/modules/db/explorer/action/ToggleImportantAction.java
new file mode 100644
--- /dev/null
+++ 
b/db/src/org/netbeans/modules/db/explorer/action/ToggleImportantAction.java
@@ -0,0 +1,147 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
+ *
+ * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
+ * Other names may be trademarks of their respective owners.
+ *
+ * The contents of this file are subject to the terms of either the GNU
+ * General Public License Version 2 only ("GPL") or the Common
+ * Development and Distribution License("CDDL") (collectively, the
+ * "License"). You may not use this file except in compliance with the
+ * License. You can obtain a copy of the License at
+ * http://www.netbeans.org/cddl-gplv2.html
+ * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
+ * specific language governing permissions and limitations under the
+ * License.  When distributing the software, include this License Header
+ * Notice in each file and include the License file at
+ * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the GPL Version 2 section of the License file that
+ * accompanied this code. If applicable, add the following below the
+ * License Header, with the fields enclosed by brackets [] replaced by
+ * your own identifying information:
+ * "Portions Copyrighted [year] [name of copyright owner]"
+ *
+ * If you wish your version of this file to be governed by only the CDDL
+ * or only the GPL Version 2, indicate your decision by adding
+ * "[Contributor] elects to include this software in this distribution
+ * under the [CDDL or GPL Version 2] license." If you do not indicate a
+ * single choice of license, a recipient has the option to distribute
+ * your version of this file under either the CDDL, the GPL Version 2 or
+ * to extend the choice of license to its licensees as provided above.
+ * However, if you add GPL Version 2 code and therefore, elected the GPL
+ * Version 2 license, then the option applies only if the new code is
+ * made subject to such option by the copyright holder.
+ *
+ * Contributor(s):
+ *
+ * Portions Copyrighted 2009-2010 Sun Microsystems, Inc.
+ */
+package org.netbeans.modules.db.explorer.action;
+
+import org.netbeans.modules.db.explorer.DatabaseConnection;
+import org.netbeans.modules.db.explorer.node.ToggleImportantInfo;
+import org.netbeans.modules.db.metadata.model.api.Catalog;
+import org.netbeans.modules.db.metadata.model.api.Schema;
+import org.openide.nodes.Node;
+import org.openide.util.HelpCtx;
+import org.openide.util.NbBundle;
+
+/**
+ *
+ * @author Jaroslav Havlin
+ */
+public class ToggleImportantAction extends BaseAction {
+
+    private String actionDisplayName;
+
+    @Override
+    public String getName() {
+        return actionDisplayName;
+    }
+
+    @Override
+    protected boolean enable(Node[] activatedNodes) {
+        boolean toggleImportant = false;
+        boolean toggleUnImportant = false;
+
+        for (Node node : activatedNodes) {
+            ToggleImportantInfo tii = 
node.getLookup().lookup(ToggleImportantInfo.class);
+            if (tii != null) {
+                if (!tii.isDefault()) {
+                    if (tii.isImportant()) {
+                        toggleUnImportant = true;
+                    } else {
+                        toggleImportant = true;
+                    }
+                }
+            }
+        }
+
+        if (toggleUnImportant && toggleImportant) {
+            actionDisplayName = 
NbBundle.getMessage(ToggleImportantAction.class,
+                    "ToggleImportant");                                 
//NOI18N
+            return true;
+        } else if (toggleUnImportant) {
+            actionDisplayName = 
NbBundle.getMessage(ToggleImportantAction.class,
+                    "ToggleImportantRemove");                           
//NOI18N
+            return true;
+        } else if (toggleImportant) {
+            actionDisplayName = actionDisplayName = NbBundle.getMessage(
+                    ToggleImportantAction.class,
+                    "ToggleImportantAdd");                              
//NOI18N
+            return true;
+        }
+
+        return false;
+    }
+
+    @Override
+    protected void performAction(final Node[] activatedNodes) {
+
+        for (Node node : activatedNodes) {
+            DatabaseConnection conn = 
node.getLookup().lookup(DatabaseConnection.class);
+            ToggleImportantInfo tii = 
node.getLookup().lookup(ToggleImportantInfo.class);
+            if (conn != null && tii != null
+                    && Catalog.class.isAssignableFrom(tii.getType())) {
+                String name = node.getName();
+                if (name.equals(conn.getDefaultCatalog())) {
+                    tii.setDefault(true);
+                    tii.setImportant(false);
+                    conn.removeImportantCatalog(name);
+                } else if (!conn.isImportantCatalog(name)) {
+                    conn.addImportantCatalog(name);
+                    tii.setDefault(false);
+                    tii.setImportant(true);
+                } else {
+                    conn.removeImportantCatalog(name);
+                    tii.setDefault(false);
+                    tii.setImportant(false);
+                }
+            } else if (conn != null && tii != null
+                    && Schema.class.isAssignableFrom(tii.getType())) {
+                String name = node.getName();
+                if (name.equals(conn.getDefaultSchema())) {
+                    tii.setDefault(true);
+                    tii.setImportant(false);
+                    conn.removeImportantSchema(name);
+                } else if (!conn.isImportantSchema(name)) {
+                    conn.addImportantSchema(name);
+                    tii.setDefault(false);
+                    tii.setImportant(true);
+                } else {
+                    conn.removeImportantSchema(name);
+                    tii.setDefault(false);
+                    tii.setImportant(false);
+                }
+            }
+        }
+    }
+
+    @Override
+    public HelpCtx getHelpCtx() {
+        return null;
+    }
+}
diff --git a/db/src/org/netbeans/modules/db/explorer/node/CatalogNode.java 
b/db/src/org/netbeans/modules/db/explorer/node/CatalogNode.java
--- a/db/src/org/netbeans/modules/db/explorer/node/CatalogNode.java
+++ b/db/src/org/netbeans/modules/db/explorer/node/CatalogNode.java
@@ -44,6 +44,7 @@
 
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
+import javax.swing.SwingWorker;
 import org.netbeans.api.db.explorer.node.BaseNode;
 import org.netbeans.api.db.explorer.node.ChildNodeFactory;
 import org.netbeans.api.db.explorer.node.NodeProvider;
@@ -81,16 +82,19 @@
     private String htmlName = null;
     private final DatabaseConnection connection;
     private final MetadataElementHandle<Catalog> catalogHandle;
+    private final ToggleImportantInfo toggleImportantInfo =
+            new ToggleImportantInfo(Catalog.class);
 
     @SuppressWarnings("unchecked")
     private CatalogNode(NodeDataLookup lookup, NodeProvider provider) {
         super(new ChildNodeFactory(lookup), lookup, FOLDER, provider);
         connection = getLookup().lookup(DatabaseConnection.class);
         catalogHandle = getLookup().lookup(MetadataElementHandle.class);
+        lookup.add(toggleImportantInfo);
     }
 
     protected void initialize() {
-        setupNames();
+        refreshMetaData();
 
         connection.addPropertyChangeListener(
             new PropertyChangeListener() {
@@ -103,28 +107,37 @@
         );
     }
 
-    private void setupNames() {
-        MetadataModel metaDataModel = connection.getMetadataModel();
+    private void refreshMetaData() {
+        final MetadataModel metaDataModel = connection.getMetadataModel();
         boolean connected = !connection.getConnector().isDisconnected();
         if (connected && metaDataModel != null) {
+            new SwingWorker() {
+                @Override
+                protected Object doInBackground() throws Exception {
             try {
                 metaDataModel.runReadAction(
                     new Action<Metadata>() {
+                            @Override
                         public void run(Metadata metaData) {
                             Catalog catalog = 
catalogHandle.resolve(metaData);
+                                
toggleImportantInfo.setDefault(catalog.isDefault());
+                                
toggleImportantInfo.setImportant(connection.isImportantCatalog(name));
                             renderNames(catalog);
                         }
-                    }
-                );
+                        });
+                        return null;
             } catch (MetadataModelException e) {
                 NodeRegistry.handleMetadataModelException(this.getClass(), 
connection, e, true);
+                        return null;
             }
         }
+            }.run();
+        }
     }
 
     @Override
     protected void updateProperties() {
-        setupNames();
+        refreshMetaData();
         super.updateProperties();
     }
 
diff --git 
a/db/src/org/netbeans/modules/db/explorer/node/CatalogNodeProvider.java 
b/db/src/org/netbeans/modules/db/explorer/node/CatalogNodeProvider.java
--- a/db/src/org/netbeans/modules/db/explorer/node/CatalogNodeProvider.java
+++ b/db/src/org/netbeans/modules/db/explorer/node/CatalogNodeProvider.java
@@ -42,6 +42,8 @@
 
 package org.netbeans.modules.db.explorer.node;
 
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Comparator;
@@ -55,14 +57,19 @@
 import org.netbeans.modules.db.metadata.model.api.MetadataElementHandle;
 import org.netbeans.modules.db.metadata.model.api.MetadataModel;
 import org.netbeans.modules.db.metadata.model.api.MetadataModelException;
+import org.openide.nodes.AbstractNode;
+import org.openide.nodes.ChildFactory;
+import org.openide.nodes.Children;
 import org.openide.nodes.Node;
 import org.openide.util.Lookup;
+import org.openide.util.NbBundle;
+import org.openide.util.WeakListeners;
 
 /**
  *
  * @author Rob Englander
  */
-public class CatalogNodeProvider extends NodeProvider {
+public class CatalogNodeProvider extends NodeProvider implements 
PropertyChangeListener {
 
     // lazy initialization holder class idiom for static fields is used
     // for retrieving the factory
@@ -78,7 +85,7 @@
             }
         };
     }
-
+    private final List<Node> nodes = new ArrayList<Node>();
     private final DatabaseConnection connection;
 
     private CatalogNodeProvider(Lookup lookup) {
@@ -89,41 +96,38 @@
     @Override
     protected synchronized void initialize() {
         final List<Node> newList = new ArrayList<Node>();
+        final List<Node> otherList = new ArrayList<Node>();
 
         MetadataModel metaDataModel = connection.getMetadataModel();
         boolean isConnected = !connection.getConnector().isDisconnected();
 
+        nodes.clear();
+
         if (isConnected && metaDataModel != null) {
             try {
                 metaDataModel.runReadAction(
                     new Action<Metadata>() {
+                    @Override
                         public void run(Metadata metaData) {
                             Collection<Catalog> catalogs = 
metaData.getCatalogs();
 
-                            String defaultCatalog = 
metaData.getDefaultCatalog().getName();
-
                             for (Catalog catalog : catalogs) {
                                 boolean oneCatalog = catalogs.size() == 1;
                                 if (catalog.getName() != null || oneCatalog) 
{
+                                if (isDefaultCatalog(catalog, connection)) {
+                                    updateNode(newList, catalog);
+                                } else {
+                                    updateNode(otherList, catalog);
+                                }
+                            }
+                        }
 
-                                    boolean use = true;
-                                    //if (defaultCatalog != null) {
-                                    //    use = 
defaultCatalog.equals(catalog.getName());
-                                    //}
+                        nodes.addAll(newList);
+                        nodes.addAll(otherList);
 
-                                    if (use) {
-                                        MetadataElementHandle<Catalog> 
catalogHandle = MetadataElementHandle.create(catalog);
-                                        Collection<Node> matches = 
getNodes(catalogHandle);
-                                        if (matches.size() > 0) {
-                                            newList.addAll(matches);
-                                        } else {
-                                            NodeDataLookup lookup = new 
NodeDataLookup();
-                                            lookup.add(connection);
-                                            lookup.add(catalogHandle);
-                                            
newList.add(CatalogNode.create(lookup, CatalogNodeProvider.this));
-                                        }
-                                    }
-                                }
+                        if (!otherList.isEmpty()) {
+                            newList.add(new 
CatalogNodeProvider.OtherCatalogsNode(
+                                    otherList));
                             }
 
                             if (newList.size() == 1) {
@@ -132,22 +136,103 @@
                                 setNodes(newList);
                             }
                         }
-                    }
-                );
+                });
             } catch (MetadataModelException e) {
                 NodeRegistry.handleMetadataModelException(this.getClass(), 
connection, e, true);
             }
         } else if (!isConnected) {
            setNodes(newList);
         }
+        
connection.addPropertyChangeListener(WeakListeners.propertyChange(this, 
connection));
+    }
 
+    private boolean isDefaultCatalog(Catalog catalog,
+            DatabaseConnection connection) {
+
+        String def = connection.getDefaultCatalog();
+        return (def == null && catalog.isDefault())
+                || (def != null && def.equals(catalog.getName())
+                || connection.isImportantCatalog(catalog.getName()));
+    }
+
+    private void updateNode(List<Node> newList, Catalog catalog) {
+        MetadataElementHandle<Catalog> catalogHandle = 
MetadataElementHandle.create(catalog);
+        Collection<Node> matches = getNodes(catalogHandle);
+        if (matches != null && matches.size() > 0) {
+            newList.addAll(matches);
+        } else {
+            NodeDataLookup lookup = new NodeDataLookup();
+            lookup.add(connection);
+            lookup.add(catalogHandle);
+
+            newList.add(CatalogNode.create(lookup, this));
+        }
+    }
+
+    @Override
+    public void propertyChange(PropertyChangeEvent pce) {
+        if (this.initialized && 
"importantCatalogs".equals(pce.getPropertyName())) { //NOI18N
+            final List<Node> mainList = new ArrayList<Node>();
+            final List<Node> otherList = new ArrayList<Node>();
+
+            for (Node node : new ArrayList<Node>(nodes)) {
+                if (connection.isImportantCatalog(node.getName())) {
+                    mainList.add(node.cloneNode());
+                } else {
+                    otherList.add(node.cloneNode());
+                }
+            }
+
+            if (!otherList.isEmpty()) {
+                mainList.add(new OtherCatalogsNode(otherList));
+            }
+
+            setNodes(mainList);
+        }
     }
 
     static class CatalogComparator implements Comparator<Node> {
 
+        @Override
         public int compare(Node node1, Node node2) {
+            if (node1 instanceof OtherCatalogsNode) {
+                return 1;
+            }
+            if (node2 instanceof OtherCatalogsNode) {
+                return -1;
+            }
             return 
node1.getDisplayName().compareToIgnoreCase(node2.getDisplayName());
         }
+    }
 
+    @NbBundle.Messages({
+        "LBL_OtherDatabases=Other databases"
+    })
+    private static class OtherCatalogsNode extends AbstractNode {
+
+        private static final String ICON_BASE =
+                "org/netbeans/modules/db/resources/database.gif";       
//NOI18N
+
+        public OtherCatalogsNode(List<Node> otherList) {
+            super(createChildren(otherList));
+            setDisplayName(Bundle.LBL_OtherDatabases());
+            setIconBaseWithExtension(ICON_BASE);
     }
+
+        private static Children createChildren(final List<Node> otherList) {
+            Children c = Children.create(new ChildFactory<Node>() {
+                @Override
+                protected boolean createKeys(final List<Node> toPopulate) {
+                    toPopulate.addAll(otherList);
+                    return true;
 }
+
+                @Override
+                protected Node createNodeForKey(Node key) {
+                    return key;
+                }
+            }, false);
+            return c;
+        }
+    }
+}
diff --git a/db/src/org/netbeans/modules/db/explorer/node/SchemaNode.java 
b/db/src/org/netbeans/modules/db/explorer/node/SchemaNode.java
--- a/db/src/org/netbeans/modules/db/explorer/node/SchemaNode.java
+++ b/db/src/org/netbeans/modules/db/explorer/node/SchemaNode.java
@@ -44,6 +44,7 @@
 
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
+import javax.swing.SwingWorker;
 import org.netbeans.api.db.explorer.node.BaseNode;
 import org.netbeans.api.db.explorer.node.ChildNodeFactory;
 import org.netbeans.api.db.explorer.node.NodeProvider;
@@ -82,12 +83,15 @@
 
     private final MetadataElementHandle<Schema> schemaHandle;
     private final DatabaseConnection connection;
+    private final ToggleImportantInfo toggleImportantInfo =
+            new ToggleImportantInfo(Schema.class);
 
     @SuppressWarnings("unchecked")
     private SchemaNode(NodeDataLookup lookup, NodeProvider provider) {
         super(new ChildNodeFactory(lookup), lookup, FOLDER, provider);
         connection = getLookup().lookup(DatabaseConnection.class);
         schemaHandle = getLookup().lookup(MetadataElementHandle.class);
+        lookup.add(toggleImportantInfo);
     }
 
     @Override
@@ -108,22 +112,30 @@
 
     private void setupNames() {
         boolean connected = !connection.getConnector().isDisconnected();
-        MetadataModel metaDataModel = connection.getMetadataModel();
+        final MetadataModel metaDataModel = connection.getMetadataModel();
         if (connected && metaDataModel != null) {
+            new SwingWorker() {
+                @Override
+                protected Object doInBackground() throws Exception {
             try {
                 metaDataModel.runReadAction(
                     new Action<Metadata>() {
                     @Override
                         public void run(Metadata metaData) {
                             Schema schema = schemaHandle.resolve(metaData);
+                                
toggleImportantInfo.setDefault(schema.isDefault());
+                                
toggleImportantInfo.setImportant(connection.isImportantSchema(name));
                             renderNames(schema);
                         }
-                    }
-                );
+                        });
+                        return null;
             } catch (MetadataModelException e) {
                 NodeRegistry.handleMetadataModelException(this.getClass(), 
connection, e, true);
+                        return null;
             }
         }
+            }.run();
+        }
     }
 
     @Override
diff --git 
a/db/src/org/netbeans/modules/db/explorer/node/SchemaNodeProvider.java 
b/db/src/org/netbeans/modules/db/explorer/node/SchemaNodeProvider.java
--- a/db/src/org/netbeans/modules/db/explorer/node/SchemaNodeProvider.java
+++ b/db/src/org/netbeans/modules/db/explorer/node/SchemaNodeProvider.java
@@ -42,6 +42,8 @@
 
 package org.netbeans.modules.db.explorer.node;
 
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Comparator;
@@ -56,14 +58,19 @@
 import org.netbeans.modules.db.metadata.model.api.MetadataModel;
 import org.netbeans.modules.db.metadata.model.api.MetadataModelException;
 import org.netbeans.modules.db.metadata.model.api.Schema;
+import org.openide.nodes.AbstractNode;
+import org.openide.nodes.ChildFactory;
+import org.openide.nodes.Children;
 import org.openide.nodes.Node;
 import org.openide.util.Lookup;
+import org.openide.util.NbBundle;
+import org.openide.util.WeakListeners;
 
 /**
  *
  * @author Rob Englander
  */
-public class SchemaNodeProvider extends NodeProvider {
+public class SchemaNodeProvider extends NodeProvider implements 
PropertyChangeListener {
 
     // lazy initialization holder class idiom for static fields is used
     // for retrieving the factory
@@ -81,6 +88,7 @@
         };
     }
 
+    private final List<Node> nodes = new ArrayList<Node>();
     private final DatabaseConnection connection;
     private final MetadataElementHandle<Catalog> catalogHandle;
 
@@ -93,9 +101,13 @@
     @Override
     protected synchronized void initialize() {
         final List<Node> newList = new ArrayList<Node>();
+        final List<Node> otherList = new ArrayList<Node>();
 
         boolean connected = !connection.getConnector().isDisconnected();
         MetadataModel metaDataModel = connection.getMetadataModel();
+
+        nodes.clear();
+
         if (connected && metaDataModel != null) {
             try {
                 metaDataModel.runReadAction(
@@ -111,7 +123,18 @@
                                 } else {
                                     Collection<Schema> schemas = 
cat.getSchemas();
                                     for (Schema schema : schemas) {
+                                        if (isDefaultSchema(schema, 
connection)) {
                                         updateNode(newList, schema);
+                                        } else {
+                                            updateNode(otherList, schema);
+                                        }
+                                    }
+                                    nodes.addAll(newList);
+                                    nodes.addAll(otherList);
+
+                                    if (!otherList.isEmpty()) {
+                                        newList.add(new OtherSchemasNode(
+                                                otherList));
                                     }
                                 }
 
@@ -130,12 +153,22 @@
         } else {
             setNodes(newList);
         }
+        
connection.addPropertyChangeListener(WeakListeners.propertyChange(this, 
connection));
+    }
+
+    private boolean isDefaultSchema(Schema schema,
+            DatabaseConnection connection) {
+
+        String def = connection.getDefaultSchema();
+        return (def == null && schema.isDefault())
+                || (def != null && def.equals(schema.getName())
+                || connection.isImportantSchema(schema.getName()));
     }
 
     private void updateNode(List<Node> newList, Schema schema) {
         MetadataElementHandle<Schema> schemaHandle = 
MetadataElementHandle.create(schema);
         Collection<Node> matches = getNodes(schemaHandle);
-        if (matches.size() > 0) {
+        if (matches != null && matches.size() > 0) {
             newList.addAll(matches);
         } else {
             NodeDataLookup lookup = new NodeDataLookup();
@@ -146,14 +179,70 @@
         }
     }
 
+    @Override
+    public void propertyChange(PropertyChangeEvent pce) {
+        if (this.initialized && 
"importantSchemas".equals(pce.getPropertyName())) {
+            final List<Node> mainList = new ArrayList<Node>();
+            final List<Node> otherList = new ArrayList<Node>();
+
+            for(Node node: new ArrayList<Node>(nodes)) {
+                if(connection.isImportantSchema(node.getName())) {
+                    mainList.add(node.cloneNode());
+                } else {
+                    otherList.add(node.cloneNode());
+                }
+            }
+            if(! otherList.isEmpty()) {
+                mainList.add(new OtherSchemasNode(otherList));
+            }
+            setNodes(mainList);
+        }
+    }
+
     static class SchemaComparator implements Comparator<Node> {
 
         @Override
         public int compare(Node node1, Node node2) {
             assert node1.getDisplayName() != null : node1 + " has display 
name.";
             assert node2.getDisplayName() != null : node2 + " has display 
name.";
+            if(node1 instanceof OtherSchemasNode) {
+                return 1;
+            }
+            if(node2 instanceof OtherSchemasNode) {
+                return -1;
+            }
             return 
node1.getDisplayName().compareToIgnoreCase(node2.getDisplayName());
         }
+    }
 
+    @NbBundle.Messages({
+        "LBL_OtherSchemas=Other schemas"
+    })
+    private static class OtherSchemasNode extends AbstractNode {
+
+        private static final String ICON_BASE =
+                "org/netbeans/modules/db/resources/schema.png";         
//NOI18N
+
+        public OtherSchemasNode(List<Node> otherList) {
+            super(createChildren(otherList));
+            setDisplayName(Bundle.LBL_OtherSchemas());
+            setIconBaseWithExtension(ICON_BASE);
     }
+
+        private static Children createChildren(final List<Node> otherList) {
+            Children c = Children.create(new ChildFactory<Node>() {
+                @Override
+                protected boolean createKeys(final List<Node> toPopulate) {
+                    toPopulate.addAll(otherList);
+                    return true;
 }
+
+                @Override
+                protected Node createNodeForKey(Node key) {
+                    return key;
+                }
+            }, false);
+            return c;
+        }
+    }
+}
diff --git 
a/db/src/org/netbeans/modules/db/explorer/node/ToggleImportantInfo.java 
b/db/src/org/netbeans/modules/db/explorer/node/ToggleImportantInfo.java
new file mode 100644
--- /dev/null
+++ b/db/src/org/netbeans/modules/db/explorer/node/ToggleImportantInfo.java
@@ -0,0 +1,84 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
+ *
+ * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
+ * Other names may be trademarks of their respective owners.
+ *
+ * The contents of this file are subject to the terms of either the GNU
+ * General Public License Version 2 only ("GPL") or the Common
+ * Development and Distribution License("CDDL") (collectively, the
+ * "License"). You may not use this file except in compliance with the
+ * License. You can obtain a copy of the License at
+ * http://www.netbeans.org/cddl-gplv2.html
+ * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
+ * specific language governing permissions and limitations under the
+ * License.  When distributing the software, include this License Header
+ * Notice in each file and include the License file at
+ * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the GPL Version 2 section of the License file that
+ * accompanied this code. If applicable, add the following below the
+ * License Header, with the fields enclosed by brackets [] replaced by
+ * your own identifying information:
+ * "Portions Copyrighted [year] [name of copyright owner]"
+ *
+ * If you wish your version of this file to be governed by only the CDDL
+ * or only the GPL Version 2, indicate your decision by adding
+ * "[Contributor] elects to include this software in this distribution
+ * under the [CDDL or GPL Version 2] license." If you do not indicate a
+ * single choice of license, a recipient has the option to distribute
+ * your version of this file under either the CDDL, the GPL Version 2 or
+ * to extend the choice of license to its licensees as provided above.
+ * However, if you add GPL Version 2 code and therefore, elected the GPL
+ * Version 2 license, then the option applies only if the new code is
+ * made subject to such option by the copyright holder.
+ *
+ * Contributor(s):
+ *
+ * Portions Copyrighted 2013 Sun Microsystems, Inc.
+ */
+package org.netbeans.modules.db.explorer.node;
+
+import org.netbeans.modules.db.metadata.model.api.MetadataElement;
+
+/**
+ * Represent "important" state for catalogs and schemas
+ *
+ * @author matthias
+ */
+public class ToggleImportantInfo {
+
+    private Class<? extends MetadataElement> type;
+    private boolean important;
+    private boolean isDefault;
+
+    public ToggleImportantInfo(Class type) {
+        this.type = type;
+    }
+
+    public Class getType() {
+        return type;
+    }
+
+    public void setType(Class type) {
+        this.type = type;
+    }
+
+    public boolean isImportant() {
+        return important;
+    }
+
+    public void setImportant(boolean important) {
+        this.important = important;
+    }
+
+    public boolean isDefault() {
+        return isDefault;
+    }
+
+    public void setDefault(boolean dflt) {
+        this.isDefault = dflt;
+    }
+}
diff --git a/db/src/org/netbeans/modules/db/resources/mf-layer.xml 
b/db/src/org/netbeans/modules/db/resources/mf-layer.xml
--- a/db/src/org/netbeans/modules/db/resources/mf-layer.xml
+++ b/db/src/org/netbeans/modules/db/resources/mf-layer.xml
@@ -177,6 +177,9 @@
                     <file 
name="org-netbeans-modules-db-explorer-action-RefreshAction.instance">
                         <attr name="position" intvalue="350"/>
                     </file>
+                    <file 
name="org-netbeans-modules-db-explorer-action-ToggleImportantAction.instance">
+                        <attr name="position" intvalue="450"/>
+                    </file>
                 </folder>
             </folder>
             <folder name="Schema">
@@ -204,6 +207,9 @@
                     <file 
name="org-netbeans-modules-db-explorer-action-RefreshAction.instance">
                         <attr name="position" intvalue="350"/>
                     </file>
+                    <file 
name="org-netbeans-modules-db-explorer-action-ToggleImportantAction.instance">
+                        <attr name="position" intvalue="450"/>
+                    </file>
                 </folder>
             </folder>
             <folder name="TableList">

[hg] main-silver: #202833: Add ability to filter the listed data...

Jaroslav Havlin 02/18/2013

Project Features

About this Project

DB was started in November 2009, is owned by Antonin Nebuzelsky, and has 113 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20131025.e7cbc9d). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close