Changeset 32091 for main/trunk

Show
Ignore:
Timestamp:
11.12.2017 17:28:04 (22 months ago)
Author:
ak19
Message:

Find functionality for GS3 GLI's Config file editor.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/gui/ConfigFileEditor.java

    r31582 r32091  
    2525 
    2626package org.greenstone.gatherer.gui; 
     27 
     28// for RSyntaxTextArea editor's search functionality: 
     29import org.fife.ui.rtextarea.SearchEngine; 
     30import org.fife.ui.rtextarea.SearchContext; 
    2731 
    2832import org.greenstone.gatherer.Configuration; 
     
    3539import org.w3c.dom.*; 
    3640 
    37  
    3841import java.io.File; 
    3942import java.io.IOException; 
     
    5760    private JTextArea editor_msgarea; 
    5861 
     62     
     63    // https://github.com/bobbylight/RSyntaxTextArea/wiki/Example:-Using-Find-and-Replace 
     64    private JTextField searchField; 
     65    private JCheckBox regexCB; 
     66    private JCheckBox matchCaseCB; 
     67    private JButton nextButton; 
     68    private JButton prevButton; 
     69     
    5970    //private final String DEFAULT_PROCESSING_INSTRUCTION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";     
    6071 
     
    6273 
    6374    public ConfigFileEditor() { 
    64  
    65       super(Gatherer.g_man, true); 
    66       setModal(true); 
    67       setSize(SIZE); 
    68  
     75     
     76    super(Gatherer.g_man, true); 
     77    setModal(true); 
     78    setSize(SIZE); 
     79     
    6980    String collection_folder_path = Gatherer.getCollectDirectoryPath(); 
    7081    String collectionName  = Gatherer.c_man.getCollection().getName(); // we won't be in this constructor if collection is null. 
     
    8899    validation_msg_panel.add(validation_label, BorderLayout.WEST); 
    89100    validation_msg_panel.add(new JScrollPane(editor_msgarea), BorderLayout.CENTER); 
     101 
     102    // Find functionality - can extend for Find and Replace 
     103    // taken from https://github.com/bobbylight/RSyntaxTextArea/wiki/Example:-Using-Find-and-Replace 
     104    // Create a toolbar with searching options. 
     105    JToolBar toolBar = new JToolBar(); 
     106    searchField = new JTextField(30); 
     107    toolBar.add(searchField); 
     108    nextButton = new JButton("Find Next"); 
     109    nextButton.setActionCommand("FindNext"); 
     110    nextButton.addActionListener(this); 
     111    toolBar.add(nextButton); 
     112    searchField.addActionListener(this); 
     113    prevButton = new JButton("Find Previous"); 
     114    prevButton.setActionCommand("FindPrev"); 
     115    prevButton.addActionListener(this); 
     116    toolBar.add(prevButton); 
     117    regexCB = new JCheckBox("Regex"); 
     118    toolBar.add(regexCB); 
     119    matchCaseCB = new JCheckBox("Match Case"); 
     120    toolBar.add(matchCaseCB); 
    90121     
    91122    // the all important XML editor 
     
    106137    button_panel.add(save_button); 
    107138 
     139    // toolbars_panel to contain find-and-replace toolbar and undo/redo/cancel/save button panel 
     140    JPanel toolbars_panel = new JPanel(new BorderLayout()); 
     141    toolbars_panel.add(toolBar, BorderLayout.NORTH); 
     142    toolbars_panel.add(button_panel, BorderLayout.CENTER); 
     143     
    108144    JPanel content_pane = (JPanel) getContentPane(); 
    109145    content_pane.setComponentOrientation(Dictionary.getOrientation()); 
     
    111147    content_pane.add(validation_msg_panel, BorderLayout.NORTH); 
    112148    content_pane.add(new JScrollPane(editor), BorderLayout.CENTER); // make editor scrollable 
    113     content_pane.add(button_panel, BorderLayout.SOUTH); 
    114  
     149    //content_pane.add(button_panel, BorderLayout.SOUTH); 
     150    content_pane.add(toolbars_panel, BorderLayout.SOUTH); 
    115151     
    116152    // NOW WE CAN PREPARE THE ACTUAL CONTENT TO GO INTO THE XML EDITOR TEXTAREA 
     
    166202    Dimension screen_size = Configuration.screen_size; 
    167203    setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2); 
     204    editor.setCaretPosition(0); // start at top of config XML 
    168205    } 
    169206 
     
    190227        Gatherer.c_man.loadCollection(current_collection_filepath); 
    191228    } 
    192     } 
    193  
     229     
     230    // Find functionality, can extend for Find and Replace 
     231    // taken from https://github.com/bobbylight/RSyntaxTextArea/wiki/Example:-Using-Find-and-Replace 
     232    else if(e.getSource() == searchField) { 
     233        nextButton.doClick(0); 
     234    } 
     235    else if(e.getSource() == nextButton || e.getSource() == prevButton) { 
     236        // "FindNext" => search forward, "FindPrev" => search backward 
     237        String command = e.getActionCommand(); 
     238        boolean forward = "FindNext".equals(command); 
     239         
     240        // Create an object defining our search parameters. 
     241        SearchContext context = new SearchContext(); 
     242        String text = searchField.getText(); 
     243        if (text.length() == 0) { 
     244        return; 
     245        } 
     246        context.setSearchFor(text); 
     247        context.setMatchCase(matchCaseCB.isSelected()); 
     248        context.setRegularExpression(regexCB.isSelected()); 
     249        context.setSearchForward(forward); 
     250        context.setWholeWord(false); 
     251 
     252        //boolean found = SearchEngine.find(this.editor, context).wasFound(); 
     253        boolean found = false; 
     254        try { 
     255        found = SearchEngine.find(this.editor, context); 
     256        // if the current search string is a correction to a previous invalid regex, 
     257        // then return background colour back to normal 
     258        searchField.setBackground(Color.white); 
     259        if (!found) { 
     260            JOptionPane.showMessageDialog(this, "Text not found"); 
     261        } 
     262        } catch(java.util.regex.PatternSyntaxException regex_exception) { 
     263        searchField.setBackground(Color.red); 
     264        JOptionPane.showMessageDialog(this, "Invalid regex"); 
     265        } 
     266         
     267    } 
     268    } 
    194269     
    195270