Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 package org.apache.cayenne.modeler.dialog.objentity;
 import  org.apache.cayenne.util.Util;
 import  org.scopemvc.core.ModelChangeEvent;
 import  org.scopemvc.core.Selector;
 import  org.scopemvc.model.basic.BasicModel;

A model representing an Entity with a set of Relationships, with zero or one selected Relationship.

Andrei Adamchik
 public class EntityRelationshipsModel extends BasicModel {
     static final Pattern NAME_PATTERN = Pattern.compile("\\s\\[.*\\]$");
     public static final Selector RELATIONSHIP_DISPLAY_NAME_SELECTOR = Selector
     protected Entity sourceEntity;
     protected String relationshipDisplayName;
     protected String defaultTargetName;
     protected Object[] relationshipNames;
     static String nameFromDisplayName(String displayName) {
         if (displayName == null) {
             return null;
         return .matcher(displayName).replaceAll("");
     static String displayName(Relationship relationship) {
         if (relationship == null) {
             return null;
         return displayName(
     static String displayName(String name, Entity source, Entity target) {
         return name + " [" + source.getName() + " -> " + target.getName() + "]";

Creates EntityRelationshipsModel with two unconnected Entities.
     public EntityRelationshipsModel(Entity sourceEntity, Entity targetEntity) {
         this. = sourceEntity;
         this. = targetEntity.getName();
         this. = "";

Creates EntityRelationshipsModel over the relationship connecting two Entities.
     public EntityRelationshipsModel(Relationship relationship) {
         this. = relationship.getSourceEntity();
         this. = displayName(relationship);
     public synchronized Object[] getRelationshipNames() {
         // build an ordered list of available relationship names
         // on demand
         if ( == null) {
             Collection relationships = getSourceEntity().getRelationships();
             int size = relationships.size();
             Object[] names = new Object[size];
            Iterator it = relationships.iterator();
            for (int i = 0; i < sizei++) {
                DbRelationship next = (DbRelationship);
                names[i] = displayName(next);
            this. = names;
        return ;

Returns a root entity of this model.

    public Entity getSourceEntity() {
        return ;

Returns a String describing currently selected relationship.
        return ;
    public void setRelationshipDisplayName(String relationshipDisplayName) {
        if (!Util.nullSafeEquals(relationshipDisplayNamethis.)) {
            this. = relationshipDisplayName;
             = null;
    public void setRelationshipName(String relationshipName) {
    public Relationship getSelectedRelationship() {
        return .getRelationship(nameFromDisplayName());
    public String getSourceEntityName() {
        return .getName();
    public String getTargetEntityName() {
        Relationship selected = getSelectedRelationship();
        return (selected != null) ? selected.getTargetEntityName() : ;
New to GrepCode? Check out our FAQ X