/*
*/
package ehr_editor;
//import com.sun.imageio.plugins.jpeg.JPEG;
import ehr_editor.leafNodes.ElementLeaf;
import ehr_editor.leafNodes.IntervalEventLeaf;
import ehr_editor.leafNodes.LeafNode;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.tree.DefaultTreeCellEditor;
import org.openehr.am.archetype.Archetype;
import org.openehr.am.archetype.constraintmodel.CAttribute;
import org.openehr.am.archetype.constraintmodel.CComplexObject;
import org.openehr.am.archetype.constraintmodel.CMultipleAttribute;
import org.openehr.am.archetype.constraintmodel.CObject;
import org.openehr.am.archetype.constraintmodel.CSingleAttribute;
import org.openehr.am.archetype.constraintmodel.Cardinality;
import org.openehr.am.archetype.ontology.ArchetypeOntology;
import org.openehr.am.openehrprofile.datatypes.quantity.CDvQuantity;
import org.openehr.am.serialize.ADLSerializer;
import org.openehr.am.serialize.XMLSerializer;
import org.openehr.build.RMObjectBuilder;
import org.openehr.build.RMObjectBuildingException;
import org.openehr.rm.RMObject;
import org.openehr.rm.datatypes.quantity.DvQuantity;
/**
*
* @author
* marek
*/
public class ContainerPanel extends JPanel implements MouseListener, Cloneable {
private static String[] leafNodesNames = {"ELEMENT","INTERVAL_EVENT"};
private static Random generator = new Random();
private CObject object = null;
private ArchetypeOntology ontology = null;
private ContainerPanel parentContainerPanel = null;
private ArrayList<ContainerPanel> childrenContainerPanels = null;
private String rmAttributeName = null;
private CAttribute cAttribute = null;
private int numberOfOccurences = 0;
private int maxNumberOfOccurences = 0;
private int minNumberOfOccurences = 0;
private boolean isRootPanel = false;
private boolean visible = true;
private boolean childrenVisibility = false;
private JComboBox comboBox = null;
private boolean leafCObject = true;
private LeafNode leafNode = null;
public ContainerPanel(ArchetypeOntology ontology) {
this.ontology = ontology;
//Random generator = new Random( );
BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
this.setLayout(boxLayout);
Border border = new LineBorder(new Color(0, 0, 0, 0), 10);
this.setBorder(border);
this.setBackground(new Color(Math.abs(generator.nextInt()) % 128 + 128, Math.abs(generator.nextInt()) % 128 + 128, Math.abs(generator.nextInt()) % 128 + 128));
this.setSize(100, 30);
//this.setPreferredSize(new Dimension(100, 30));
//this.setMaximumSize(new Dimension(500, 30));
this.setVisible(this.visible);
this.childrenContainerPanels = new ArrayList<>();
}
public void setIsRootPanel(boolean status) {
this.isRootPanel = status;
}
public boolean isRootPanel() {
return this.isRootPanel;
}
public void addContainerPanel(ContainerPanel containerPanel) {
this.add(containerPanel);
}
public void setTooltip(String text) {
this.setToolTipText("<html><body>" + text + "</body></html>");
//System.out.println("tooltip: "+text);
}
public void setCObject(CObject object) {
this.object = object;
}
public CObject getCobject() {
return this.object;
}
public void setParentContainerPanel(ContainerPanel containerPanel) {
this.parentContainerPanel = containerPanel;
}
public void addChildrenContainerPanel(ContainerPanel containerPanel) {
this.childrenContainerPanels.add(containerPanel);
}
public ArrayList<ContainerPanel> getChildrenContainerPanels() {
return this.childrenContainerPanels;
}
public void setVisibility(boolean visibility) {
this.visible = visibility;
this.setVisible(this.visible);
//this.revalidate();
}
// @Override
// public void paintComponent(Graphics g){
//
//// this.setSize(100,100);
//// this.setPreferredSize(new Dimension(100,100));
//// if(this.hasParent()){
//// this.setVisibility(true);
//// } else {
//// this.setVisibility(false);
//// }
// if(this.parentContainerPanel != null){
// this.setVisibility(this.parentContainerPanel.getChildrenVisibility());
// }
//
// this.setVisibility(true);
//
// super.paintComponent(g);
// //g.drawString("test", 0, 10); // pravy spodny bod textu
// //this.revalidate();
// //super.repaint();
// //this.repaint();
// }
private boolean hasParent() {
if (this.parentContainerPanel == null) {
return true;
}
if (this.parentContainerPanel.isRootPanel()) {
return true;
} else {
return false;
}
}
public void setRmAttributeName(String rmAttributeName) {
this.rmAttributeName = rmAttributeName;
}
public void setCAttribute(CAttribute cAttribute) {
this.cAttribute = cAttribute;
}
public boolean getChildrenVisibility() {
if (this.isRootPanel == true) {
return true;
}
return this.childrenVisibility;
}
public String getObjectOntologyText() {
String nodeID = this.object.getNodeID();
if (nodeID != null) {
return ontology.termDefinition("en", this.object.getNodeID()).getText();
}
return "???"+nodeID;
}
public String getObjectOntologyDescription(){
String nodeID = this.object.getNodeID();
if (nodeID != null) {
return ontology.termDefinition("en", this.object.getNodeID()).getDescription();
}
return "???"+nodeID;
}
public void initializeContainer() {
if (this.object != null && this.object.getOccurrences() != null) {
if (this.object.getOccurrences().isUpperIncluded()) {
this.maxNumberOfOccurences = this.object.getOccurrences().getUpper();
} else {
this.maxNumberOfOccurences = Integer.MAX_VALUE; // co ak upperInclude false a upperUnbounded false/true?
}
if (this.object.getOccurrences().isLowerIncluded()) {
this.minNumberOfOccurences = this.object.getOccurrences().getLower();
} else {
this.minNumberOfOccurences = 0;
}
}
if (this.object == null) {
for (ContainerPanel child : this.childrenContainerPanels) {
child.initializeContainer();
this.add(child);
}
//this.revalidate();
return;
} else {
// leaf node
for (ContainerPanel child : this.childrenContainerPanels) {
child.initializeContainer();
//this.add(child);
}
}
if(Arrays.asList(leafNodesNames).contains(this.object.getRmTypeName())){
this.leafCObject = true;
} else {
this.leafCObject = false;
}
// COMBOBOX SETUP
if( !this.isLeafCObject() ){
//this.add(new JTextField());
this.add(new JLabel(this.object.getRmTypeName() + " = " + this.object.getClass().getSimpleName()));
String nodeID = this.object.getNodeID();
if (nodeID != null) {
this.add(new JLabel("Ontology: " + ontology.termDefinition("en", this.object.getNodeID()).getText()));
}
this.add(new JLabel(this.cAttribute.getClass().getSimpleName() + " - " + this.cAttribute.path()));
//this.add(new JLabel(this.cAttribute.getClass().getSimpleName() + " - " + this.object.path()));
//this.add(new JLabel("leaf CObject: " + this.isLeafCObject()));
this.add(new JLabel("path: " + this.object.path()));
//this.add(new JLabel("children size: " + this.childrenContainerPanels.size()));
// if( nodeID != null ){
// this.add(new JLabel(ontology.termDefinition("en", this.object.getNodeID()).getDescription()));
// this.add(new JLabel(ontology.termDefinition("en", this.object.getNodeID()).getText()));
// this.add(new JLabel(ontology.termDefinition("en", this.object.getNodeID()).getCode()));
// //this.add(new JLabel());
// }
comboBox = new JComboBox();
comboBox.setPreferredSize(new Dimension((int)comboBox.getPreferredSize().getWidth(), 20));
comboBox.setMaximumSize(new Dimension((int)comboBox.getMaximumSize().getWidth(), 20));
comboBox.addItem("---");
System.out.println(this.childrenContainerPanels);
for (ContainerPanel child : this.childrenContainerPanels) {
comboBox.addItem(child.getObjectOntologyText());
}
this.add(comboBox);
JButton confirm = new JButton("add");
confirm.addMouseListener(this);
this.add(confirm);
} else {
this.analyzeLeafNode();
}
//this.add(new JLabel(this.leafCObject+""));
// if( !this.leafCObject ){
// if (this.cAttribute instanceof CMultipleAttribute) {
// CMultipleAttribute cMultipleAttribute = (CMultipleAttribute) cAttribute;
// Cardinality cardinality = cMultipleAttribute.getCardinality();
// this.add(new JLabel(cardinality.toString()));
// } else {
// System.out.println("initializing singleAttribute");
// this.childrenVisibility = true;
// CSingleAttribute cSingleAttribute = (CSingleAttribute) this.cAttribute;
// if (this.object instanceof CComplexObject) {
// CComplexObject cComplexObject = (CComplexObject) this.object;
// if (cComplexObject.getAttributes() != null) {
// CAttribute childCAttribute = cComplexObject.getAttributes().get(0);
//
// if (childCAttribute instanceof CMultipleAttribute) {
// CMultipleAttribute childCMultipleAttribute = (CMultipleAttribute) childCAttribute;
// Cardinality childCardinality = childCMultipleAttribute.getCardinality();
// //this.add(new JLabel(childCardinality.getInterval().toString()));
// }
// }
// }
// }
// } else {
//
// //this.add(new JLabel("---- leaf info ---"));
//
// this.add(new JLabel("leaf node children size: "+ this.cAttribute.getChildren().size()));
// if( this.object instanceof CComplexObject ){
// CComplexObject complexObject = (CComplexObject) this.object;
//
// for( CAttribute attribute : complexObject.getAttributes() ){
// this.add(new JLabel(attribute.getRmAttributeName()));
// for( CObject cObject : attribute.getChildren()){
// this.add(new JLabel( "\t"+cObject.getRmTypeName() ));
// this.add(new JLabel( "\t"+cObject.getClass().getSimpleName() ));
// if( cObject instanceof CDvQuantity ){
// CDvQuantity cDvQuantity = (CDvQuantity) cObject;
// DvQuantity dvQuantity = cDvQuantity.getDefaultValue();
// //dvQuantity.get
//
// }
// }
// }
// }
// //System.out.println(this.object.getRmTypeName());
// this.add(new JLabel());
//
//
// }
this.revalidate();
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
int index = this.comboBox.getSelectedIndex();
if (index == 0) {
return;
}
if (this.childrenContainerPanels.get(index - 1).isNewClonePossible()) {
// dietatu nastavim pocet vyskytov occurences a potom ho naklonujem
this.childrenContainerPanels.get(index - 1).increaseNumberOfClones();
} else {
return;
}
ContainerPanel cp = (ContainerPanel) this.childrenContainerPanels.get(index - 1).clone();
this.add(cp);
this.revalidate();
// if( !this.childrenContainerPanels.get(index-1).isNewClonePossible() ){
// this.comboBox.removeItemAt(index);
// }
this.comboBox.setSelectedIndex(0);
}
@Override
public void mouseReleased(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseEntered(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void mouseExited(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public ContainerPanel clone() {
ContainerPanel clone = new ContainerPanel(ontology);
clone.setRmAttributeName(this.cAttribute.getRmAttributeName());
clone.setCAttribute(this.cAttribute);
clone.setCObject(this.object);
clone.setParentContainerPanel(this.parentContainerPanel);
for (ContainerPanel child : this.childrenContainerPanels) {
clone.addChildrenContainerPanel(child);
}
clone.setLeafCObject(this.leafCObject);
clone.initializeContainer();
clone.revalidate();
return clone;
}
public boolean isNewClonePossible() {
//System.out.println(this.numberOfOccurences +"/"+ this.maxNumberOfOccurences);
if (this.numberOfOccurences < this.maxNumberOfOccurences) {
return true;
} else {
return false;
}
}
public void increaseNumberOfClones() {
this.numberOfOccurences++;
}
public void decreaseNumberOfClones() {
this.numberOfOccurences--;
}
public boolean isLeafCObject() {
return leafCObject;
}
public void setLeafCObject(boolean leafCObject) {
this.leafCObject = leafCObject;
}
public void analyzeLeafNode() {
switch (this.object.getRmTypeName()) {
case "ELEMENT":
this.leafNode = new ElementLeaf(this);
this.add(this.leafNode);
break;
case "INTERVAL_EVENT":
this.leafNode = new IntervalEventLeaf(this);
this.add(this.leafNode);
break;
default:
}
}
public CObject getObject() {
return object;
}
public ArchetypeOntology getOntology() {
return ontology;
}
public CAttribute getcAttribute() {
return cAttribute;
}
}