/* * by De Franciscis Dimitri * * megadix@gmail.com */ import java.util.regex.Pattern; import java.util.LinkedList; LinkedList history = new LinkedList(); selectLinesContainingRegex() { dialog = new JDialog(view, "Select lines that match regex", false); content = new JPanel(); dialog.setContentPane(content); fieldPanel = new JPanel(); regexLabel = new JLabel("Regex:"); fieldPanel.add(regexLabel); regexField = new HistoryTextField("org.megadix.jedit.macro.selectLinesContainingRegex.regex"); regexField.setColumns(20); fieldPanel.add(regexField); content.add(fieldPanel); buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); buttonPanel.add(Box.createGlue()); ok = new JButton("OK"); cancel = new JButton("Cancel"); ok.setPreferredSize(cancel.getPreferredSize()); dialog.getRootPane().setDefaultButton(ok); buttonPanel.add(ok); buttonPanel.add(Box.createHorizontalStrut(6)); buttonPanel.add(cancel); buttonPanel.add(Box.createGlue()); content.add(buttonPanel, "South"); // register this method as an ActionListener for // the buttons and text fields (see the section called "Register the Action Listeners") ok.addActionListener(this); cancel.addActionListener(this); regexField.addActionListener(this); // locate the dialog in the center of the // editing pane and make it visible (see the section called "Make the Dialog Visible") dialog.pack(); dialog.setLocationRelativeTo(view); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); // this method will be called when a button is clicked // or when ENTER is pressed (see the section called "The Action Listener") void actionPerformed(e) { if(e.getSource() != cancel) { processText(); } dialog.dispose(); } // this is where the work gets done to insert // the prefix and suffix (see the section called "Get the User's Input") void processText() { regex = regexField.getText(); if(regex.length() == 0) return; regexField.addCurrentToHistory(); pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); int count = 0; for(int i = 0; i < textArea.getLineCount(); i++){ String line = textArea.getLineText(i); m = pattern.matcher(line); if (m.find()) { lineStart = textArea.getBuffer().getLineStartOffset(i); lineEnd = textArea.getBuffer().getLineEndOffset(i); if (lineStart < lineEnd) { Selection range = new Selection.Range(lineStart, lineEnd - 1); textArea.addToSelection(range); count++; } } } } } selectLinesContainingRegex();