Package clips.reportgen.debugpanel

Source Code of clips.reportgen.debugpanel.DebugPanel

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

/*
* DebugPanel.java
*
* Created on 20.07.2009, 13:37:48
*/

package clips.reportgen.debugpanel;

import cli_fmw.delegate.DelegateLine2;
import cli_fmw.main.ClipsException;
import cli_fmw.main.PageContainer;
import cli_fmw.main.PageGeneric;
import clips.reportgen.PanelReportGenerator;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.table_config_states.StateSaver;
import clips.reportgen.QueryLocal;
import java.awt.Window;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import reportgen.utils.ReportException;

/**
*
* @author axe
*/
public class DebugPanel extends PageGeneric {

    private final QueryLocal query;
    private final Window parent;

    private final static String REGEX = "regex";

    /** Creates new form DebugPanel */
    public DebugPanel(Window parent, PageContainer container, QueryLocal aQuery) throws ClipsException {
        super(container);
        initComponents();
        this.query = aQuery;
        this.parent = parent;

        String raw = query.getRaw();
        mainTxt.setText(raw);
        mainTxt.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) { update(e.getDocument()); }
            public void removeUpdate(DocumentEvent e) { update(e.getDocument()); }
            public void changedUpdate(DocumentEvent e) { }
        });
        StateSaver.attachTo(this);
    }

    private void update(Document doc) {
        try {
            String raw = doc.getText(0, doc.getLength());
            query.setRaw(raw);
        } catch (ClipsException ex) {
            MessageBox.showWarning(ex.getLocalizedMessage());
        } catch (BadLocationException ex) {
            MessageBox.showWarning("Некорректный текст");
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        toolbar = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        regexCheck = new javax.swing.JCheckBox();
        ignoreCase = new javax.swing.JCheckBox();
        multilineCheck = new javax.swing.JCheckBox();
        dotAllcheck = new javax.swing.JCheckBox();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        findField = new javax.swing.JTextField();
        findBtn = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        replaceField = new javax.swing.JTextField();
        replaceBtn = new javax.swing.JButton();
        status = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        mainTxt = new javax.swing.JTextArea();
        jPanel3 = new javax.swing.JPanel();
        patchBtn = new javax.swing.JButton();
        checkBtn = new javax.swing.JButton();
        toEditorBtn = new javax.swing.JButton();

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.BorderLayout(5, 5));

        toolbar.setLayout(new java.awt.GridLayout(0, 1, 5, 5));

        jPanel6.setLayout(new javax.swing.BoxLayout(jPanel6, javax.swing.BoxLayout.LINE_AXIS));

        regexCheck.setSelected(true);
        regexCheck.setText("Регулярное выражение");
        jPanel6.add(regexCheck);

        ignoreCase.setSelected(true);
        ignoreCase.setText("Игнорировать регистр");
        jPanel6.add(ignoreCase);

        multilineCheck.setSelected(true);
        multilineCheck.setText("Multiline");
        jPanel6.add(multilineCheck);

        dotAllcheck.setSelected(true);
        dotAllcheck.setText("DotAll");
        jPanel6.add(dotAllcheck);

        toolbar.add(jPanel6);

        jPanel4.setLayout(new java.awt.BorderLayout(5, 5));

        jLabel1.setText("Найти:");
        jPanel4.add(jLabel1, java.awt.BorderLayout.WEST);

        findField.setPreferredSize(new java.awt.Dimension(263, 23));
        jPanel4.add(findField, java.awt.BorderLayout.CENTER);

        findBtn.setText("Найти");
        findBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findBtnActionPerformed(evt);
            }
        });
        jPanel4.add(findBtn, java.awt.BorderLayout.EAST);

        toolbar.add(jPanel4);

        jPanel5.setLayout(new java.awt.BorderLayout(5, 5));

        jLabel2.setText("Заменить:");
        jPanel5.add(jLabel2, java.awt.BorderLayout.WEST);

        replaceField.setPreferredSize(new java.awt.Dimension(263, 23));
        jPanel5.add(replaceField, java.awt.BorderLayout.CENTER);

        replaceBtn.setText("Заменить");
        replaceBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                replaceBtnActionPerformed(evt);
            }
        });
        jPanel5.add(replaceBtn, java.awt.BorderLayout.EAST);

        toolbar.add(jPanel5);

        status.setText("Статус:");
        toolbar.add(status);

        jPanel2.add(toolbar, java.awt.BorderLayout.NORTH);

        mainTxt.setColumns(20);
        mainTxt.setRows(5);
        mainTxt.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                mainTxtCaretUpdate(evt);
            }
        });
        jScrollPane1.setViewportView(mainTxt);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel2, java.awt.BorderLayout.CENTER);

        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        patchBtn.setText("Patch...");
        patchBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                patchBtnActionPerformed(evt);
            }
        });
        jPanel3.add(patchBtn);

        checkBtn.setText("Check");
        checkBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBtnActionPerformed(evt);
            }
        });
        jPanel3.add(checkBtn);

        toEditorBtn.setText("В редактор...");
        toEditorBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toEditorBtnActionPerformed(evt);
            }
        });
        jPanel3.add(toEditorBtn);

        jPanel1.add(jPanel3, java.awt.BorderLayout.SOUTH);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 711, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void toEditorBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toEditorBtnActionPerformed
        try {
            PanelReportGenerator panel = new PanelReportGenerator(getContainer(), query);
            getContainer().addNewPage(panel, this);
            getContainer().activatePage(panel);
            getContainer().removePage(this, true);
        } catch (ClipsException ex) {
            MessageBox.showExceptionOnly(ex);
        }
}//GEN-LAST:event_toEditorBtnActionPerformed

    private void findBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findBtnActionPerformed
        String searchFor = findField.getText();
        if(searchFor.length() == 0) {
            JOptionPane.showMessageDialog(mainTxt, "Введите текст поиска");
        }
        String txt = mainTxt.getText();
        int pos = mainTxt.getCaretPosition();
        int start = -1;
        int end = -1;
        if(!regexCheck.isSelected()) {
            start = txt.indexOf(searchFor, pos);
            if(start == -1) {
                start = txt.indexOf(searchFor);
            }
            if(start != -1) {
                end = start + searchFor.length();
            }
        } else {
            try {
                Pattern compiled = Pattern.compile(searchFor, getFlags());
                Matcher matcher = compiled.matcher(txt);
                boolean found = matcher.find(pos);
                if (!found) {
                    matcher.reset();
                    found = matcher.find();
                }
                if (found) {
                    start = matcher.start();
                    end = matcher.end();
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, e.getLocalizedMessage(),
                        "Error", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        if(start != -1
                && end != -1) {
            mainTxt.select(start, end);
            status.setText("Статус: найдено");
        } else {
            status.setText("Статус: не найдено");
        }
        mainTxt.requestFocus();
    }//GEN-LAST:event_findBtnActionPerformed

    private int getFlags() {
        int flag = Pattern.UNICODE_CASE;
        if(ignoreCase.isSelected()) {
            flag |= Pattern.CASE_INSENSITIVE;
        }
        if(dotAllcheck.isSelected()) {
            flag |= Pattern.DOTALL;
        }
        if(multilineCheck.isSelected()) {
            flag |= Pattern.MULTILINE;
        }
        return flag;
    }
   
    private void replaceBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_replaceBtnActionPerformed
        String searchFor = findField.getText();
        if(searchFor.length() == 0) {
            JOptionPane.showMessageDialog(mainTxt, "Введите текст поиска");
        }
        String replaceBy = replaceField.getText();
        String txt = mainTxt.getText();
        if(!regexCheck.isSelected()) {
            if(txt.indexOf(searchFor) == -1) {
                status.setText("Статус: не найдено");
            } else {
                mainTxt.setText(txt.replace(searchFor, replaceBy));
                status.setText("Статус: заменено");
            }
        } else {
            try {
                Pattern compiled = Pattern.compile(searchFor, getFlags());
                Matcher matcher = compiled.matcher(txt);
                boolean found = matcher.find();
                if (!found) {
                    status.setText("Статус: не найдено");
                } else {
                    mainTxt.setText(matcher.replaceAll(replaceBy));
                    status.setText("Статус: заменено");
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, e.getLocalizedMessage(),
                        "Error", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        update(mainTxt.getDocument());
    }//GEN-LAST:event_replaceBtnActionPerformed


    private void patchBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_patchBtnActionPerformed
        PatchDialog patchDialog = new PatchDialog(parent);
        patchDialog.setVisible(true);
        if(patchDialog.isOk()) {
            String script = patchDialog.getScript();
            if(script.startsWith(REGEX + "\n")) {
                patchRegex(script);
            } else {
                unknownFormatMsg();
            }
        }
}//GEN-LAST:event_patchBtnActionPerformed

    private void unknownFormatMsg() {
        JOptionPane.showMessageDialog(this, "Patch format unknown",
                "Patch error", JOptionPane.INFORMATION_MESSAGE);       
    }

    private void patchRegex(String script) {
        StringTokenizer tokenizer = new StringTokenizer(script, "\n");
        if(!tokenizer.nextToken().equals(REGEX)) {
            unknownFormatMsg();
        } else {
            String txt = mainTxt.getText();
            try {
                while (tokenizer.hasMoreTokens()) {
                    String searchFor = "", replaceBy="";
                    while (tokenizer.hasMoreTokens() && searchFor.length()==0) {
                        searchFor = tokenizer.nextToken().trim();
                    }
                    while (tokenizer.hasMoreTokens() && replaceBy.length()==0) {
                        replaceBy = tokenizer.nextToken().trim();
                    }
                    if (replaceBy.length() == 0) {
                        throw new Exception("Redundant string");
                    }
                    Pattern compiled = Pattern.compile(searchFor, getFlags());
                    Matcher matcher = compiled.matcher(txt);
                    txt = matcher.replaceAll(replaceBy);
                }
                mainTxt.setText(txt);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, ex.getLocalizedMessage(),
                    "Patch error", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }

    private void checkBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBtnActionPerformed
        try {
            query.getQuery();
            JOptionPane.showMessageDialog(this, "Check successful",
                    "Check report", JOptionPane.INFORMATION_MESSAGE);
        } catch (ReportException ex) {
            JOptionPane.showMessageDialog(this, ex.getLocalizedMessage(),
                    "Check report", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_checkBtnActionPerformed

    private void mainTxtCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_mainTxtCaretUpdate
        int caretPos = mainTxt.getCaretPosition();
        try {
            String text = mainTxt.getDocument().getText(0, caretPos);
            int col = text.length() - text.lastIndexOf("\n") - 1;
            status.setText("Status: line #" + text.split("\n").length
                    + " column #" + col);
        } catch (Exception ex) {
            //do nothing
        }
    }//GEN-LAST:event_mainTxtCaretUpdate

    @Override
    public String getPageTitle() {
        return "Отладка отчета " + query.getID();
    }

    @Override
    public DelegateLine2 getDelegate() {
        return query;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton checkBtn;
    private javax.swing.JCheckBox dotAllcheck;
    private javax.swing.JButton findBtn;
    private javax.swing.JTextField findField;
    private javax.swing.JCheckBox ignoreCase;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea mainTxt;
    private javax.swing.JCheckBox multilineCheck;
    private javax.swing.JButton patchBtn;
    private javax.swing.JCheckBox regexCheck;
    private javax.swing.JButton replaceBtn;
    private javax.swing.JTextField replaceField;
    private javax.swing.JLabel status;
    private javax.swing.JButton toEditorBtn;
    private javax.swing.JPanel toolbar;
    // End of variables declaration//GEN-END:variables

}
TOP

Related Classes of clips.reportgen.debugpanel.DebugPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.