package jrex; import com.google.common.collect.Table; import java.awt.Window; import java.util.Arrays; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.JTextArea; /** * * @author Mark George <mark.george@otago.ac.nz> */ public class ReplaceDialog extends javax.swing.JDialog { private final MutableTableModel expressionTableModel = new MutableTableModel(Arrays.asList("Search", "Replace")); private final JTextArea input; /** * Creates new form ReplaceDialog */ public ReplaceDialog(Window parent, JTextArea input) { super(parent); setModal(true); initComponents(); setLocationRelativeTo(parent); tblExpressions.setModel(expressionTableModel); txtInput.setText(input.getText()); this.input = input; expressionTableModel.addRow(new String[]{"",""}); expressionTableModel.fireTableStructureChanged(); tblExpressions.setShowGrid(true); // synchronise the scrolling of the two text areas scrlInput.getVerticalScrollBar().setModel(scrlOutput.getVerticalScrollBar().getModel()); } public String performReplace(String input, String search, String replace) { Pattern pattern = Pattern.compile(search, Pattern.MULTILINE + Pattern.DOTALL + Pattern.COMMENTS); Matcher matcher = pattern.matcher(input); return matcher.replaceAll(replace); } /** * 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() { spltMain = new javax.swing.JSplitPane(); spltLeft = new javax.swing.JSplitPane(); scrlInput = new javax.swing.JScrollPane(); txtInput = new javax.swing.JTextArea(); scrlOutput = new javax.swing.JScrollPane(); txtOutput = new javax.swing.JTextArea(); pblRight = new javax.swing.JPanel(); scrlTable = new javax.swing.JScrollPane(); tblExpressions = new javax.swing.JTable(); btnAddRow = new javax.swing.JButton(); btnReplace = new javax.swing.JButton(); btnSync = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); spltMain.setDividerLocation(500); spltMain.setResizeWeight(0.8); spltMain.setName("spltMain"); // NOI18N spltLeft.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); spltLeft.setResizeWeight(0.5); spltLeft.setName("spltLeft"); // NOI18N scrlInput.setName("scrlInput"); // NOI18N txtInput.setColumns(20); txtInput.setRows(5); txtInput.setName("txtInput"); // NOI18N scrlInput.setViewportView(txtInput); spltLeft.setTopComponent(scrlInput); scrlOutput.setName("scrlOutput"); // NOI18N txtOutput.setColumns(20); txtOutput.setRows(5); txtOutput.setName("txtOutput"); // NOI18N scrlOutput.setViewportView(txtOutput); spltLeft.setRightComponent(scrlOutput); spltMain.setLeftComponent(spltLeft); pblRight.setName("pblRight"); // NOI18N scrlTable.setAlignmentY(0.0F); tblExpressions.setFillsViewportHeight(true); scrlTable.setViewportView(tblExpressions); btnAddRow.setText("Add Row"); btnAddRow.setName("btnAddRow"); // NOI18N btnAddRow.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddRowActionPerformed(evt); } }); btnReplace.setText("Replace"); btnReplace.setName("btnReplace"); // NOI18N btnReplace.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnReplaceActionPerformed(evt); } }); btnSync.setText("Sync Output"); btnSync.setName("btnSync"); // NOI18N btnSync.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSyncActionPerformed(evt); } }); javax.swing.GroupLayout pblRightLayout = new javax.swing.GroupLayout(pblRight); pblRight.setLayout(pblRightLayout); pblRightLayout.setHorizontalGroup( pblRightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(scrlTable, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE) .addComponent(btnAddRow, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnReplace, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnSync, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE) ); pblRightLayout.setVerticalGroup( pblRightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pblRightLayout.createSequentialGroup() .addComponent(scrlTable, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnAddRow) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnReplace) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnSync) .addContainerGap()) ); spltMain.setRightComponent(pblRight); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(spltMain, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 771, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(spltMain) ); pack(); }// </editor-fold>//GEN-END:initComponents private void btnAddRowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddRowActionPerformed expressionTableModel.addRow(new String[]{"", ""}); expressionTableModel.fireTableStructureChanged(); }//GEN-LAST:event_btnAddRowActionPerformed @SuppressWarnings({"unchecked", "UseOfObsoleteCollectionType"}) private void btnReplaceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReplaceActionPerformed String data = txtInput.getText(); Table<Integer, Integer,String> table = expressionTableModel.getTable(); for (Map<Integer, String> row : table.rowMap().values()) { if (!row.get(0).isEmpty()) { String search = row.get(0); String replace = row.get(1); data = performReplace(data, search, replace); } } txtOutput.setText(data); }//GEN-LAST:event_btnReplaceActionPerformed private void btnSyncActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSyncActionPerformed this.input.setText(txtOutput.getText()); }//GEN-LAST:event_btnSyncActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnAddRow; private javax.swing.JButton btnReplace; private javax.swing.JButton btnSync; private javax.swing.JPanel pblRight; private javax.swing.JScrollPane scrlInput; private javax.swing.JScrollPane scrlOutput; private javax.swing.JScrollPane scrlTable; private javax.swing.JSplitPane spltLeft; private javax.swing.JSplitPane spltMain; private javax.swing.JTable tblExpressions; private javax.swing.JTextArea txtInput; private javax.swing.JTextArea txtOutput; // End of variables declaration//GEN-END:variables }