Search in sources :

Example 11 with ConfigurationNode

use of org.apache.cayenne.configuration.ConfigurationNode in project cayenne by apache.

the class ProjectTest method testRootNode.

@Test
public void testRootNode() {
    ConfigurationNode object = new ConfigurationNode() {

        public <T> T acceptVisitor(ConfigurationNodeVisitor<T> visitor) {
            return null;
        }
    };
    Project project = new Project(new ConfigurationTree<ConfigurationNode>(object));
    assertSame(object, project.getRootNode());
    assertFalse(project.isModified());
    project.setModified(true);
    assertTrue(project.isModified());
}
Also used : ConfigurationNodeVisitor(org.apache.cayenne.configuration.ConfigurationNodeVisitor) ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) Test(org.junit.Test)

Example 12 with ConfigurationNode

use of org.apache.cayenne.configuration.ConfigurationNode in project cayenne by apache.

the class ImportDataMapAction method importDataMap.

protected void importDataMap() {
    File dataMapFile = selectDataMap(Application.getFrame());
    if (dataMapFile == null) {
        return;
    }
    DataMap newMap;
    try {
        URL url = dataMapFile.toURI().toURL();
        DataMapLoader loader = application.getInjector().getInstance(DataMapLoader.class);
        newMap = loader.load(new URLResource(url));
        ConfigurationNode root = getProjectController().getProject().getRootNode();
        newMap.setName(NameBuilder.builder(newMap, root).baseName(newMap.getName()).name());
        Resource baseResource = ((DataChannelDescriptor) root).getConfigurationSource();
        if (baseResource != null) {
            Resource dataMapResource = baseResource.getRelativeResource(nameMapper.configurationLocation(newMap));
            newMap.setConfigurationSource(dataMapResource);
        }
        getProjectController().addDataMap(this, newMap);
    } catch (Exception ex) {
        logObj.info("Error importing DataMap.", ex);
        JOptionPane.showMessageDialog(Application.getFrame(), "Error reading DataMap: " + ex.getMessage(), "Can't Open DataMap", JOptionPane.OK_OPTION);
    }
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) File(java.io.File) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap)

Example 13 with ConfigurationNode

use of org.apache.cayenne.configuration.ConfigurationNode in project cayenne by apache.

the class ImportEOModelAction method addDataMap.

/**
 * Adds DataMap into the project.
 */
protected void addDataMap(DataMap map, DataMap currentMap) {
    ProjectController mediator = getProjectController();
    if (currentMap != null) {
        // merge with existing map... have to memorize map state before and after
        // to do the right events
        Collection<ObjEntity> originalOE = new ArrayList<>(currentMap.getObjEntities());
        Collection<DbEntity> originalDE = new ArrayList<>(currentMap.getDbEntities());
        Collection<QueryDescriptor> originalQueries = new ArrayList<>(currentMap.getQueryDescriptors());
        currentMap.mergeWithDataMap(map);
        map = currentMap;
        // postprocess changes
        Collection<ObjEntity> newOE = new ArrayList<>(currentMap.getObjEntities());
        Collection<DbEntity> newDE = new ArrayList<>(currentMap.getDbEntities());
        Collection<QueryDescriptor> newQueries = new ArrayList<>(currentMap.getQueryDescriptors());
        EntityEvent entityEvent = new EntityEvent(Application.getFrame(), null);
        QueryEvent queryEvent = new QueryEvent(Application.getFrame(), null);
        // 1. ObjEntities
        Collection<ObjEntity> addedOE = new ArrayList<>(newOE);
        addedOE.removeAll(originalOE);
        for (ObjEntity e : addedOE) {
            entityEvent.setEntity(e);
            entityEvent.setId(MapEvent.ADD);
            mediator.fireObjEntityEvent(entityEvent);
        }
        Collection<ObjEntity> removedOE = new ArrayList<>(originalOE);
        removedOE.removeAll(newOE);
        for (ObjEntity e : removedOE) {
            entityEvent.setEntity(e);
            entityEvent.setId(MapEvent.REMOVE);
            mediator.fireObjEntityEvent(entityEvent);
        }
        // 2. DbEntities
        Collection<DbEntity> addedDE = new ArrayList<>(newDE);
        addedDE.removeAll(originalDE);
        for (DbEntity e : addedDE) {
            entityEvent.setEntity(e);
            entityEvent.setId(MapEvent.ADD);
            mediator.fireDbEntityEvent(entityEvent);
        }
        Collection<DbEntity> removedDE = new ArrayList<>(originalDE);
        removedDE.removeAll(newDE);
        for (DbEntity e : removedDE) {
            entityEvent.setEntity(e);
            entityEvent.setId(MapEvent.REMOVE);
            mediator.fireDbEntityEvent(entityEvent);
        }
        // 3. queries
        Collection<QueryDescriptor> addedQueries = new ArrayList<>(newQueries);
        addedQueries.removeAll(originalQueries);
        for (QueryDescriptor q : addedQueries) {
            queryEvent.setQuery(q);
            queryEvent.setId(MapEvent.ADD);
            mediator.fireQueryEvent(queryEvent);
        }
        Collection<QueryDescriptor> removedQueries = new ArrayList<>(originalQueries);
        removedQueries.removeAll(newQueries);
        for (QueryDescriptor q : removedQueries) {
            queryEvent.setQuery(q);
            queryEvent.setId(MapEvent.REMOVE);
            mediator.fireQueryEvent(queryEvent);
        }
        mediator.fireDataMapDisplayEvent(new DataMapDisplayEvent(Application.getFrame(), map, (DataChannelDescriptor) mediator.getProject().getRootNode(), mediator.getCurrentDataNode()));
    } else {
        // fix DataMap name, as there maybe a map with the same name already
        ConfigurationNode root = mediator.getProject().getRootNode();
        map.setName(NameBuilder.builder(map, root).baseName(map.getName()).name());
        // side effect of this operation is that if a node was created, this DataMap
        // will be linked with it...
        mediator.addDataMap(Application.getFrame(), map);
    }
}
Also used : DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) ArrayList(java.util.ArrayList) QueryEvent(org.apache.cayenne.configuration.event.QueryEvent) DataMapDisplayEvent(org.apache.cayenne.modeler.event.DataMapDisplayEvent) ProjectController(org.apache.cayenne.modeler.ProjectController) QueryDescriptor(org.apache.cayenne.map.QueryDescriptor) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) EntityEvent(org.apache.cayenne.map.event.EntityEvent)

Example 14 with ConfigurationNode

use of org.apache.cayenne.configuration.ConfigurationNode in project cayenne by apache.

the class ProjectController method getCurrentObject.

/**
 * Returns currently selected object, null if there are none, List if there
 * are several
 */
public Object getCurrentObject() {
    if (getCurrentObjEntity() != null) {
        return getCurrentObjEntity();
    } else if (getCurrentDbEntity() != null) {
        return getCurrentDbEntity();
    } else if (getCurrentEmbeddable() != null) {
        return getCurrentEmbeddable();
    } else if (getCurrentQuery() != null) {
        return getCurrentQuery();
    } else if (getCurrentProcedure() != null) {
        return getCurrentProcedure();
    } else if (getCurrentDataMap() != null) {
        return getCurrentDataMap();
    } else if (getCurrentDataNode() != null) {
        return getCurrentDataNode();
    } else if (getCurrentDataChanel() != null) {
        return getCurrentDataChanel();
    } else if (getCurrentPaths() != null) {
        // multiple objects
        ConfigurationNode[] paths = getCurrentPaths();
        ConfigurationNodeParentGetter parentGetter = getApplication().getInjector().getInstance(ConfigurationNodeParentGetter.class);
        Object parent = parentGetter.getParent(paths[0]);
        List<ConfigurationNode> result = new ArrayList<>();
        result.addAll(Arrays.asList(paths));
        /*
             * Here we sort the list of objects to minimize the risk that
             * objects will be pasted incorrectly. For instance, ObjEntity
             * should go before Query, to increase chances that Query's root
             * would be set.
             */
        Collections.sort(result, parent instanceof DataMap ? Comparators.getDataMapChildrenComparator() : Comparators.getDataDomainChildrenComparator());
        return result;
    }
    return null;
}
Also used : ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) ArrayList(java.util.ArrayList) ConfigurationNodeParentGetter(org.apache.cayenne.project.ConfigurationNodeParentGetter) EventObject(java.util.EventObject) DataMap(org.apache.cayenne.map.DataMap)

Example 15 with ConfigurationNode

use of org.apache.cayenne.configuration.ConfigurationNode in project cayenne by apache.

the class EntityTreeModel method sortedChildren.

private ConfigurationNode[] sortedChildren(Object node) {
    Entity entity = entityForNonLeafNode(node);
    // may happen in incomplete relationships
    if (entity == null) {
        return new ConfigurationNode[0];
    }
    ConfigurationNode[] sortedForNode = sortedChildren.get(node);
    if (sortedForNode == null) {
        Collection<? extends Attribute> attributes = entity.getAttributes();
        Collection<? extends Relationship> relationships = entity.getRelationships();
        List<ConfigurationNode> nodes = new ArrayList<>();
        // combine two collections in an array
        for (Attribute attr : attributes) {
            if (filter == null || filter.attributeMatch(node, attr)) {
                nodes.add((ConfigurationNode) attr);
            }
        }
        for (Relationship rel : relationships) {
            if (filter == null || filter.relationshipMatch(node, rel)) {
                nodes.add((ConfigurationNode) rel);
            }
        }
        sortedForNode = nodes.toArray(new ConfigurationNode[0]);
        Arrays.sort(sortedForNode, Comparators.getEntityChildrenComparator());
        sortedChildren.put(node, sortedForNode);
    }
    return sortedForNode;
}
Also used : Entity(org.apache.cayenne.map.Entity) ConfigurationNode(org.apache.cayenne.configuration.ConfigurationNode) Attribute(org.apache.cayenne.map.Attribute) Relationship(org.apache.cayenne.map.Relationship) ArrayList(java.util.ArrayList)

Aggregations

ConfigurationNode (org.apache.cayenne.configuration.ConfigurationNode)16 ArrayList (java.util.ArrayList)5 DataMap (org.apache.cayenne.map.DataMap)5 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)4 MultipleObjectsDisplayEvent (org.apache.cayenne.modeler.event.MultipleObjectsDisplayEvent)3 URLResource (org.apache.cayenne.resource.URLResource)3 File (java.io.File)2 URL (java.net.URL)2 EventObject (java.util.EventObject)2 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)2 TreePath (javax.swing.tree.TreePath)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 ProjectController (org.apache.cayenne.modeler.ProjectController)2 Resource (org.apache.cayenne.resource.Resource)2 Rectangle (java.awt.Rectangle)1 IOException (java.io.IOException)1 BackingStoreException (java.util.prefs.BackingStoreException)1 TreeSelectionEvent (javax.swing.event.TreeSelectionEvent)1 TreeSelectionListener (javax.swing.event.TreeSelectionListener)1 CompoundEdit (javax.swing.undo.CompoundEdit)1