Author: daceywang
Date: Mon Apr 20 13:12:05 2009
New Revision: 2983

Modified:
trunk/regress/ca/sqlpower/architect/swingui/TestingArchitectSwingSession.java
   trunk/src/ca/sqlpower/architect/swingui/ArchitectSwingSession.java
   trunk/src/ca/sqlpower/architect/swingui/ArchitectSwingSessionImpl.java
   trunk/src/ca/sqlpower/architect/swingui/BasicRelationshipUI.java
   trunk/src/ca/sqlpower/architect/swingui/ProjectSettingsPanel.java
   trunk/src/ca/sqlpower/architect/swingui/Relationship.java
   trunk/src/ca/sqlpower/architect/swingui/SwingUIProject.java
   trunk/src/ca/sqlpower/architect/swingui/messages.properties

Log:
Fixed the bug 1483 which requires relationship label to be added. Now user can toggle to display or hide relationship label from project settings and the label is designed to be clockwise readable. This commit doesn't support dragging labels to different places, but I think that feature will be accomplished by Architect User Created Label function in the future. I suppose the bug 1483 is resolved.

Modified: trunk/regress/ca/sqlpower/architect/swingui/TestingArchitectSwingSession.java
==============================================================================
--- trunk/regress/ca/sqlpower/architect/swingui/TestingArchitectSwingSession.java (original) +++ trunk/regress/ca/sqlpower/architect/swingui/TestingArchitectSwingSession.java Mon Apr 20 13:12:05 2009
@@ -79,6 +79,7 @@
     private boolean showFkTag = true;
     private boolean showAkTag = true;

+    private boolean displayRelationshipLabel = false;
     private boolean usingLogicalNames = false;

     private ColumnVisibility choice = ColumnVisibility.ALL;
@@ -352,5 +353,12 @@
     public void setUsingLogicalNames(boolean usingLogicalNames) {
         this.usingLogicalNames = usingLogicalNames;
     }
+
+    public boolean isDisplayRelationshipLabel() {
+        return displayRelationshipLabel;
+    }

+ public void setDisplayRelationshipLabel(boolean displayRelationshipLabel) {
+        this.displayRelationshipLabel = displayRelationshipLabel;
+    }
 }

Modified: trunk/src/ca/sqlpower/architect/swingui/ArchitectSwingSession.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/ArchitectSwingSession.java (original) +++ trunk/src/ca/sqlpower/architect/swingui/ArchitectSwingSession.java Mon Apr 20 13:12:05 2009
@@ -202,6 +202,19 @@
      * physical names are to be displayed.
      */
     public void setUsingLogicalNames(boolean usingLogicalNames);
+
+    /**
+     * Relationship Label: True means display the relationship label; false
+     * means hide the relationship label.
+     */
+    public boolean isDisplayRelationshipLabel();
+
+    /**
+     * Relationship Label: True means display the relationship label; false
+ * means hide the relationship label. Updating this will hide or display all
+     * the relationship labels in this session's play pen.
+     */
+ public void setDisplayRelationshipLabel(boolean displayRelationshipLabel);

     /**
      * Returns whether the PK Tags will be shown

Modified: trunk/src/ca/sqlpower/architect/swingui/ArchitectSwingSessionImpl.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/ArchitectSwingSessionImpl.java (original) +++ trunk/src/ca/sqlpower/architect/swingui/ArchitectSwingSessionImpl.java Mon Apr 20 13:12:05 2009
@@ -139,6 +139,8 @@
     private Set<SPSwingWorker> swingWorkers;

     private ProjectModificationWatcher projectModificationWatcher;
+
+    private boolean displayRelationshipLabel;

     private boolean relationshipLinesDirect;

@@ -952,4 +954,11 @@
         return printSettings;
     }

+    public boolean isDisplayRelationshipLabel() {
+        return displayRelationshipLabel;
+    }
+
+ public void setDisplayRelationshipLabel(boolean displayRelationshipLabel) {
+        this.displayRelationshipLabel = displayRelationshipLabel;
+    }
 }

Modified: trunk/src/ca/sqlpower/architect/swingui/BasicRelationshipUI.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/BasicRelationshipUI.java (original) +++ trunk/src/ca/sqlpower/architect/swingui/BasicRelationshipUI.java Mon Apr 20 13:12:05 2009
@@ -21,6 +21,7 @@
 import java.awt.BasicStroke;
 import java.awt.Color;
 import java.awt.Dimension;
+import java.awt.FontMetrics;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.Point;
@@ -123,6 +124,8 @@
                logger.debug("BasicRelationshipUI is painting");
                Relationship r = (Relationship) c;
                Graphics2D g2 = (Graphics2D) g;
+               FontMetrics fm = r.getFontMetrics(r.getFont());
+               logger.debug("orientation is: " + orientation);
                if (g2 == null)
                {
                        throw new NullPointerException("Graphics g2 is null");
@@ -173,7 +176,29 @@

                        // XXX: could optimise by checking if PK or FK tables 
have moved
                        containmentPath = new 
GeneralPath(GeneralPath.WIND_NON_ZERO, 10);
-
+                       
+                       float parentToChildLabelStart = 0;
+                       float childToParentLabelStart = 0;
+                       g2.setColor(r.getForegroundColor());
+                       
+                       float leftmost = 0;
+            float rightmost = 0;
+            if (r.displayRelationshipLabel()) {
+                if (lineStart.x > lineEnd.x) {
+ logger.debug("Parent is at the right side of children");
+                    leftmost = lineEnd.x;
+                    rightmost = lineStart.x;
+                } else {
+                    logger.debug("Parent is at the left side of children");
+                    leftmost = lineStart.x;
+                    rightmost = lineEnd.x;
+                }
+ parentToChildLabelStart = calculateRelationshipLabelStart(leftmost, rightmost, + fm.stringWidth(Relationship.PAREENT_TO_CHILD)); + childToParentLabelStart = calculateRelationshipLabelStart(leftmost, rightmost, + fm.stringWidth(Relationship.CHILD_TO_PARENT));
+            }
+
             if (relationship.getPkTable() == relationship.getFkTable()) {
                                // special case hack for self-referencing table
                                // assume orientation is PARENT_FACES_BOTTOM | 
CHILD_FACES_LEFT
@@ -184,7 +209,17 @@
                                containmentPath.lineTo(lineEnd.x - 
getTerminationLength(), lineEnd.y);
                 containmentPath.lineTo(lineEnd.x, lineEnd.y);
                 containmentPath.lineTo(end.x, end.y);
-                               path = new GeneralPath(containmentPath);
+                path = new GeneralPath(containmentPath);
+                // draw relationship labels
+                if (r.displayRelationshipLabel()) {
+ parentToChildLabelStart = calculateRelationshipLabelStart(lineEnd.x - getTerminationLength(), + lineStart.x, fm.stringWidth(Relationship.PAREENT_TO_CHILD)); + childToParentLabelStart = calculateRelationshipLabelStart(lineEnd.x - getTerminationLength(), + lineStart.x, fm.stringWidth(Relationship.CHILD_TO_PARENT)); + logger.debug("relationship label starts at: " + parentToChildLabelStart); + g2.drawString(Relationship.PAREENT_TO_CHILD, parentToChildLabelStart, lineStart.y + fm.getHeight()); + g2.drawString(Relationship.CHILD_TO_PARENT, childToParentLabelStart, lineStart.y + 2*fm.getHeight());
+                }
                                
                                containmentPath.lineTo(lineEnd.x - 
getTerminationLength(), lineEnd.y);
containmentPath.lineTo(lineEnd.x - getTerminationLength(), lineStart.y + getTerminationLength());
@@ -207,7 +242,31 @@
                                containmentPath.lineTo(lineEnd.x, lineEnd.y);
                 containmentPath.lineTo(end.x, end.y);
                                path = new GeneralPath(containmentPath);
-
+                               // draw relationship labels
+                               if (r.displayRelationshipLabel()) {
+                               if ((orientation & PARENT_FACES_LEFT) != 0) {
+                                   if (lineStart.y < lineEnd.y) {
+                                       logger.debug("pk table is at right and 
higher");
+ g2.drawString(Relationship.PAREENT_TO_CHILD, parentToChildLabelStart, lineEnd.y + fm.getAscent()); + g2.drawString(Relationship.CHILD_TO_PARENT, childToParentLabelStart, lineStart.y - fm.getDescent());
+                                   } else {
+                                       logger.debug("pk table is at right and 
lower");
+ g2.drawString(Relationship.PAREENT_TO_CHILD, parentToChildLabelStart, lineStart.y + fm.getAscent()); + g2.drawString(Relationship.CHILD_TO_PARENT, childToParentLabelStart, lineEnd.y - fm.getDescent());
+                                   }
+                               } else {
+                                   if (lineStart.y < lineEnd.y) {
+                                       logger.debug("pk table is at left and 
higher");
+ g2.drawString(Relationship.PAREENT_TO_CHILD, parentToChildLabelStart, lineStart.y - fm.getDescent()); + g2.drawString(Relationship.CHILD_TO_PARENT, childToParentLabelStart, lineEnd.y + fm.getAscent());
+                        } else {
+                            logger.debug("pk table is at left and lower");
+ g2.drawString(Relationship.PAREENT_TO_CHILD, parentToChildLabelStart, lineEnd.y - fm.getDescent()); + g2.drawString(Relationship.CHILD_TO_PARENT, childToParentLabelStart, lineStart.y + fm.getAscent());
+                        }
+                               }
+                               }
+                               
                                containmentPath.lineTo(midx, lineEnd.y);
                                containmentPath.lineTo(midx, lineStart.y);
                                containmentPath.moveTo(lineStart.x, 
lineStart.y);
@@ -221,7 +280,29 @@
                                containmentPath.lineTo(lineEnd.x, lineEnd.y);
                 containmentPath.lineTo(end.x, end.y);
                                path = new GeneralPath(containmentPath);
-
+                               // draw relationship labels
+                               if (r.displayRelationshipLabel()) {
+                               if ((orientation & PARENT_FACES_TOP) != 0) {
+                        logger.debug("pk table is at bottom");
+                        if (lineStart.x > lineEnd.x) {
+ g2.drawString(Relationship.PAREENT_TO_CHILD, parentToChildLabelStart, midy + fm.getAscent()); + g2.drawString(Relationship.CHILD_TO_PARENT, childToParentLabelStart, midy - fm.getDescent());
+                        } else {
+ g2.drawString(Relationship.PAREENT_TO_CHILD, parentToChildLabelStart, midy - fm.getDescent()); + g2.drawString(Relationship.CHILD_TO_PARENT, childToParentLabelStart, midy + fm.getAscent());
+                        }
+                    } else {
+                        logger.debug("pk table is at top");
+                        if (lineStart.x > lineEnd.x) {
+ g2.drawString(Relationship.PAREENT_TO_CHILD, parentToChildLabelStart, midy + fm.getAscent()); + g2.drawString(Relationship.CHILD_TO_PARENT, childToParentLabelStart, midy - fm.getDescent());
+                        } else {
+ g2.drawString(Relationship.PAREENT_TO_CHILD, parentToChildLabelStart, midy - fm.getDescent()); + g2.drawString(Relationship.CHILD_TO_PARENT, childToParentLabelStart, midy + fm.getAscent());
+                        }
+                    }
+                               }
+                               
// now retrace our steps so the shape doesn't autoclose with a straight line from finish to start
                                containmentPath.lineTo(lineEnd.x, midy);
                                containmentPath.lineTo(lineStart.x, midy);
@@ -233,7 +314,19 @@
                                containmentPath.lineTo(lineEnd.x, lineEnd.y);
                 containmentPath.lineTo(end.x, end.y);
                                path = new GeneralPath(containmentPath);
-
+                               // draw relationship labels
+                               if (r.displayRelationshipLabel()) {
+                               if ((orientation & PARENT_FACES_LEFT) != 0) {
+                        logger.debug("pk table is at right");
+ g2.drawString(Relationship.PAREENT_TO_CHILD, parentToChildLabelStart, lineStart.y + fm.getAscent()); + g2.drawString(Relationship.CHILD_TO_PARENT, childToParentLabelStart, lineStart.y - fm.getDescent());
+                    } else {
+                        logger.debug("pk table is at left");
+ g2.drawString(Relationship.PAREENT_TO_CHILD, parentToChildLabelStart, lineStart.y - fm.getDescent()); + g2.drawString(Relationship.CHILD_TO_PARENT, childToParentLabelStart, lineStart.y + fm.getAscent());
+                    }
+                               }
+                               
// now retrace our steps so the shape doesn't autoclose with a straight line from finish to start
                                containmentPath.lineTo(lineEnd.x, lineStart.y);
                                containmentPath.moveTo(lineStart.x, 
lineStart.y);
@@ -244,7 +337,29 @@
                                containmentPath.lineTo(lineEnd.x, lineEnd.y);
                 containmentPath.lineTo(end.x, end.y);
                                path = new GeneralPath(containmentPath);
-
+                               // draw relationship labels
+                               if (r.displayRelationshipLabel()) {
+                               if ((orientation & PARENT_FACES_TOP) != 0) {
+                        logger.debug("pk table is at bottom");
+                        if (lineStart.x > lineEnd.x) {
+ g2.drawString(Relationship.PAREENT_TO_CHILD, parentToChildLabelStart, lineEnd.y + fm.getAscent()); + g2.drawString(Relationship.CHILD_TO_PARENT, childToParentLabelStart, lineEnd.y - fm.getDescent());
+                        } else {
+ g2.drawString(Relationship.PAREENT_TO_CHILD, parentToChildLabelStart, lineEnd.y - fm.getDescent()); + g2.drawString(Relationship.CHILD_TO_PARENT, childToParentLabelStart, lineEnd.y + fm.getAscent());
+                        }
+                    } else {
+                        logger.debug("pk table is at top");
+                        if (lineStart.x > lineEnd.x) {
+ g2.drawString(Relationship.PAREENT_TO_CHILD, parentToChildLabelStart, lineEnd.y + fm.getAscent()); + g2.drawString(Relationship.CHILD_TO_PARENT, childToParentLabelStart, lineEnd.y - fm.getDescent());
+                        } else {
+ g2.drawString(Relationship.PAREENT_TO_CHILD, parentToChildLabelStart, lineEnd.y - fm.getDescent()); + g2.drawString(Relationship.CHILD_TO_PARENT, childToParentLabelStart, lineEnd.y + fm.getAscent());
+                        }
+                    }
+                               }
+                               
// now retrace our steps so the shape doesn't autoclose with a straight line from finish to start
                                containmentPath.lineTo(lineStart.x, lineEnd.y);
                                containmentPath.moveTo(lineStart.x, 
lineStart.y);
@@ -1169,4 +1284,16 @@
                Rectangle b = path.getBounds();
                return b.width + b.height;
        }
-}
+
+       /**
+        * Calculate and return the appropriate x-position for relationship 
label.
+        * @param leftmost The leftmost x coordinate of the relationship line
+        * @param rightmost The rightmost x coordinate of the relationship line
+ * @param StringWidth The width of the display string under the specified fontMetrics
+        */
+ private float calculateRelationshipLabelStart(float leftmost, float rightmost, float StringWidth) {
+           float x;
+           x = leftmost + (rightmost - leftmost - StringWidth)/2;
+           return x;
+       }
+}
\ No newline at end of file

Modified: trunk/src/ca/sqlpower/architect/swingui/ProjectSettingsPanel.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/ProjectSettingsPanel.java (original) +++ trunk/src/ca/sqlpower/architect/swingui/ProjectSettingsPanel.java Mon Apr 20 13:12:05 2009
@@ -61,6 +61,9 @@
     private JRadioButton rectilinearRelationships;
     private JRadioButton directRelationships;

+    private JRadioButton displayRelationshipLabel;
+    private JRadioButton hideRelationshipLabel;
+
     private JCheckBox showPkTag;
     private JCheckBox showFkTag;
     private JCheckBox showAkTag;
@@ -84,7 +87,7 @@
            CellConstraints cc = new CellConstraints();
com.jgoodies.forms.layout.FormLayout layout = new com.jgoodies.forms.layout.FormLayout(
                    "pref,pref",  //$NON-NLS-1$
- "pref,4dlu,pref,4dlu,pref,4dlu,4dlu,pref,pref,4dlu,4dlu,pref,pref,4dlu,pref,pref,pref,pref,pref,pref,4dlu"); //$NON-NLS-1$ + "pref,4dlu,pref,4dlu,pref,4dlu,4dlu,pref,pref,4dlu,4dlu,pref,pref,4dlu,4dlu,pref,pref,4dlu,pref,pref,pref,pref,pref,pref,4dlu"); //$NON-NLS-1$
                setLayout(layout);
                int row = 1;
add(new JLabel(Messages.getString("ProjectSettingsPanel.snapshotSourceDbOption")), cc.xy(1, row)); //$NON-NLS-1$
@@ -126,7 +129,24 @@
         nameDisplay.add(logicalNames);
         nameDisplay.add(physicalNames);

+        logger.info(row);
+        row+=2;
+        add(new JSeparator(), cc.xyw(1, row, 2));
+        logger.info(row);
+
         row++;
+ add(new JLabel(Messages.getString("ProjectSettingsPanel.visibilityOfRelationshipLabel")), cc.xy(1, row)); //$NON-NLS-1$ + add(hideRelationshipLabel = new JRadioButton(Messages.getString("ProjectSettingsPanel.hideRelationshipLabel")), cc.xy(2, row)); //$NON-NLS-1$
+
+
+        row++;
+        add(new JLabel(), cc.xy(1, row));
+ add(displayRelationshipLabel = new JRadioButton(Messages.getString("ProjectSettingsPanel.displayRelationshipLabel")), cc.xy(2, row)); //$NON-NLS-1$
+        ButtonGroup DisplayRelationshipLabel = new ButtonGroup();
+        DisplayRelationshipLabel.add(displayRelationshipLabel);
+        DisplayRelationshipLabel.add(hideRelationshipLabel);
+
+        row+=2;
         add(new JSeparator(), cc.xyw(1, row, 2));

         row++;
@@ -173,6 +193,11 @@
         } else {
             physicalNames.setSelected(true);
         }
+        if (session.isDisplayRelationshipLabel()) {
+            displayRelationshipLabel.setSelected(true);
+        } else {
+            hideRelationshipLabel.setSelected(true);
+        }

             showPkTag.setSelected(session.isShowPkTag());
             showFkTag.setSelected(session.isShowFkTag());
@@ -223,6 +248,12 @@
             session.setUsingLogicalNames(true);
         } else {
             session.setUsingLogicalNames(false);
+        }
+
+        if (displayRelationshipLabel.isSelected()) {
+            session.setDisplayRelationshipLabel(true);
+        } else {
+            session.setDisplayRelationshipLabel(false);
         }

         session.setShowPkTag(showPkTag.isSelected());

Modified: trunk/src/ca/sqlpower/architect/swingui/Relationship.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/Relationship.java   (original)
+++ trunk/src/ca/sqlpower/architect/swingui/Relationship.java Mon Apr 20 13:12:05 2009
@@ -61,6 +61,9 @@

public class Relationship extends PlayPenComponent implements SQLObjectListener, LayoutEdge {
        private static final Logger logger = 
Logger.getLogger(Relationship.class);
+       
+       public static final String PAREENT_TO_CHILD = "receives";
+       public static final String CHILD_TO_PARENT = "is received by";

        public static final WebColour[] SUGGESTED_COLOURS;
        static {
@@ -531,6 +534,17 @@
         return false;
     }

+    public boolean displayRelationshipLabel() {
+        PlayPen pp = getPlayPen();
+        if (pp != null) {
+            ArchitectSwingSession session = pp.getSession();
+            if (session != null) {
+                return session.isDisplayRelationshipLabel();
+            }
+        }
+        return false;
+    }
+
     /**
      * Sets the connectionPoints of the relationship from an array of
      * points. Currently, it's only accessed when undo/redo movement

Modified: trunk/src/ca/sqlpower/architect/swingui/SwingUIProject.java
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/SwingUIProject.java (original)
+++ trunk/src/ca/sqlpower/architect/swingui/SwingUIProject.java Mon Apr 20 13:12:05 2009
@@ -332,6 +332,15 @@
                 getSession().setUsingLogicalNames(true);
             }
                
+ String displayRelationshipLabel = attributes.getValue("relationshipLabelVisibility"); //$NON-NLS-1$
+            if (displayRelationshipLabel == null) {
+                getSession().setDisplayRelationshipLabel(false);
+            } else if (!Boolean.valueOf(displayRelationshipLabel)) {
+                getSession().setDisplayRelationshipLabel(false);
+            } else {
+                getSession().setDisplayRelationshipLabel(true);
+            }
+
String relStyle = attributes.getValue("relationship-style"); //$NON-NLS-1$
             boolean direct;
             if (relStyle == null) {
@@ -972,11 +981,12 @@
             String relStyle = getSession().getRelationshipLinesDirect() ?
RELATIONSHIP_STYLE_DIRECT : RELATIONSHIP_STYLE_RECTILINEAR; tagText.append(" relationship-style=").append(quote(relStyle)); //$NON-NLS-1$ - tagText.append(" names-displayLogicalNames=\"").append(getSession().isUsingLogicalNames()).append("\""); //$NON-NLS-1$ + tagText.append(" names-displayLogicalNames=\"").append(getSession().isUsingLogicalNames()).append("\""); //$NON-NLS-1$ //$NON-NLS-2$ tagText.append(" showPrimaryTag=\"").append(getSession().isShowPkTag()).append("\""); //$NON-NLS-1$ //$NON-NLS-2$ tagText.append(" showForeignTag=\"").append(getSession().isShowFkTag()).append("\""); //$NON-NLS-1$ //$NON-NLS-2$ tagText.append(" showAlternateTag=\"").append(getSession().isShowAkTag()).append("\""); //$NON-NLS-1$ //$NON-NLS-2$ tagText.append(" columnVisibility=\"").append(getSession().getColumnVisibility()).append("\""); //$NON-NLS-1$ //$NON-NLS-2$ + tagText.append(" relationshipLabelVisibility=\"").append(getSession().isDisplayRelationshipLabel()).append("\""); //$NON-NLS-1$ //$NON-NLS-2$
         }
         tagText.append(">"); //$NON-NLS-1$
         ioo.println(out, tagText.toString());

Modified: trunk/src/ca/sqlpower/architect/swingui/messages.properties
==============================================================================
--- trunk/src/ca/sqlpower/architect/swingui/messages.properties (original)
+++ trunk/src/ca/sqlpower/architect/swingui/messages.properties Mon Apr 20 13:12:05 2009
@@ -276,6 +276,8 @@
 ProjectSettingsPanel.displayLogicalNames= Logical Names
 ProjectSettingsPanel.displayPhysicalNames= Physical Names
ProjectSettingsPanel.displayPhysicalOrLogical= Display Tables and Columns With:
+ProjectSettingsPanel.displayRelationshipLabel=Display
+ProjectSettingsPanel.hideRelationshipLabel=Hide
ProjectSettingsPanel.numCommonProfileValues=Number of Common Values in Profiles:
 ProjectSettingsPanel.profileMode=Profile Creator Mode:
 ProjectSettingsPanel.rectilinearLineOption=Rectilinear Lines
@@ -289,6 +291,7 @@
 ProjectSettingsPanel.showPK=Show PK Columns only
 ProjectSettingsPanel.showPKFKUnique=Show PK + FK + Unique Columns
ProjectSettingsPanel.snapshotSourceDbOption=Snapshot Entire Source Database in Project File? +ProjectSettingsPanel.visibilityOfRelationshipLabel=Display or Hide Relationship Label:
 Relationship.setFocusMenu=Set Focus to...
 Relationship.relationshipLineColor=Relationship Line Color
 RelationshipEditPanel.cardinality=Cardinality

Reply via email to