Search in sources :

Example 1 with EntityDiagram

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

the class DelegatingLayoutManager method layout.

@Override
public void layout(IFigure container) {
    EntityDiagram entityDiagram = diagram.getDiagram();
    try {
        if (entityDiagram.isLayoutManualDesired()) {
            if (activeLayoutManager != xyLayoutManager) {
                if (entityDiagram.isLayoutManualAllowed() && !entityDiagram.isNeedsAutoLayout()) {
                    // yes we are okay to start populating the table bounds
                    setLayoutManager(container, xyLayoutManager);
                    activeLayoutManager.layout(container);
                } else {
                    // we first have to set the constraint data
                    if (diagram.setTableFigureBounds(true)) {
                        // we successfully set bounds for all the existing
                        // tables so we can start using xyLayout immediately
                        setLayoutManager(container, xyLayoutManager);
                        activeLayoutManager.layout(container);
                    } else {
                        // we did not - we still need to run autolayout once
                        // before we can set xyLayout
                        activeLayoutManager.layout(container);
                        // run this again so that it will work again next time
                        setLayoutManager(container, xyLayoutManager);
                    }
                }
            } else {
                setLayoutManager(container, xyLayoutManager);
                activeLayoutManager.layout(container);
            }
        } else {
            setLayoutManager(container, graphLayoutManager);
            activeLayoutManager.layout(container);
        }
    } finally {
        if (!diagram.getChildren().isEmpty()) {
            entityDiagram.setNeedsAutoLayout(false);
        }
    }
}
Also used : EntityDiagram(org.jkiss.dbeaver.erd.ui.model.EntityDiagram)

Example 2 with EntityDiagram

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

the class ERDEditorEmbedded method loadFromDatabase.

private EntityDiagram loadFromDatabase(DBRProgressMonitor monitor) throws DBException {
    DBSObject dbObject = getRootObject();
    if (dbObject == null) {
        log.error("Database object must be entity container to render ERD diagram");
        return null;
    }
    EntityDiagram diagram;
    if (!dbObject.isPersisted()) {
        diagram = new EntityDiagram(dbObject, "New Object", getContentProvider(), getDecorator());
    } else {
        diagram = new EntityDiagram(dbObject, dbObject.getName(), getContentProvider(), getDecorator());
        // Fill from database even if we loaded from state (something could change since last view)
        diagram.fillEntities(monitor, ERDUtils.collectDatabaseTables(monitor, dbObject, diagram, ERDUIActivator.getDefault().getPreferenceStore().getBoolean(ERDUIConstants.PREF_DIAGRAM_SHOW_VIEWS), ERDUIActivator.getDefault().getPreferenceStore().getBoolean(ERDUIConstants.PREF_DIAGRAM_SHOW_PARTITIONS)), dbObject);
        boolean hasPersistedState = false;
        try {
            // Load persisted state
            DBVObject vObject = this.getVirtualObject();
            if (vObject != null) {
                Map<String, Object> diagramState = vObject.getProperty(PROP_DIAGRAM_STATE);
                if (diagramState != null) {
                    String serializedDiagram = (String) diagramState.get(PROPS_DIAGRAM_SERIALIZED);
                    if (!CommonUtils.isEmpty(serializedDiagram)) {
                        Document xmlDocument = XMLUtils.parseDocument(new StringReader(serializedDiagram));
                        DiagramLoader.loadDiagram(monitor, xmlDocument, dbObject.getDataSource().getContainer().getProject(), diagram);
                        hasPersistedState = true;
                    }
                }
            }
        } catch (Exception e) {
            log.error("Error loading ER diagram from saved state", e);
        }
        diagram.setLayoutManualAllowed(true);
        diagram.setNeedsAutoLayout(!hasPersistedState);
    }
    return diagram;
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) EntityDiagram(org.jkiss.dbeaver.erd.ui.model.EntityDiagram) StringReader(java.io.StringReader) DBVObject(org.jkiss.dbeaver.model.virtual.DBVObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) Document(org.w3c.dom.Document) DBException(org.jkiss.dbeaver.DBException) DBVObject(org.jkiss.dbeaver.model.virtual.DBVObject)

Example 3 with EntityDiagram

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

the class ERDEditorStandalone method loadContentFromFile.

private EntityDiagram loadContentFromFile(DBRProgressMonitor progressMonitor) throws DBException {
    IFile resFile = getEditorFile();
    IProject project = resFile == null ? DBWorkbench.getPlatform().getWorkspace().getActiveProject().getEclipseProject() : resFile.getProject();
    final File localFile = EditorUtils.getLocalFileFromInput(getEditorInput());
    final DiagramPart diagramPart = getDiagramPart();
    EntityDiagram entityDiagram = new EntityDiagram(null, localFile.getName(), getContentProvider(), getDecorator());
    entityDiagram.clear();
    entityDiagram.setLayoutManualAllowed(true);
    entityDiagram.setLayoutManualDesired(true);
    diagramPart.setModel(entityDiagram);
    try (final InputStream fileContent = new FileInputStream(localFile)) {
        DiagramLoader.load(progressMonitor, project, diagramPart, fileContent);
    } catch (Exception e) {
        log.error("Error loading ER diagram from '" + localFile.getName() + "'", e);
    }
    return entityDiagram;
}
Also used : EntityDiagram(org.jkiss.dbeaver.erd.ui.model.EntityDiagram) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) DiagramPart(org.jkiss.dbeaver.erd.ui.part.DiagramPart) FileInputStream(java.io.FileInputStream) PartInitException(org.eclipse.ui.PartInitException) DBException(org.jkiss.dbeaver.DBException)

Example 4 with EntityDiagram

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

the class ERDResourceHandler method createDiagram.

public static IFile createDiagram(final EntityDiagram copyFrom, final String title, IFolder folder, DBRProgressMonitor monitor) throws DBException {
    if (folder == null) {
        try {
            folder = getDiagramsFolder(DBWorkbench.getPlatform().getWorkspace().getActiveProject(), true);
        } catch (CoreException e) {
            throw new DBException("Can't obtain folder for diagram", e);
        }
    }
    if (folder == null) {
        throw new DBException("Can't detect folder for diagram");
    }
    ContentUtils.checkFolderExists(folder, monitor);
    final IFile file = ContentUtils.getUniqueFile(folder, CommonUtils.escapeFileName(title), ERD_EXT);
    try {
        DBRRunnableWithProgress runnable = monitor1 -> {
            try {
                EntityDiagram newDiagram = copyFrom == null ? new EntityDiagram(null, "<Diagram>", new ERDContentProviderDecorated(), new ERDDecoratorDefault()) : copyFrom.copy();
                newDiagram.setName(title);
                newDiagram.setLayoutManualAllowed(true);
                newDiagram.setLayoutManualDesired(true);
                String diagramState = DiagramLoader.serializeDiagram(monitor1, null, newDiagram, false, false);
                InputStream data = new ByteArrayInputStream(diagramState.getBytes(StandardCharsets.UTF_8));
                file.create(data, true, RuntimeUtils.getNestedMonitor(monitor1));
            } catch (Exception e) {
                throw new InvocationTargetException(e);
            }
        };
        if (monitor == null) {
            UIUtils.runInProgressService(runnable);
        } else {
            runnable.run(monitor);
        }
    } catch (InvocationTargetException e) {
        throw new DBException("Error creating diagram", e.getTargetException());
    } catch (InterruptedException e) {
    // interrupted
    }
    return file;
}
Also used : DBWorkbench(org.jkiss.dbeaver.runtime.DBWorkbench) IFolder(org.eclipse.core.resources.IFolder) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) ERDContentProviderDecorated(org.jkiss.dbeaver.erd.ui.model.ERDContentProviderDecorated) CoreException(org.eclipse.core.runtime.CoreException) NotNull(org.jkiss.code.NotNull) DBNResource(org.jkiss.dbeaver.model.navigator.DBNResource) IProject(org.eclipse.core.resources.IProject) ByteArrayInputStream(java.io.ByteArrayInputStream) UIUtils(org.jkiss.dbeaver.ui.UIUtils) Log(org.jkiss.dbeaver.Log) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) IFile(org.eclipse.core.resources.IFile) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) ERDEditorStandalone(org.jkiss.dbeaver.erd.ui.editor.ERDEditorStandalone) DiagramLoader(org.jkiss.dbeaver.erd.ui.model.DiagramLoader) CommonUtils(org.jkiss.utils.CommonUtils) FileEditorInput(org.eclipse.ui.part.FileEditorInput) ContentUtils(org.jkiss.dbeaver.utils.ContentUtils) EntityDiagram(org.jkiss.dbeaver.erd.ui.model.EntityDiagram) StandardCharsets(java.nio.charset.StandardCharsets) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) AbstractResourceHandler(org.jkiss.dbeaver.ui.resources.AbstractResourceHandler) RuntimeUtils(org.jkiss.dbeaver.utils.RuntimeUtils) DBException(org.jkiss.dbeaver.DBException) IResource(org.eclipse.core.resources.IResource) ERDDecoratorDefault(org.jkiss.dbeaver.erd.ui.model.ERDDecoratorDefault) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) InputStream(java.io.InputStream) DBException(org.jkiss.dbeaver.DBException) EntityDiagram(org.jkiss.dbeaver.erd.ui.model.EntityDiagram) IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ERDContentProviderDecorated(org.jkiss.dbeaver.erd.ui.model.ERDContentProviderDecorated) ERDDecoratorDefault(org.jkiss.dbeaver.erd.ui.model.ERDDecoratorDefault) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)

Example 5 with EntityDiagram

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

the class ERDEditorPart method restoreVisualSettings.

private boolean restoreVisualSettings(DiagramPart oldDiagram, EntityDiagram newDiagram) {
    boolean hasChanges = false;
    // Collect visual settings from old diagram and apply them to the new one
    for (ERDEntity newEntity : newDiagram.getEntities()) {
        NodePart oldEntity = oldDiagram.getChildByObject(newEntity.getObject());
        if (oldEntity instanceof EntityPart) {
            EntityDiagram.NodeVisualInfo vi = new EntityDiagram.NodeVisualInfo(oldEntity);
            newDiagram.addVisualInfo(newEntity.getObject(), vi);
            hasChanges = true;
        }
    }
    for (ERDNote newNote : newDiagram.getNotes()) {
        NodePart oldNotePart = oldDiagram.getChildByObject(newNote.getObject());
        if (oldNotePart instanceof NotePart) {
            EntityDiagram.NodeVisualInfo vi = new EntityDiagram.NodeVisualInfo(oldNotePart);
            vi.initBounds = oldNotePart.getBounds();
            newDiagram.addVisualInfo(newNote, vi);
            hasChanges = true;
        }
    }
    return hasChanges;
}
Also used : EntityDiagram(org.jkiss.dbeaver.erd.ui.model.EntityDiagram) NotePart(org.jkiss.dbeaver.erd.ui.part.NotePart) NodePart(org.jkiss.dbeaver.erd.ui.part.NodePart) EntityPart(org.jkiss.dbeaver.erd.ui.part.EntityPart)

Aggregations

EntityDiagram (org.jkiss.dbeaver.erd.ui.model.EntityDiagram)10 DBException (org.jkiss.dbeaver.DBException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ERDEditorPart (org.jkiss.dbeaver.erd.ui.editor.ERDEditorPart)2 NotePart (org.jkiss.dbeaver.erd.ui.part.NotePart)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 StringReader (java.io.StringReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 List (java.util.List)1 IFile (org.eclipse.core.resources.IFile)1 IFolder (org.eclipse.core.resources.IFolder)1 IProject (org.eclipse.core.resources.IProject)1 IResource (org.eclipse.core.resources.IResource)1 CoreException (org.eclipse.core.runtime.CoreException)1 Rectangle (org.eclipse.draw2d.geometry.Rectangle)1 PartInitException (org.eclipse.ui.PartInitException)1 FileEditorInput (org.eclipse.ui.part.FileEditorInput)1