/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BGAlphaBaseSamplesGUIBasicUse.java
 *
 * Created on 2011-1-4, 22:14:53
 */

package bgalphabasesamplesgui;

import org.jdesktop.application.Action;
import bg.alpha.base.*;

/**
 *
 * @author z
 */
public class BGAlphaBaseSamplesGUIBasicUse extends javax.swing.JDialog {
    ZCP zcp;
    long lId_r1;

    // Recursive print elements:
    void printElements(long lId, ZCP zcp, String spaces, boolean bAll) throws
        ZZZException {
        if(zcp.moveToFirstElement(lId)) {
            do {
                //System.out.println(spaces+zcp.getElement(lId));
                jtaElement.append("\n"+spaces+zcp.getElement(lId));
                if(bAll) {
                    zcp.moveToSubSet(lId);
                    printElements(lId, zcp, spaces+".", bAll);
                    zcp.moveToSuperSet(lId);
                }
            } while(zcp.moveToNextElement(lId));
        }
    }

    /** Creates new form BGAlphaBaseSamplesGUIBasicUse */
    public BGAlphaBaseSamplesGUIBasicUse(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        try {
            zcp = new ZCP("\\testzcp1.zzz","\\testzcp2.zzz",
                ZZZDefs.cZCO_IN_FILE);

            lId_r1 = zcp.BeginReadProcess();
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jtfElement = new javax.swing.JTextField();
        jbtnMoveToElement = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtaElement = new javax.swing.JTextArea();
        jbtnAddElement = new javax.swing.JButton();
        jbtnChangeElement = new javax.swing.JButton();
        jbtnGetElement = new javax.swing.JButton();
        jbtnRemoveElement = new javax.swing.JButton();
        jbtnMoveToSubSet = new javax.swing.JButton();
        jbtnMoveToSuperSet = new javax.swing.JButton();
        jbtnMoveToFirstElement = new javax.swing.JButton();
        jbtnMoveToLastElement = new javax.swing.JButton();
        jbtnMoveToNextElement = new javax.swing.JButton();
        jbtnMoveToPreviousElement = new javax.swing.JButton();
        jbtnMoveToBeginElement = new javax.swing.JButton();
        jbtnMoveToEndElement = new javax.swing.JButton();
        jbtnClose = new javax.swing.JButton();
        jbtnPrintAll = new javax.swing.JButton();
        jbtnClear = new javax.swing.JButton();
        jbtnPrint = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(bgalphabasesamplesgui.BGAlphaBaseSamplesGUIApp.class).getContext().getResourceMap(BGAlphaBaseSamplesGUIBasicUse.class);
        jtfElement.setText(resourceMap.getString("jtfElement.text")); // NOI18N
        jtfElement.setName("jtfElement"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(bgalphabasesamplesgui.BGAlphaBaseSamplesGUIApp.class).getContext().getActionMap(BGAlphaBaseSamplesGUIBasicUse.class, this);
        jbtnMoveToElement.setAction(actionMap.get("jbtnMoveToElementAction")); // NOI18N
        jbtnMoveToElement.setText(resourceMap.getString("jbtnMoveToElement.text")); // NOI18N
        jbtnMoveToElement.setName("jbtnMoveToElement"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jtaElement.setColumns(20);
        jtaElement.setFont(resourceMap.getFont("jtaElement.font")); // NOI18N
        jtaElement.setRows(5);
        jtaElement.setName("jtaElement"); // NOI18N
        jScrollPane1.setViewportView(jtaElement);

        jbtnAddElement.setAction(actionMap.get("jbtnAddElementAction")); // NOI18N
        jbtnAddElement.setText(resourceMap.getString("jbtnAddElement.text")); // NOI18N
        jbtnAddElement.setName("jbtnAddElement"); // NOI18N

        jbtnChangeElement.setAction(actionMap.get("jbtnChangeElementAction")); // NOI18N
        jbtnChangeElement.setText(resourceMap.getString("jbtnChangeElement.text")); // NOI18N
        jbtnChangeElement.setName("jbtnChangeElement"); // NOI18N

        jbtnGetElement.setAction(actionMap.get("jbtnGetElementAction")); // NOI18N
        jbtnGetElement.setText(resourceMap.getString("jbtnGetElement.text")); // NOI18N
        jbtnGetElement.setName("jbtnGetElement"); // NOI18N

        jbtnRemoveElement.setAction(actionMap.get("jbtnRemoveElementAction")); // NOI18N
        jbtnRemoveElement.setText(resourceMap.getString("jbtnRemoveElement.text")); // NOI18N
        jbtnRemoveElement.setName("jbtnRemoveElement"); // NOI18N

        jbtnMoveToSubSet.setAction(actionMap.get("jbtnMoveToSubSetAction")); // NOI18N
        jbtnMoveToSubSet.setText(resourceMap.getString("jbtnMoveToSubSet.text")); // NOI18N
        jbtnMoveToSubSet.setName("jbtnMoveToSubSet"); // NOI18N

        jbtnMoveToSuperSet.setAction(actionMap.get("jbtnMoveToSuperSetAction")); // NOI18N
        jbtnMoveToSuperSet.setText(resourceMap.getString("jbtnMoveToSuperSet.text")); // NOI18N
        jbtnMoveToSuperSet.setName("jbtnMoveToSuperSet"); // NOI18N

        jbtnMoveToFirstElement.setAction(actionMap.get("jbtnMoveToFirstElementAction")); // NOI18N
        jbtnMoveToFirstElement.setText(resourceMap.getString("jbtnMoveToFirstElement.text")); // NOI18N
        jbtnMoveToFirstElement.setName("jbtnMoveToFirstElement"); // NOI18N

        jbtnMoveToLastElement.setAction(actionMap.get("jbtnMoveToLastElementAction")); // NOI18N
        jbtnMoveToLastElement.setText(resourceMap.getString("jbtnMoveToLastElement.text")); // NOI18N
        jbtnMoveToLastElement.setName("jbtnMoveToLastElement"); // NOI18N

        jbtnMoveToNextElement.setAction(actionMap.get("jbtnMoveToNextElementAction")); // NOI18N
        jbtnMoveToNextElement.setText(resourceMap.getString("jbtnMoveToNextElement.text")); // NOI18N
        jbtnMoveToNextElement.setName("jbtnMoveToNextElement"); // NOI18N

        jbtnMoveToPreviousElement.setAction(actionMap.get("jbtnMoveToPreviousElementAction")); // NOI18N
        jbtnMoveToPreviousElement.setText(resourceMap.getString("jbtnMoveToPreviousElement.text")); // NOI18N
        jbtnMoveToPreviousElement.setName("jbtnMoveToPreviousElement"); // NOI18N

        jbtnMoveToBeginElement.setAction(actionMap.get("jbtnMoveToBeginElementAction")); // NOI18N
        jbtnMoveToBeginElement.setText(resourceMap.getString("jbtnMoveToBeginElement.text")); // NOI18N
        jbtnMoveToBeginElement.setName("jbtnMoveToBeginElement"); // NOI18N

        jbtnMoveToEndElement.setAction(actionMap.get("jbtnMoveToEndElementAction")); // NOI18N
        jbtnMoveToEndElement.setText(resourceMap.getString("jbtnMoveToEndElement.text")); // NOI18N
        jbtnMoveToEndElement.setName("jbtnMoveToEndElement"); // NOI18N

        jbtnClose.setAction(actionMap.get("jbtnCloseAction")); // NOI18N
        jbtnClose.setText(resourceMap.getString("jbtnClose.text")); // NOI18N
        jbtnClose.setName("jbtnClose"); // NOI18N

        jbtnPrintAll.setAction(actionMap.get("jbtnPrintAllAction")); // NOI18N
        jbtnPrintAll.setText(resourceMap.getString("jbtnPrintAll.text")); // NOI18N
        jbtnPrintAll.setName("jbtnPrintAll"); // NOI18N

        jbtnClear.setAction(actionMap.get("jbtnClearAction")); // NOI18N
        jbtnClear.setText(resourceMap.getString("jbtnClear.text")); // NOI18N
        jbtnClear.setName("jbtnClear"); // NOI18N

        jbtnPrint.setAction(actionMap.get("PrintAction")); // NOI18N
        jbtnPrint.setText(resourceMap.getString("jbtnPrint.text")); // NOI18N
        jbtnPrint.setName("jbtnPrint"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtfElement)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtnClose)
                        .addGap(5, 5, 5)
                        .addComponent(jbtnClear)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnPrintAll)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnPrint, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbtnMoveToElement, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                    .addComponent(jbtnAddElement, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                    .addComponent(jbtnChangeElement, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                    .addComponent(jbtnGetElement, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                    .addComponent(jbtnRemoveElement, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                    .addComponent(jbtnMoveToSubSet, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                    .addComponent(jbtnMoveToSuperSet, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                    .addComponent(jbtnMoveToFirstElement, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                    .addComponent(jbtnMoveToLastElement, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                    .addComponent(jbtnMoveToNextElement, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                    .addComponent(jbtnMoveToPreviousElement, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtnMoveToBeginElement, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                    .addComponent(jbtnMoveToEndElement, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtnMoveToElement)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnAddElement)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnChangeElement)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnGetElement)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnRemoveElement)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnMoveToSubSet)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnMoveToSuperSet)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnMoveToFirstElement)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnMoveToLastElement)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnMoveToNextElement)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnMoveToPreviousElement)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnMoveToBeginElement)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtnMoveToEndElement))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jtfElement, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jbtnClose)
                            .addComponent(jbtnPrintAll)
                            .addComponent(jbtnClear)
                            .addComponent(jbtnPrint))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                BGAlphaBaseSamplesGUIBasicUse dialog = new BGAlphaBaseSamplesGUIBasicUse(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void jbtnCloseAction() {
        try {
            zcp.EndProcess(lId_r1);

            zcp.ZZZClose();
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
        dispose();
    }

    @Action
    public void jbtnClearAction() {
        jtaElement.setText("");
    }

    @Action
    public void jbtnMoveToElementAction() {
        try {
            zcp.moveToElement(lId_r1, jtfElement.getText());
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
    }

    @Action
    public void jbtnAddElementAction() {
        try {
            // Make path to source element:
            byte path[] = zcp.ZMakePathToElement(lId_r1, true);
            // Begin write processs:
            long lId_w1 = zcp.BeginWriteProcess(1000);
            try {
                if(path!=null) {
                    // Move to source element:
                    zcp.ZMoveToElementByPath(lId_w1, path, path.length);
                    // Add new element from the text field:
                    if(!zcp.addElement(lId_w1, jtfElement.getText())) {
                        throw new ZZZException("Cannot addElement");
                    }
                }
            } finally {
                // End write process:
                zcp.EndProcess(lId_w1);
            }
        } catch (ZZZException ex) {
            zcp.BreakWriteProcess();
            ex.printStackTrace();
        }
    }

    @Action
    public void jbtnChangeElementAction() {
        try {
            // Make path to source element:
            byte path[] = zcp.ZMakePathToElement(lId_r1, true);
            // Begin write processs:
            long lId_w1 = zcp.BeginWriteProcess(1000);
            try {
                if(path!=null) {
                    // Move to source element:
                    zcp.ZMoveToElementByPath(lId_w1, path, path.length);
                    // Change element from the text field:
                    zcp.changeElement(lId_w1, jtfElement.getText());
                }
            } finally {
                // End write process:
                zcp.EndProcess(lId_w1);
            }
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
    }

    @Action
    public void jbtnGetElementAction() {
        try {
            jtaElement.append("\n"+zcp.getElement(lId_r1));
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
    }

    @Action
    public void jbtnRemoveElementAction() {
        try {
            // Make path to source element:
            byte path[] = zcp.ZMakePathToElement(lId_r1, true);
            // Begin write processs:
            long lId_w1 = zcp.BeginWriteProcess(1000);
            try {
                if(path!=null) {
                    // Move to source element:
                    zcp.ZMoveToElementByPath(lId_w1, path, path.length);
                    // Remove element:
                    zcp.removeElement(lId_w1);
                }
            } finally {
                // End write process:
                zcp.EndProcess(lId_w1);
            }
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
    }

    @Action
    public void jbtnPrintAllAction() {
        try {
            printElements(lId_r1, zcp, "", true);
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
    }

    @Action
    public void PrintAction() {
        try {
            printElements(lId_r1, zcp, "", false);
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
    }

    @Action
    public void jbtnMoveToSubSetAction() {
        try {
            zcp.moveToSubSet(lId_r1);
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
    }

    @Action
    public void jbtnMoveToSuperSetAction() {
        try {
            zcp.moveToSuperSet(lId_r1);
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
    }

    @Action
    public void jbtnMoveToFirstElementAction() {
        try {
            zcp.moveToFirstElement(lId_r1);
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
    }

    @Action
    public void jbtnMoveToLastElementAction() {
        try {
            zcp.moveToLastElement(lId_r1);
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
    }

    @Action
    public void jbtnMoveToNextElementAction() {
        try {
            zcp.moveToNextElement(lId_r1);
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
    }

    @Action
    public void jbtnMoveToPreviousElementAction() {
        try {
            zcp.moveToPreviousElement(lId_r1);
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
    }

    @Action
    public void jbtnMoveToBeginElementAction() {
        try {
            zcp.moveToBeginElement(lId_r1, jtfElement.getText());
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
    }

    @Action
    public void jbtnMoveToEndElementAction() {
        try {
            zcp.moveToEndElement(lId_r1, jtfElement.getText());
        } catch (ZZZException ex) {
            ex.printStackTrace();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnAddElement;
    private javax.swing.JButton jbtnChangeElement;
    private javax.swing.JButton jbtnClear;
    private javax.swing.JButton jbtnClose;
    private javax.swing.JButton jbtnGetElement;
    private javax.swing.JButton jbtnMoveToBeginElement;
    private javax.swing.JButton jbtnMoveToElement;
    private javax.swing.JButton jbtnMoveToEndElement;
    private javax.swing.JButton jbtnMoveToFirstElement;
    private javax.swing.JButton jbtnMoveToLastElement;
    private javax.swing.JButton jbtnMoveToNextElement;
    private javax.swing.JButton jbtnMoveToPreviousElement;
    private javax.swing.JButton jbtnMoveToSubSet;
    private javax.swing.JButton jbtnMoveToSuperSet;
    private javax.swing.JButton jbtnPrint;
    private javax.swing.JButton jbtnPrintAll;
    private javax.swing.JButton jbtnRemoveElement;
    private javax.swing.JTextArea jtaElement;
    private javax.swing.JTextField jtfElement;
    // End of variables declaration//GEN-END:variables

}
