Ignore:
Timestamp:
2020-07-13T20:37:55+12:00 (4 years ago)
Author:
ak19
Message:
  1. Added rudimentary support for inserting the basic skeletons of security-related elements into collectionConfiguration.xml when using GLI's ConfigFileEditor. Hopefully this is useful. 2. Removed the self-evident tooltip from the ConfigFileEditor as it was always annoying floating over the editor, obscuring stuff. Had to addd a no-arg constructor in NumberedJTextArea for this to be possible.
File:
1 edited

Legend:

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

    r34246 r34266  
    5555    public final File config_file;
    5656
     57    /** User can add some complex elements by clcking a button */
     58    private Document elements_available_for_insertion;
     59   
    5760    private GLIButton cancel_button = null;
    5861    private GLIButton save_button = null;
     
    6063    private JTextArea editor_msgarea;
    6164
     65    private JComboBox chooseElementComboBox;
     66    private GLIButton addElementButton;
    6267   
    6368    // https://github.com/bobbylight/RSyntaxTextArea/wiki/Example:-Using-Find-and-Replace
     
    6772    private JButton nextButton;
    6873    private JButton prevButton;
    69    
     74   
    7075    //private final String DEFAULT_PROCESSING_INSTRUCTION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";   
    7176
    7277    private static final Dimension SIZE = new Dimension(850,550);
    7378
     79   
     80    // Enum to define elements the user can add
     81    // enum available from Java 1.5
     82    // https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
     83    // https://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html
     84    private enum ElementOption {   
     85    HIDE_COLLECTION (Dictionary.get("ConfigFileEditor.Hide_Collection")),
     86    SECURE_COLLECTION (Dictionary.get("ConfigFileEditor.Secure_Collection")),
     87    SECURE_ALL_DOCUMENTS(Dictionary.get("ConfigFileEditor.Secure_All_Documents")),
     88    SECURE_CERTAIN_DOCUMENTS(Dictionary.get("ConfigFileEditor.Secure_Certain_Documents")),
     89    ADD_ANOTHER_DOCUMENTSET(Dictionary.get("ConfigFileEditor.Add_Another_DocumentSet")),
     90    ADD_ANOTHER_EXCEPTION(Dictionary.get("ConfigFileEditor.Add_Another_Exception"));   
     91   
     92    private final String displayString;
     93    // Constructor may not be declared as public
     94    ElementOption(String displayStr) {
     95        // interesting that Enum implementation classes don't
     96        // have to call protected superclass constructor
     97        displayString = displayStr;
     98    }
     99
     100    // override default behaviour of returning the ENUM string itself
     101    // to display the displayString instead
     102    public String toString() {
     103        return displayString;
     104    }
     105    }
     106
     107   
    74108    public ConfigFileEditor() {
    75109   
     
    127161   
    128162    // the all important XML editor
    129     editor = new NumberedJTextArea(Dictionary.get("ConfigFileEditor.Tooltip"));
     163    editor = new NumberedJTextArea(); // Don't pass in tooltip string for editor:
     164                                      // The editor's purpose should be obvious, and
     165                                      // the tooltip annoyingly constantly floats over the editor all the time
    130166    editor.getDocument().addDocumentListener(this);
    131167    // if Ctrl (or Mac equiv) + F is pressed in the config file editing area,
     
    138174    save_button.addActionListener(this);
    139175
     176    // widgets in section for adding security-related XML elements into the collectionConfigXML file
     177    JLabel addElementLabel = new JLabel(Dictionary.get("ConfigFileEditor.Add_Element_Label"));
     178
     179    chooseElementComboBox = new JComboBox(ElementOption.values()); // calls the implicit static values() method on Enum class to get an array of all Enum values
     180    addElementButton = new GLIButton(Dictionary.get("ConfigFileEditor.Add_Label"),
     181                     Dictionary.get("ConfigFileEditor.Add_Element_Tooltip"));
     182    elements_available_for_insertion = XMLTools.parseXMLFile("xml/elementsForInsertion.xml", true);
     183    addElementButton.addActionListener(this);
     184
     185    // Elements can't be added if gli/classes/xml/elementsForInsertion.xml file
     186    // does not exist or can't be parsed
     187    if(elements_available_for_insertion == null) {
     188        addElementButton.setEnabled(false);
     189    }
     190   
    140191    // LAYOUT
     192    JPanel add_elements_panel = new JPanel(new FlowLayout());
     193    add_elements_panel.setComponentOrientation(Dictionary.getOrientation());
     194    add_elements_panel.add(addElementLabel);
     195    add_elements_panel.add(chooseElementComboBox);
     196    add_elements_panel.add(addElementButton);
     197   
    141198    JPanel button_panel = new JPanel(new FlowLayout());
    142199    button_panel.setComponentOrientation(Dictionary.getOrientation());
     
    145202    button_panel.add(cancel_button);
    146203    button_panel.add(save_button);
    147 
     204   
    148205    // toolbars_panel to contain find-and-replace toolbar and undo/redo/cancel/save button panel
    149206    JPanel toolbars_panel = new JPanel(new BorderLayout());
    150207    toolbars_panel.add(toolBar, BorderLayout.NORTH);
    151     toolbars_panel.add(button_panel, BorderLayout.CENTER);
     208    toolbars_panel.add(add_elements_panel, BorderLayout.CENTER);
     209    toolbars_panel.add(button_panel, BorderLayout.SOUTH);
    152210   
    153211    JPanel content_pane = (JPanel) getContentPane();
     
    196254        editor_msgarea.setText(msg); // display the parsing error
    197255        save_button.setEnabled(false);
     256        addElementButton.setEnabled(false);
    198257
    199258        editor.setText(xml_str); // display the xml contents with error and all     
     
    204263        }       
    205264    }
    206    
     265
    207266
    208267    // Final dialog setup & positioning.
     
    320379       
    321380    }
    322     }
    323    
     381    else if(e.getSource() == addElementButton) {
     382       
     383        ElementOption elementToAdd = (ElementOption)chooseElementComboBox.getSelectedItem();
     384        addElementToConfigFile(elementToAdd);
     385       
     386    }
     387    }
     388
     389    /** Method to add the selected security-related element to the config file
     390     * so that the user can then edit it to behave as they wish.
     391     */
     392    private void addElementToConfigFile(ElementOption elementOption) {
     393
     394    // if the user just undid a previous add operation, then the editor would be empty
     395    // and can't be parsed. In such a case, don't try to add any elements into the editor
     396    String config_xml_str = editor.getText();
     397    if(config_xml_str.equals("")) {
     398        editor_msgarea.setText(Dictionary.get("ConfigFileEditor.Press_Undo_Once_More_First"));
     399        editor_msgarea.setBackground(Color.orange);
     400        return;
     401    }
     402    Document config_xml_doc = XMLTools.getDOM(config_xml_str);
     403    // The addElementButton would not have been enabled/available for pressing
     404    // and we wouldn't be here unless the collConfig xml parses fine.   
     405
     406    Element targetParent = config_xml_doc.getDocumentElement(); // tentative target element
     407   
     408
     409    final String SECURITY = "security"; // just looking for security elements to insert
     410    final String EXCEPTION = "exception"; // just looking for security elements to insert
     411    final String DOCUMENTSET = "documentSet"; // just looking for security elements to insert
     412   
     413    NodeList children = elements_available_for_insertion.getElementsByTagName(SECURITY);       
     414
     415    Element elementToAdd = null;
     416   
     417    // To find out if the collectionConfig.xml has any security nodes in it already
     418    NodeList configXMLSecurityNodes = config_xml_doc.getElementsByTagName(SECURITY);
     419
     420   
     421    // Switch on an Enum type is interesting:
     422    // don't have to do "case ElementOption.HIDE_COLLECTION:"
     423    // and just "case HIDE_COLLECTION:" is fine
     424    // see https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
     425
     426    switch(elementOption) {
     427    case HIDE_COLLECTION:
     428        Element publicElement = XMLTools.getNamedElement(
     429                          config_xml_doc.getDocumentElement(), // parent
     430                          "metadata", // element name
     431                          "name", // attribute name
     432                          "public" // attribute value
     433                      );
     434       
     435        if(publicElement != null) {
     436        XMLTools.setNodeText(publicElement, "false");
     437        // done, with elementToAdd = null, no more processing
     438        } else { // collConfig.xml missing <metadata name="public">true</metadata>
     439        // need to add in the necessary element from elements_available_for_insertion XML,
     440        // which is set to hide
     441        elementToAdd = XMLTools.getNamedElement(
     442                    elements_available_for_insertion.getDocumentElement(), // parent
     443                    "metadata", // element name
     444                    "name", // attribute name
     445                    "public" // attribute value
     446                );
     447        }
     448        break;
     449       
     450    case SECURE_COLLECTION:
     451    case SECURE_ALL_DOCUMENTS:
     452    case SECURE_CERTAIN_DOCUMENTS:
     453        if(configXMLSecurityNodes.getLength() == 0) {
     454        elementToAdd = (Element)children.item(elementOption.ordinal()-1);
     455        } else {
     456        editor_msgarea.setText(Dictionary.get("ConfigFileEditor.Config_Already_Has_Security_Element"));
     457        editor_msgarea.setBackground(Color.orange);
     458        }
     459        break;
     460       
     461       
     462    case ADD_ANOTHER_DOCUMENTSET:
     463    case ADD_ANOTHER_EXCEPTION:
     464       
     465        if(configXMLSecurityNodes.getLength() > 0) {
     466
     467        // get first security element child of collectionConfigXML file.
     468        // There should at most be one
     469        targetParent = (Element)configXMLSecurityNodes.item(0);
     470        Element lastSecurityElement = (Element)children.item(children.getLength()-1);
     471       
     472        if(elementOption == ElementOption.ADD_ANOTHER_DOCUMENTSET) {
     473            // Get the immediate child called documentSet, not any other descendants by that name
     474            // i.e. not the child <exception> element's child called <documentSet>, but the last
     475            // <security> element's <documentSet> element
     476            elementToAdd = (Element)XMLTools.getChildByTagName(lastSecurityElement, DOCUMENTSET);
     477        }
     478        else { // ElementOption.ADD_ANOTHER_EXCEPTION:
     479
     480            // Get the last <security> element's sole <exception> child
     481            elementToAdd = (Element)XMLTools.getChildByTagName(lastSecurityElement, EXCEPTION);
     482        }
     483        }
     484        else {
     485        // Can't Add Another DocSet/Exception element if the collectionConfigXML has no
     486        // Security element in place to add them into.
     487        // Display an error/warning if no security element and don't do anything.       
     488       
     489        targetParent = null;
     490        editor_msgarea.setText(Dictionary.get("ConfigFileEditor.Need_Security_Element_To_Add"));
     491        editor_msgarea.setBackground(Color.orange);
     492        }
     493        break;
     494    }
     495
     496    if(elementToAdd != null) {
     497        Node copiedNode = config_xml_doc.importNode(elementToAdd, true);
     498        targetParent.appendChild(copiedNode);
     499        StringBuffer xml_str_buf = new StringBuffer();
     500        // now set the text in the XML editor to reflect the changes to the doc     
     501        XMLTools.xmlNodeToString(xml_str_buf, config_xml_doc.getDocumentElement(), true, "  ", 0);
     502        editor.setText(xml_str_buf.toString()); // display the xml contents with error and all     
     503    } // TODO: sadly, the act of doing a editor.setText() adds 2 actions to undo history instead of 1
     504    }
    324505   
    325506    // This method returns a proper processing instruction (starts with <?xml and ends with ?>)
     
    395576        editor_msgarea.setBackground(Color.red);
    396577        save_button.setEnabled(false);
     578        addElementButton.setEnabled(false);
    397579        }
    398580    }
Note: See TracChangeset for help on using the changeset viewer.