use of easik.model.attribute.EntityAttribute 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);
}
}
use of easik.model.attribute.EntityAttribute 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);
}
}
use of easik.model.attribute.EntityAttribute 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;
}
}
use of easik.model.attribute.EntityAttribute in project fql by CategoricalData.
the class UniqueKeyUI method getOptions.
/**
* Generates the options/controls to prompt the user for unique key details
*
* @return
*/
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Option> getOptions() {
LinkedList<Option> opts = new LinkedList<>();
opts.add(new Option("Unique key name:", _keyNameField = JUtils.textField("")));
if (_uniqueKey != null) {
_keyNameField.setText(_uniqueKey.getKeyName());
}
// Add the attributes box (only if there are attributes)
EntityAttribute[] atts = _entity.getEntityAttributes().toArray(new EntityAttribute[0]);
_attListField = new JList(atts);
if (atts.length > 0) {
opts.add(new Option("Unique key attributes:", JUtils.fixHeight(new JScrollPane(_attListField))));
}
// Add the outgoing edges box (only if there are outgoing edges)
UniqueIndexable[] edges = _entity.getIndexableEdges().toArray(new UniqueIndexable[0]);
_edgeListField = new JList(edges);
if (edges.length > 0) {
opts.add(new Option("Unique key edges:", JUtils.fixHeight(new JScrollPane(_edgeListField))));
}
if (_uniqueKey != null) {
Set<UniqueIndexable> elems = _uniqueKey.getElements();
ArrayList<Integer> setAtt = new ArrayList<>();
ArrayList<Integer> setEdges = new ArrayList<>();
for (int i = 0; i < atts.length; i++) {
if (elems.contains(atts[i])) {
setAtt.add(i);
}
}
for (int i = 0; i < edges.length; i++) {
if (elems.contains(edges[i])) {
setEdges.add(i);
}
}
int[] setA = new int[setAtt.size()];
int[] setE = new int[setEdges.size()];
for (int i = 0; i < setAtt.size(); i++) {
setA[i] = setAtt.get(i);
}
for (int i = 0; i < setEdges.size(); i++) {
setE[i] = setEdges.get(i);
}
_attListField.setSelectedIndices(setA);
_edgeListField.setSelectedIndices(setE);
}
return opts;
}
use of easik.model.attribute.EntityAttribute 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