csalter | 9b7e109 | 2006-07-29 04:02:26 +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 | *******************************************************************************/ |
| 11 | package org.eclipse.wst.xsd.ui.internal.common.commands; |
| 12 | |
csalter | 9b7e109 | 2006-07-29 04:02:26 +0000 | [diff] [blame] | 13 | import org.eclipse.wst.xml.ui.internal.tabletree.TreeContentHelper; |
| 14 | import org.w3c.dom.Element; |
csalter | 2a0d500 | 2006-09-20 04:58:29 +0000 | [diff] [blame] | 15 | import org.w3c.dom.Node; |
csalter | 9b7e109 | 2006-07-29 04:02:26 +0000 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * This command is used from the extension view to edit extension elements |
| 19 | * and which are implemented as DOM objects (not part of the EMF model) |
| 20 | */ |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 21 | public class UpdateTextValueCommand extends BaseCommand |
csalter | 9b7e109 | 2006-07-29 04:02:26 +0000 | [diff] [blame] | 22 | { |
| 23 | Element element; |
| 24 | String value; |
| 25 | |
| 26 | public UpdateTextValueCommand(Element element, String value) |
| 27 | { |
| 28 | this.element = element; |
| 29 | this.value = value; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | public void execute() |
| 34 | { |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 35 | try |
| 36 | { |
csalter | 2a0d500 | 2006-09-20 04:58:29 +0000 | [diff] [blame] | 37 | beginRecording(element); |
| 38 | Node textNode = null; |
| 39 | TreeContentHelper helper = new TreeContentHelper(); |
| 40 | for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) |
| 41 | { |
| 42 | if (node.getNodeType() == Node.TEXT_NODE && |
| 43 | !helper.isIgnorableText(node)) |
| 44 | { |
| 45 | textNode = node; |
| 46 | break; |
| 47 | } |
| 48 | } |
| 49 | if (textNode == null) |
| 50 | { |
| 51 | textNode = element.getOwnerDocument().createTextNode(""); |
| 52 | element.appendChild(textNode); |
| 53 | } |
| 54 | helper.setNodeValue(textNode, value); |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 55 | } |
| 56 | finally |
| 57 | { |
| 58 | endRecording(); |
| 59 | } |
csalter | 9b7e109 | 2006-07-29 04:02:26 +0000 | [diff] [blame] | 60 | } |
| 61 | } |