blob: f28e18acbe6944603915d5e152a5d6493ac3f706 [file] [log] [blame]
kchong9fe072d2007-05-02 18:10:23 +00001/*******************************************************************************
2 * Copyright (c) 2001, 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
csaltera28ae452006-04-16 22:04:44 +000011package org.eclipse.wst.xsd.ui.internal.common.properties.sections.appinfo;
12
csalter9b7e1092006-07-29 04:02:26 +000013import java.text.Collator;
14import java.util.Arrays;
csaltera28ae452006-04-16 22:04:44 +000015import java.util.Collection;
csalter9b7e1092006-07-29 04:02:26 +000016import java.util.Comparator;
csaltera28ae452006-04-16 22:04:44 +000017import java.util.HashMap;
csalterdf29c4e2006-05-16 21:43:16 +000018import java.util.Iterator;
csalterbb1e4ef2006-05-17 03:39:24 +000019import java.util.List;
csaltera28ae452006-04-16 22:04:44 +000020import org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration;
21import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration;
csaltera28ae452006-04-16 22:04:44 +000022import org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery;
23import org.eclipse.wst.xml.core.internal.modelquery.ModelQueryUtil;
csalter9b7e1092006-07-29 04:02:26 +000024import org.eclipse.wst.xml.ui.internal.tabletree.TreeContentHelper;
csalterdf29c4e2006-05-16 21:43:16 +000025import org.eclipse.wst.xsd.ui.internal.common.properties.sections.appinfo.custom.DefaultListNodeEditorConfiguration;
26import org.eclipse.wst.xsd.ui.internal.common.properties.sections.appinfo.custom.NodeCustomizationRegistry;
27import org.eclipse.wst.xsd.ui.internal.common.properties.sections.appinfo.custom.NodeEditorConfiguration;
28import org.eclipse.wst.xsd.ui.internal.common.properties.sections.appinfo.custom.NodeEditorProvider;
29import org.eclipse.wst.xsd.ui.internal.editor.XSDEditorPlugin;
csaltera28ae452006-04-16 22:04:44 +000030import org.w3c.dom.Attr;
31import org.w3c.dom.Element;
32import org.w3c.dom.NamedNodeMap;
33
csaltera28ae452006-04-16 22:04:44 +000034public class DOMExtensionDetailsContentProvider implements ExtensionDetailsContentProvider
35{
csalter6b61eb02006-07-28 20:20:14 +000036 private static final Object[] EMPTY_ARRAY = {};
37 private static final String[] EMPTY_STRING_ARRAY = {};
38 private static final String XMLNS = "xmlns"; //$NON-NLS
csalter9b7e1092006-07-29 04:02:26 +000039 private static final String TEXT_NODE_KEY = "text()"; //$NON-NLS
csalterdf29c4e2006-05-16 21:43:16 +000040
csaltera28ae452006-04-16 22:04:44 +000041 public Object[] getItems(Object input)
csalterdf29c4e2006-05-16 21:43:16 +000042 {
csaltera28ae452006-04-16 22:04:44 +000043 HashMap resultMap = new HashMap();
44 if (input instanceof Element)
csalterdf29c4e2006-05-16 21:43:16 +000045 {
csalter9b7e1092006-07-29 04:02:26 +000046 Element element = (Element) input;
47
48 // here we compute items for the attributes that physically in the document
49 //
csaltera28ae452006-04-16 22:04:44 +000050 NamedNodeMap attributes = element.getAttributes();
51 for (int i = 0; i < attributes.getLength(); i++)
52 {
csalterdf29c4e2006-05-16 21:43:16 +000053 Attr attr = (Attr) attributes.item(i);
csalter6b61eb02006-07-28 20:20:14 +000054 if (!XMLNS.equals(attr.getName()) && !XMLNS.equals(attr.getPrefix())) //$NON-NLS-1$ //$NON-NLS-2$
csalterdf29c4e2006-05-16 21:43:16 +000055 {
csalter6b61eb02006-07-28 20:20:14 +000056 resultMap.put(attr.getName(), DOMExtensionItem.createItemForElementAttribute(element, attr));
csaltera28ae452006-04-16 22:04:44 +000057 }
csalterdf29c4e2006-05-16 21:43:16 +000058 }
csalter9b7e1092006-07-29 04:02:26 +000059
60 // here we compute an item for the text node that is physically in the document
61 //
62 String textNodeValue = new TreeContentHelper().getNodeValue(element);
63 if (textNodeValue != null)
64 {
65 resultMap.put(TEXT_NODE_KEY, DOMExtensionItem.createItemForElementText(element));
66 }
67
csaltera28ae452006-04-16 22:04:44 +000068 ModelQuery modelQuery = ModelQueryUtil.getModelQuery(element.getOwnerDocument());
69 if (modelQuery != null)
csalterdf29c4e2006-05-16 21:43:16 +000070 {
csaltera28ae452006-04-16 22:04:44 +000071 CMElementDeclaration ed = modelQuery.getCMElementDeclaration(element);
72 if (ed != null)
73 {
csalter9b7e1092006-07-29 04:02:26 +000074 // here we compute items for the attributes that may be added to the document according to the grammar
75 //
csalterbb1e4ef2006-05-17 03:39:24 +000076 List list = modelQuery.getAvailableContent(element, ed, ModelQuery.INCLUDE_ATTRIBUTES);
77 for (Iterator i = list.iterator(); i.hasNext(); )
78 {
79 CMAttributeDeclaration ad = (CMAttributeDeclaration)i.next();
csalter6b61eb02006-07-28 20:20:14 +000080 if (ad != null && resultMap.get(ad.getNodeName()) == null)
csalterdf29c4e2006-05-16 21:43:16 +000081 {
csalter6b61eb02006-07-28 20:20:14 +000082 resultMap.put(ad.getNodeName(), DOMExtensionItem.createItemForElementAttribute(element, ad));
csalterbb1e4ef2006-05-17 03:39:24 +000083 }
csaltera28ae452006-04-16 22:04:44 +000084 }
csalter9b7e1092006-07-29 04:02:26 +000085 if (resultMap.get(TEXT_NODE_KEY) == null)
csaltera28ae452006-04-16 22:04:44 +000086 {
csalter9b7e1092006-07-29 04:02:26 +000087 // here we compute an item for the text node that may be added to the document according to the grammar
88 //
89 int contentType = ed.getContentType();
90 if (contentType == CMElementDeclaration.PCDATA || contentType == CMElementDeclaration.MIXED)
91 {
92 resultMap.put(TEXT_NODE_KEY, DOMExtensionItem.createItemForElementText(element));
93 }
94 }
csaltera28ae452006-04-16 22:04:44 +000095 }
csalterdf29c4e2006-05-16 21:43:16 +000096 }
csaltera28ae452006-04-16 22:04:44 +000097 Collection collection = resultMap.values();
csalterdf29c4e2006-05-16 21:43:16 +000098 // initialize the editor information for each item
99 //
100 for (Iterator i = collection.iterator(); i.hasNext();)
101 {
102 initPropertyEditorConfiguration((DOMExtensionItem) i.next());
103 }
csaltera28ae452006-04-16 22:04:44 +0000104 DOMExtensionItem[] items = new DOMExtensionItem[collection.size()];
105 resultMap.values().toArray(items);
csalter9b7e1092006-07-29 04:02:26 +0000106
107 // here we sort the list alphabetically
108 //
109 if (items.length > 0)
110 {
111 Comparator comparator = new Comparator()
112 {
113 public int compare(Object arg0, Object arg1)
114 {
115 DOMExtensionItem a = (DOMExtensionItem)arg0;
116 DOMExtensionItem b = (DOMExtensionItem)arg1;
117
118 // begin special case to ensure 'text nodes' come last
119 if (a.isTextValue() && !b.isTextValue())
120 {
121 return 1;
122 }
123 else if (b.isTextValue() && !a.isTextValue())
124 {
125 return -1;
126 }
127 // end special case
128 else
129 {
130 return Collator.getInstance().compare(a.getName(), b.getName());
131 }
132 }
133 };
134 Arrays.sort(items, comparator);
135 }
csalterdf29c4e2006-05-16 21:43:16 +0000136 return items;
csaltera28ae452006-04-16 22:04:44 +0000137 }
138 else if (input instanceof Attr)
139 {
csalterdf29c4e2006-05-16 21:43:16 +0000140 Attr attr = (Attr) input;
csalter6b61eb02006-07-28 20:20:14 +0000141 DOMExtensionItem item = DOMExtensionItem.createItemForAttributeText(attr.getOwnerElement(), attr);
csaltera28ae452006-04-16 22:04:44 +0000142 DOMExtensionItem[] items = {item};
143 return items;
csalterdf29c4e2006-05-16 21:43:16 +0000144 }
csaltera28ae452006-04-16 22:04:44 +0000145 return EMPTY_ARRAY;
146 }
csaltera28ae452006-04-16 22:04:44 +0000147
148 public String getName(Object item)
149 {
150 if (item instanceof DOMExtensionItem)
151 {
csalterdf29c4e2006-05-16 21:43:16 +0000152 return ((DOMExtensionItem) item).getName();
153 }
kchong62c89ad2006-04-19 16:40:50 +0000154 return ""; //$NON-NLS-1$
csaltera28ae452006-04-16 22:04:44 +0000155 }
156
157 public String getValue(Object item)
158 {
159 if (item instanceof DOMExtensionItem)
160 {
csalterdf29c4e2006-05-16 21:43:16 +0000161 return ((DOMExtensionItem) item).getValue();
162 }
kchong62c89ad2006-04-19 16:40:50 +0000163 return ""; //$NON-NLS-1$
csaltera28ae452006-04-16 22:04:44 +0000164 }
csalterdf29c4e2006-05-16 21:43:16 +0000165
csaltera28ae452006-04-16 22:04:44 +0000166 public String[] getPossibleValues(Object item)
167 {
168 if (item instanceof DOMExtensionItem)
169 {
csalterdf29c4e2006-05-16 21:43:16 +0000170 return ((DOMExtensionItem) item).getPossibleValues();
171 }
172 return EMPTY_STRING_ARRAY;
173 }
174
175 protected void initPropertyEditorConfiguration(DOMExtensionItem item)
176 {
177 String namespace = item.getNamespace();
178 String name = item.getName();
179 String parentName = item.getParentName();
180 NodeEditorConfiguration configuration = null;
181 if (namespace != null)
182 {
183 // TODO (cs) remove reference to XSDEditorPlugin... make generic
184 // perhaps push down the xml.ui ?
185 //
csalter785c16c2006-05-16 21:49:50 +0000186 NodeCustomizationRegistry registry = XSDEditorPlugin.getDefault().getNodeCustomizationRegistry();
csalterdf29c4e2006-05-16 21:43:16 +0000187 NodeEditorProvider provider= registry.getNodeEditorProvider(namespace);
188 if (provider != null)
189 {
csalter167ec9f2006-05-17 17:39:34 +0000190 configuration = provider.getNodeEditorConfiguration(parentName, name);
191 if (configuration != null)
192 {
193 configuration.setParentNode(item.getParentNode());
194 if (item.getNode() != null)
195 {
196 configuration.setNode(item.getNode());
197 }
198 }
csalterdf29c4e2006-05-16 21:43:16 +0000199 }
200 }
201 String[] values = item.getPossibleValues();
202 if (values != null && values.length > 1)
203 {
204 configuration = new DefaultListNodeEditorConfiguration(values);
205 }
206
207 // Note that it IS expected that the configaration may be null
208 //
209 item.setPropertyEditorConfiguration(configuration);
csaltera28ae452006-04-16 22:04:44 +0000210 }
211}