1 56 package org.objectstyle.cayenne.modeler.dialog.objentity; 57 58 import java.util.Arrays ; 59 import java.util.Collection ; 60 import java.util.Iterator ; 61 62 import org.apache.oro.text.perl.Perl5Util; 63 import org.objectstyle.cayenne.map.DbRelationship; 64 import org.objectstyle.cayenne.map.Entity; 65 import org.objectstyle.cayenne.map.Relationship; 66 import org.objectstyle.cayenne.util.Util; 67 import org.scopemvc.core.ModelChangeEvent; 68 import org.scopemvc.core.Selector; 69 import org.scopemvc.model.basic.BasicModel; 70 71 78 public class EntityRelationshipsModel extends BasicModel { 79 private static final Perl5Util regexUtil = new Perl5Util(); 80 81 public static final Selector RELATIONSHIP_DISPLAY_NAME_SELECTOR = 82 Selector.fromString("relationshipDisplayName"); 83 84 protected Entity sourceEntity; 85 protected String relationshipDisplayName; 86 protected String defaultTargetName; 87 protected Object [] relationshipNames; 88 89 static String nameFromDisplayName(String displayName) { 90 if (displayName == null) { 91 return null; 92 } 93 94 return regexUtil.match("/\\s\\[.+\\]$/", displayName) 95 ? regexUtil.substitute("s/\\s\\[.+\\]$//g", displayName) 96 : displayName; 97 } 98 99 static String displayName(Relationship relationship) { 100 if (relationship == null) { 101 return null; 102 } 103 return displayName( 104 relationship.getName(), 105 relationship.getSourceEntity(), 106 relationship.getTargetEntity()); 107 } 108 109 static String displayName(String name, Entity source, Entity target) { 110 return name + " [" + source.getName() + " -> " + target.getName() + "]"; 111 } 112 113 116 public EntityRelationshipsModel(Entity sourceEntity, Entity targetEntity) { 117 this.sourceEntity = sourceEntity; 118 this.defaultTargetName = targetEntity.getName(); 119 this.relationshipDisplayName = ""; 120 } 121 122 126 public EntityRelationshipsModel(Relationship relationship) { 127 this.sourceEntity = relationship.getSourceEntity(); 128 this.relationshipDisplayName = displayName(relationship); 129 } 130 131 public synchronized Object [] getRelationshipNames() { 132 if (relationshipNames == null) { 135 Collection relationships = getSourceEntity().getRelationships(); 136 int size = relationships.size(); 137 Object [] names = new Object [size]; 138 139 Iterator it = relationships.iterator(); 140 for (int i = 0; i < size; i++) { 141 DbRelationship next = (DbRelationship) it.next(); 142 names[i] = displayName(next); 143 } 144 Arrays.sort(names); 145 this.relationshipNames = names; 146 } 147 148 return relationshipNames; 149 } 150 151 155 public Entity getSourceEntity() { 156 return sourceEntity; 157 } 158 159 162 public String getRelationshipDisplayName() { 163 return relationshipDisplayName; 164 } 165 166 public void setRelationshipDisplayName(String relationshipDisplayName) { 167 if (!Util 168 .nullSafeEquals(relationshipDisplayName, this.relationshipDisplayName)) { 169 this.relationshipDisplayName = relationshipDisplayName; 170 relationshipNames = null; 171 fireModelChange( 172 ModelChangeEvent.VALUE_CHANGED, 173 RELATIONSHIP_DISPLAY_NAME_SELECTOR); 174 } 175 } 176 177 public void setRelationshipName(String relationshipName) { 178 setRelationshipDisplayName( 179 displayName(sourceEntity.getRelationship(relationshipName))); 180 } 181 182 public Relationship getSelectedRelationship() { 183 return sourceEntity.getRelationship(nameFromDisplayName(relationshipDisplayName)); 184 } 185 186 public String getSourceEntityName() { 187 return sourceEntity.getName(); 188 } 189 190 public String getTargetEntityName() { 191 Relationship selected = getSelectedRelationship(); 192 return (selected != null) ? selected.getTargetEntityName() : defaultTargetName; 193 } 194 } 195 | Popular Tags |