Package net.sf.jiga.xtended.ui

Source Code of net.sf.jiga.xtended.ui.ReplaceExpressionPane

package net.sf.jiga.xtended.ui;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* A set of JTextFields to serve regular expressions replacements at any level
* of your swing install steps. Can be extended to mask visibility.
*/
public class ReplaceExpressionPane extends InstructionSet {

    /**
     * instructions panel owner
     */
    private InstructionsPane owner;
    /**
     * add TextField button
     */
    private JButton addTextFieldButton;
    /**
     * expressions to replace
     */
    private Vector<JTextField> regExpr;
    /**
     * the default value of regular expression fields
     */
    public final static String regExprDefault = "regular expression or string";
    /**
     * replacement expressions
     */
    private Vector<JTextField> adjExpr;
    /**
     * the default value of regular expression replacement fields
     */
    public final static String adjExprDefault = "replacement string";
    /**
     * textField iterator
     */
    private int textN;
    /**
     * reset TextField button
     */
    private Vector<JButton> resetTextFieldButton;

    /**
     * contructs a new Replace expression pane
     *
     * @param owner the owning frame
     */
    public ReplaceExpressionPane(InstructionsPane owner) {
        this.owner = owner;
        addTextFieldButton = new JButton("add a parameter...");
        regExpr = new Vector<JTextField>();
        adjExpr = new Vector<JTextField>();
        resetTextFieldButton = new Vector<JButton>();
        addTextFieldButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                regExpr.add(new JTextField(regExprDefault));
                adjExpr.add(new JTextField(adjExprDefault));
                resetTextFieldButton.add(new JButton("Reset field"));
                resetTextFieldButton.elementAt(resetTextFieldButton.size() - 1).addActionListener(new ActionListener() {

                    int textIndex = regExpr.size() - 1;

                    public void actionPerformed(ActionEvent e) {
                        regExpr.elementAt(textIndex).setText(regExprDefault);
                        adjExpr.elementAt(textIndex).setText(adjExprDefault);
                        regExpr.elementAt(textIndex).repaint();
                        adjExpr.elementAt(textIndex).repaint();
                    }
                });
                add(new JLabel("Text to replace:"));
                regExpr.elementAt(regExpr.size() - 1).setDragEnabled(true);
                add(regExpr.elementAt(regExpr.size() - 1));
                add(new JLabel("Replacement text:"));
                add(adjExpr.elementAt(regExpr.size() - 1));
                adjExpr.elementAt(adjExpr.size() - 1).setDragEnabled(true);
                add(new JLabel("-----------------"));
                add(resetTextFieldButton.elementAt(resetTextFieldButton.size() - 1));
                if (ReplaceExpressionPane.this.owner != null) {
                    ReplaceExpressionPane.this.owner.validate();
                }
            }
        });

        add(null);
        add(new JLabel("If any, set up your in-files parameters:"), 1, 2, 0.0, 0.0);
        add(null);
        add(addTextFieldButton, 1, 2, 0.0, 0.0);
        if (owner != null) {
            owner.validate();
        }
    }

    /**
     * returns the original expresssions Vector
     *
     * @return replacement expressions Vector
     */
    public Vector<JTextField> getRegExpr() {
        return regExpr;
    }

    /**
     * returns the qdjusted replacement expressions Vector
     *
     * @return adjusted replacement expressions
     */
    public Vector<JTextField> getAdjExpr() {
        return adjExpr;
    }

    /**
     * replaces the regular expressions in the specfied source file with the
     * specified text contained in the TextField's. A logger and/or a
     * JProgressBar can be specified, as well.
     *
     * @param src the source file containing the text to process
     * @param regText the JTextField's that contain the regular expressions to
     * find for replacement in the specified source file
     * @param adjText the JTextField's that contain the replacement texts
     * @return the File that contains the adjusted text; its filename is
     * extended with ".rdy"
     * @throws AsynchronousCloseException if the Thread is interrupted while
     * writing
     * @see #_replaceExpr(File, JTextField[], JTextField[], LogArea,
     * JProgressBar)
     */
    public static File _replaceExpr(File src, JTextField[] regText, JTextField[] adjText) throws IOException {
        return _replaceExpr(src, regText, adjText, null, null);
    }

    /**
     * replaces the regular expressions in the specfied source file with the
     * specified text contained in the TextField's. A logger and/or a
     * JProgressBar can be specified, as well.
     *
     * @param src the source file containing the text to process
     * @param regText the JTextField's that contain the regular expressions to
     * find for replacement in the specified source file
     * @param adjText the JTextField's that contain the replacement texts
     * @param log null, or a LogArea instance
     * @param jpb null, or a JProgressBar instance
     * @return the File that contains the adjusted text; its filename is
     * extended with ".rdy"
     * @throws AsynchronousCloseException if the Thread is interrupted while
     * writing
     * @see #_replaceExpr(File, JTextField[], JTextField[])
     */
    public static File _replaceExpr(File src, JTextField[] regText, JTextField[] adjText, LogArea log, JProgressBar jpb) throws IOException {
        RandomAccessFile raf = new RandomAccessFile(src, "r");
        MappedByteBuffer map = raf.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, raf.length());
        map.order(ByteOrder.nativeOrder());
        StringBuffer strBuffer = new StringBuffer(Charset.defaultCharset().decode(map));
        for (int j = 0; j < regText.length; j++) {
            try {
                JTextField field = (JTextField) regText[j];
                if (!field.getText().equals("") && !field.getText().equals(regExprDefault)) {
                    //off = 0;
                    int matchOff = 0;
                    String replaceStr = ((JTextField) adjText[j]).getText();
                    Pattern p = Pattern.compile(field.getText());
                    Matcher m = p.matcher(strBuffer);
                    StringBuffer replaceSb = new StringBuffer();
                    while (m.find()) {
                        m.appendReplacement(replaceSb, replaceStr);
                    }
                    m.appendTail(replaceSb);
                    strBuffer = replaceSb;
                }
            } catch (ClassCastException e) {
                break;
            }
        }
        raf.close();
        if (log instanceof LogArea) {
            log.pushNewLog("making the replacement file of " + src.getName() + ":");
        }
        File preparedFile = new File(src.getPath() + ".rdy");
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(preparedFile.getPath()));
        if (jpb instanceof JProgressBar) {
            jpb.setMaximum(strBuffer.length());
        }
        int off = 0;
        byte[] buffer = new byte[512];
        try {
            while (off < strBuffer.length()) {
                buffer = strBuffer.substring(off, Math.max(off + buffer.length, strBuffer.length())).getBytes();
                bos.write(buffer);
                off += buffer.length;
                if (jpb instanceof JProgressBar) {
                    jpb.setValue(off);
                }
            }
        } catch (AsynchronousCloseException e) {
            throw e;
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
            if (log instanceof LogArea) {
                log.pushNewLog(".");
            }
        } finally {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (log instanceof LogArea) {
                    log.pushNewLog("done.");
                }
                return preparedFile;
            }
        }
    }
}
TOP

Related Classes of net.sf.jiga.xtended.ui.ReplaceExpressionPane

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.