in modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/objentity/ObjAttributeInfoDialog.java [120:267]
private void initController(ObjAttribute attr) {
for (String embeddableName : embeddableNames) {
((DefaultComboBoxModel<String>) view.getTypeComboBox().getModel()).addElement(embeddableName);
}
// need to initialize this early, to react to the later call of the view.getTypeComboBox().setSelectedItem()
view.getTypeComboBox().addActionListener(e -> {
boolean isType = false;
String[] typeNames = ModelerUtil.getRegisteredTypeNames();
for (String typeName : typeNames) {
if (view.getTypeComboBox().getSelectedItem() == null ||
typeName.equals(view.getTypeComboBox().getSelectedItem().toString())) {
isType = true;
}
}
if (isType || !mediator.getEmbeddableNamesInCurrentDataDomain()
.contains((String)view.getTypeComboBox().getSelectedItem())) {
((CardLayout) view.getTypeManagerPane().getLayout()).show(view.getTypeManagerPane(), FLATTENED_PANEL);
} else {
((CardLayout) view.getTypeManagerPane().getLayout()).show(view.getTypeManagerPane(), EMBEDDABLE_PANEL);
view.getCurrentPathLabel().setText("");
}
});
this.attribute = attr;
if (attribute instanceof EmbeddedAttribute || embeddableNames.contains(attribute.getType())) {
this.attributeSaved = new EmbeddedAttribute();
} else {
this.attributeSaved = new ObjAttribute();
}
copyObjAttribute(attributeSaved, attribute);
relTargets = new ArrayList<>(attribute.getEntity().getDataMap().getDbEntities());
/*
* Register auto-selection of the target
*/
view.getPathBrowser().addTreeSelectionListener(this);
view.getAttributeName().setText(attribute.getName());
if (attribute.getDbAttributePath() != null) {
if (attribute.getDbAttributePath().length() > 1) {
String path = attribute.getDbAttributePath().value();
view.getCurrentPathLabel().setText(path.replace(".", " -> "));
} else {
view.getCurrentPathLabel().setText(attribute.getDbAttributePath().value());
}
} else {
view.getCurrentPathLabel().setText("");
}
view.getSourceEntityLabel().setText(attribute.getEntity().getName());
view.getTypeComboBox().setSelectedItem(attribute.getType());
view.getUsedForLockingCheckBox().setSelected(attribute.isUsedForLocking());
view.getLazyCheckBox().setSelected(attribute.isLazy());
view.getCommentField().setText(ObjectInfo
.getFromMetaData(mediator.getApplication().getMetaData(),
attr,
ObjectInfo.COMMENT));
BindingBuilder builder = new BindingBuilder(getApplication().getBindingFactory(), this);
builder.bindToAction(view.getCancelButton(), "closeAction()");
builder.bindToAction(view.getSelectPathButton(), "setPath(true)");
builder.bindToAction(view.getSaveButton(), "saveMapping()");
/*
* set filter for ObjAttributePathBrowser
*/
if (view.getPathBrowser().getModel() == null) {
DbEntity firstEntity = null;
if (attribute.getDbAttribute() == null) {
if (attribute.getParent() instanceof ObjEntity) {
DbEntity dbEnt = ((ObjEntity) attribute.getParent()).getDbEntity();
if (dbEnt != null) {
Collection<DbAttribute> attrib = dbEnt.getAttributes();
Collection<DbRelationship> rel = dbEnt.getRelationships();
if (attrib.size() > 0) {
Iterator<DbAttribute> iter = attrib.iterator();
firstEntity = iter.next().getEntity();
} else if (rel.size() > 0) {
Iterator<DbRelationship> iter = rel.iterator();
firstEntity = iter.next().getSourceEntity();
}
}
}
} else {
firstEntity = getFirstEntity();
}
if (firstEntity != null) {
EntityTreeModel treeModel = new EntityTreeModel(firstEntity);
treeModel.setFilter(new EntityTreeAttributeRelationshipFilter());
view.getPathBrowser().setModel(treeModel);
}
}
if (attribute.getDbAttribute() != null) {
setSelectionPath();
}
view.getTypeComboBox().addItemListener(e -> {
if (lastObjectType != null) {
if (!lastObjectType.equals(e.getItemSelectable())) {
if (embeddableNames.contains(e.getItemSelectable().getSelectedObjects()[0].toString())) {
EmbeddedAttribute copyAttrSaved = new EmbeddedAttribute();
copyObjAttribute(copyAttrSaved, attributeSaved);
attributeSaved = copyAttrSaved;
} else {
if (attributeSaved instanceof EmbeddedAttribute) {
ObjAttribute copyAttrSaved = new ObjAttribute();
copyObjAttribute(copyAttrSaved, attributeSaved);
attributeSaved = copyAttrSaved;
}
}
attributeSaved.setType(e.getItemSelectable().getSelectedObjects()[0].toString());
rebuildTable();
setEnabledSaveButton();
}
}
});
view.getAttributeName().addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
if (!view.getAttributeName().getText().equals(attribute.getName())) {
setEnabledSaveButton();
}
}
public void keyReleased(KeyEvent e) {
if (!view.getAttributeName().getText().equals(attribute.getName())) {
setEnabledSaveButton();
}
}
public void keyTyped(KeyEvent e) {
}
});
rebuildTable();
}