kchong | e0590cf | 2006-04-19 14:44:09 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
kchong | 4ad1846 | 2008-01-09 23:00:31 +0000 | [diff] [blame] | 2 | * Copyright (c) 2001, 2008 IBM Corporation and others. |
kchong | e0590cf | 2006-04-19 14:44:09 +0000 | [diff] [blame] | 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 | *******************************************************************************/ |
csalter | a28ae45 | 2006-04-16 22:04:44 +0000 | [diff] [blame] | 11 | package org.eclipse.wst.xsd.ui.internal.common.commands; |
| 12 | |
kchong | 4ad1846 | 2008-01-09 23:00:31 +0000 | [diff] [blame] | 13 | import org.eclipse.osgi.util.NLS; |
| 14 | import org.eclipse.wst.xsd.ui.internal.common.util.Messages; |
csalter | a28ae45 | 2006-04-16 22:04:44 +0000 | [diff] [blame] | 15 | import org.w3c.dom.Element; |
| 16 | |
| 17 | /* |
| 18 | * This command is used from the extension view to edit extension elements |
| 19 | * and attributes which are implemented as DOM objects (not part of the EMF model) |
kchong | 2c45fdf | 2006-09-07 10:53:10 +0000 | [diff] [blame] | 20 | * |
| 21 | * (trung) also used in XSDComplexTypeAdvancedSection to change attribute of |
| 22 | * specical attributes like block, restriction which is not part of EMF Model |
csalter | a28ae45 | 2006-04-16 22:04:44 +0000 | [diff] [blame] | 23 | */ |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 24 | public class UpdateAttributeValueCommand extends BaseCommand |
csalter | a28ae45 | 2006-04-16 22:04:44 +0000 | [diff] [blame] | 25 | { |
kchong | 4ad1846 | 2008-01-09 23:00:31 +0000 | [diff] [blame] | 26 | protected Element element; |
| 27 | protected String attributeName; |
| 28 | protected String attributeValue; |
csalter | a28ae45 | 2006-04-16 22:04:44 +0000 | [diff] [blame] | 29 | |
kchong | 2c45fdf | 2006-09-07 10:53:10 +0000 | [diff] [blame] | 30 | /** Whether the attribute should be deleted if value to |
| 31 | * be set is an empty String or null */ |
| 32 | protected boolean deleteIfValueEmpty = false; |
| 33 | |
csalter | 2a0d500 | 2006-09-20 04:58:29 +0000 | [diff] [blame] | 34 | public UpdateAttributeValueCommand(Element element, String attributeName, String attributeValue, boolean deleteIfValueEmpty) |
csalter | a28ae45 | 2006-04-16 22:04:44 +0000 | [diff] [blame] | 35 | { |
| 36 | this.element = element; |
| 37 | this.attributeName = attributeName; |
| 38 | this.attributeValue = attributeValue; |
csalter | 2a0d500 | 2006-09-20 04:58:29 +0000 | [diff] [blame] | 39 | this.deleteIfValueEmpty = deleteIfValueEmpty; |
| 40 | } |
kchong | 4ad1846 | 2008-01-09 23:00:31 +0000 | [diff] [blame] | 41 | |
| 42 | public UpdateAttributeValueCommand(Element element, String attributeName, String attributeValue, String label) |
| 43 | { |
| 44 | this(element, attributeName, attributeValue, false); |
| 45 | setLabel(NLS.bind(Messages._UI_ACTION_CHANGE, label)); |
| 46 | } |
csalter | 2a0d500 | 2006-09-20 04:58:29 +0000 | [diff] [blame] | 47 | |
| 48 | public UpdateAttributeValueCommand(Element element, String attributeName, String attributeValue) |
| 49 | { |
kchong | 4ad1846 | 2008-01-09 23:00:31 +0000 | [diff] [blame] | 50 | this(element, attributeName, attributeValue, false); |
| 51 | setLabel(NLS.bind(Messages._UI_ACTION_CHANGE, attributeName)); |
csalter | a28ae45 | 2006-04-16 22:04:44 +0000 | [diff] [blame] | 52 | } |
| 53 | |
kchong | 2c45fdf | 2006-09-07 10:53:10 +0000 | [diff] [blame] | 54 | public void setDeleteIfEmpty(boolean v) |
| 55 | { |
kchong | 4ad1846 | 2008-01-09 23:00:31 +0000 | [diff] [blame] | 56 | deleteIfValueEmpty = v; |
| 57 | } |
| 58 | |
| 59 | public void setAttributeName(String attributeName) |
| 60 | { |
| 61 | this.attributeName = attributeName; |
kchong | 2c45fdf | 2006-09-07 10:53:10 +0000 | [diff] [blame] | 62 | } |
csalter | a28ae45 | 2006-04-16 22:04:44 +0000 | [diff] [blame] | 63 | |
| 64 | public void execute() |
| 65 | { |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 66 | try |
| 67 | { |
| 68 | beginRecording(element); |
kchong | 2c45fdf | 2006-09-07 10:53:10 +0000 | [diff] [blame] | 69 | if (deleteIfValueEmpty && |
| 70 | (attributeValue == null || attributeValue.equals("") ) ) |
| 71 | { |
| 72 | element.removeAttribute(attributeName); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | element.setAttribute(attributeName, attributeValue); |
| 77 | } |
kchong | 4ad1846 | 2008-01-09 23:00:31 +0000 | [diff] [blame] | 78 | |
| 79 | doPostProcessing(); |
kchong | 5996604 | 2006-08-21 21:09:18 +0000 | [diff] [blame] | 80 | } |
| 81 | finally |
| 82 | { |
| 83 | endRecording(); |
| 84 | } |
kchong | 4ad1846 | 2008-01-09 23:00:31 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | protected void doPostProcessing() |
| 88 | { |
| 89 | |
| 90 | } |
csalter | a28ae45 | 2006-04-16 22:04:44 +0000 | [diff] [blame] | 91 | } |