org.databene.edifatto.gui.mapping.MappingTreeTableModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of edifatto Show documentation
Show all versions of edifatto Show documentation
'Edifatto' is an open source software library for parsing,
generating and verifying EDIFACT and X12 documents written by Volker Bergmann.
The newest version!
/*
* Copyright (C) 2013-2015 Volker Bergmann ([email protected]).
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.databene.edifatto.gui.mapping;
import org.databene.commons.Named;
import org.databene.edifatto.definition.Definition;
import org.databene.edifatto.gui.ParameterizationHelper;
import org.databene.edifatto.model.Component;
import org.databene.edifatto.model.EdiItem;
import org.databene.edifatto.model.Interchange;
import org.databene.edifatto.template.ClassInfo;
import org.databene.edifatto.template.DataModel;
import org.databene.edifatto.template.EdiItemReferrer;
import org.databene.edifatto.template.PropertyInfo;
import org.jdesktop.swingx.treetable.AbstractTreeTableModel;
/**
* TreeTableModel implementation for changing the default mapping of EDI elements to JavaBeans.
* Created: 28.07.2014 14:41:01
* @since 2.0.0
* @author Volker Bergmann
*/
public class MappingTreeTableModel extends AbstractTreeTableModel {
private DataModel dataModel;
public MappingTreeTableModel() {
this.dataModel = new DataModel();
this.dataModel.addClassInfo(new ClassInfo("none", null));
}
public void setDataModel(DataModel dataModel) {
this.dataModel = dataModel;
modelSupport.fireNewRoot();
}
@Override
public Object getRoot() {
return dataModel.getRootClass();
}
@Override
public int getChildCount(Object node) {
if (node instanceof ClassInfo) {
return ((ClassInfo) node).getPropertyInfoCount();
} else if (node instanceof PropertyInfo) {
PropertyInfo prop = (PropertyInfo) node;
if (prop.isCollection())
return getChildCount(dataModel.getClassByName(prop.getComponentType()));
}
return 0;
}
@Override
public Object getChild(Object node, int index) {
if (node instanceof ClassInfo) {
return ((ClassInfo) node).getPropertyInfo(index);
} else if (node instanceof PropertyInfo) {
PropertyInfo prop = (PropertyInfo) node;
if (prop.isCollection())
return getChild(dataModel.getClassByName(prop.getComponentType()), index);
}
return "?";
}
@Override
public int getIndexOfChild(Object child, Object parentNode) {
if (parentNode instanceof ClassInfo) {
ClassInfo classInfo = (ClassInfo) parentNode;
for (int i = 0; i < classInfo.getPropertyInfoCount(); i++)
if (classInfo.getPropertyInfo(i) == child)
return i;
return -1;
} else if (parentNode instanceof PropertyInfo) {
PropertyInfo prop = (PropertyInfo) parentNode;
String propeTypeName = prop.getComponentType();
ClassInfo propType = dataModel.getClassByName(propeTypeName);
return getIndexOfChild(child, propType);
} else {
return -1;
}
}
@Override
public int getColumnCount() {
return 4;
}
@Override
public int getHierarchicalColumn() {
return 0;
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0: return "Property";
case 1: return "Sample";
case 2: return "Class";
case 3: return "EDI Component";
default: return "";
}
}
@Override
public Class> getColumnClass(int columnIndex) {
switch (columnIndex) {
default : return String.class;
}
}
@Override
public Object getValueAt(Object node, int column) {
switch (column) {
case 0: return ((Named) node).getName();
case 1: return getSample(node);
case 2: return getClass(node);
case 3: return getEDIComponent(node);
default : return null;
}
}
@Override
public boolean isCellEditable(Object node, int column) {
return ((column == 0 && !(node instanceof ClassInfo)) ||
(column == 2 && (node instanceof ClassInfo) || (node instanceof PropertyInfo && ((PropertyInfo) node).isCollection())));
}
@Override
public void setValueAt(Object value, Object node, int column) {
if (column == 0)
changePropertyName(value, node);
else if (column == 2)
changeClassName(value, node);
}
@Override
public boolean isLeaf(Object node) {
return (node instanceof PropertyInfo && !((PropertyInfo) node).isCollection());
}
// private helpers -------------------------------------------------------------------------------------------------
private void changePropertyName(Object value, Object node) {
String newName = (String) value;
EdiItem ediItem = ((EdiItemReferrer) node).getEdiItem();
String oldName = ParameterizationHelper.getPropertyName(ediItem);
ClassInfo clazz = ((PropertyInfo) node).getOwner();
dataModel.changePropertyName(oldName, clazz, newName);
ParameterizationHelper.setPropertyName(newName, ediItem);
}
private void changeClassName(Object value, Object node) {
String newClassName = (String) value;
EdiItem ediItem = ((EdiItemReferrer) node).getEdiItem();
String oldClassName = null;
if (node instanceof ClassInfo)
oldClassName = ((ClassInfo) node).getName();
else
oldClassName = ((PropertyInfo) node).getComponentType();
if (oldClassName != null) {
dataModel.changeClassName(oldClassName, newClassName);
ParameterizationHelper.setBeanClassName(newClassName, ediItem);
if (!(ediItem instanceof Interchange))
ParameterizationHelper.setItemName(newClassName.toLowerCase(), ediItem);
}
}
private static String getSample(Object node) {
if (node instanceof PropertyInfo) {
PropertyInfo prop = (PropertyInfo) node;
if (!prop.isCollection())
return ((Component) prop.getEdiItem()).getData();
}
return null;
}
private static String getClass(Object node) {
if (node instanceof ClassInfo)
return ((ClassInfo) node).getName();
else if (node instanceof PropertyInfo && ((PropertyInfo) node).isCollection())
return ((PropertyInfo) node).getComponentType();
else
return null;
}
private static String getEDIComponent(Object node) {
Definition definition = ((EdiItemReferrer) node).getEdiItem().getDefinition();
return (definition != null ? definition.getName() : null);
}
}