blob: cc443d3978d46b6098adfe7fc47f33b9b08d6496 [file] [log] [blame]
kchonge0590cf2006-04-19 14:44:09 +00001/*******************************************************************************
kchong4ad18462008-01-09 23:00:31 +00002 * Copyright (c) 2001, 2008 IBM Corporation and others.
kchonge0590cf2006-04-19 14:44:09 +00003 * 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.commands;
12
kchong4ad18462008-01-09 23:00:31 +000013import org.eclipse.osgi.util.NLS;
14import org.eclipse.wst.xsd.ui.internal.common.util.Messages;
csaltera28ae452006-04-16 22:04:44 +000015import 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)
kchong2c45fdf2006-09-07 10:53:10 +000020 *
21 * (trung) also used in XSDComplexTypeAdvancedSection to change attribute of
22 * specical attributes like block, restriction which is not part of EMF Model
csaltera28ae452006-04-16 22:04:44 +000023 */
kchong59966042006-08-21 21:09:18 +000024public class UpdateAttributeValueCommand extends BaseCommand
csaltera28ae452006-04-16 22:04:44 +000025{
kchong4ad18462008-01-09 23:00:31 +000026 protected Element element;
27 protected String attributeName;
28 protected String attributeValue;
csaltera28ae452006-04-16 22:04:44 +000029
kchong2c45fdf2006-09-07 10:53:10 +000030 /** Whether the attribute should be deleted if value to
31 * be set is an empty String or null */
32 protected boolean deleteIfValueEmpty = false;
33
csalter2a0d5002006-09-20 04:58:29 +000034 public UpdateAttributeValueCommand(Element element, String attributeName, String attributeValue, boolean deleteIfValueEmpty)
csaltera28ae452006-04-16 22:04:44 +000035 {
36 this.element = element;
37 this.attributeName = attributeName;
38 this.attributeValue = attributeValue;
csalter2a0d5002006-09-20 04:58:29 +000039 this.deleteIfValueEmpty = deleteIfValueEmpty;
40 }
kchong4ad18462008-01-09 23:00:31 +000041
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 }
csalter2a0d5002006-09-20 04:58:29 +000047
48 public UpdateAttributeValueCommand(Element element, String attributeName, String attributeValue)
49 {
kchong4ad18462008-01-09 23:00:31 +000050 this(element, attributeName, attributeValue, false);
51 setLabel(NLS.bind(Messages._UI_ACTION_CHANGE, attributeName));
csaltera28ae452006-04-16 22:04:44 +000052 }
53
kchong2c45fdf2006-09-07 10:53:10 +000054 public void setDeleteIfEmpty(boolean v)
55 {
kchong4ad18462008-01-09 23:00:31 +000056 deleteIfValueEmpty = v;
57 }
58
59 public void setAttributeName(String attributeName)
60 {
61 this.attributeName = attributeName;
kchong2c45fdf2006-09-07 10:53:10 +000062 }
csaltera28ae452006-04-16 22:04:44 +000063
64 public void execute()
65 {
kchong59966042006-08-21 21:09:18 +000066 try
67 {
68 beginRecording(element);
kchong2c45fdf2006-09-07 10:53:10 +000069 if (deleteIfValueEmpty &&
70 (attributeValue == null || attributeValue.equals("") ) )
71 {
72 element.removeAttribute(attributeName);
73 }
74 else
75 {
76 element.setAttribute(attributeName, attributeValue);
77 }
kchong4ad18462008-01-09 23:00:31 +000078
79 doPostProcessing();
kchong59966042006-08-21 21:09:18 +000080 }
81 finally
82 {
83 endRecording();
84 }
kchong4ad18462008-01-09 23:00:31 +000085 }
86
87 protected void doPostProcessing()
88 {
89
90 }
csaltera28ae452006-04-16 22:04:44 +000091}