Search in sources :

Example 1 with EasikType

use of easik.database.types.EasikType in project fql by CategoricalData.

the class RowEntryDialog method getOptions.

/**
 * @return
 */
@Override
public List<Option> getOptions() {
    final List<Option> options = new LinkedList<>();
    if (attsToTypes.isEmpty() && foreignKeys.isEmpty()) {
        if (getTitle().startsWith("Update")) {
            options.add(new Option("<html><i>(No attributes or outgoing edges)</i><br>Cannot update foreign keys involved in constraint."));
        } else {
            options.add(new Option("<html><i>(No attributes or outgoing edges)</i><br>Press OK to add a new row."));
        }
        setSize(300, 145);
        return options;
    }
    for (final String name : attsToTypes.keySet()) {
        final EasikType type = attsToTypes.get(name);
        final JComponent c;
        // else
        if (type instanceof easik.database.types.Time) {
            c = JUtils.fixHeight(new TimeChooser());
        } else // else if (type instanceof easik.database.types.Timestamp)
        // {
        // c = JUtils.fixHeight(new DateTimeChooser());
        // }
        // else allow any input
        {
            c = JUtils.textField("");
            c.setToolTipText(type.toString());
            if (defaults.keySet().contains(name)) {
                ((JTextField) c).setText(defaults.get(name));
            }
        }
        options.add(new Option("<html><b>" + name + "</b>", c));
        dialogRows.add(new DialogRow(c, name, type));
    }
    for (final String fkName : foreignKeys.keySet()) {
        // Grab target table name for "type"
        final String targetType = foreignKeys.get(fkName).getName();
        final JTextField idDisplay = JUtils.textField(5);
        if (defaults.keySet().contains(fkName)) {
            idDisplay.setText(defaults.get(fkName));
        }
        idDisplay.setEditable(false);
        final JButton b = new JButton("Choose " + targetType);
        b.addActionListener(new ForeignKeyListener(foreignKeys.get(fkName), idDisplay));
        options.add(new Option("<html><b>" + fkName + "</b>", idDisplay, b));
        dialogRows.add(new DialogRow(idDisplay, fkName, new Int()));
    }
    if (options.size() < 5) {
        setSize(465, 275);
    }
    return options;
}
Also used : JComponent(javax.swing.JComponent) JButton(javax.swing.JButton) JTextField(javax.swing.JTextField) LinkedList(java.util.LinkedList) Int(easik.database.types.Int) Option(easik.ui.Option) EasikType(easik.database.types.EasikType)

Example 2 with EasikType

use of easik.database.types.EasikType in project fql by CategoricalData.

the class JDBCUpdateMonitor method getDialogOptions.

/**
 * Gets the options needed for open a dialog for row insertion: map of
 * attribute names to their type, and a map of foreign key names to a JTable
 * of data they point to.
 *
 * @param table
 *            The node representing the table who's information we want
 * @return Wrapper class holding the maps needed for insertion dialog
 */
private DialogOptions getDialogOptions(final EntityNode table) {
    @SuppressWarnings("unused") final HashSet<ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> constraints = new HashSet<ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>>(table.getConstraints());
    final HashMap<String, EasikType> attToType = new HashMap<>(25);
    final LinkedHashMap<String, EntityNode> fKeys = new LinkedHashMap<>(10);
    // find attributes, and map to their EasikType
    for (final EntityAttribute<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> ea : table.getEntityAttributes()) {
        attToType.put(ea.getName(), ea.getType());
    }
    // find all foreign keys, and add to foreign key set
    for (final SketchEdge ske : table.getOutgoingEdges()) {
        fKeys.put(cn.tableFK(ske), ske.getTargetEntity());
    }
    return new DialogOptions(attToType, fKeys);
}
Also used : ModelConstraint(easik.model.constraint.ModelConstraint) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode) LinkedHashMap(java.util.LinkedHashMap) SketchFrame(easik.ui.SketchFrame) SketchEdge(easik.sketch.edge.SketchEdge) Sketch(easik.sketch.Sketch) EasikType(easik.database.types.EasikType) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with EasikType

use of easik.database.types.EasikType in project fql by CategoricalData.

the class AddAttributeAction method actionPerformed.

/**
 * Inserts an attribute to the currently selected entity (or parent entity
 * if attribute is selected) in the tree
 *
 * @param e
 *            The action event
 */
@Override
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent e) {
    // cancel operation
    if (_theFrame.getMModel().isSynced()) {
        int choice = JOptionPane.showConfirmDialog(_theFrame, "Warning: this sketch is currently synced with a db; continue and break synchronization?", "Warning!", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
        if (choice == JOptionPane.CANCEL_OPTION) {
            return;
        }
    }
    // Get currently selected object
    Object[] selected = _theFrame.getMModel().getSelectionCells();
    N curEntity;
    // Check what is currently selected
    if ((selected.length == 1) && (selected[0] instanceof ModelVertex)) {
        // Entity is selected so set it as current entity
        curEntity = (N) selected[0];
    } else {
        JOptionPane.showMessageDialog(_theFrame, "You do not have an entity selected. \nPlease select a single entity and try again.", "No Entity Selected", JOptionPane.ERROR_MESSAGE);
        return;
    }
    AttributeUI<F, GM, M, N, E> myUI = new AttributeUI<>(_theFrame, curEntity);
    if (myUI.isAccepted()) {
        // Get values from dialog
        String newAttName = myUI.getName();
        EasikType newAttType = myUI.getCustomType();
        // Create Entity Attribute
        EntityAttribute<F, GM, M, N, E> newAtt = new EntityAttribute<>(newAttName, newAttType, curEntity);
        // Add attribute to entity
        curEntity.addEntityAttribute(newAtt);
        // TODO
        /*
			 * need to find way to do this now with generics. Want to add to
			 * queryNodes when adding to entityNode
			 * 
			 * for(ViewNode vn :_theFrame.getMModel().getViews()){ HashMap<N,
			 * QueryNode> nodePairs = vn.getMModel().getEntityNodePairs();
			 * //potentially throws QueryException but won't in this case since
			 * we are just adding attributes try {
			 * nodePairs.get(curEntity).processAttributes(); } catch
			 * (QueryException e1) { e1.printStackTrace(); } }
			 */
        // Refresh view of
        _theFrame.getInfoTreeUI().refreshTree(curEntity);
        // entity
        _theFrame.getMModel().clearSelection();
        _theFrame.getMModel().setDirty();
        _theFrame.getMModel().setSynced(false);
    }
}
Also used : AttributeUI(easik.model.attribute.AttributeUI) EntityAttribute(easik.model.attribute.EntityAttribute) ModelVertex(easik.model.vertex.ModelVertex) EasikType(easik.database.types.EasikType)

Example 4 with EasikType

use of easik.database.types.EasikType in project fql by CategoricalData.

the class EditAttributeAction method actionPerformed.

/**
 * Brings up a dialog to edit the currently selected attribute
 *
 * @param e
 *            The action event
 */
@Override
public void actionPerformed(ActionEvent e) {
    // If there is nothing seleceted then just do nothing
    if (_theFrame.getInfoTreeUI().getInfoTree().isSelectionEmpty()) {
        return;
    }
    // cancel operation
    if (_theFrame.getMModel().isSynced()) {
        int choice = JOptionPane.showConfirmDialog(_theFrame, "Warning: this sketch is currently synced with a db; continue and break synchronization?", "Warning!", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
        if (choice == JOptionPane.CANCEL_OPTION) {
            return;
        }
    }
    // Get currently selected object
    DefaultMutableTreeNode curSelected = (DefaultMutableTreeNode) _theFrame.getInfoTreeUI().getInfoTree().getSelectionPath().getLastPathComponent();
    // Selection is an attribute
    if (curSelected instanceof EntityAttribute) {
        @SuppressWarnings("unchecked") EntityAttribute<F, GM, M, N, E> curAttribute = (EntityAttribute<F, GM, M, N, E>) curSelected;
        // we can cast it because we will only edit in sketches
        N parentEntity = curAttribute.getEntity();
        AttributeUI<F, GM, M, N, E> myUI = new AttributeUI<>(_theFrame, parentEntity, curAttribute);
        if (myUI.isAccepted()) {
            // Get values from dialog
            @SuppressWarnings("unused") String newAttName = myUI.getName();
            @SuppressWarnings("unused") EasikType newAttType = myUI.getCustomType();
            curAttribute.setName(myUI.getName());
            curAttribute.setType(myUI.getCustomType());
            _theFrame.getInfoTreeUI().refreshTree(parentEntity);
            Object[] myCell = new Object[] { parentEntity };
            _theFrame.getMModel().getGraphLayoutCache().hideCells(myCell, true);
            _theFrame.getMModel().getGraphLayoutCache().showCells(myCell, true);
            _theFrame.getMModel().repaint();
            _theFrame.getMModel().setDirty();
            _theFrame.getMModel().setSynced(false);
        }
    } else // Selection is not an attribute
    {
        JOptionPane.showMessageDialog(_theFrame, "You don't have an attribute selected. \nPlease select an attribute and try again.", "No Attribute Selected", JOptionPane.ERROR_MESSAGE);
    }
}
Also used : AttributeUI(easik.model.attribute.AttributeUI) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) EntityAttribute(easik.model.attribute.EntityAttribute) EasikType(easik.database.types.EasikType)

Example 5 with EasikType

use of easik.database.types.EasikType in project fql by CategoricalData.

the class SketchFileIO method sketchToElement.

/**
 * Converts a sketch to an Element.
 *
 * @param document
 *            The Document in which our information is being placed.
 * @param sketch
 * @return All of the information needed to rebuild our sketch containted in
 *         an Element. Returns null in the event that the element could not
 *         be created.
 */
public static Element sketchToElement(Document document, Sketch sketch) {
    try {
        Element rootElement = document.createElement("easketch");
        Element header = document.createElement("header");
        // Add Header info to document
        DocumentInfo d = sketch.getDocInfo();
        Element name = document.createElement("title");
        name.appendChild(document.createTextNode(d.getName()));
        header.appendChild(name);
        for (String aut : d.getAuthors()) {
            Element author = document.createElement("author");
            author.appendChild(document.createTextNode(aut));
            header.appendChild(author);
        }
        Element desc = document.createElement("description");
        desc.appendChild(document.createTextNode(d.getDesc()));
        header.appendChild(desc);
        Element creationDate = document.createElement("creationDate");
        creationDate.appendChild(document.createTextNode(EasikConstants.XML_DATETIME.format(d.getCreationDate())));
        header.appendChild(creationDate);
        Element modDate = document.createElement("lastModificationDate");
        modDate.appendChild(document.createTextNode(EasikConstants.XML_DATETIME.format(d.getModificationDate())));
        header.appendChild(modDate);
        Map<String, String> connParams = sketch.getConnectionParams();
        for (String key : connParams.keySet()) {
            Element connParam = document.createElement("connectionParam");
            connParam.setAttribute("name", key);
            connParam.setAttribute("value", connParams.get(key));
            header.appendChild(connParam);
        }
        if (sketch.isSynced()) {
            header.appendChild(document.createElement("synchronized"));
        }
        rootElement.appendChild(header);
        Element entities = document.createElement("entities");
        // Loop through entities, add them to the document
        for (EntityNode currentEntity : sketch.getEntities()) {
            if (currentEntity == null) {
                continue;
            }
            Element thisEntity = document.createElement("entity");
            thisEntity.setAttribute("name", currentEntity.toString());
            thisEntity.setAttribute("x", currentEntity.getX() + "");
            thisEntity.setAttribute("y", currentEntity.getY() + "");
            entities.appendChild(thisEntity);
            // Loop through attributes, add them to the document
            for (EntityAttribute<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> curAttribute : currentEntity.getEntityAttributes()) {
                Element attributeElmt = document.createElement("attribute");
                attributeElmt.setAttribute("name", curAttribute.getName());
                EasikType attType = curAttribute.getType();
                attributeElmt.setAttribute("attributeTypeClass", attType.getClass().getName());
                Map<String, String> typeAttribs = attType.attributes();
                for (String key : typeAttribs.keySet()) {
                    attributeElmt.setAttribute(key, typeAttribs.get(key));
                }
                thisEntity.appendChild(attributeElmt);
            }
        // We can't go through unique keys yet: they have to come
        // *after* edges
        }
        rootElement.appendChild(entities);
        Element edges = document.createElement("edges");
        for (SketchEdge currentEdge : sketch.getEdges().values()) {
            Element thisEdge = document.createElement("edge");
            thisEdge.setAttribute("id", currentEdge.getName());
            thisEdge.setAttribute("source", currentEdge.getSourceEntity().getName());
            thisEdge.setAttribute("target", currentEdge.getTargetEntity().getName());
            thisEdge.setAttribute("type", (currentEdge instanceof PartialEdge) ? "partial" : (currentEdge instanceof InjectiveEdge) ? "injective" : "normal");
            thisEdge.setAttribute("cascade", (currentEdge.getCascading() == SketchEdge.Cascade.SET_NULL) ? "set_null" : (currentEdge.getCascading() == SketchEdge.Cascade.CASCADE) ? "cascade" : "restrict");
            edges.appendChild(thisEdge);
        }
        rootElement.appendChild(edges);
        Element keys = document.createElement("keys");
        // Loop through unique keys for every node, add them to the document
        for (EntityNode currentEntity : sketch.getEntities()) {
            for (UniqueKey<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> curKey : currentEntity.getUniqueKeys()) {
                Element uniqueKeyElmt = document.createElement("uniqueKey");
                uniqueKeyElmt.setAttribute("name", curKey.getKeyName());
                uniqueKeyElmt.setAttribute("noderef", currentEntity.toString());
                keys.appendChild(uniqueKeyElmt);
                for (UniqueIndexable curElem : curKey.getElements()) {
                    if (curElem instanceof EntityAttribute) {
                        Element attributeElmt = document.createElement("attref");
                        attributeElmt.setAttribute("name", curElem.getName());
                        uniqueKeyElmt.appendChild(attributeElmt);
                    } else if (curElem instanceof SketchEdge) {
                        Element edgeElmt = document.createElement("edgekeyref");
                        edgeElmt.setAttribute("id", curElem.getName());
                        uniqueKeyElmt.appendChild(edgeElmt);
                    } else {
                        System.err.println("Unknown unique key item encountered: element '" + curElem.getName() + "' is neither EntityAttribute nor SketchEdge");
                    }
                }
            }
        }
        rootElement.appendChild(keys);
        Element constraints = document.createElement("constraints");
        // Now add the constraints
        for (ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> curConstraint : sketch.getConstraints().values()) {
            Element thisConstraint = document.createElement(curConstraint.getType());
            thisConstraint.setAttribute("x", curConstraint.getX() + "");
            thisConstraint.setAttribute("y", curConstraint.getY() + "");
            thisConstraint.setAttribute("isVisible", curConstraint.isVisible() ? "true" : "false");
            if (curConstraint instanceof LimitConstraint) {
                LimitConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> lc = (LimitConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) curConstraint;
                // TODO A better way? really long
                // cone - AB
                Element pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getCone().AB.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getCone().AB.getCoDomain().getName());
                for (SketchEdge edge : lc.getCone().AB.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // cone - BC
                pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getCone().BC.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getCone().BC.getCoDomain().getName());
                for (SketchEdge edge : lc.getCone().BC.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // cone - AC
                pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getCone().AC.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getCone().AC.getCoDomain().getName());
                for (SketchEdge edge : lc.getCone().AC.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // limit cone 1 - AB
                pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getLimitCone1().AB.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getLimitCone1().AB.getCoDomain().getName());
                for (SketchEdge edge : lc.getLimitCone1().AB.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // limit cone 1 - BC
                pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getLimitCone1().BC.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getLimitCone1().BC.getCoDomain().getName());
                for (SketchEdge edge : lc.getLimitCone1().BC.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // limit cone 1 - AC
                pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getLimitCone1().AC.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getLimitCone1().AC.getCoDomain().getName());
                for (SketchEdge edge : lc.getLimitCone1().AC.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // limit cone 2 - AB
                pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getLimitCone2().AB.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getLimitCone2().AB.getCoDomain().getName());
                for (SketchEdge edge : lc.getLimitCone2().AB.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // limit cone 2 - BC
                pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getLimitCone2().BC.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getLimitCone2().BC.getCoDomain().getName());
                for (SketchEdge edge : lc.getLimitCone2().BC.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // limit cone 2 - AC
                pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getLimitCone2().AC.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getLimitCone2().AC.getCoDomain().getName());
                for (SketchEdge edge : lc.getLimitCone2().AC.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // Add constraint to constraints
                constraints.appendChild(thisConstraint);
                continue;
            }
            for (ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> path : curConstraint.getPaths()) {
                // Add pathref to constraint
                Element pathElem = document.createElement("path");
                pathElem.setAttribute("domain", path.getDomain().getName());
                pathElem.setAttribute("codomain", path.getCoDomain().getName());
                for (SketchEdge edge : path.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
            }
            // Add constraint to constraints
            constraints.appendChild(thisConstraint);
        }
        rootElement.appendChild(constraints);
        return rootElement;
    } catch (Exception e) {
        return null;
    }
}
Also used : LimitConstraint(easik.model.constraint.LimitConstraint) EntityAttribute(easik.model.attribute.EntityAttribute) Element(org.w3c.dom.Element) PartialEdge(easik.sketch.edge.PartialEdge) EntityNode(easik.sketch.vertex.EntityNode) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) SketchFrame(easik.ui.SketchFrame) InjectiveEdge(easik.sketch.edge.InjectiveEdge) SketchEdge(easik.sketch.edge.SketchEdge) Sketch(easik.sketch.Sketch) EasikType(easik.database.types.EasikType) DocumentInfo(easik.DocumentInfo) UniqueIndexable(easik.model.keys.UniqueIndexable)

Aggregations

EasikType (easik.database.types.EasikType)9 EntityAttribute (easik.model.attribute.EntityAttribute)4 Sketch (easik.sketch.Sketch)4 SketchEdge (easik.sketch.edge.SketchEdge)4 SketchGraphModel (easik.sketch.util.graph.SketchGraphModel)4 EntityNode (easik.sketch.vertex.EntityNode)4 SketchFrame (easik.ui.SketchFrame)4 ModelConstraint (easik.model.constraint.ModelConstraint)3 Int (easik.database.types.Int)2 AttributeUI (easik.model.attribute.AttributeUI)2 LimitConstraint (easik.model.constraint.LimitConstraint)2 UniqueIndexable (easik.model.keys.UniqueIndexable)2 InjectiveEdge (easik.sketch.edge.InjectiveEdge)2 PartialEdge (easik.sketch.edge.PartialEdge)2 Option (easik.ui.Option)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 LinkedList (java.util.LinkedList)2