use of easik.model.constraint.ModelConstraint 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);
}
use of easik.model.constraint.ModelConstraint in project fql by CategoricalData.
the class JDBCViewUpdateMonitor method insert.
/**
* Determines if insertion into a given table requires special handling due
* to constraints it may be in. As of now, special cases that may result
* from being in multiple constraints are not supported.
*
* @param table
* The table into which we wish to insert data
* @return Success of the insertion
*/
@Override
public boolean insert(final EntityNode table) {
final DialogOptions dOpts = getDialogOptions(table);
final String lineSep = EasikTools.systemLineSeparator();
// a set of column-value pairs of which we wish to force a specific
// value, leaving the user out
final Set<ColumnEntry> forced = new HashSet<>(10);
// contstraint. Tighten up?
for (final ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> c : table.getConstraints()) {
if (c instanceof SumConstraint) {
// of its foreign key, so remove it from the dialog's selection
for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> sp : c.getPaths()) {
if (sp.getDomain() == table) {
// we force the value 0 to avoid out driver to kick back
// an error for having a null fKey
final String columnName = cn.tableFK(sp.getFirstEdge());
dOpts.fKeys.remove(columnName);
forced.add(new ColumnEntry(columnName, "0", new Int()));
break;
}
}
}
if (c instanceof CommutativeDiagram) {
// commute
if (c.getPaths().get(0).getDomain() == table) {
JOptionPane.showMessageDialog(null, "Be sure that the following paths commute:" + lineSep + EasikTools.join(lineSep, c.getPaths()), "Commutative diagram", JOptionPane.INFORMATION_MESSAGE);
try {
return promptAndInsert(table, dOpts, forced);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Not all of the following paths commute -- insert aborted!" + lineSep + EasikTools.join(lineSep, c.getPaths()), "Commutative diagram failure", JOptionPane.ERROR_MESSAGE);
}
}
}
if (c instanceof PullbackConstraint) {
// happens, we want to let the user update the new record
if (((PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c).getTarget() != table) {
final EntityNode pullback = ((PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c).getSource();
try {
// get row count pre-insert
ResultSet result = dbd.executeQuery("SELECT COUNT(*) FROM " + pullback.getName() + " X");
result.next();
final int preRowCount = result.getInt(1);
if (!promptAndInsert(table, dOpts)) {
return false;
}
// get row count post-insert
result = dbd.executeQuery("SELECT COUNT(*) FROM " + pullback.getName() + " X");
result.next();
final int postRowCount = result.getInt(1);
// new row (the one with the highest primary ID)
if (postRowCount > preRowCount) {
result = dbd.executeQuery("SELECT MAX(" + cn.tablePK(pullback) + ") FROM " + pullback.getName() + " X");
result.next();
final int pk = result.getInt(1);
if (JOptionPane.showConfirmDialog(null, "New record in pullback table '" + pullback.getName() + "'. Enter column data?", "Insert column data?", JOptionPane.YES_NO_OPTION) == 0) {
updateRow(pullback, pk);
}
}
return true;
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Could not execute update: " + e.getMessage());
}
}
}
if (c instanceof ProductConstraint) {
// inserting into the product.
for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> sp : c.getPaths()) {
if (sp.getCoDomain() == table) {
final EntityNode product = sp.getDomain();
try {
if (!promptAndInsert(table, dOpts)) {
return false;
}
// get the new records from the product. They are
// any record who's fk to our INSERT factor matches
// the primary id of the last insert
ResultSet result = dbd.executeQuery("SELECT MAX(" + cn.tablePK(table) + ") FROM " + table.getName() + " X");
result.next();
final int newPK = result.getInt(1);
result = dbd.executeQuery("SELECT * FROM " + product.getName() + " WHERE " + cn.tableFK(sp.getFirstEdge()) + " = " + newPK);
// get count of new rows as result of INSERT
result.last();
final int newRows = result.getRow();
result.beforeFirst();
if ((newRows > 0) && (JOptionPane.showConfirmDialog(null, newRows + " new rows in product table '" + product.getName() + "'. Insert column data?", "Insert column data?", JOptionPane.YES_NO_OPTION) == 0)) {
while (result.next()) {
updateRow(product, result.getInt(1));
}
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
return true;
}
}
}
if (c instanceof LimitConstraint) {
// TRIANGLES TODO CF2012 Incomplete
}
}
try {
return promptAndInsert(table, dOpts, forced);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Could not execute update: " + e.getMessage());
return false;
}
}
use of easik.model.constraint.ModelConstraint in project fql by CategoricalData.
the class DeleteConstraintAction method actionPerformed.
/**
* Called when clicked upon, will delete constraint.
*
* @param e
* The action event
*/
@Override
@SuppressWarnings("unchecked")
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();
if (curSelected instanceof ModelConstraint) {
_theFrame.getMModel().removeConstraint((ModelConstraint<F, GM, M, N, E>) curSelected);
_theFrame.getMModel().setDirty();
_theFrame.getMModel().setSynced(false);
}
}
use of easik.model.constraint.ModelConstraint in project fql by CategoricalData.
the class ViewGraphModel method getAttributes.
/**
* Overridden method to get cell attributes; we make sure the appropriate
* attributes are applied to the Easik objects before returning them.
*
* @see DefaultGraphModel.getAttributes(Object)
*
* @param o
*
* @return
*/
@Override
@SuppressWarnings("unchecked")
public AttributeMap getAttributes(Object o) {
_mode = (_view.getSketch().getFrame().getMode() == Mode.EDIT) ? "edit_" : "manip_";
if (o instanceof GraphCell) {
GraphCell cell = (GraphCell) o;
AttributeMap attribs = cell.getAttributes();
AttributeMap easikAttribs = null;
if (cell instanceof View_Edge) {
easikAttribs = (cell instanceof InjectiveViewEdge) ? injectiveEdgeAttributes() : (cell instanceof PartialViewEdge) ? partialEdgeAttributes() : normalEdgeAttributes();
} else if (cell instanceof TriangleEdge) {
easikAttribs = triangleEdgeAttributes((TriangleEdge<ViewFrame, ViewGraphModel, View, QueryNode, View_Edge>) cell);
} else if (cell instanceof GuideEdge) {
easikAttribs = ((GuideEdge<ViewFrame, ViewGraphModel, View, QueryNode, View_Edge>) cell).isHighlighted() ? virtualHighlightedEdgeAttributes() : virtualEdgeAttributes();
} else if (cell instanceof ModelConstraint) {
easikAttribs = virtualVertexAttributes();
} else if (cell instanceof QueryNode) {
easikAttribs = vertexAttributes();
}
if (easikAttribs != null) {
if (_view.isCellSelected(cell)) {
Color selColor;
float lineWidth;
if (_view.getStateManager().peekState() instanceof GetPathState) {
selColor = getColor("path_selection");
lineWidth = getWidth("path_selection", 2);
} else {
selColor = getColor("selection");
lineWidth = getWidth("selection", 3);
}
int borderWidth = getIntWidth(_mode + ((cell instanceof ModelConstraint) ? "constraint" : "entity") + "_border", 1);
GraphConstants.setBorder(easikAttribs, BorderFactory.createLineBorder(selColor, borderWidth));
GraphConstants.setForeground(easikAttribs, selColor);
GraphConstants.setLineColor(easikAttribs, selColor);
GraphConstants.setLineWidth(easikAttribs, lineWidth);
}
if (attribs == null) {
cell.setAttributes(easikAttribs);
attribs = easikAttribs;
} else {
attribs.applyMap(easikAttribs);
}
return attribs;
}
}
return super.getAttributes(o);
}
use of easik.model.constraint.ModelConstraint in project fql by CategoricalData.
the class ModelInfoTreeUI method setPopMenuItems.
/**
* Sets which of the menu items will be visible
*
* @return true if the popup should be displayed, false otherwise
*/
public boolean setPopMenuItems() {
// If there is nothing seleceted then just do nothing
if (_theFrame.getInfoTreeUI().getInfoTree().isSelectionEmpty()) {
return false;
} else // sketch
if (_theFrame.getMode() == F.Mode.MANIPULATE) {
return false;
}
// Get currently selected object
DefaultMutableTreeNode curSelected = (DefaultMutableTreeNode) _theFrame.getInfoTreeUI().getInfoTree().getSelectionPath().getLastPathComponent();
// Hide all elements
for (Component c : _popupMenu.getComponents()) {
c.setVisible(false);
}
// Check what is currently selected
if (curSelected instanceof ModelVertex) {
_addAttributeItem.setVisible(true);
_addUniqueKeyItem.setVisible(true);
_renameEntityItem.setVisible(true);
_deleteEntityItem.setVisible(true);
} else if (curSelected instanceof EntityAttribute) {
if (_theFrame.getMModel() instanceof Sketch) {
_editAttributeItem.setVisible(true);
_deleteAttributeItem.setVisible(true);
}
} else if (curSelected instanceof UniqueKey) {
if (_theFrame.getMModel() instanceof Sketch) {
_editUniqueKeyItem.setVisible(true);
_deleteUniqueKeyItem.setVisible(true);
}
} else if (curSelected instanceof ModelConstraint) {
if (_theFrame.getMModel() instanceof Sketch) {
if ((curSelected instanceof SumConstraint) || (curSelected instanceof ProductConstraint) || (curSelected instanceof CommutativeDiagram)) {
_addPathItem.setVisible(true);
}
_deleteConstraintItem.setVisible(true);
}
} else if (curSelected instanceof ModelPath) {
Object myConst = curSelected.getParent();
if ((myConst instanceof SumConstraint) || (myConst instanceof ProductConstraint) || (myConst instanceof CommutativeDiagram)) {
_deletePathItem.setVisible(true);
}
} else if (curSelected == _tree_entities) {
_addEntityItem.setVisible(true);
} else if (curSelected == _tree_constraints) {
_addCommutativeItem.setVisible(true);
_addProductItem.setVisible(true);
_addPullbackItem.setVisible(true);
_addSumItem.setVisible(true);
// _addLimItem.setVisible(true);
} else if (curSelected == _tree_constraints_commutative) {
_addCommutativeItem.setVisible(true);
} else if (curSelected == _tree_constraints_product) {
_addProductItem.setVisible(true);
} else if (curSelected == _tree_constraints_pullback) {
_addPullbackItem.setVisible(true);
} else if (curSelected == _tree_constraints_equalizer) {
_addEqualizerItem.setVisible(true);
} else if (curSelected == _tree_constraints_sum) {
_addSumItem.setVisible(true);
} else if (curSelected == _tree_constraints_limit) {
// _addLimItem.setVisible(true);
} else {
return false;
}
return true;
}
Aggregations