blob: 564746781233abe1a1bb1c0bd27addc6377575f0 [file] [log] [blame]
kchong38cbf172006-03-29 03:38:21 +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 *******************************************************************************/
kchong2be71b32006-04-11 16:32:03 +000011package org.eclipse.wst.xsd.ui.internal.common.commands;
kchong38cbf172006-03-29 03:38:21 +000012
13import java.util.List;
14
kchong38cbf172006-03-29 03:38:21 +000015import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
kchong38cbf172006-03-29 03:38:21 +000016import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
kchong2be71b32006-04-11 16:32:03 +000017import org.eclipse.wst.xsd.ui.internal.common.util.XSDCommonUIUtils;
kchong38cbf172006-03-29 03:38:21 +000018import org.eclipse.xsd.XSDAnnotation;
19import org.eclipse.xsd.XSDConcreteComponent;
kchongffa99e22006-05-15 21:08:00 +000020import org.eclipse.xsd.XSDSchema;
kchong38cbf172006-03-29 03:38:21 +000021import org.w3c.dom.Element;
22import org.w3c.dom.Node;
23
24public class AddDocumentationCommand extends BaseCommand
25{
26 XSDAnnotation xsdAnnotation;
27 XSDConcreteComponent input;
28 String newValue, oldValue;
29 boolean documentationExists;
30 Element documentationElement;
31
32 public AddDocumentationCommand(String label, XSDAnnotation xsdAnnotation, XSDConcreteComponent input, String newValue, String oldValue)
33 {
34 super(label);
35 this.xsdAnnotation = xsdAnnotation;
36 this.input = input;
37 this.newValue = newValue;
38 this.oldValue = oldValue;
39 }
40
41 public void execute()
42 {
kchongffa99e22006-05-15 21:08:00 +000043 if (input instanceof XSDSchema)
44 {
45 ensureSchemaElement((XSDSchema)input);
46 }
47
kchong38cbf172006-03-29 03:38:21 +000048 try
49 {
kchong59966042006-08-21 21:09:18 +000050 beginRecording(input.getElement());
51
52 xsdAnnotation = XSDCommonUIUtils.getInputXSDAnnotation(input, true);
53 Element element = xsdAnnotation.getElement();
54
55 List documentationList = xsdAnnotation.getUserInformation();
56 documentationElement = null;
57 documentationExists = false;
58 if (documentationList.size() > 0)
59 {
60 documentationExists = true;
61 documentationElement = (Element) documentationList.get(0);
62 }
63
64 if (documentationElement == null)
65 {
66 documentationElement = xsdAnnotation.createUserInformation(null);
67 element.appendChild(documentationElement);
68 formatChild(documentationElement);
69 // Defect in model....I create it but the model object doesn't appear
70 // to be updated
71 xsdAnnotation.updateElement();
72 xsdAnnotation.setElement(element);
73 }
74
kchong38cbf172006-03-29 03:38:21 +000075 if (documentationElement.hasChildNodes())
76 {
77 if (documentationElement instanceof IDOMElement)
78 {
79 IDOMElement domElement = (IDOMElement) documentationElement;
80
81 Node firstChild = documentationElement.getFirstChild();
82 Node lastChild = documentationElement.getLastChild();
83 int start = 0;
84 int end = 0;
85
kchong59966042006-08-21 21:09:18 +000086 // IDOMModel model = domElement.getModel();
87 // IDOMDocument doc = model.getDocument();
kchong38cbf172006-03-29 03:38:21 +000088 IDOMNode first = null;
89 if (firstChild instanceof IDOMNode)
90 {
91 first = (IDOMNode) firstChild;
92 start = first.getStartOffset();
93 }
94 if (lastChild instanceof IDOMNode)
95 {
96 IDOMNode last = (IDOMNode) lastChild;
97 end = last.getEndOffset();
98 }
99
100 if (domElement != null)
101 {
102 oldValue = domElement.getModel().getStructuredDocument().get(start, end - start);
103 domElement.getModel().getStructuredDocument().replaceText(documentationElement, start, end - start, newValue);
104 }
105 }
106 }
107 else
108 {
109 if (newValue.length() > 0)
110 {
kchong62c89ad2006-04-19 16:40:50 +0000111 oldValue = ""; //$NON-NLS-1$
kchong38cbf172006-03-29 03:38:21 +0000112 Node childNode = documentationElement.getOwnerDocument().createTextNode(newValue);
113 documentationElement.appendChild(childNode);
114 }
115 }
kchong59966042006-08-21 21:09:18 +0000116 formatChild(xsdAnnotation.getElement());
kchong38cbf172006-03-29 03:38:21 +0000117 }
118 catch (Exception e)
119 {
120
121 }
kchong59966042006-08-21 21:09:18 +0000122 finally
123 {
124 endRecording();
125 }
kchong38cbf172006-03-29 03:38:21 +0000126 }
127
128 public void undo()
129 {
130 super.undo();
131 }
132}