Author: jfuerth
Date: Fri Oct 17 09:11:40 2008
New Revision: 2789

Added:
   trunk/src/ca/sqlpower/architect/swingui/ProfiledTableIconFilter.java
   trunk/src/ca/sqlpower/architect/swingui/dbtree/IconFilter.java
Modified:
   trunk/src/ca/sqlpower/architect/swingui/DBTree.java
   trunk/src/ca/sqlpower/architect/swingui/DataMoverPanel.java
   trunk/src/ca/sqlpower/architect/swingui/ProfileRowComponent.java
   trunk/src/ca/sqlpower/architect/swingui/dbtree/DBTreeCellRenderer.java

Log:
Added an IconFilter interface to allow specifying a new icon on DBTree objects based on a definition from a different part of the app. This removed all knowledge of the ArchitectSession and profiler from the DBTree module.

Modified: trunk/src/ca/sqlpower/architect/swingui/DBTree.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/DBTree.java (original)
+++ trunk/src/ca/sqlpower/architect/swingui/DBTree.java Fri Oct 17 09:11:40 2008
@@ -126,7 +126,9 @@
collapseAllAction = new JTreeCollapseAllAction(this, Messages.getString("DBTree.collapseAllActionName")); expandAllAction = new JTreeExpandAllAction(this, Messages.getString("DBTree.expandAllActionName"));
                addMouseListener(new PopupListener());
-        setCellRenderer(new DBTreeCellRenderer(session));
+        DBTreeCellRenderer tcr = new DBTreeCellRenderer();
+        tcr.addIconFilter(new ProfiledTableIconFilter());
+        setCellRenderer(tcr);
         selectAllChildTablesAction = new SelectAllChildTablesAction();
        }


Modified: trunk/src/ca/sqlpower/architect/swingui/DataMoverPanel.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/DataMoverPanel.java (original)
+++ trunk/src/ca/sqlpower/architect/swingui/DataMoverPanel.java Fri Oct 17 09:11:40 2008
@@ -105,12 +105,12 @@
         sourceTree = new JTree(new DBTreeModel(treeRoot));
         sourceTree.setRootVisible(false);
         sourceTree.setShowsRootHandles(true);
-        sourceTree.setCellRenderer(new DBTreeCellRenderer(session));
+        sourceTree.setCellRenderer(new DBTreeCellRenderer());

         destTree = new JTree(new DBTreeModel(treeRoot));
         destTree.setRootVisible(false);
         destTree.setShowsRootHandles(true);
-        destTree.setCellRenderer(new DBTreeCellRenderer(session));
+        destTree.setCellRenderer(new DBTreeCellRenderer());

         PanelBuilder pb = new PanelBuilder(
                 new FormLayout(

Modified: trunk/src/ca/sqlpower/architect/swingui/ProfileRowComponent.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/ProfileRowComponent.java (original) +++ trunk/src/ca/sqlpower/architect/swingui/ProfileRowComponent.java Fri Oct 17 09:11:40 2008
@@ -79,7 +79,7 @@
     }

     /** The icon for all the rows (shared) */
-    private static ImageIcon tableIcon = DBTreeCellRenderer.tableIcon;
+    private static ImageIcon tableIcon = DBTreeCellRenderer.TABLE_ICON;

     /** The Stop Sign icon for all the rows (shared) */
     private static ImageIcon stopIcon =

Added: trunk/src/ca/sqlpower/architect/swingui/ProfiledTableIconFilter.java
==============================================================================
--- (empty file)
+++ trunk/src/ca/sqlpower/architect/swingui/ProfiledTableIconFilter.java Fri Oct 17 09:11:40 2008
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2008, SQL Power Group Inc.
+ *
+ * This file is part of Power*Architect.
+ *
+ * Power*Architect is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Power*Architect is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package ca.sqlpower.architect.swingui;
+
+import javax.swing.Icon;
+import javax.swing.ImageIcon;
+
+import ca.sqlpower.architect.SQLObject;
+import ca.sqlpower.architect.SQLTable;
+import ca.sqlpower.architect.profile.ProfileManager;
+import ca.sqlpower.architect.swingui.dbtree.IconFilter;
+import ca.sqlpower.swingui.SPSUtils;
+
+/**
+ * Replaces the table icon with the "profiled table" icon for tables that
+ * have been profiled.
+ */
+public class ProfiledTableIconFilter implements IconFilter {
+
+ public static final ImageIcon PROFILED_DATABASE_ICON = SPSUtils.createIcon("Database_profiled", "SQL Database", ArchitectSwingSessionContext.ICON_SIZE); //$NON-NLS-1$ //$NON-NLS-2$ + public static final ImageIcon PROFILED_TABLE_ICON = SPSUtils.createIcon("Table_profiled", "SQL Table", ArchitectSwingSessionContext.ICON_SIZE); //$NON-NLS-1$ //$NON-NLS-2$
+
+    public Icon filterIcon(Icon original, SQLObject node) {
+        if (node instanceof SQLTable) {
+ Object profileCount = node.getClientProperty(ProfileManager.class, ProfileManager.PROFILE_COUNT_PROPERTY); + if (profileCount != null && ((Integer) profileCount).intValue() > 0) {
+                return PROFILED_TABLE_ICON;
+            }
+        }
+        return original;
+    }
+
+
+}

Modified: trunk/src/ca/sqlpower/architect/swingui/dbtree/DBTreeCellRenderer.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/dbtree/DBTreeCellRenderer.java (original) +++ trunk/src/ca/sqlpower/architect/swingui/dbtree/DBTreeCellRenderer.java Fri Oct 17 09:11:40 2008
@@ -21,12 +21,14 @@

 import java.awt.Color;
 import java.awt.Component;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;

 import javax.swing.ImageIcon;
 import javax.swing.JTree;
 import javax.swing.tree.DefaultTreeCellRenderer;

-import ca.sqlpower.architect.ArchitectSession;
 import ca.sqlpower.architect.SQLCatalog;
 import ca.sqlpower.architect.SQLColumn;
 import ca.sqlpower.architect.SQLDatabase;
@@ -36,9 +38,7 @@
 import ca.sqlpower.architect.SQLSchema;
 import ca.sqlpower.architect.SQLTable;
 import ca.sqlpower.architect.SQLIndex.Column;
-import ca.sqlpower.architect.swingui.ArchitectSwingSessionContext;
 import ca.sqlpower.architect.swingui.Messages;
-import ca.sqlpower.swingui.SPSUtils;

 /**
  * The DBTreeCellRenderer renders nodes of a JTree which are of
@@ -52,30 +52,25 @@
  */
 public class DBTreeCellRenderer extends DefaultTreeCellRenderer {

- public static final ImageIcon dbProfiledIcon = SPSUtils.createIcon("Database_profiled", "SQL Database", ArchitectSwingSessionContext.ICON_SIZE); //$NON-NLS-1$ //$NON-NLS-2$ - public static final ImageIcon tableProfiledIcon = SPSUtils.createIcon("Table_profiled", "SQL Table", ArchitectSwingSessionContext.ICON_SIZE); //$NON-NLS-1$ //$NON-NLS-2$
-
- public static final ImageIcon dbIcon = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Database16.png")); - public static final ImageIcon targetIcon = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Database_target16.png")); - public static final ImageIcon cataIcon = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Catalog16.png")); - public static final ImageIcon schemaIcon = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Schema16.png")); - public static final ImageIcon tableIcon = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Table16.png")); - public static final ImageIcon exportedKeyIcon = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/ExportedKey16.png")); - public static final ImageIcon importedKeyIcon = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/ImportedKey16.png")); - public static final ImageIcon ownerIcon = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Owner16.png")); - public static final ImageIcon indexIcon = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Index16.png")); - public static final ImageIcon pkIndexIcon = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Index_key16.png")); - public static final ImageIcon uniqueIndexIcon = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Index_unique16.png")); - public static final ImageIcon columnIcon = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Column16.png"));
-    private final ArchitectSession session;
+ public static final ImageIcon DB_ICON = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Database16.png")); + public static final ImageIcon TARGET_DB_ICON = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Database_target16.png")); + public static final ImageIcon CATALOG_ICON = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Catalog16.png")); + public static final ImageIcon SCHEMA_ICON = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Schema16.png")); + public static final ImageIcon TABLE_ICON = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Table16.png")); + public static final ImageIcon EXPORTED_KEY_ICON = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/ExportedKey16.png")); + public static final ImageIcon IMPORTED_KEY_ICON = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/ImportedKey16.png")); + public static final ImageIcon OWNER_ICON = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Owner16.png")); + public static final ImageIcon INDEX_ICON = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Index16.png")); + public static final ImageIcon PK_ICON = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Index_key16.png")); + public static final ImageIcon UNIQUE_INDEX_ICON = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Index_unique16.png")); + public static final ImageIcon COLUMN_ICON = new ImageIcon(DBTreeCellRenderer.class.getResource("icons/Column16.png"));

+ private final List<IconFilter> iconFilterChain = new ArrayList<IconFilter>();

-       public DBTreeCellRenderer(ArchitectSession session) {
+       public DBTreeCellRenderer() {
         super();
-        this.session = session;
     }

-
     public Component getTreeCellRendererComponent(JTree tree,
                                                                                
                  Object value,
                                                                                
                  boolean sel,
@@ -87,35 +82,31 @@
                if (value instanceof SQLDatabase) {
                        SQLDatabase db = (SQLDatabase) value;
                        if (db.isPlayPenDatabase()) {
-                               setIcon(targetIcon);
+                               setIcon(TARGET_DB_ICON);
setText(Messages.getString("DBTreeCellRenderer.playpenNodeText")); //$NON-NLS-1$
                        } else {
-                               setIcon(dbIcon);
+                               setIcon(DB_ICON);
                        }
                } else if (value instanceof SQLCatalog) {
if (((SQLCatalog) value).getNativeTerm().equals("owner")) { //$NON-NLS-1$
-                               setIcon(ownerIcon);
+                               setIcon(OWNER_ICON);
} else if (((SQLCatalog) value).getNativeTerm().equals("database")) { //$NON-NLS-1$
-                               setIcon(dbIcon);
+                               setIcon(DB_ICON);
} else if (((SQLCatalog) value).getNativeTerm().equals("schema")) { //$NON-NLS-1$
-                               setIcon(schemaIcon);
+                               setIcon(SCHEMA_ICON);
                        } else {
-                               setIcon(cataIcon);
+                               setIcon(CATALOG_ICON);
                        }
                } else if (value instanceof SQLSchema) {
                        if (((SQLSchema) 
value).getNativeTerm().equals("owner")) { //$NON-NLS-1$
-                               setIcon(ownerIcon);
+                               setIcon(OWNER_ICON);
                        } else {
-                               setIcon(schemaIcon);
+                               setIcon(SCHEMA_ICON);
                        }
                } else if (value instanceof SQLTable) {
-
-                       SQLTable table = (SQLTable) value;
-            if (session.getProfileManager().getResults(table).size() > 0) {
-                setIcon(tableProfiledIcon);
-            } else {
-                setIcon(tableIcon);
-            }
+                   setIcon(TABLE_ICON);
+                       
+                   SQLTable table = (SQLTable) value;
             if ((table).getObjectType() != null) {
setText((table).getName()+" ("+(table).getObjectType()+")"); //$NON-NLS-1$ //$NON-NLS-2$
                        } else {
@@ -125,25 +116,25 @@
//XXX ARRRRRRGGGGGHHHHHHH!!!! No way of knowing which end of a relationship we're // looking at because the relationship has two parents. Maybe able to do it with the row number.
             if (true) {
-                setIcon(exportedKeyIcon);
+                setIcon(EXPORTED_KEY_ICON);
             } else {
-                setIcon(importedKeyIcon);
+                setIcon(IMPORTED_KEY_ICON);
             }
                } else if (value instanceof SQLIndex) {
             SQLIndex i = (SQLIndex) value;
             if (i.isPrimaryKeyIndex()) {
-                setIcon(pkIndexIcon);
+                setIcon(PK_ICON);
             } else if (i.isUnique()) {
-                setIcon(uniqueIndexIcon);
+                setIcon(UNIQUE_INDEX_ICON);
             } else {
-                setIcon(indexIcon);
+                setIcon(INDEX_ICON);
             }
         } else if (value instanceof SQLColumn) {
             tagColumn((SQLColumn)value);
-            setIcon(columnIcon);
+            setIcon(COLUMN_ICON);
         } else if (value instanceof Column) {
             tagColumn(((Column)value).getColumn());
-            setIcon(columnIcon);
+            setIcon(COLUMN_ICON);
         } else {
                        setIcon(null);
                }
@@ -159,6 +150,13 @@
                    }
                }
            setToolTipText(getText());
+       
+           if (value instanceof SQLObject || value == null) {
+               for (IconFilter filter : getIconFilterChain()) {
+                   setIcon(filter.filterIcon(getIcon(), (SQLObject) value));
+               }
+           }
+       
                return this;
        }

@@ -192,5 +190,41 @@
             fullTag.append(" ]"); //$NON-NLS-1$
             setText(getText() + fullTag.toString());
         }
+    }
+
+    /**
+ * Adds the given icon filter to the end of the filter chain. The filter + * will be invoked after all currently existing filters on this renderer.
+     *
+     * @param filter
+     *            The filter to add. Must not be null.
+     */
+    public void addIconFilter(IconFilter filter) {
+        if (filter == null) {
+ throw new NullPointerException("Null icon filters not allowed");
+        }
+        iconFilterChain.add(filter);
+    }
+
+    /**
+ * Removed the given icon filter chain from this renderer's filter chain. If + * the given filter is not in the list, calling this method has no effect.
+     *
+     * @param filter
+     *            The filter to remove
+     * @return True if the filter was in the list (so it has been removed);
+     *         false if the filter was not in the list (so the list remains
+     *         unchanged).
+     */
+    public boolean removeIconFilter(IconFilter filter) {
+        return iconFilterChain.remove(filter);
+    }
+
+    /**
+ * Returns a read-only view of this renderer's filter chain. The filters
+     * are invoked in the order that they exist in this list.
+     */
+    public List<IconFilter> getIconFilterChain() {
+        return Collections.unmodifiableList(iconFilterChain);
     }
 }

Added: trunk/src/ca/sqlpower/architect/swingui/dbtree/IconFilter.java
==============================================================================
--- (empty file)
+++ trunk/src/ca/sqlpower/architect/swingui/dbtree/IconFilter.java Fri Oct 17 09:11:40 2008
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2008, SQL Power Group Inc.
+ *
+ * This file is part of Power*Architect.
+ *
+ * Power*Architect is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Power*Architect is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package ca.sqlpower.architect.swingui.dbtree;
+
+import javax.swing.Icon;
+
+import ca.sqlpower.architect.SQLObject;
+
+/**
+ * This interface is a hook into the DBTreeCellRenderer that allows clients
+ * to modify or substitute the icon displayed for every node in the DB Tree.
+ * <p>
+ * Implementing classes are given the icon that the renderer would have used
+ * in the absence of filters, and the SQLObject that is represented by the
+ * icon. The filter may then either return the original icon as-is, return a + * different icon, or return the original icon wrapped in some way (for instance, + * composing the original icon with a decoration that indicates some difference + * in state such as an error or warning condition, or a greyed-out version of
+ * the icon to indicate the object is disabled or unavailable).
+ */
+public interface IconFilter {
+
+    /**
+ * Returns an icon to use in place of the given icon, or returns the given
+     * icon if no substitution is necessary.
+     *
+     * @param original
+     *            The icon that would have been used if this filter did not
+ * exist. If there was not going to be an icon on the given node,
+     *            this parameter will be null.
+     * @param node
+     *            The tree node the icon applies to.
+     * @return The icon that should be used instead of original, or
+ * <code>null</code> if the tree node should have no icon at all.
+     */
+    Icon filterIcon(Icon original, SQLObject node);
+
+}

Reply via email to