- Timestamp:
- 2020-07-13T20:37:55+12:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/gli/src/org/greenstone/gatherer/gui/ConfigFileEditor.java
r34246 r34266 55 55 public final File config_file; 56 56 57 /** User can add some complex elements by clcking a button */ 58 private Document elements_available_for_insertion; 59 57 60 private GLIButton cancel_button = null; 58 61 private GLIButton save_button = null; … … 60 63 private JTextArea editor_msgarea; 61 64 65 private JComboBox chooseElementComboBox; 66 private GLIButton addElementButton; 62 67 63 68 // https://github.com/bobbylight/RSyntaxTextArea/wiki/Example:-Using-Find-and-Replace … … 67 72 private JButton nextButton; 68 73 private JButton prevButton; 69 74 70 75 //private final String DEFAULT_PROCESSING_INSTRUCTION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; 71 76 72 77 private static final Dimension SIZE = new Dimension(850,550); 73 78 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 74 108 public ConfigFileEditor() { 75 109 … … 127 161 128 162 // 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 130 166 editor.getDocument().addDocumentListener(this); 131 167 // if Ctrl (or Mac equiv) + F is pressed in the config file editing area, … … 138 174 save_button.addActionListener(this); 139 175 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 140 191 // 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 141 198 JPanel button_panel = new JPanel(new FlowLayout()); 142 199 button_panel.setComponentOrientation(Dictionary.getOrientation()); … … 145 202 button_panel.add(cancel_button); 146 203 button_panel.add(save_button); 147 204 148 205 // toolbars_panel to contain find-and-replace toolbar and undo/redo/cancel/save button panel 149 206 JPanel toolbars_panel = new JPanel(new BorderLayout()); 150 207 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); 152 210 153 211 JPanel content_pane = (JPanel) getContentPane(); … … 196 254 editor_msgarea.setText(msg); // display the parsing error 197 255 save_button.setEnabled(false); 256 addElementButton.setEnabled(false); 198 257 199 258 editor.setText(xml_str); // display the xml contents with error and all … … 204 263 } 205 264 } 206 265 207 266 208 267 // Final dialog setup & positioning. … … 320 379 321 380 } 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 } 324 505 325 506 // This method returns a proper processing instruction (starts with <?xml and ends with ?>) … … 395 576 editor_msgarea.setBackground(Color.red); 396 577 save_button.setEnabled(false); 578 addElementButton.setEnabled(false); 397 579 } 398 580 }
Note:
See TracChangeset
for help on using the changeset viewer.