Search in sources :

Example 1 with ERDEntity

use of org.jkiss.dbeaver.erd.model.ERDEntity in project dbeaver by serge-rider.

the class NodeDropTargetListener method createTargetRequest.

@Override
protected Request createTargetRequest() {
    CreateRequest request = new CreateRequest();
    request.setFactory(new CreationFactory() {

        @Override
        public Object getNewObject() {
            Collection<DBPNamedObject> objects = DatabaseObjectTransfer.getInstance().getObject();
            if (objects == null) {
                return null;
            }
            DBRRunnableWithResult<List<ERDEntity>> collector = new DBRRunnableWithResult<List<ERDEntity>>() {

                @Override
                public void run(DBRProgressMonitor monitor) {
                    result = DiagramObjectCollector.generateEntityList(monitor, ((DiagramPart) getViewer().getRootEditPart().getContents()).getDiagram(), objects, new DiagramCollectSettingsDefault(), true);
                }
            };
            try {
                UIUtils.runInProgressService(collector);
            } catch (InvocationTargetException e) {
                DBWorkbench.getPlatformUI().showError("Entity collect error", "Error during diagram entities collect", e);
            } catch (InterruptedException e) {
            // ignore
            }
            return collector.getResult();
        }

        @Override
        public Object getObjectType() {
            return RequestConstants.REQ_CREATE;
        }
    });
    request.setLocation(getDropLocation());
    return request;
}
Also used : CreateRequest(org.eclipse.gef.requests.CreateRequest) ERDEntity(org.jkiss.dbeaver.erd.model.ERDEntity) CreationFactory(org.eclipse.gef.requests.CreationFactory) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBRRunnableWithResult(org.jkiss.dbeaver.model.runtime.DBRRunnableWithResult) DiagramCollectSettingsDefault(org.jkiss.dbeaver.erd.ui.model.DiagramCollectSettingsDefault) Collection(java.util.Collection) DBPNamedObject(org.jkiss.dbeaver.model.DBPNamedObject) List(java.util.List) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 2 with ERDEntity

use of org.jkiss.dbeaver.erd.model.ERDEntity in project dbeaver by serge-rider.

the class EntityAddCommand method execute.

@Override
public void execute() {
    VoidProgressMonitor monitor = new VoidProgressMonitor();
    Point curLocation = location == null ? null : new Point(location);
    for (ERDEntity entity : entities) {
        boolean resolveRelations = false;
        if (entity.getObject() == null) {
            // Entity is not initialized
            if (entity.getDataSource() != null) {
                DBSObject selectedObject = DBUtils.getSelectedObject(DBUtils.getDefaultContext(entity.getDataSource(), false));
                DBNDatabaseNode dsNode = DBNUtils.getNodeByObject(selectedObject != null ? selectedObject : entity.getDataSource().getContainer());
                if (dsNode != null) {
                    DBNNode tableNode = DBWorkbench.getPlatformUI().selectObject(UIUtils.getActiveWorkbenchShell(), "Select a table", dsNode, null, new Class[] { DBSTable.class }, new Class[] { DBSTable.class }, null);
                    if (tableNode instanceof DBNDatabaseNode && ((DBNDatabaseNode) tableNode).getObject() instanceof DBSEntity) {
                        entity = ERDUtils.makeEntityFromObject(monitor, diagramPart.getDiagram(), Collections.emptyList(), (DBSEntity) ((DBNDatabaseNode) tableNode).getObject(), null);
                        // This actually only loads unresolved relations.
                        // This happens only with entities added on diagram during editing
                        entity.addModelRelations(monitor, diagramPart.getDiagram(), false, false);
                    }
                }
            }
        }
        if (entity.getObject() == null) {
            continue;
        }
        diagramPart.getDiagram().addEntity(entity, true);
        if (curLocation != null) {
            // Put new entities in specified location
            for (Object diagramChild : diagramPart.getChildren()) {
                if (diagramChild instanceof EntityPart) {
                    EntityPart entityPart = (EntityPart) diagramChild;
                    if (entityPart.getEntity() == entity) {
                        final Rectangle newBounds = new Rectangle();
                        final Dimension size = entityPart.getFigure().getPreferredSize();
                        newBounds.x = curLocation.x;
                        newBounds.y = curLocation.y;
                        newBounds.width = size.width;
                        newBounds.height = size.height;
                        entityPart.modifyBounds(newBounds);
                        curLocation.x += size.width + (size.width / 2);
                        break;
                    }
                }
            }
        }
        handleEntityChange(entity, false);
    }
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) ERDEntity(org.jkiss.dbeaver.erd.model.ERDEntity) Rectangle(org.eclipse.draw2d.geometry.Rectangle) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) Point(org.eclipse.draw2d.geometry.Point) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity) Dimension(org.eclipse.draw2d.geometry.Dimension) EntityPart(org.jkiss.dbeaver.erd.ui.part.EntityPart) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 3 with ERDEntity

use of org.jkiss.dbeaver.erd.model.ERDEntity in project dbeaver by serge-rider.

the class ERDHandlerPaste method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    Control control = (Control) HandlerUtil.getVariable(event, ISources.ACTIVE_FOCUS_CONTROL_NAME);
    if (control != null) {
        ERDEditorPart editor = ERDEditorAdapter.getEditor(control);
        if (editor != null && !editor.isReadOnly()) {
            final Collection<DBPNamedObject> objects = DatabaseObjectTransfer.getInstance().getObject();
            if (!CommonUtils.isEmpty(objects)) {
                try {
                    UIUtils.runInProgressService(monitor -> {
                        final List<ERDEntity> erdEntities = DiagramObjectCollector.generateEntityList(monitor, editor.getDiagram(), objects, new DiagramCollectSettingsDefault(), true);
                        if (!CommonUtils.isEmpty(erdEntities)) {
                            UIUtils.syncExec(() -> {
                                Command command = editor.getDiagramPart().createEntityAddCommand(erdEntities, new Point(10, 10));
                                editor.getCommandStack().execute(command);
                            });
                        }
                    });
                } catch (InvocationTargetException e) {
                    DBWorkbench.getPlatformUI().showError("Entity collect error", "Error during diagram entities collect", e);
                } catch (InterruptedException e) {
                // ignore
                }
            }
        }
    }
    return null;
}
Also used : Control(org.eclipse.swt.widgets.Control) DBPNamedObject(org.jkiss.dbeaver.model.DBPNamedObject) ERDEditorPart(org.jkiss.dbeaver.erd.ui.editor.ERDEditorPart) Command(org.eclipse.gef.commands.Command) ERDEntity(org.jkiss.dbeaver.erd.model.ERDEntity) DiagramCollectSettingsDefault(org.jkiss.dbeaver.erd.ui.model.DiagramCollectSettingsDefault) Point(org.eclipse.draw2d.geometry.Point) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with ERDEntity

use of org.jkiss.dbeaver.erd.model.ERDEntity in project dbeaver by serge-rider.

the class EntityPart method revertNameChange.

/**
 * Reverts to existing name in model when exiting from a direct edit
 * (possibly before a commit which will result in a change in the label
 * value)
 */
public void revertNameChange() {
    EntityFigure entityFigure = getFigure();
    EditableLabel label = entityFigure.getNameLabel();
    ERDEntity entity = getEntity();
    label.setText(entity.getObject().getName());
    label.setVisible(true);
    refreshVisuals();
}
Also used : EditableLabel(org.jkiss.dbeaver.erd.ui.figures.EditableLabel) EntityFigure(org.jkiss.dbeaver.erd.ui.figures.EntityFigure) ERDEntity(org.jkiss.dbeaver.erd.model.ERDEntity)

Example 5 with ERDEntity

use of org.jkiss.dbeaver.erd.model.ERDEntity in project dbeaver by dbeaver.

the class ERDExportGraphML method exportDiagram.

@Override
public void exportDiagram(EntityDiagram diagram, IFigure figure, DiagramPart diagramPart, File targetFile) throws DBException {
    try {
        try (FileOutputStream fos = new FileOutputStream(targetFile)) {
            XMLBuilder xml = new XMLBuilder(fos, GeneralUtils.UTF8_ENCODING);
            xml.setButify(true);
            xml.startElement("graphml");
            xml.addAttribute("xmlns", "http://graphml.graphdrawing.org/xmlns");
            xml.addAttribute("xmlns:java", "http://www.yworks.com/xml/yfiles-common/1.0/java");
            xml.addAttribute("xmlns:sys", "http://www.yworks.com/xml/yfiles-common/markup/primitives/2.0");
            xml.addAttribute("xmlns:x", "http://www.yworks.com/xml/yfiles-common/markup/2.0");
            xml.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            xml.addAttribute("xmlns:y", "http://www.yworks.com/xml/graphml");
            xml.addAttribute("xmlns:yed", "http://www.yworks.com/xml/yed/3");
            xml.addAttribute("xsi:schemaLocation", "http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd");
            xml.startElement("key");
            xml.addAttribute("for", "node");
            xml.addAttribute("id", "nodegraph");
            xml.addAttribute("yfiles.type", "nodegraphics");
            xml.endElement();
            xml.startElement("key");
            xml.addAttribute("for", "edge");
            xml.addAttribute("id", "edgegraph");
            xml.addAttribute("yfiles.type", "edgegraphics");
            xml.endElement();
            xml.startElement("graph");
            xml.addAttribute("edgedefault", "directed");
            xml.addAttribute("id", "G");
            Map<ERDEntity, String> entityMap = new HashMap<>();
            int nodeNum = 0;
            for (ERDEntity entity : diagram.getEntities()) {
                nodeNum++;
                String nodeId = "n" + nodeNum;
                entityMap.put(entity, nodeId);
                // node
                xml.startElement("node");
                xml.addAttribute("id", nodeId);
                {
                    // Graph data
                    xml.startElement("data");
                    xml.addAttribute("key", "nodegraph");
                    {
                        // Generic node
                        EntityPart entityPart = diagramPart.getEntityPart(entity);
                        EntityFigure entityFigure = entityPart.getFigure();
                        Rectangle partBounds = entityPart.getBounds();
                        xml.startElement("y:GenericNode");
                        xml.addAttribute("configuration", "com.yworks.entityRelationship.big_entity");
                        // Geometry
                        xml.startElement("y:Geometry");
                        xml.addAttribute("height", partBounds.height);
                        xml.addAttribute("width", partBounds.width + getExtraTableLength(diagram, entity));
                        xml.addAttribute("x", partBounds.x());
                        xml.addAttribute("y", partBounds.y());
                        xml.endElement();
                        // Fill
                        xml.startElement("y:Fill");
                        xml.addAttribute("color", getHtmlColor(entityFigure.getBackgroundColor()));
                        // xml.addAttribute("color2", partBounds.width);
                        xml.addAttribute("transparent", "false");
                        xml.endElement();
                        // Border
                        xml.startElement("y:BorderStyle");
                        xml.addAttribute("color", getHtmlColor(entityFigure.getForegroundColor()));
                        xml.addAttribute("type", "line");
                        xml.addAttribute("width", "1.0");
                        xml.endElement();
                        {
                            // Entity Name
                            Rectangle nameBounds = entityFigure.getNameLabel().getBounds();
                            xml.startElement("y:NodeLabel");
                            xml.addAttribute("alignment", "center");
                            xml.addAttribute("autoSizePolicy", "content");
                            xml.addAttribute("configuration", "com.yworks.entityRelationship.label.name");
                            xml.addAttribute("fontFamily", "Courier");
                            xml.addAttribute("fontSize", fontSize);
                            xml.addAttribute("fontStyle", "plain");
                            xml.addAttribute("hasLineColor", "false");
                            xml.addAttribute("modelName", "internal");
                            xml.addAttribute("modelPosition", "t");
                            xml.addAttribute("backgroundColor", getHtmlColor(entityFigure.getNameLabel().getBackgroundColor()));
                            xml.addAttribute("textColor", "#FFFFFF");
                            xml.addAttribute("visible", "true");
                            xml.addAttribute("horizontalTextPosition", "center");
                            xml.addAttribute("iconTextGap", "4");
                            xml.addAttribute("height", nameBounds.height);
                            xml.addAttribute("width", nameBounds.width);
                            xml.addAttribute("x", 0);
                            xml.addAttribute("y", 4);
                            xml.addText(entity.getName());
                            xml.endElement();
                        }
                        {
                            // Attributes
                            AttributeListFigure columnsFigure = entityFigure.getColumnsFigure();
                            Rectangle attrsBounds = columnsFigure.getBounds();
                            xml.startElement("y:NodeLabel");
                            xml.addAttribute("alignment", "left");
                            xml.addAttribute("autoSizePolicy", "content");
                            xml.addAttribute("configuration", "com.yworks.entityRelationship.label.attributes");
                            xml.addAttribute("fontFamily", "Courier");
                            xml.addAttribute("fontSize", fontSize);
                            xml.addAttribute("fontStyle", "plain");
                            xml.addAttribute("hasLineColor", "false");
                            xml.addAttribute("modelName", "custom");
                            xml.addAttribute("modelPosition", "t");
                            xml.addAttribute("backgroundColor", getHtmlColor(columnsFigure.getBackgroundColor()));
                            xml.addAttribute("textColor", getHtmlColor(columnsFigure.getForegroundColor()));
                            xml.addAttribute("visible", "true");
                            xml.addAttribute("horizontalTextPosition", "center");
                            xml.addAttribute("iconTextGap", "4");
                            xml.addAttribute("height", attrsBounds.height);
                            xml.addAttribute("width", attrsBounds.width);
                            // numbers from yEd Graph Editor
                            xml.addAttribute("x", 2);
                            xml.addAttribute("y", 31.66796875);
                            StringBuilder attrsString = new StringBuilder();
                            for (ERDEntityAttribute attr : entity.getAttributes()) {
                                if (attrsString.length() > 0) {
                                    attrsString.append("\n");
                                }
                                attrsString.append(ERDUIUtils.getFullAttributeLabel(diagram, attr, true));
                            }
                            xml.addText(attrsString.toString());
                            xml.startElement("y:LabelModel");
                            xml.startElement("y:ErdAttributesNodeLabelModel");
                            xml.endElement();
                            xml.endElement();
                            xml.startElement("y:ModelParameter");
                            xml.startElement("y:ErdAttributesNodeLabelModelParameter");
                            xml.endElement();
                            xml.endElement();
                            xml.endElement();
                        }
                        xml.endElement();
                    }
                    xml.endElement();
                }
                xml.endElement();
            }
            int edgeNum = 0;
            for (ERDEntity entity : diagram.getEntities()) {
                EntityPart entityPart = diagramPart.getEntityPart(entity);
                for (ERDAssociation association : entity.getAssociations()) {
                    AssociationPart associationPart = entityPart.getConnectionPart(association, true);
                    if (associationPart == null) {
                        log.debug("Association part not found");
                        continue;
                    }
                    edgeNum++;
                    String edgeId = "e" + edgeNum;
                    xml.startElement("edge");
                    xml.addAttribute("id", edgeId);
                    xml.addAttribute("source", entityMap.get(entity));
                    xml.addAttribute("target", entityMap.get(association.getTargetEntity()));
                    xml.startElement("data");
                    xml.addAttribute("key", "edgegraph");
                    xml.startElement("y:PolyLineEdge");
                    // sx="0.0" sy="0.0" tx="0.0" ty="0.0"/>
                    xml.startElement("y:Path");
                    xml.addAttribute("sx", 0.0);
                    xml.addAttribute("sy", 0.0);
                    xml.addAttribute("tx", 0.0);
                    xml.addAttribute("ty", 0.0);
                    for (Bendpoint bp : associationPart.getBendpoints()) {
                        xml.startElement("y:Point");
                        xml.addAttribute("x", bp.getLocation().x);
                        xml.addAttribute("y", bp.getLocation().y);
                        xml.endElement();
                    }
                    xml.endElement();
                    boolean identifying = ERDUtils.isIdentifyingAssociation(association);
                    xml.startElement("y:LineStyle");
                    xml.addAttribute("color", "#000000");
                    xml.addAttribute("type", !identifying || association.isLogical() ? "dashed" : "line");
                    xml.addAttribute("width", "1.0");
                    xml.endElement();
                    xml.startElement("y:Arrows");
                    String sourceStyle = !identifying ? "white_diamond" : "none";
                    xml.addAttribute("source", sourceStyle);
                    xml.addAttribute("target", "circle");
                    xml.endElement();
                    xml.startElement("y:BendStyle");
                    xml.addAttribute("smoothed", "false");
                    xml.endElement();
                    xml.endElement();
                    xml.endElement();
                    xml.endElement();
                }
            }
            xml.endElement();
            xml.endElement();
            xml.flush();
            fos.flush();
        }
        UIUtils.launchProgram(targetFile.getAbsolutePath());
    } catch (Exception e) {
        DBWorkbench.getPlatformUI().showError("Save ERD as GraphML", null, e);
    }
}
Also used : EntityFigure(org.jkiss.dbeaver.erd.ui.figures.EntityFigure) HashMap(java.util.HashMap) ERDEntity(org.jkiss.dbeaver.erd.model.ERDEntity) Rectangle(org.eclipse.draw2d.geometry.Rectangle) ERDAssociation(org.jkiss.dbeaver.erd.model.ERDAssociation) XMLBuilder(org.jkiss.utils.xml.XMLBuilder) Bendpoint(org.eclipse.draw2d.Bendpoint) AttributeListFigure(org.jkiss.dbeaver.erd.ui.figures.AttributeListFigure) DBException(org.jkiss.dbeaver.DBException) ERDEntityAttribute(org.jkiss.dbeaver.erd.model.ERDEntityAttribute) FileOutputStream(java.io.FileOutputStream) AssociationPart(org.jkiss.dbeaver.erd.ui.part.AssociationPart) EntityPart(org.jkiss.dbeaver.erd.ui.part.EntityPart) Bendpoint(org.eclipse.draw2d.Bendpoint)

Aggregations

ERDEntity (org.jkiss.dbeaver.erd.model.ERDEntity)16 Point (org.eclipse.draw2d.geometry.Point)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Collection (java.util.Collection)4 Rectangle (org.eclipse.draw2d.geometry.Rectangle)4 Command (org.eclipse.gef.commands.Command)4 EntityFigure (org.jkiss.dbeaver.erd.ui.figures.EntityFigure)4 DiagramCollectSettingsDefault (org.jkiss.dbeaver.erd.ui.model.DiagramCollectSettingsDefault)4 EntityPart (org.jkiss.dbeaver.erd.ui.part.EntityPart)4 DBPNamedObject (org.jkiss.dbeaver.model.DBPNamedObject)4 FileOutputStream (java.io.FileOutputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Bendpoint (org.eclipse.draw2d.Bendpoint)2 Dimension (org.eclipse.draw2d.geometry.Dimension)2 CreateRequest (org.eclipse.gef.requests.CreateRequest)2 CreationFactory (org.eclipse.gef.requests.CreationFactory)2 Control (org.eclipse.swt.widgets.Control)2 NotNull (org.jkiss.code.NotNull)2