From 4c0c12c0f70aab990d7aefae3ab82d92d24c7cef Mon Sep 17 00:00:00 2001 From: nickdavis Date: Wed, 11 Apr 2001 20:43:21 +0000 Subject: [PATCH] add support for optional elements git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268938 13f79535-47bb-0310-9956-ffa450edef68 --- .../tools/ant/gui/acs/ACSDocumentType.java | 76 +++-- .../ant/gui/acs/ACSDtdDefinedElement.java | 34 ++- .../apache/tools/ant/gui/acs/project-ext.dtd | 273 ++++++++++++++++-- .../org/apache/tools/ant/gui/acs/project.dtd | 183 +++++++----- .../org/apache/tools/ant/gui/acs/share.dtd | 118 ++++++++ .../tools/ant/gui/command/NewElementCmd.java | 4 +- .../tools/ant/gui/command/NewElementDlg.java | 90 +++++- 7 files changed, 648 insertions(+), 130 deletions(-) create mode 100644 src/antidote/org/apache/tools/ant/gui/acs/share.dtd diff --git a/src/antidote/org/apache/tools/ant/gui/acs/ACSDocumentType.java b/src/antidote/org/apache/tools/ant/gui/acs/ACSDocumentType.java index 565c4f5b6..4ad48f039 100644 --- a/src/antidote/org/apache/tools/ant/gui/acs/ACSDocumentType.java +++ b/src/antidote/org/apache/tools/ant/gui/acs/ACSDocumentType.java @@ -72,16 +72,32 @@ import com.sun.xml.parser.Resolver; * @author Nick Davisnick_home_account@yahoo.com */ public class ACSDocumentType extends java.lang.Object { + /** ID for core elements */ + public final static int CORE_ELEMENT = 0; + /** ID for optional elements */ + public final static int OPTIONAL_ELEMENT = 1; /** True if the DTD has been loaded */ private boolean isInit = false; + /** Hold the core DTD elements */ + private HashMap coreElementMap = new HashMap(); + /** Hold the optional DTD elements */ + private HashMap optionalElementMap = new HashMap(); /** Hold the DTD elements */ - private HashMap elementMap = new HashMap(); - /** XML document used to load the DTD */ - final static String XMLDOC = + private HashMap elementMap; + /** First part of the XML document used to load the DTD */ + private final static String XMLDOC_1 = "" + - "" + - "" + + "" + ""; + /** DTD which holds the core tasks */ + private final static String DTD_1 = "project.dtd"; + /** DTD which holds the optional tasks */ + private final static String DTD_2 = "project-ext.dtd"; + /** DTD which holds the shared elements */ + private final static String DTD_SHARE = "share.dtd"; /** * Standard ctor. @@ -114,12 +130,20 @@ public class ACSDocumentType extends java.lang.Object { DtdHandler dtdh = new DtdHandler(); p.setDTDHandler(dtdh); - // Create the default xml file - InputSource xmldoc = new InputSource( - new ByteArrayInputStream(XMLDOC.getBytes())); + String coreDoc = XMLDOC_1 + DTD_1 + XMLDOC_2; + String optionalDoc = XMLDOC_1 + DTD_2 + XMLDOC_2; - // Parse the document - p.parse(xmldoc); + // Parse the core task DTD + elementMap = coreElementMap; + InputSource xmldocCore = new InputSource( + new ByteArrayInputStream(coreDoc.getBytes())); + p.parse(xmldocCore); + + // Parse the core task DTD + elementMap = optionalElementMap; + InputSource xmldocOptional = new InputSource( + new ByteArrayInputStream(optionalDoc.getBytes())); + p.parse(xmldocOptional); isInit = true; } catch (Exception e) { @@ -130,10 +154,14 @@ public class ACSDocumentType extends java.lang.Object { /** * Returns the dtd element. * + * @param elementType CORE_ELEMENT or OPTIONAL_ELEMENT * @param name the element name */ - public DtdElement findElement(String name) { - return (DtdElement) elementMap.get(name); + public DtdElement findElement(int elementType, String name) { + if (elementType == OPTIONAL_ELEMENT) { + return (DtdElement) optionalElementMap.get(name); + } + return (DtdElement) coreElementMap.get(name); } /** @@ -247,7 +275,7 @@ public class ACSDocumentType extends java.lang.Object { Iterator i = values().iterator(); while(i.hasNext()) { DtdAttribute a = (DtdAttribute)i.next(); - if (a.isRequired()) { + if (!a.isRequired()) { list.add(a.getName()); } } @@ -264,7 +292,7 @@ public class ACSDocumentType extends java.lang.Object { Iterator i = values().iterator(); while(i.hasNext()) { DtdAttribute a = (DtdAttribute)i.next(); - if (!a.isRequired()) { + if (a.isRequired()) { list.add(a.getName()); } } @@ -438,22 +466,30 @@ public class ACSDocumentType extends java.lang.Object { String systemId) throws SAXException, IOException { - final String PROJECT = "project.dtd"; - final String PROJECTEXT = "project-ext.dtd"; InputStream result = null; // Is it the project.dtd? - if (systemId.indexOf(PROJECT) != -1) { + if (systemId.indexOf(DTD_1) != -1) { try { // Look for it as a resource - result = getClass().getResourceAsStream(PROJECT); + result = getClass().getResourceAsStream(DTD_1); } catch (Exception e) {} } // Is it the project-ext.dtd? - if (systemId.indexOf(PROJECTEXT) != -1) { + if (systemId.indexOf(DTD_2) != -1) { + try { + // Look for it as a resource + result = getClass().getResourceAsStream(DTD_2); + } catch (Exception e) {} + } + if (result != null) { + return new InputSource(result); + } + // Is it the share.dtd? + if (systemId.indexOf(DTD_SHARE) != -1) { try { // Look for it as a resource - result = getClass().getResourceAsStream(PROJECTEXT); + result = getClass().getResourceAsStream(DTD_SHARE); } catch (Exception e) {} } if (result != null) { diff --git a/src/antidote/org/apache/tools/ant/gui/acs/ACSDtdDefinedElement.java b/src/antidote/org/apache/tools/ant/gui/acs/ACSDtdDefinedElement.java index 3806e0be1..88fda4ff9 100644 --- a/src/antidote/org/apache/tools/ant/gui/acs/ACSDtdDefinedElement.java +++ b/src/antidote/org/apache/tools/ant/gui/acs/ACSDtdDefinedElement.java @@ -53,6 +53,7 @@ */ package org.apache.tools.ant.gui.acs; import org.apache.tools.ant.gui.command.NewElementCmd; +import org.apache.tools.ant.gui.util.Collections; import org.w3c.dom.*; import java.beans.*; import java.util.*; @@ -181,12 +182,21 @@ implements ACSInfoProvider { } ACSDocumentType.DtdElement e = - docType.findElement(name); + docType.findElement(ACSDocumentType.CORE_ELEMENT, name); + if (e == null) { + e = docType.findElement(ACSDocumentType.OPTIONAL_ELEMENT, name); + } if (e != null) { // Use the content model (all the possible // sub-elements) to create the menu. String[] temp = e.getContentModel(); + + // Sort the items + List list = Collections.fill(null, temp); + java.util.Collections.sort(list); + list.toArray(temp); + int size = (temp.length > 5) ? 5 : temp.length; // The project doesn't need a delete menu @@ -222,15 +232,17 @@ implements ACSInfoProvider { } /** - * Retuns a string array which contains this elements - * possible children. It is created from the DTD's - * content model. + * Returns a string array which contains this elements + * possible children. + * + * @param childType ACSDocumentType.CORE_ELEMENT or + * ACSDocumentType.OPTIONAL_ELEMENT */ - public String[] getPossibleChildren() { + public String[] getPossibleChildren(int childType) { String name = getTagName(); + ACSDocumentType.DtdElement e = - docType.findElement(name); - + docType.findElement(childType, name); if (e != null) { return e.getContentModel(); } @@ -266,7 +278,13 @@ implements ACSInfoProvider { } String name = getNodeName(); - _dtdElement = docType.findElement(name); + + _dtdElement = docType.findElement(ACSDocumentType.CORE_ELEMENT, name); + if (_dtdElement == null) { + _dtdElement = docType.findElement( + ACSDocumentType.OPTIONAL_ELEMENT, name); + } + return _dtdElement; } } diff --git a/src/antidote/org/apache/tools/ant/gui/acs/project-ext.dtd b/src/antidote/org/apache/tools/ant/gui/acs/project-ext.dtd index 09243990d..abbac97e7 100644 --- a/src/antidote/org/apache/tools/ant/gui/acs/project-ext.dtd +++ b/src/antidote/org/apache/tools/ant/gui/acs/project-ext.dtd @@ -1,34 +1,255 @@ - + + + +%share-file; + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/antidote/org/apache/tools/ant/gui/acs/project.dtd b/src/antidote/org/apache/tools/ant/gui/acs/project.dtd index cdd11a948..e30e87a4b 100644 --- a/src/antidote/org/apache/tools/ant/gui/acs/project.dtd +++ b/src/antidote/org/apache/tools/ant/gui/acs/project.dtd @@ -1,80 +1,84 @@ - - - -%ext-file; - - - + + + + +%share-file; + - - - - - - - - - - - - - - - + + + + + - + - + + + + + + + - - + + - + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/antidote/org/apache/tools/ant/gui/command/NewElementCmd.java b/src/antidote/org/apache/tools/ant/gui/command/NewElementCmd.java index 183068def..f5480278c 100644 --- a/src/antidote/org/apache/tools/ant/gui/command/NewElementCmd.java +++ b/src/antidote/org/apache/tools/ant/gui/command/NewElementCmd.java @@ -124,7 +124,9 @@ public class NewElementCmd extends AbstractCommand { ACSDtdDefinedElement dtde = (ACSDtdDefinedElement) e; NewElementDlg dlg = new NewElementDlg( getContext().getParentFrame(), true); - dlg.setList(dtde.getPossibleChildren()); + dlg.setLists( + dtde.getPossibleChildren(ACSDocumentType.CORE_ELEMENT), + dtde.getPossibleChildren(ACSDocumentType.OPTIONAL_ELEMENT) ); dlg.pack(); WindowUtils.centerWindow(dlg); dlg.setTitle("Select the new element type"); diff --git a/src/antidote/org/apache/tools/ant/gui/command/NewElementDlg.java b/src/antidote/org/apache/tools/ant/gui/command/NewElementDlg.java index a63b0f897..792ae7a1e 100644 --- a/src/antidote/org/apache/tools/ant/gui/command/NewElementDlg.java +++ b/src/antidote/org/apache/tools/ant/gui/command/NewElementDlg.java @@ -54,6 +54,9 @@ package org.apache.tools.ant.gui.command; import javax.swing.*; +import java.util.List; +import java.util.ArrayList; +import org.apache.tools.ant.gui.util.Collections; /** * A Dialog which asks for a new xml element's type. @@ -67,6 +70,7 @@ public class NewElementDlg extends javax.swing.JDialog { private javax.swing.JPanel _buttonPanel; private javax.swing.JButton _buttonOK; private javax.swing.JButton _buttonCancel; + private javax.swing.JCheckBox _optionalButton; private javax.swing.JPanel _selectPanel; private javax.swing.JPanel _panelData; private javax.swing.JLabel _label; @@ -77,11 +81,17 @@ public class NewElementDlg extends javax.swing.JDialog { private boolean _cancel = true; /** holds the element type */ private String _elementName; + /** list of core tasks */ + private List _coreElements; + /** list of optional tasks */ + private List _optionalElements; + /** list of tasks to display */ + private List _elements; /** * Creates new form NewElementDlg */ - public NewElementDlg(java.awt.Frame parent,boolean modal) { + public NewElementDlg(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); enableButtons(); @@ -90,12 +100,54 @@ public class NewElementDlg extends javax.swing.JDialog { /** * Fills the listbox with the input list. */ - public void setList(String[] list) { - if (list == null || list.length == 0) { + public void setLists(String[] coreElements, String[] optionalElements) { + + // Are there any items to display? + if ( (coreElements == null || coreElements.length == 0) && + (optionalElements == null || optionalElements.length == 0 ) ) { + + // Hide the list _listScrollPane.setVisible(false); + _optionalButton.setVisible(false); } else { - _elementList.setListData(list); + + // Are there any core elements? + if (coreElements == null) { + _coreElements = new ArrayList(); + + // Display the optional elements + _optionalButton.setSelected(true); + _optionalButton.setVisible(false); + } else { + // Create a sorted list of the core elements + List temp = Collections.fill(null, coreElements); + java.util.Collections.sort(temp); + _coreElements = temp; + } + + // Are there any optional elements? + if (optionalElements == null) { + _optionalElements = new ArrayList(); + + // Display the core elements + _optionalButton.setSelected(false); + _optionalButton.setVisible(false); + } else { + // Create a sorted list of the optional elements + List temp = Collections.fill(null, optionalElements); + java.util.Collections.sort(temp); + _optionalElements = temp; + } + + // Are the lists the same? + if (_optionalElements.containsAll(_coreElements) && + _coreElements.containsAll(_optionalElements) ) { + + // Hide the button + _optionalButton.setVisible(false); + } } + enableButtons(); } /** @@ -116,11 +168,26 @@ public class NewElementDlg extends javax.swing.JDialog { * Enable or disable buttons */ private void enableButtons() { + + // Enable the OK button? if (isInputValid()) { _buttonOK.setEnabled(true); } else { _buttonOK.setEnabled(false); } + + // Display the core or optional elements? + Object oldList = _elements; + if (_optionalButton.isSelected()) { + _elements = _optionalElements; + } else { + _elements = _coreElements; + } + + // Did the list change? + if (oldList != _elements) { + _elementList.setListData(_elements.toArray()); + } } /** @@ -159,6 +226,8 @@ public class NewElementDlg extends javax.swing.JDialog { return true; else if (c == '>') return false; + else if (c >= '0' && c <= '9') + return true; else if (c == '.' || c == '-' || c == '_' || c == ':') return true; else @@ -193,6 +262,8 @@ public class NewElementDlg extends javax.swing.JDialog { _elementText = new javax.swing.JTextField(); _listScrollPane = new javax.swing.JScrollPane(); _elementList = new javax.swing.JList(); + _optionalButton = new javax.swing.JCheckBox( + "show optional elements", false); getContentPane().setLayout(new java.awt.BorderLayout(10, 10)); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { @@ -272,7 +343,16 @@ public class NewElementDlg extends javax.swing.JDialog { } ); _listScrollPane.setViewportView(_elementList); - + + _optionalButton.setMargin(new java.awt.Insets(2, 2, 2, 2)); + _optionalButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + enableButtons(); + } + } + ); + + _selectPanel.add(_optionalButton, java.awt.BorderLayout.NORTH); _selectPanel.add(_listScrollPane, java.awt.BorderLayout.CENTER); getContentPane().add(_selectPanel, java.awt.BorderLayout.CENTER); pack();