You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Nick Davis <ni...@yahoo.com> on 2001/04/04 05:59:42 UTC
[PATCH-ANTIDOTE]Selection of available attributes
Hi,
I would like to submit a patch to the ANTIDOTE
project. This patch allows a combobox to be
displayed when the user selects the "name"
column from the properties pane. The
combobox displays the possible attributes
available for the select element.
There is also a small change which removes
the use of a JDK 1.3 feature.
Cheers,
Nick
p.s. please let me know if this patch is not in
the correct format.
------------------------------------------------------------------
Note:
Added "DtdAttributes" class and fixed some problem
with DtdElement's
getters and setters.
Index: ACSDocumentType.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/antidote/org/apache/tools/ant/gui/acs/ACSDocumentType.java,v
retrieving revision 1.1
diff -u -r1.1 ACSDocumentType.java
--- ACSDocumentType.java 2001/03/28 12:29:38 1.1
+++ ACSDocumentType.java 2001/04/04 01:57:31
@@ -142,7 +142,7 @@
static class DtdElement {
private String _name;
private String[] _contentModel;
- private HashMap _map = new HashMap();
+ private DtdAttributes _map = new
DtdAttributes();
public String getName() {
return _name;
@@ -156,7 +156,7 @@
public void setContentModel(String[] model) {
_contentModel = model;
}
- public HashMap getMap() {
+ public DtdAttributes getAttributes() {
return _map;
}
}
@@ -176,13 +176,13 @@
return _name;
}
public void setName(String name) {
- _name = _name;
+ _name = name;
}
public String getType() {
return _type;
}
public void setType(String type) {
- _name = type;
+ _type = type;
}
public String getDefaultValue() {
return _defaultValue;
@@ -211,6 +211,84 @@
}
/**
+ * Class which represents a collection of DTD
attributes.
+ */
+ public static class DtdAttributes extends HashMap
{
+ /**
+ * Default constructor
+ */
+ public DtdAttributes() {
+ }
+
+ /**
+ * Adds the Attribute
+ *
+ * @param attribute new attribute
+ */
+ public void addAttribute(DtdAttribute
attribute) {
+ put(attribute.getName(), attribute);
+ }
+
+ /**
+ * Return the requested attribute
+ *
+ * @param name attribute name
+ * @returns the requested attribute
+ */
+ public DtdAttribute getAttribute(String name)
{
+ return (DtdAttribute) get(name);
+ }
+
+ /**
+ * @returns an array of the optional
attribute names
+ */
+ public String[] getOptionalAttributes() {
+ ArrayList list = new ArrayList();
+ Iterator i = values().iterator();
+ while(i.hasNext()) {
+ DtdAttribute a =
(DtdAttribute)i.next();
+ if (a.isRequired()) {
+ list.add(a.getName());
+ }
+ }
+ String[] result = new
String[list.size()];
+ list.toArray(result);
+ return result;
+ }
+
+ /**
+ * @returns an array of the required
attribute names
+ */
+ public String[] getRequiredAttributes() {
+ ArrayList list = new ArrayList();
+ Iterator i = values().iterator();
+ while(i.hasNext()) {
+ DtdAttribute a =
(DtdAttribute)i.next();
+ if (!a.isRequired()) {
+ list.add(a.getName());
+ }
+ }
+ String[] result = new
String[list.size()];
+ list.toArray(result);
+ return result;
+ }
+ /**
+ * @returns an array of the all attribute
names
+ */
+ public String[] getAttributes() {
+ ArrayList list = new ArrayList();
+ Iterator i = values().iterator();
+ while(i.hasNext()) {
+ DtdAttribute a =
(DtdAttribute)i.next();
+ list.add(a.getName());
+ }
+ String[] result = new
String[list.size()];
+ list.toArray(result);
+ return result;
+ }
+ }
+
+ /**
* When parsing XML documents, DTD related events
are signaled through
* this interface.
*/
@@ -303,7 +381,7 @@
attrib.setRequired(isRequired);
attrib.setDefaultValue(defaultValue);
attrib.setOptions(options);
- e.getMap().put(attrib.getName(), e);
+ e.getAttributes().addAttribute(attrib);
}
/**
-------------------------------------------------------------------
Note:
Change the parameter's for "setNamedValues" and
"getNamedValues"
from Properties to ACSDtdDefinedAttributes. Added two
methods:
addRequiredAttributes and getDtdElement. Added
_dtdElement
member.
Index: ACSDtdDefinedElement.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/antidote/org/apache/tools/ant/gui/acs/ACSDtdDefinedElement.java,v
retrieving revision 1.1
diff -u -r1.1 ACSDtdDefinedElement.java
--- ACSDtdDefinedElement.java 2001/03/28 12:29:38 1.1
+++ ACSDtdDefinedElement.java 2001/04/04 02:15:14
@@ -75,6 +75,8 @@
static ACSDocumentType docType = new
ACSDocumentType();
/** Our menu string */
public String[] menuString = null;
+ /** The DTD element we represent */
+ private ACSDocumentType.DtdElement _dtdElement =
null;
/**
* Default ctor.
@@ -110,8 +112,9 @@
*
* @return Name-value mappings.
*/
- public Properties getNamedValues() {
- Properties retval = new Properties();
+ public ACSDtdDefinedAttributes getNamedValues() {
+ ACSDtdDefinedAttributes retval =
+ new
ACSDtdDefinedAttributes(getDtdElement());
NamedNodeMap attribs = getAttributes();
for(int i = 0, len = attribs.getLength(); i <
len; i++) {
@@ -128,34 +131,34 @@
*
* @param attributes New attribute set.
*/
- public void setNamedValues(Properties props) {
- // XXX this code really sucks. It is really
annoying that the
+ public void
setNamedValues(ACSDtdDefinedAttributes attributes) {
+ // XXX this code really sucks. It is really
annoying that the
// DOM interfaces don't have a general
"setAttributes()" or
- // "removeAllAttributes()" method, but
instead make you
+ // "removeAllAttributes()" method, but
instead make you
// remove each attribute individually, or
require you to figure
- // out what the differences are between the
two.
+ // out what the differences are between the
two.
// Although this is very inefficient, I'm
taking the conceptually
- // simplistic approach to this and brute
force removing the existing
- // set and replacing it with a brand new set.
If this becomes a
- // performance concern (which I doubt it
will) it can be optimized
+ // simplistic approach to this and brute
force removing the existing
+ // set and replacing it with a brand new set.
If this becomes a
+ // performance concern (which I doubt it
will) it can be optimized
// later.
- Properties old = getNamedValues();
+ ACSDtdDefinedAttributes old =
(ACSDtdDefinedAttributes) getNamedValues();
Enumeration enum = old.propertyNames();
while(enum.hasMoreElements()) {
String name = (String)
enum.nextElement();
removeAttribute(name);
}
-
- enum = props.propertyNames();
+
+ enum = attributes.propertyNames();
while(enum.hasMoreElements()) {
String key = (String) enum.nextElement();
- setAttribute(key,
props.getProperty(key));
+ setAttribute(key,
attributes.getProperty(key));
}
- firePropertyChange(NAMED_VALUES, old, props);
+ firePropertyChange(NAMED_VALUES, old,
attributes);
}
/**
@@ -210,6 +213,33 @@
return e.getContentModel();
}
return null;
+ }
+
+ /**
+ * Adds the attributes which are required by this
node.
+ */
+ public void addRequiredAttributes() {
+ ACSDocumentType.DtdElement e =
getDtdElement();
+ String[] attributes =
e.getAttributes().getRequiredAttributes();
+ for(int i = 0; i < attributes.length; i++) {
+ if (
getAttributes().getNamedItem(attributes[i]) == null) {
+ // XXX should set to default?
+ setAttribute(attributes[i], "");
+ }
+ }
+ }
+
+ /**
+ * Returns the DTD element we represent
+ */
+ private ACSDocumentType.DtdElement
getDtdElement() {
+ if (_dtdElement != null) {
+ return _dtdElement;
+ }
+
+ String name = getNodeName();
+ _dtdElement = docType.findElement(name);
+ return _dtdElement;
}
}
-------------------------------------------------------------------
Note:
Added registerEditor call.
Index: ACSDtdDefinedElementBeanInfo.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/antidote/org/apache/tools/ant/gui/acs/ACSDtdDefinedElementBeanInfo.java,v
retrieving revision 1.1
diff -u -r1.1 ACSDtdDefinedElementBeanInfo.java
--- ACSDtdDefinedElementBeanInfo.java 2001/03/28
12:29:38 1.1
+++ ACSDtdDefinedElementBeanInfo.java 2001/04/04
02:19:49
@@ -53,6 +53,7 @@
*/
package org.apache.tools.ant.gui.acs;
import
org.apache.tools.ant.gui.customizer.DynamicCustomizer;
+import
org.apache.tools.ant.gui.modules.edit.DtdAttributePropertyEditor;
import java.beans.*;
/**
@@ -125,6 +126,11 @@
/** Customizer for this bean info. */
public static class Customizer extends
DynamicCustomizer {
+ static {
+ PropertyEditorManager.registerEditor(
+ ACSDtdDefinedAttributes.class,
DtdAttributePropertyEditor.class);
+ }
+
public Customizer() {
super(ACSDtdDefinedElement.class);
}
-------------------------------------------------------------------
Note:
Added "addRequiredAttributes" method and modified
"createElement"
to call "addRequiredAttributes".
Index: ACSFactory.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/antidote/org/apache/tools/ant/gui/acs/ACSFactory.java,v
retrieving revision 1.9
diff -u -r1.9 ACSFactory.java
--- ACSFactory.java 2001/03/28 12:25:49 1.9
+++ ACSFactory.java 2001/04/04 02:21:27
@@ -244,8 +244,22 @@
getOwnerDocument().createElement(name);
// XXX fixme.
indent(node, 1);
+ addRequiredAttributes(retval);
node.appendChild(retval);
return retval;
+ }
+
+ /**
+ * Add required attributes to the node.
+ *
+ * @param node the Node to add the attrinutes to.
+ */
+ public void addRequiredAttributes(ACSElement
node) {
+ if (node instanceof ACSDtdDefinedElement) {
+ ACSDtdDefinedElement dtdElement =
+ (ACSDtdDefinedElement) node;
+ dtdElement.addRequiredAttributes();
+ }
}
/**
-------------------------------------------------------------------
Note:
This file was changed to remove JDK 1.3 feature. The
feature
was used in NewElementCmd.java. The method getAction
was added
to this file.
Index: ActionManager.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/antidote/org/apache/tools/ant/gui/core/ActionManager.java,v
retrieving revision 1.6
diff -u -r1.6 ActionManager.java
--- ActionManager.java 2001/03/28 12:25:56 1.6
+++ ActionManager.java 2001/04/04 03:46:14
@@ -319,6 +319,16 @@
}
+ /**
+ * Get the Action with the given id.
+ *
+ * @param actionID Id of action to get command for.
+ * @return AntAction associated with the given id.
+ */
+ public AntAction getAction(String actionID) {
+ return (AntAction) _actions.get(actionID);
+ }
+
/**
* Add tool tip, Mnemonic, etc.
*
-------------------------------------------------------------------
Note:
This file was changed to remove JDK 1.3 feature.
This file had a small change made to it, but the
linefeeds were
screwed up so the entire file is included in the diff.
(sorry)
This was the code which was added:
String cmdStr = button.getActionCommand();
AntAction antAction =
getContext().getActions().getAction(cmdStr);
if (antAction == null) {
return;
}
And this was the code replaced by the above code:
Action action = button.getAction();
if (!(action instanceof AntAction)) {
return;
}
AntAction antAction = (AntAction) action;
Index: NewElementCmd.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/antidote/org/apache/tools/ant/gui/command/NewElementCmd.java,v
retrieving revision 1.1
diff -w -u -r1.1 NewElementCmd.java
--- NewElementCmd.java 2001/03/28 12:29:39 1.1
+++ NewElementCmd.java 2001/04/04 03:37:36
@@ -1,155 +1,304 @@
/*
+
* The Apache Software License, Version 1.1
+
*
+
* Copyright (c) 2001 The Apache Software Foundation.
All rights
+
* reserved.
+
*
+
* Redistribution and use in source and binary forms,
with or without
+
* modification, are permitted provided that the
following conditions
+
* are met:
+
*
+
* 1. Redistributions of source code must retain the
above copyright
+
* notice, this list of conditions and the
following disclaimer.
+
*
+
* 2. Redistributions in binary form must reproduce
the above copyright
+
* notice, this list of conditions and the
following disclaimer in
+
* the documentation and/or other materials
provided with the
+
* distribution.
+
*
+
* 3. The end-user documentation included with the
redistribution, if
+
* any, must include the following acknowlegement:
+
* "This product includes software developed by
the
+
* Apache Software Foundation
(http://www.apache.org/)."
+
* Alternately, this acknowlegement may appear in
the software itself,
+
* if and wherever such third-party
acknowlegements normally appear.
+
*
+
* 4. The names "The Jakarta Project", "Ant", and
"Apache Software
+
* Foundation" must not be used to endorse or
promote products derived
+
* from this software without prior written
permission. For written
+
* permission, please contact apache@apache.org.
+
*
+
* 5. Products derived from this software may not be
called "Apache"
+
* nor may "Apache" appear in their names without
prior written
+
* permission of the Apache Group.
+
*
+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY
EXPRESSED OR IMPLIED
+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES
+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE
+
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE
FOUNDATION OR
+
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL,
+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT
+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF
+
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND
+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY,
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT
+
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF
+
* SUCH DAMAGE.
+
*
====================================================================
+
*
+
* This software consists of voluntary contributions
made by many
+
* individuals on behalf of the Apache Software
Foundation. For more
+
* information on the Apache Software Foundation,
please see
+
* <http://www.apache.org/>.
+
*/
+
package org.apache.tools.ant.gui.command;
+
import java.util.EventObject;
+
import javax.swing.AbstractButton;
+
import javax.swing.Action;
+
import org.apache.tools.ant.gui.core.AppContext;
+
import
org.apache.tools.ant.gui.event.NewBaseElementEvent;
+
import
org.apache.tools.ant.gui.event.RefreshDisplayEvent;
+
import org.apache.tools.ant.gui.acs.*;
+
import org.apache.tools.ant.gui.util.WindowUtils;
+
import org.apache.tools.ant.gui.core.AntAction;
+
+
/**
+
* Command for creating a new propertyh.
+
*
+
* @version $Revision: 1.1 $
+
* @author Simeon Fitch
+
*/
+
public class NewElementCmd extends AbstractCommand {
+
/** New count for this session. Used to create
default names,
+
* numbered as a convenience. */
+
private static int _count = 1;
+
private EventObject _event = null;
+
+
/**
+
* Standard ctor.
+
*
+
* @param context Application context.
+
*/
+
public NewElementCmd(AppContext context,
EventObject event) {
+
super(context);
+
_event = event;
+
}
+
+
/**
+
* Creates a new xml element based on the button
which
+
* was pressed. The button text may contain the
name
+
* of the new element or a dialog box is
presented which
+
* asks the user for the element type.
+
*/
+
public void run() {
+
+
// Find which element is selected.
+
ACSElement[] vals =
getContext().getSelectionManager().
+
getSelectedElements();
+
if(vals == null || vals.length == 0) {
+
return;
+
}
+
+
// Find the text of the button which was
pressed
+
// to determine the type of element to
create.
+
Object source = _event.getSource();
+
if (!(source instanceof AbstractButton)) {
+
return;
+
}
+
AbstractButton button = (AbstractButton)
source;
+
String name = button.getText();
+
+
// Get the AntAction
- Action action = button.getAction();
- if (!(action instanceof AntAction)) {
+ String cmdStr = button.getActionCommand();
+ AntAction antAction =
getContext().getActions().getAction(cmdStr);
+ if (antAction == null) {
return;
}
- AntAction antAction = (AntAction) action;
+
ACSElement e = vals[vals.length - 1];
+
+
// Should we prompt the user use the element
type?
+
if (antAction.getName().equals(name)) {
+
+
// Display the dialog box.
+
ACSDtdDefinedElement dtde =
(ACSDtdDefinedElement) e;
+
NewElementDlg dlg = new NewElementDlg(
+
getContext().getParentFrame(), true);
+
dlg.setList(dtde.getPossibleChildren());
+
dlg.pack();
+
WindowUtils.centerWindow(dlg);
+
dlg.setTitle("Select the new element
type");
+
dlg.setVisible(true);
+
+
// Get the element type
+
if (dlg.getCancel()) {
+
name = "";
+
} else {
+
name = dlg.getElementName();
+
}
+
}
+
+
if (name.length() > 0) {
+
// Create the new element
+
ACSElement retval =
+
ACSFactory.getInstance().createElement(e, name);
+
getContext().getEventBus().postEvent(
+
new NewBaseElementEvent(getContext(),
retval));
+
} else {
+
// Request a refresh so the popup menu is
removed
+
// from the display.
+
getContext().getEventBus().postEvent(
+
new
RefreshDisplayEvent(getContext()));
+
}
+
}
+
}
+
+
__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail.
http://personal.mail.yahoo.com/