kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 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 | *******************************************************************************/ |
kchong | 2be71b3 | 2006-04-11 16:32:03 +0000 | [diff] [blame] | 11 | package org.eclipse.wst.xsd.ui.internal.common.commands; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 12 | |
| 13 | import java.util.List; |
| 14 | |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 15 | import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 16 | import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode; |
kchong | 2be71b3 | 2006-04-11 16:32:03 +0000 | [diff] [blame] | 17 | import org.eclipse.wst.xsd.ui.internal.common.util.XSDCommonUIUtils; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 18 | import org.eclipse.xsd.XSDAnnotation; |
| 19 | import org.eclipse.xsd.XSDConcreteComponent; |
kchong | ffa99e2 | 2006-05-15 21:08:00 +0000 | [diff] [blame] | 20 | import org.eclipse.xsd.XSDSchema; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 21 | import org.w3c.dom.Element; |
| 22 | import org.w3c.dom.Node; |
| 23 | |
| 24 | public 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 | { |
kchong | ffa99e2 | 2006-05-15 21:08:00 +0000 | [diff] [blame] | 43 | if (input instanceof XSDSchema) |
| 44 | { |
| 45 | ensureSchemaElement((XSDSchema)input); |
| 46 | } |
| 47 | |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 48 | try |
| 49 | { |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 50 | 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 | |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 75 | 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 | |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 86 | // IDOMModel model = domElement.getModel(); |
| 87 | // IDOMDocument doc = model.getDocument(); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 88 | 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 | { |
kchong | 62c89ad | 2006-04-19 16:40:50 +0000 | [diff] [blame] | 111 | oldValue = ""; //$NON-NLS-1$ |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 112 | Node childNode = documentationElement.getOwnerDocument().createTextNode(newValue); |
| 113 | documentationElement.appendChild(childNode); |
| 114 | } |
| 115 | } |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 116 | formatChild(xsdAnnotation.getElement()); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 117 | } |
| 118 | catch (Exception e) |
| 119 | { |
| 120 | |
| 121 | } |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 122 | finally |
| 123 | { |
| 124 | endRecording(); |
| 125 | } |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | public void undo() |
| 129 | { |
| 130 | super.undo(); |
| 131 | } |
| 132 | } |