Skip to main content
summaryrefslogtreecommitdiffstats
blob: 223f4ec5914bf484a845b1ff7b088afda1b1210e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.commands.single;

import java.util.Iterator;
import java.util.Map;

import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;

/**
 * This command is for change an single attribute of an IDOMElement. Normally
 * used by the properties view.
 * 
 * @author mengbo
 */
public class ChangeAttributeCommand extends SingleNodeCommand {
	private IDOMElement _element;

	private String _attrValue;

	private String _attrName;

	private Map _attributes;

	private boolean _keepEmptyAttribute = false;

	/**
	 * 
	 * @param label
	 * @param node
	 * @param attrName
	 * @param attrValue
	 *            if null means remove the specified attribute
	 */
	public ChangeAttributeCommand(String label, IDOMElement node,
			String attrName, String attrValue) {
		super(label, node);
		_element = node;
		_attrName = attrName;
		_attrValue = attrValue;
		_attributes = null;
	}

	/** TODO: can these two constructors be merged?
	 * @param label
	 * @param node
	 * @param attributes
	 */
	public ChangeAttributeCommand(String label, IDOMElement node, Map attributes) {
		super(label, node);
		_element = node;
		_attributes = attributes;
		_attrName = null;
		_attrValue = null;
	}

	protected void doExecute() {
		if (_attrName != null) {
			updateElement(_attrName, _attrValue);
		} else if (_attributes != null) {
			for (Iterator iterator = _attributes.keySet().iterator(); iterator
					.hasNext();) {
				String name = (String) iterator.next();
				String value = (String) _attributes.get(name);
				if (isSameValue(value, _element.getAttribute(name))) {
					continue;
				}
				updateElement(name, value);
			}
		}
	}

	private void updateElement(String name, String value) {
		if (_element.hasAttribute(name) && isEmptyString(value)
				&& !_keepEmptyAttribute) {
			_element.removeAttribute(name);
		}
		if (!isEmptyString(value) || _keepEmptyAttribute) {
			_element.setAttribute(name, value);
		}
	}

	private boolean isSameValue(String value1, String value2) {
		value1 = value1 == null ? "" : value1; //$NON-NLS-1$
		value2 = value2 == null ? "" : value2; //$NON-NLS-1$
		return value1.equals(value2);
	}

	private boolean isEmptyString(String str) {
		if (str == null || str.equals("")) { //$NON-NLS-1$
			return true;
		}
        return false;
	}

	/**
	 * @return Returns the keepEmptyAttribute.
	 */
	public boolean isKeepEmptyAttribute() {
		return _keepEmptyAttribute;
	}

	/**
	 * @param keepEmptyAttribute
	 *            The keepEmptyAttribute to set.
	 */
	public void setKeepEmptyAttribute(boolean keepEmptyAttribute) {
		this._keepEmptyAttribute = keepEmptyAttribute;
	}
}

Back to the top